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): cancel drag captures from authored visibility changes
yhhb00e752c96cb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3e560d5da..48a0dafc0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Authored UI hiding cancels dragging before another layout pass.** Hiding a dragged node or its ancestor stops capture and emits one drag-end event immediately on the next sample, including visibility changes inside drag callbacks. Stale hover cannot start a hidden control, and showing it again does not resume an old capture. Dragging and keyboard focus share the same live visibility rules.+- **Dense change queries bind their live sequence index once.** Added and Changed filters reuse component-specific matchers during entity iteration, avoiding repeated world and component lookups while retaining precise system boundaries, stable-frame skipping and visibility of later component replacements.- **System change queries see additions and writes after earlier runs in the same frame.** Added and Changed filters use completed-system mutation sequences while explicit tick-based query instances keep their existing windows. Systems consume their own writebacks without retriggering themselves; compiled writers and asynchronous reads share the same boundaries. Stable queries skip candidate enumeration, and live sequence stamps reuse tick records and disappear with removed components.diff --git a/sdk/src/ui/input/drag.ts b/sdk/src/ui/input/drag.tsindex b753ff175..862e07a2f 100644--- a/sdk/src/ui/input/drag.ts+++ b/sdk/src/ui/input/drag.ts@@ -26,6 +26,7 @@ import { uiPointerFor } from '../util/ui-pick';import type { UICameraData } from '../core/ui-camera-info';import { playModeOnly } from '../../ecs/env';import { getEntityDepth } from '../util/helpers';+import { createLiveVisibilityQuery } from '../util/live-visibility';import { SystemLabel, PluginName } from '../../ecs/systemLabels';import { quaternionToAngle2D } from '../util/math';@@ -138,12 +139,15 @@ export class DragPlugin implements Plugin {const x = (d * dx - c * dy) / det, y = (a * dy - b * dx) / det;return Number.isFinite(x) && Number.isFinite(y) ? { x, y } : null;};- const eligible = (entity: Entity): boolean => world.isEntityActive(entity)+ const eligible = (entity: Entity): boolean => world.valid(entity) && world.isEntityActive(entity)&& !world.isDestructionPending(entity)&& world.has(entity, Draggable) && world.get(entity, Draggable).enabled&& world.has(entity, Transform)&& (!world.has(entity, Interactable) || world.get(entity, Interactable).enabled)- && (!world.has(entity, UINode) || !engine?.getUINodeHiddenInTree?.(registry!, entity));+ // Fresh per check: drag callbacks may hide an ancestor before the+ // next layout pass, invalidating an earlier visibility snapshot.+ && !createLiveVisibilityQuery(world, target => !!(world.has(target, UINode)+ && engine?.getUINodeHiddenInTree?.(registry!, target)))(entity);let pendingEntity: Entity | null = null;let pendingStartWorld = { x: 0, y: 0 };@@ -190,9 +194,8 @@ export class DragPlugin implements Plugin {let bestDepth = -1;for (const entity of draggableEntities) {- if (!eligible(entity)) continue;const interaction = world.get(entity, UIInteraction) as UIInteractionData;- if (interaction.hovered) {+ if (interaction.hovered && eligible(entity)) {let layer = 0;if (world.has(entity, Sprite)) {layer = (world.get(entity, Sprite) as SpriteData).layer;diff --git a/sdk/src/ui/input/focus-inspection.ts b/sdk/src/ui/input/focus-inspection.tsindex b80003baa..6b8fcd033 100644--- a/sdk/src/ui/input/focus-inspection.ts+++ b/sdk/src/ui/input/focus-inspection.ts@@ -7,8 +7,7 @@ import { Focusable } from './focusable';import { Interactable } from './interactable';import { UIDialog, isDialogOpen } from '../behavior/dialog';import { walkParentChain } from '../util/helpers';-import { Parent } from '../../ecs/component';-import { UINode, UIDisplay, type UINodeData } from '../core/ui-node';+import { createLiveVisibilityQuery } from '../util/live-visibility';/** Read-only scene queries needed to inspect focus. @experimental */export type FocusInspectionWorld = Pick<World, 'getWasmModule' | 'getCppRegistry' | 'getEntitiesWithComponents' | 'valid' | 'has' | 'get'>;@@ -32,7 +31,7 @@ function focusContext(world: FocusInspectionWorld) {const engine = worldEngineApi(world);const registry = world.getCppRegistry();const visibilityResolved = !!(registry && engine?.getUINodeHiddenInTree);- const hiddenInTree = liveVisibility(world, entity =>+ const hiddenInTree = createLiveVisibilityQuery(world, entity =>visibilityResolved && engine!.getUINodeHiddenInTree!(registry!, entity));const roots = new Set(world.getEntitiesWithComponents([UIDialog]).filter(entity => isDialogOpen(world, entity)@@ -48,44 +47,6 @@ function focusContext(world: FocusInspectionWorld) {return { visibilityResolved, roots, hiddenInTree };}-/** Authored display is current even between layout passes. Memoize shared- * ancestors only within this read-only query, never across scene mutations. */-function liveVisibility(world: FocusInspectionWorld, fallback: (entity: Entity) => boolean) {- const fastWorld = world as FocusInspectionWorld & Partial<Pick<World, 'resolveGetter'>>;- const getter = fastWorld.resolveGetter?.(UINode) as ((entity: Entity) => UINodeData) | null | undefined;- const readNode = getter ?? ((entity: Entity) => world.get(entity, UINode));- const parentGetter = fastWorld.resolveGetter?.(Parent) as ((entity: Entity) => { entity: Entity }) | null | undefined;- const readParent = parentGetter ?? ((entity: Entity) => world.get(entity, Parent));- const HIDDEN = 1, AUTHORED = 2, PENDING = 4;- const memo = new Map<Entity, number>();- const path: Entity[] = [];- return (entity: Entity): boolean => {- path.length = 0;- let current = entity, flags = 0;- for (;;) {- const cached = memo.get(current);- if (cached !== undefined) {- // Invalid cyclic ancestry cannot receive keyboard input.- flags = cached === PENDING ? HIDDEN | AUTHORED : cached;- break;- }- if (!world.valid(current)) break;- memo.set(current, PENDING);- path.push(current);- if (!world.has(current, Parent)) break;- current = readParent(current).entity;- }- for (let i = path.length - 1; i >= 0; i--) {- if (world.has(path[i], UINode)) {- flags |= AUTHORED;- if (!(flags & HIDDEN) && readNode(path[i]).display === UIDisplay.None) flags |= HIDDEN;- }- memo.set(path[i], flags);- }- return flags & AUTHORED ? !!(flags & HIDDEN) : fallback(entity);- };-}-function skipReason(world: FocusInspectionWorld, entity: Entity,{ hiddenInTree, roots }: ReturnType<typeof focusContext>): FocusSkipReason | null {let skipped: FocusSkipReason | null = null;diff --git a/sdk/src/ui/util/live-visibility.ts b/sdk/src/ui/util/live-visibility.tsnew file mode 100644index 000000000..1ba51f99d--- /dev/null+++ b/sdk/src/ui/util/live-visibility.ts@@ -0,0 +1,47 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import type { World } from '../../ecs/world';+import type { Entity } from '../../types';+import { Parent } from '../../ecs/component';+import { UINode, UIDisplay, type UINodeData } from '../core/ui-node';++type VisibilityWorld = Pick<World, 'valid' | 'has' | 'get'> & Partial<Pick<World, 'resolveGetter'>>;++/** Authored display is current even between layout passes. Memoize shared+ * ancestors only within this read-only query, never across scene mutations. */+export function createLiveVisibilityQuery(world: VisibilityWorld, fallback: (entity: Entity) => boolean) {+ const fastWorld = world as VisibilityWorld & Partial<Pick<World, 'resolveGetter'>>;+ const getter = fastWorld.resolveGetter?.(UINode) as ((entity: Entity) => UINodeData) | null | undefined;+ const readNode = getter ?? ((entity: Entity) => world.get(entity, UINode));+ const parentGetter = fastWorld.resolveGetter?.(Parent) as ((entity: Entity) => { entity: Entity }) | null | undefined;+ const readParent = parentGetter ?? ((entity: Entity) => world.get(entity, Parent));+ const HIDDEN = 1, AUTHORED = 2, PENDING = 4;+ const memo = new Map<Entity, number>();+ const path: Entity[] = [];+ return (entity: Entity): boolean => {+ path.length = 0;+ let current = entity, flags = 0;+ for (;;) {+ const cached = memo.get(current);+ if (cached !== undefined) {+ // Invalid cyclic ancestry cannot receive keyboard input.+ flags = cached === PENDING ? HIDDEN | AUTHORED : cached;+ break;+ }+ if (!world.valid(current)) break;+ memo.set(current, PENDING);+ path.push(current);+ if (!world.has(current, Parent)) break;+ current = readParent(current).entity;+ }+ for (let i = path.length - 1; i >= 0; i--) {+ if (world.has(path[i], UINode)) {+ flags |= AUTHORED;+ if (!(flags & HIDDEN) && readNode(path[i]).display === UIDisplay.None) flags |= HIDDEN;+ }+ memo.set(path[i], flags);+ }+ return flags & AUTHORED ? !!(flags & HIDDEN) : fallback(entity);+ };+}+diff --git a/sdk/tests/ui-drag-cancel-wasm.test.ts b/sdk/tests/ui-drag-cancel-wasm.test.tsindex f444bf57b..2452c9724 100644--- a/sdk/tests/ui-drag-cancel-wasm.test.ts+++ b/sdk/tests/ui-drag-cancel-wasm.test.ts@@ -29,6 +29,7 @@ describe.skipIf(!HAS_WASM)('drag cancellation on real WASM', () => {dragPlugin = new DragPlugin();dragPlugin.build(app); flushPendingRegistrations(app);entity = app.world.spawn(); app.world.insert(entity, Transform); app.world.insert(entity, Draggable);app.world.insert(entity, UIInteraction, { hovered: true });+ engine.uiLayout_update(registry, -50, -50, 50, 50, true);});afterEach(() => { dragPlugin.cleanup(app);app.quit({ keepRenderer: true }); registry.delete(); });async function step(x: number) {@@ -108,4 +109,59 @@ describe.skipIf(!HAS_WASM)('drag cancellation on real WASM', () => {expect(app.world.get(entity, DragState).isDragging).toBe(false);expect(app.world.get(entity, Transform).position.x).toBe(resolved);});+ it.each(['self', 'ancestor'] as const)('cancels authored %s hiding before layout resolves it', async kind => {+ await drag();+ expect(app.world.get(entity, DragState).isDragging).toBe(true);+ const root = kind === 'self' ? entity : app.world.spawn();+ app.world.insert(root, Canvas);+ if (root !== entity) {+ app.world.insert(root, Transform);+ app.world.setParent(entity, root);+ }+ app.world.insert(root, UINode);+ app.world.insert(entity, UINode);+ engine.uiLayout_update(registry, -50, -50, 50, 50, true);+ engine.transform_update(registry);+ expect(engine.getUINodeHiddenInTree!(registry, entity)).toBe(false);+ const before = app.world.get(entity, UINode).insetLeft.value;+ expect(app.world.get(entity, DragState).isDragging).toBe(true);+ app.getResource(UIEvents).drain();+ app.world.update(root, UINode, node => { node.display = 1; });+ expect(engine.getUINodeHiddenInTree!(registry, entity)).toBe(false);+ await step(100);+ expect(app.world.get(entity, DragState).isDragging).toBe(false);+ expect(app.world.get(entity, UINode).insetLeft.value).toBe(before);+ expect(app.getResource(UIEvents).drain().filter(event => event.type === 'drag_end')).toHaveLength(1);+ });+ it('rejects a stale hover when authored visibility is already hidden', async () => {+ app.world.insert(entity, UINode, { display: 1 });+ expect(engine.getUINodeHiddenInTree!(registry, entity)).toBe(false);+ await drag();+ expect(app.world.has(entity, DragState)).toBe(false);+ expect(app.world.get(entity, Transform).position.x).toBe(0);+ expect(app.getResource(UIEvents).drain()).toEqual([]);+ });+ it('ends a drag hidden by its start callback before applying a move', async () => {+ app.getResource(UIEvents).on(entity, 'drag_start', () => {+ app.world.insert(entity, UINode, { display: 1 });+ });+ await drag();+ expect(app.world.get(entity, DragState).isDragging).toBe(false);+ expect(app.world.get(entity, DragState).totalDeltaWorld.x).toBe(0);+ expect(app.getResource(UIEvents).drain().map(event => event.type)).toEqual(['drag_start', 'drag_end']);+ });+ it('ends a drag hidden by its move callback and does not resume when shown', async () => {+ app.getResource(UIEvents).on(entity, 'drag_move', () => {+ app.world.insert(entity, UINode, { display: 1 });+ });+ await drag();+ expect(app.world.get(entity, DragState).isDragging).toBe(false);+ const before = app.world.get(entity, UINode).insetLeft.value;+ expect(app.getResource(UIEvents).drain().map(event => event.type)).toEqual(['drag_start', 'drag_move', 'drag_end']);+ app.world.update(entity, UINode, node => { node.display = 0; });+ await step(100);+ expect(app.world.get(entity, DragState).isDragging).toBe(false);+ expect(app.world.get(entity, UINode).insetLeft.value).toBe(before);+ expect(app.getResource(UIEvents).drain()).toEqual([]);+ });});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex ed166ac28..d6f488c25 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): cancel drag captures from authored visibility changes':+ { note: 'Authored UI hiding cancels dragging before another layout pass.' },'fix(test): present golden browser packages on a shared display':{ internal: 'CI presents ordinary Web and playable packages without taking focus and owns one virtual display for the controller and workers, preventing hidden-page frame throttling while preserving settle deadlines, per-worker timeout shares, minigame launch paths and failure verdicts.' },'perf(ecs): bind live change matchers before entity iteration':