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): resize scroll extents together and retire missing content

yhh04af548bf1a9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f2bd996cc..01c66507e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Scroll views retain valid offsets across simultaneous resizes.** Viewport and content dimensions update together before clamping, avoiding position jumps and duplicate scroll notifications. Destroyed content references detach their containers instead of leaving stale scroll targets.
+
- **Scene-authored scroll views receive hover and wheel input.** Hit testing no longer inserts hidden interaction components outside the SDK's component index. The interaction system owns creation and query invalidation, so wheel scrolling and per-frame hover/press state work for controls placed in the editor.
- **Claimed input releases cannot leave gameplay controls held.** Keyboard, mouse and touch releases consumed by an editor or UI handler silently retire the previous held state without manufacturing gameplay release edges. UI capture receives primary-pointer cancellation instead of a click, even across frame-edge clearing; unowned touch moves and releases do not create ghost contacts.
diff --git a/sdk/src/ui/behavior/scroll.ts b/sdk/src/ui/behavior/scroll.ts
index 71c792acd..4e70aca00 100644
--- a/sdk/src/ui/behavior/scroll.ts
+++ b/sdk/src/ui/behavior/scroll.ts
@@ -17,7 +17,7 @@ import type { Entity } from '../../types';
import type { World } from '../../ecs/world';
import type { CppRegistry } from '../../wasm';
import type { EngineApi } from '../../ecs/bridge/engineApi';
-import { UINode, UIPositionType, type UINodeData } from '../core/ui-node';
+import { UINode, UIPositionType } from '../core/ui-node';
import { UIScroll, ScrollMovement, type UIScrollData } from '../core/ui-scroll';
import { ScrollContainer } from '../collection/scroll-container';
import type { Vec2 } from '../../types';
@@ -83,7 +83,7 @@ export function createScrollAdoptSystem(
const attach = (entity: Entity, scroll: UIScrollData): void => {
const content = contentOf(entity, scroll);
- if (content == null) return; // nothing to move yet — try again next tick
+ if (content == null || !world.valid(content) || !world.has(content, UINode)) return;
const container = new ScrollContainer({
viewportSize: size(entity),
@@ -96,7 +96,7 @@ export function createScrollAdoptSystem(
// The offset moves the content by its insets, exactly as the widget does,
// so both paths leave the same thing in the scene.
const unsubscribe = container.onScroll((offset) => {
- const node = world.get(content, UINode) as UINodeData | undefined;
+ const node = world.tryGet(content, UINode);
if (!node) return;
node.position = UIPositionType.Absolute;
node.insetLeft = px(-offset.x);
@@ -123,19 +123,16 @@ export function createScrollAdoptSystem(
// A content swap is a different container, not a resize. Only an
// explicit ref is re-read each frame; the implicit "first child" is
// trusted while it lives, so the steady state costs no wasm calls.
- const swapped = data.content
- ? (data.content as Entity) !== a.content
- : !world.valid(a.content);
+ const swapped = !world.valid(a.content) || !world.has(a.content, UINode)
+ || (data.content !== 0 && (data.content as Entity) !== a.content);
if (swapped) {
detach(entity);
attach(entity, data);
continue;
}
const content = a.content;
- // Layout is free to change under us (a window resize, a list that
- // grew), and both setters early-out when nothing moved.
- a.container.setViewportSize(size(entity));
- a.container.setContentSize(size(content));
+ // Clamp against one complete layout, never mixed old/new extents.
+ a.container.setSizes(size(entity), size(content));
}
for (const entity of [...attached.keys()]) {
diff --git a/sdk/tests/ui-scroll-adopt.test.ts b/sdk/tests/ui-scroll-adopt.test.ts
index e660803e9..ea4e7ac8f 100644
--- a/sdk/tests/ui-scroll-adopt.test.ts
+++ b/sdk/tests/ui-scroll-adopt.test.ts
@@ -14,7 +14,7 @@
* the layout pass; here the two size queries are faked, so what is under test is
* the adoption and not Yoga.
*/
-import { describe, it, expect, beforeAll } from 'vitest';
+import { describe, it, expect, beforeAll, vi } from 'vitest';
import { App } from '../src/app/app';
import { Schedule } from '../src/ecs/system';
import { UINode, UIScroll, ScrollMovement, type UINodeData, type UIScrollData } from '../src/ui';
@@ -81,7 +81,7 @@ describe.skipIf(!HAS_WASM)('UIScroll adoption', () => {
detachScrollContainer: (e) => { attached.delete(e as number); },
},
));
- return { app, world, viewport, content, attached };
+ return { app, world, viewport, content, attached, sizes };
}
it('attaches a container sized from the resolved layout', async () => {
@@ -128,4 +128,50 @@ describe.skipIf(!HAS_WASM)('UIScroll adoption', () => {
await app.tick(1 / 60);
expect(attached.has(viewport as number)).toBe(false);
});
+
+ it('resizing both boxes preserves a still-valid scroll offset', async () => {
+ const { app, world, viewport, content, attached, sizes } = setup();
+ await app.tick(1 / 60);
+ const container = attached.get(viewport)!;
+ container.setOffset({ x: 0, y: 900 });
+ const changed = vi.fn(); container.onScroll(changed);
+ sizes.set(viewport, { w: 400, h: 600 });
+ sizes.set(content, { w: 400, h: 1500 });
+ await app.tick(1 / 60);
+ expect(container.getOffset().y).toBe(900);
+ expect(world.get(content, UINode).insetTop.value).toBe(-900);
+ expect(changed).not.toHaveBeenCalled();
+ });
+
+ it('shrinking both boxes emits only the final clamped offset', async () => {
+ const { app, viewport, content, attached, sizes } = setup();
+ await app.tick(1 / 60);
+ const container = attached.get(viewport)!;
+ container.setOffset({ x: 0, y: 900 });
+ const changed = vi.fn(); container.onScroll(changed);
+ sizes.set(viewport, { w: 400, h: 600 });
+ sizes.set(content, { w: 400, h: 800 });
+ await app.tick(1 / 60);
+ expect(changed.mock.calls).toEqual([[{ x: 0, y: 200 }]]);
+ });
+
+ it('retires an explicit content reference when its entity is destroyed', async () => {
+ const { app, world, viewport, content, attached } = setup();
+ world.update(viewport, UIScroll, scroll => { scroll.content = content; });
+ await app.tick(1 / 60);
+ world.despawn(content);
+ await app.tick(1 / 60);
+ expect(attached.has(viewport)).toBe(false);
+ });
+
+ it('does not recreate a removed content node through a retained container', async () => {
+ const { app, world, viewport, content, attached } = setup();
+ await app.tick(1 / 60);
+ const container = attached.get(viewport)!;
+ world.remove(content, UINode);
+ container.scrollBy({ x: 0, y: 120 });
+ expect(world.has(content, UINode)).toBe(false);
+ await app.tick(1 / 60);
+ expect(attached.has(viewport)).toBe(false);
+ });
});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index eedef1e5d..2c82a63b8 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): resize scroll extents together and retire missing content':
+ { note: 'Scroll views retain valid offsets across simultaneous resizes.' },
'fix(ui): keep hit testing from bypassing interaction membership':
{ note: 'Scene-authored scroll views receive hover and wheel input.' },
'fix(input): retire held state when release events are consumed':