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): reconcile toggle indicator targets and callback removals
yhhda41859b11d4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b23da4a0c..8498694a4 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **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.+- **Slider capture expires with input and target availability.** Disabled, inactive or hidden tracks and lost pointer projections cancel capture before a value update, so restoring the control cannot resume an old drag. A consumed release followed by another press cannot retain capture, and track width is read from the slider's own World instead of shared UI helper bindings.- **Slider visuals follow range and target edits.** Changing min/max or replacing a fill/handle updates the displayed fraction even when the value is unchanged, without emitting a spurious value-change event. Stable frames do not rewrite visuals, cached display state is reused during value changes, and change callbacks may remove later sliders safely.diff --git a/sdk/src/ui/behavior/toggle.ts b/sdk/src/ui/behavior/toggle.tsindex 9787fec67..572c81a26 100644--- a/sdk/src/ui/behavior/toggle.ts+++ b/sdk/src/ui/behavior/toggle.ts@@ -74,7 +74,7 @@ function clearSiblings(world: World, group: Entity, keep: Entity): void {/** Flips on click; syncs `isOn` → check visibility; emits `change`. */export function createToggleSystem(world: World, events: UIEventQueue): SystemDef {- const shown = new EntityStateMap<boolean>(); // last isOn whose visual was applied+ const shown = new EntityStateMap<UIToggleData>();// The interaction layer defines what a click is (release over the pressed// entity); the toggle just listens.@@ -96,10 +96,16 @@ export function createToggleSystem(world: World, events: UIEventQueue): SystemDereturn defineSystem([], () => {for (const e of world.getEntitiesWithComponents([UIToggle])) {+ if (!world.valid(e) || !world.has(e, UIToggle)) continue;const d = world.get(e, UIToggle) as UIToggleData;- if (shown.get(e) === d.isOn) continue;- const emitChange = shown.has(e); // first sync is initial paint- shown.set(e, d.isOn);+ const previous = shown.get(e);+ if (previous && previous.isOn === d.isOn && previous.check === d.check) continue;+ const emitChange = previous !== undefined && previous.isOn !== d.isOn;+ if (previous) {+ previous.isOn = d.isOn; previous.check = d.check;+ } else {+ shown.set(e, { isOn: d.isOn, check: d.check });+ }setUIVisible(world, d.check, d.isOn);if (emitChange) events.emit(e, UIEventType.Change, { isOn: d.isOn });}diff --git a/sdk/tests/ui-toggle-lifecycle-wasm.test.ts b/sdk/tests/ui-toggle-lifecycle-wasm.test.tsnew file mode 100644index 000000000..ce4d93390--- /dev/null+++ b/sdk/tests/ui-toggle-lifecycle-wasm.test.ts@@ -0,0 +1,48 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';+import { App } from '../src/app/app';+import { AppContext, setDefaultContext } from '../src/ecs/context';+import { Schedule } from '../src/ecs/system';+import { UIVisual } from '../src/ui/core/ui-visual';+import { UIEventQueue } from '../src/ui/core/events';+import { UIToggle, createToggleSystem } from '../src/ui/behavior/toggle';+import { createToggle } from '../src/ui/widgets/toggle';+import type { CppRegistry, ESEngineModule } from '../src/wasm';+import { HAS_WASM, loadWasmModule } from './helpers/loadWasm';++describe.skipIf(!HAS_WASM)('toggle visual and callback lifecycle on real WASM', () => {+ let engine: ESEngineModule, registry: CppRegistry, app: App, events: UIEventQueue;+ beforeAll(async () => { engine = await loadWasmModule(); });+ beforeEach(() => {+ setDefaultContext(new AppContext()); registry = new engine.Registry();+ app = App.new(); app.connectCpp(registry, engine); events = new UIEventQueue();+ app.addSystemToSchedule(Schedule.Update, createToggleSystem(app.world, events));+ });+ afterEach(() => { app.quit({ keepRenderer: true }); registry.delete(); });+ it.each([false, true])('replacement indicator receives the current state %s without a change event', async isOn => {+ const changed = vi.fn(), toggle = createToggle({ world: app.world, events, isOn, onChange: changed });+ await app.tick(1 / 60);+ const check = app.world.spawn(); app.world.insert(check, UIVisual, { enabled: !isOn });+ app.world.update(toggle.entity, UIToggle, d => { d.check = check; });+ await app.tick(1 / 60);+ expect(app.world.get(check, UIVisual).enabled).toBe(isOn);+ expect(changed).not.toHaveBeenCalled();+ });+ it('a change callback may dispose another toggle awaiting its update', async () => {+ const error = vi.fn(); app.onError(error);+ let later: ReturnType<typeof createToggle>;+ const first = createToggle({ world: app.world, events, onChange: () => later.dispose() });+ later = createToggle({ world: app.world, events }); await app.tick(1 / 60);+ first.setValue(true); later.setValue(true);+ await app.tick(1 / 60);+ expect(error).not.toHaveBeenCalled();+ expect(app.world.valid(later.entity)).toBe(false);+ });+ it('stable frames do not rewrite indicators', async () => {+ createToggle({ world: app.world, events, isOn: true }); await app.tick(1 / 60);+ const write = vi.spyOn(app.world, 'insert');+ for (let i = 0; i < 100; i++) await app.tick(1 / 60);+ expect(write).not.toHaveBeenCalled();+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 054e935fe..9dd0caa8c 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): reconcile toggle indicator targets and callback removals':+ { note: 'Toggle indicators follow target replacements safely.' },'fix(ui): cancel unavailable slider capture and use local track dimensions':{ note: 'Slider capture expires with input and target availability.' },'fix(ui): reconcile slider visuals after range and target edits':