Skip to content

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.

fix(ui): cancel scroll capture and motion when input ownership changes

yhh8c8973fc829e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6760d6058..904975504 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Cancelled scroll gestures stop without flinging.** Editor-consumed pointer releases cancel capture even after UI pointer sampling consumes its marker. Inactive, noninteractive or hidden viewports stop dragging and coasting; hidden layout boxes no longer clamp the saved scroll position to zero; losing all pointer projections discards motion instead of resuming an old drag when the view returns.
+
- **Scene scroll settings update while the game is running.** Changes to axes, wheel speed, drag enablement and movement settings rebuild the adopted container while preserving legal offsets. Disabling both axes suspends scrolling, reactivation restores the content position, and replaced containers cannot inherit an old pointer drag or fling.
- **Scroll views follow content hierarchy edits.** Reparenting a live content node switches an implicit scroll view to its new first child, and clearing an explicit content reference restores first-child selection. Retired containers stop moving the previous content; unchanged hierarchies do not reread native child vectors every frame.
diff --git a/sdk/src/input/pointerCancellation.ts b/sdk/src/input/pointerCancellation.ts
index 5e2c81df3..be396c796 100644
--- a/sdk/src/input/pointerCancellation.ts
+++ b/sdk/src/input/pointerCancellation.ts
@@ -1,7 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
const cancelled = new WeakSet<object>();
+const versions = new WeakMap<object, number>();
/** Keep cancellation until the next UI pointer sample, even across a frame-edge clear. */
-export function cancelPrimaryPointer(input: object): void { cancelled.add(input); }
+export function cancelPrimaryPointer(input: object): void {
+ cancelled.add(input);
+ versions.set(input, primaryPointerCancellationVersion(input) + 1);
+}
export function takePrimaryPointerCancellation(input: object): boolean { return cancelled.delete(input); }
+/** Independent consumers can observe the same cancellation after pointer sampling. */
+export function primaryPointerCancellationVersion(input: object): number { return versions.get(input) ?? 0; }
diff --git a/sdk/src/ui/behavior/plugin.ts b/sdk/src/ui/behavior/plugin.ts
index cf04e4e29..3f278072b 100644
--- a/sdk/src/ui/behavior/plugin.ts
+++ b/sdk/src/ui/behavior/plugin.ts
@@ -4,6 +4,7 @@ import type { App, Plugin } from '../../app/app';
import { Res, Time, type TimeData } from '../../ecs/resource';
import { defineSystem, Schedule } from '../../ecs/system';
import { Input, type InputState } from '../../input/input';
+import { primaryPointerCancellationVersion } from '../../input/pointerCancellation';
import { Transform, type TransformData } from '../../ecs/component';
import { playModeOnly } from '../../ecs/env';
import type { Entity, Vec2 } from '../../types';
@@ -20,7 +21,8 @@ import { createScrollAdoptSystem } from './scroll';
import { engineApi } from '../../ecs/bridge/engineApi';
import type { CppRegistry } from '../../wasm';
import { KineticScroll } from '../collection/kinetic-scroll';
-import { UIInteraction, type UIInteractionData } from '../input/interactable';
+import { Interactable, UIInteraction, type UIInteractionData } from '../input/interactable';
+import { UINode } from '../core/ui-node';
import { getEntityDepth, walkParentChain } from '../util/helpers';
import { UIDialog, createDialogSystem } from './dialog';
import { UISlider, createSliderSystem } from './slider';
@@ -102,6 +104,12 @@ export class UIBehaviorPlugin implements Plugin {
app.world.onDespawn((entity) => scrollContainers.detach(entity));
const world = app.world;
+ const engine = engineApi(app);
+ const registry = world.getCppRegistry() as CppRegistry;
+ const scrollAvailable = (entity: Entity): boolean => world.isEntityActive(entity)
+ && !world.isDestructionPending(entity)
+ && (!world.has(entity, Interactable) || world.get(entity, Interactable).enabled)
+ && (!world.has(entity, UINode) || !engine?.getUINodeHiddenInTree?.(registry, entity));
registerComponent('UIDialog', UIDialog);
app.addSystemToSchedule(
@@ -186,7 +194,7 @@ export class UIBehaviorPlugin implements Plugin {
if (hovered === null) return null;
const eligible = (e: Entity): boolean => {
const c = scrollContainers.get(e);
- return !!c && (!dragOnly || c.getDragScroll());
+ return !!c && scrollAvailable(e) && (!dragOnly || c.getDragScroll());
};
if (eligible(hovered)) return hovered;
let found: Entity | null = null;
@@ -222,6 +230,7 @@ export class UIBehaviorPlugin implements Plugin {
let pendingEntity: Entity | null = null;
let activeEntity: Entity | null = null;
let capturedContainer: ScrollContainer | null = null;
+ let lastCancellationVersion = primaryPointerCancellationVersion(app.getResource(Input));
let grabStartWorld: Vec2 = { x: 0, y: 0 };
let grabStartOffset: Vec2 = { x: 0, y: 0 };
let lastOffset: Vec2 = { x: 0, y: 0 };
@@ -230,9 +239,18 @@ export class UIBehaviorPlugin implements Plugin {
[Res(Input), Res(UICameraInfo), Res(Time), Res(ScreenLayout), Res(ScreenOverlay)],
(input: InputState, camera: UICameraData, time: TimeData, layout: ScreenLayoutData,
overlay: ScreenOverlayData) => {
- if (!camera.valid && !overlay.active) return;
+ const cancellationVersion = primaryPointerCancellationVersion(input);
+ const cancelled = cancellationVersion !== lastCancellationVersion;
+ lastCancellationVersion = cancellationVersion;
+ if (!camera.valid && !overlay.active) {
+ activeEntity = pendingEntity = null;
+ capturedContainer = null;
+ dynamics.clear();
+ return;
+ }
const captured = activeEntity ?? pendingEntity;
- if (captured !== null && scrollContainers.get(captured) !== capturedContainer) {
+ if (captured !== null && (cancelled || !scrollAvailable(captured)
+ || scrollContainers.get(captured) !== capturedContainer)) {
dynamics.get(captured)?.motion.stop();
activeEntity = pendingEntity = null;
capturedContainer = null;
@@ -245,7 +263,7 @@ export class UIBehaviorPlugin implements Plugin {
// Press over a hovered container (or an interactive child of one)
// arms the drag; the nearest container wins so a nested list
// scrolls itself, not its ancestor.
- if (input.isMouseButtonPressed(0)) {
+ if (input.isMouseButtonPressed(0) && input.isMouseButtonDown(0)) {
const best = hoveredScrollTarget(true);
if (best !== null) {
const container = scrollContainers.get(best)!;
@@ -319,7 +337,7 @@ export class UIBehaviorPlugin implements Plugin {
// Coast: tick flung containers; an axis the clamp stops is dead.
for (const [entity, state] of dynamics) {
const container = scrollContainers.get(entity);
- if (container !== state.container) {
+ if (container !== state.container || !scrollAvailable(entity)) {
dynamics.delete(entity);
continue;
}
diff --git a/sdk/src/ui/behavior/scroll.ts b/sdk/src/ui/behavior/scroll.ts
index 34d131e59..a0cbf8b47 100644
--- a/sdk/src/ui/behavior/scroll.ts
+++ b/sdk/src/ui/behavior/scroll.ts
@@ -133,6 +133,8 @@ export function createScrollAdoptSystem(
const data = scroll as UIScrollData;
if (!data.enabled || (!data.horizontal && !data.vertical)) continue;
live.add(entity);
+ // Hidden layout boxes collapse to zero; they are not a content resize.
+ if (engine?.getUINodeHiddenInTree?.(registry, entity)) continue;
const a = attached.get(entity);
if (!a) {
diff --git a/sdk/tests/ui-scroll-adopt.test.ts b/sdk/tests/ui-scroll-adopt.test.ts
index 06a48f3d2..821832dfc 100644
--- a/sdk/tests/ui-scroll-adopt.test.ts
+++ b/sdk/tests/ui-scroll-adopt.test.ts
@@ -68,12 +68,14 @@ describe.skipIf(!HAS_WASM)('UIScroll adoption', () => {
[content as number, contentSize],
]);
const attached = new Map<number, ScrollContainer>();
+ const hidden = new Set<number>();
app.addSystemToSchedule(Schedule.Update, createScrollAdoptSystem(
world,
{
uiNode_computedWidth: (_r: unknown, e: number) => sizes.get(e)?.w ?? 0,
uiNode_computedHeight: (_r: unknown, e: number) => sizes.get(e)?.h ?? 0,
+ getUINodeHiddenInTree: (_r: unknown, e: number) => hidden.has(e),
} as never,
registry,
{
@@ -81,7 +83,7 @@ describe.skipIf(!HAS_WASM)('UIScroll adoption', () => {
detachScrollContainer: (e) => { attached.delete(e as number); },
},
));
- return { app, world, viewport, content, attached, sizes };
+ return { app, world, viewport, content, attached, sizes, hidden };
}
it('attaches a container sized from the resolved layout', async () => {
@@ -259,4 +261,17 @@ describe.skipIf(!HAS_WASM)('UIScroll adoption', () => {
await app.tick(1 / 60);
expect(attached.get(viewport)!.getDecelerationRate()).toBe(0);
});
+
+ it('preserves scroll position while hidden layout boxes collapse to zero', async () => {
+ const { app, viewport, content, attached, sizes, hidden } = setup();
+ await app.tick(1 / 60);
+ const scroll = attached.get(viewport)!; scroll.setOffset({ x: 0, y: 120 });
+ hidden.add(viewport); sizes.set(viewport, { w: 0, h: 0 }); sizes.set(content, { w: 0, h: 0 });
+ await app.tick(1 / 60);
+ expect(scroll.getOffset().y).toBe(120);
+ hidden.delete(viewport); sizes.set(viewport, { w: 400, h: 300 }); sizes.set(content, { w: 400, h: 1200 });
+ await app.tick(1 / 60);
+ expect(attached.get(viewport)).toBe(scroll);
+ expect(scroll.getOffset().y).toBe(120);
+ });
});
diff --git a/sdk/tests/ui-scroll-motion-wasm.test.ts b/sdk/tests/ui-scroll-motion-wasm.test.ts
index f19540467..f7f9a4553 100644
--- a/sdk/tests/ui-scroll-motion-wasm.test.ts
+++ b/sdk/tests/ui-scroll-motion-wasm.test.ts
@@ -3,10 +3,14 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { App, flushPendingRegistrations } from '../src/app/app';
import { AppContext, setDefaultContext } from '../src/ecs/context';
-import { Input, InputState } from '../src/input/input';
+import { Input, InputState, inputEventCallbacks } from '../src/input/input';
+import { inputRouter } from '../src/input/inputRouter';
+import { takePrimaryPointerCancellation } from '../src/input/pointerCancellation';
+import { Canvas, Disabled, Transform } from '../src/ecs/component';
+import { UINode } from '../src/ui/core/ui-node';
import { UICameraInfo } from '../src/ui/core/ui-camera-info';
import { UIEvents, UIEventQueue } from '../src/ui/core/events';
-import { UIInteraction } from '../src/ui/input/interactable';
+import { Interactable, UIInteraction } from '../src/ui/input/interactable';
import { UIBehaviorPlugin } from '../src/ui/behavior/plugin';
import { ScrollContainer } from '../src/ui/collection/scroll-container';
import type { CppRegistry, ESEngineModule } from '../src/wasm';
@@ -53,4 +57,42 @@ describe.skipIf(!HAS_WASM)('scroll motion ownership on real WASM', () => {
for (let i = 0; i < 10; i++) await step(100);
expect(next.getOffset().y).toBe(0);
});
+ it('an editor-owned release cancels the drag without starting a fling', async () => {
+ const scroll = container(); plugin.attachScrollContainer(entity, scroll); await drag();
+ const off = inputRouter.setEditorHandler({ onPointerUp: () => true });
+ try { inputEventCallbacks(input).onPointerUp(0); } finally { off(); }
+ takePrimaryPointerCancellation(input);
+ for (let i = 0; i < 10; i++) await step(100);
+ expect(scroll.getOffset().y).toBe(80);
+ });
+ it.each(['inactive', 'noninteractive'])('a %s captured viewport stops moving', async kind => {
+ const scroll = container(); plugin.attachScrollContainer(entity, scroll); await drag();
+ if (kind === 'inactive') app.world.insert(entity, Disabled);
+ else app.world.insert(entity, Interactable, { enabled: false });
+ await step(120);
+ expect(scroll.getOffset().y).toBe(80);
+ });
+ it('losing all pointer projections cancels capture before the view returns', async () => {
+ const scroll = container(); plugin.attachScrollContainer(entity, scroll); await drag();
+ app.getResource(UICameraInfo).valid = false; await step(120);
+ app.getResource(UICameraInfo).valid = true; await step(140);
+ expect(scroll.getOffset().y).toBe(80);
+ });
+ it('a hidden viewport stops its captured drag after layout resolves visibility', async () => {
+ const scroll = container(); plugin.attachScrollContainer(entity, scroll); await drag();
+ app.world.insert(entity, Canvas); app.world.insert(entity, Transform);
+ app.world.insert(entity, UINode, { display: 1 });
+ engine.uiLayout_update(registry, -50, -50, 50, 50, true);
+ expect(engine.getUINodeHiddenInTree!(registry, entity)).toBe(true);
+ await step(120);
+ expect(scroll.getOffset().y).toBe(80);
+ });
+ it('disabling a viewport stops an already released fling', async () => {
+ const scroll = container(); plugin.attachScrollContainer(entity, scroll); await drag();
+ input.noteMouseUp(0); await step(100);
+ const before = scroll.getOffset().y;
+ app.world.insert(entity, Disabled);
+ for (let i = 0; i < 10; i++) await step(100);
+ expect(scroll.getOffset().y).toBe(before);
+ });
});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 78e0bcfe5..aa6e24541 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 scroll capture and motion when input ownership changes':
+ { note: 'Cancelled scroll gestures stop without flinging.' },
'fix(ui): apply live scroll settings and isolate replaced motion':
{ note: 'Scene scroll settings update while the game is running.' },
'fix(ui): follow scroll content hierarchy and reference changes':