A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(ui): retire unavailable dropdown popups before selection
yhhf3c5b9a0ebb4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 67f2b14a5..3c359b7f7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Unavailable dropdowns retire their open lists.** Disabling, hiding, deactivating or removing a dropdown closes its popup even when the owner drops out of component queries. Keyboard input and stale row clicks cannot change an unavailable control; availability is checked again after popup teardown callbacks.+- **Dropdown labels and open rows follow live option edits.** Renames and label replacements repaint without a false selection event; changed option lists or row heights rebuild an open popup and keep the selection in range. Stale rows cannot choose a newly substituted option, popup teardown cannot resurrect a disposed owner, and change callbacks may remove later controls safely.- **Toggle indicators follow target replacements safely.** Replacing a check entity immediately applies the current on/off state without a false change event. Change callbacks may dispose later toggles in the same update, while stable frames avoid repeated visual writes and reuse cached state during transitions.diff --git a/sdk/src/ui/behavior/dropdown.ts b/sdk/src/ui/behavior/dropdown.tsindex b80fb8d46..6f4516a58 100644--- a/sdk/src/ui/behavior/dropdown.ts+++ b/sdk/src/ui/behavior/dropdown.ts@@ -18,9 +18,11 @@ import { defineComponent } from '../../ecs/component';import { defineSystem, type SystemDef } from '../../ecs/system';import { Res } from '../../ecs/resource';import { Input, type InputState } from '../../input/input';+import { worldEngineApi } from '../../ecs/bridge/engineApi';import type { World } from '../../ecs/world';import type { Entity } from '../../types';import { px, percent } from '../core/dimension';+import { UINode } from '../core/ui-node';import { spawnUIEntity } from '../core/compose';import { Text, type TextData } from '../core/text';import { UIEventType, type UIEventQueue } from '../core/events';@@ -64,6 +66,13 @@ function popupMatches(p: PopupState, d: UIDropdownData): boolean {return true;}+function dropdownAvailable(world: World, e: Entity): boolean {+ if (!world.isEntityActive(e) || world.isDestructionPending(e) || !world.has(e, UIDropdown)) return false;+ if (world.has(e, Interactable) && !(world.get(e, Interactable) as InteractableData).enabled) return false;+ return !world.has(e, UINode)+ || !worldEngineApi(world)?.getUINodeHiddenInTree?.(world.getCppRegistry()!, e);+}+// Popup registries are runtime-only and per-world (unit tests run many worlds).const popupsByWorld = new WeakMap<World, Map<Entity, PopupState>>();@@ -91,7 +100,7 @@ export function closeDropdown(world: World, e: Entity): void {export function openDropdown(world: World, events: UIEventQueue, e: Entity): void {const popups = popupsOf(world);- if (popups.has(e) || !world.valid(e) || !world.has(e, UIDropdown)) return;+ if (popups.has(e) || !dropdownAvailable(world, e)) return;const d = world.get(e, UIDropdown) as UIDropdownData;const options = [...d.options], optionHeight = d.optionHeight;@@ -160,8 +169,9 @@ export function openDropdown(world: World, events: UIEventQueue, e: Entity): voiconst index = i;unsubs.push(events.on(row, UIEventType.Click, () => {+ const available = dropdownAvailable(world, e);closeDropdown(world, e);- if (!world.valid(e) || !world.has(e, UIDropdown)) return;+ if (!available || !dropdownAvailable(world, e)) return;const cur = world.get(e, UIDropdown) as UIDropdownData;if (cur.options[index] !== options[index]) return;if (cur.selectedIndex !== index) {@@ -223,9 +233,10 @@ export function createDropdownSystem(world: World, events: UIEventQueue): Systemevents.on(UIEventType.Click, (ev) => {const target = ev.target;if (world.valid(target) && world.has(target, UIDropdown)) {- const enabled = !world.has(target, Interactable)- || (world.get(target, Interactable) as InteractableData).enabled;- if (!enabled) return;+ if (!dropdownAvailable(world, target)) {+ closeDropdown(world, target);+ return;+ }if (isDropdownOpen(world, target)) closeDropdown(world, target);else openDropdown(world, events, target);return;@@ -240,6 +251,13 @@ export function createDropdownSystem(world: World, events: UIEventQueue): System});return defineSystem([Res(Input)], (input: InputState) => {+ const popups = popupsOf(world);+ // Inactive owners disappear from component queries but still own popup entries.+ if (popups.size > 0) {+ for (const e of popups.keys()) {+ if (!dropdownAvailable(world, e)) closeDropdown(world, e);+ }+ }for (const e of world.getEntitiesWithComponents([UIDropdown])) {if (!world.valid(e) || !world.has(e, UIDropdown)) continue;let d = world.get(e, UIDropdown) as UIDropdownData;@@ -249,7 +267,7 @@ export function createDropdownSystem(world: World, events: UIEventQueue): Systemd.selectedIndex = clamped;world.insert(e, UIDropdown, d);}- const previousPopup = popupsOf(world).get(e);+ const previousPopup = popups.get(e);if (previousPopup && !popupMatches(previousPopup, d)) {closeDropdown(world, e);if (!world.valid(e) || !world.has(e, UIDropdown)) continue;@@ -262,10 +280,8 @@ export function createDropdownSystem(world: World, events: UIEventQueue): System// convention); Enter confirms and Escape dismisses an open popup.const focused = world.has(e, Focusable)&& (world.get(e, Focusable) as FocusableData).isFocused;- const enabled = !world.has(e, Interactable)- || (world.get(e, Interactable) as InteractableData).enabled;const open = isDropdownOpen(world, e);- if (focused && enabled && d.options.length > 0) {+ if (focused && dropdownAvailable(world, e) && d.options.length > 0) {let next = d.selectedIndex;if (input.isKeyPressed('ArrowDown')) next = Math.min(next + 1, d.options.length - 1);if (input.isKeyPressed('ArrowUp')) next = Math.max(next - 1, 0);@@ -298,7 +314,7 @@ export function createDropdownSystem(world: World, events: UIEventQueue): Systemworld.insert(d.label, Text, txt);}}- const popup = popupsOf(world).get(e);+ const popup = popups.get(e);if (popup) highlightRow(world, popup.rows, d.selectedIndex);if (emitChange) events.emit(e, UIEventType.Change, { index: d.selectedIndex });}diff --git a/sdk/tests/ui-dropdown-options-wasm.test.ts b/sdk/tests/ui-dropdown-options-wasm.test.tsindex 45988c103..8bb0a3fa5 100644--- a/sdk/tests/ui-dropdown-options-wasm.test.ts+++ b/sdk/tests/ui-dropdown-options-wasm.test.ts@@ -4,7 +4,11 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'viteimport { App } from '../src/app/app';import { AppContext, setDefaultContext } from '../src/ecs/context';import { Schedule } from '../src/ecs/system';-import { Parent } from '../src/ecs/component';+import { Canvas, Disabled, Parent } from '../src/ecs/component';+import { Input, InputState } from '../src/input/input';+import { Interactable } from '../src/ui/input/interactable';+import { Focusable } from '../src/ui/input/focusable';+import { UINode } from '../src/ui/core/ui-node';import { Text } from '../src/ui/core/text';import { UIEventQueue } from '../src/ui/core/events';import { UIDropdown, createDropdownSystem } from '../src/ui/behavior/dropdown';@@ -77,4 +81,30 @@ describe.skipIf(!HAS_WASM)('dropdown live options on real WASM', () => {for (let i = 0; i < 100; i++) await app.tick(1 / 60);expect(write).not.toHaveBeenCalled();});+ it.each(['disabled', 'inactive', 'hidden', 'removed'])('%s owners retire open popups and ignore keyboard input', async kind => {+ const dd = createDropdown({ world: app.world, events, options: ['a','b'] });+ await app.tick(1 / 60); dd.open();+ app.world.update(dd.entity, Focusable, d => { d.isFocused = true; });+ if (kind === 'disabled') app.world.update(dd.entity, Interactable, d => { d.enabled = false; });+ if (kind === 'inactive') app.world.insert(dd.entity, Disabled);+ if (kind === 'removed') app.world.remove(dd.entity, UIDropdown);+ if (kind === 'hidden') {+ app.world.insert(dd.entity, Canvas);+ app.world.update(dd.entity, UINode, n => { n.display = 1; });+ engine.uiLayout_update(registry, -50, -50, 50, 50, true);+ }+ const input = new InputState(); input.noteKeyDown('ArrowDown'); app.insertResource(Input, input);+ await app.tick(1 / 60);+ expect(dd.isOpen()).toBe(false);+ if (kind !== 'removed') expect(dd.getSelectedIndex()).toBe(0);+ });+ it('a disabled owner cannot accept a row click before the next frame', async () => {+ const dd = createDropdown({ world: app.world, events, options: ['a','b'] });+ await app.tick(1 / 60); dd.open();+ const row = app.world.getEntitiesWithComponents([UIController]).filter(e => e !== dd.entity)[1]!;+ app.world.update(dd.entity, Interactable, d => { d.enabled = false; });+ events.emit(row, 'click');+ expect(dd.getSelectedIndex()).toBe(0);+ expect(dd.isOpen()).toBe(false);+ });});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex b5208688e..d2f8efda6 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(ui): retire unavailable dropdown popups before selection':+ { note: 'Unavailable dropdowns retire their open lists.' },'fix(ui): reconcile dropdown options and guard popup teardown':{ note: 'Dropdown labels and open rows follow live option edits.' },'fix(ui): reconcile toggle indicator targets and callback removals':