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): reconcile slider visuals after range and target edits

yhhda04dd14c032
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 455df8078..5e9a36840 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **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.
+
- **Object drags stop when their input or target becomes unavailable.** Editor-consumed releases, disabled or removed drag components, hidden targets and lost pointer projections clear capture before another position write. Drag callbacks may disable or remove their target safely, and an active gesture emits its end once without resuming later.
- **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.
diff --git a/sdk/src/ui/behavior/slider.ts b/sdk/src/ui/behavior/slider.ts
index 4c3bc55ed..b99b02bea 100644
--- a/sdk/src/ui/behavior/slider.ts
+++ b/sdk/src/ui/behavior/slider.ts
@@ -6,7 +6,7 @@
*
* The component IS the slider's state (min/max/step/value + its fill/handle
* entity refs); the system is the single writer of the visuals, re-deriving
- * them whenever `value` differs from what's on screen. Any writer — pointer
+ * them whenever the value, range or visual targets change. Any writer — pointer
* drag, keyboard, `handle.setValue`, a data binding, the editor inspector —
* just writes `value`, and change events fire uniformly for all of them.
*/
@@ -75,12 +75,13 @@ function keyStep(d: UISliderData): number {
export function createSliderSystem(world: World, events: UIEventQueue): SystemDef {
// Press on a track captures the drag until release, even off-entity.
let active: Entity | null = null;
- const shown = new EntityStateMap<number>(); // last value whose visuals were applied
+ const shown = new EntityStateMap<Pick<UISliderData, 'value' | 'min' | 'max' | 'fill' | 'handle'>>();
return defineSystem(
[Res(Input), Res(UICameraInfo), Res(ScreenOverlay)],
(input: InputState, camera: UICameraData, overlay: ScreenOverlayData) => {
for (const e of world.getEntitiesWithComponents([UISlider])) {
+ if (!world.valid(e) || !world.has(e, UISlider)) continue;
const d = world.get(e, UISlider) as UISliderData;
let next = d.value;
@@ -130,9 +131,16 @@ export function createSliderSystem(world: World, events: UIEventQueue): SystemDe
}
// Value → visuals + change event, whoever wrote the value.
- if (shown.get(e) !== d.value) {
- const emitChange = shown.has(e); // first sync is initial paint, not a change
- shown.set(e, d.value);
+ const previous = shown.get(e);
+ if (!previous || previous.value !== d.value || previous.min !== d.min
+ || previous.max !== d.max || previous.fill !== d.fill || previous.handle !== d.handle) {
+ const emitChange = previous !== undefined && previous.value !== d.value;
+ if (previous) {
+ previous.value = d.value; previous.min = d.min; previous.max = d.max;
+ previous.fill = d.fill; previous.handle = d.handle;
+ } else {
+ shown.set(e, { value: d.value, min: d.min, max: d.max, fill: d.fill, handle: d.handle });
+ }
const frac = sliderFraction(d);
if (world.valid(d.fill) && world.has(d.fill, UIVisual)) {
const vis = world.get(d.fill, UIVisual) as UIVisualData;
diff --git a/sdk/tests/ui-slider-dropdown.test.ts b/sdk/tests/ui-slider-dropdown.test.ts
index 87e86663b..86c0966c4 100644
--- a/sdk/tests/ui-slider-dropdown.test.ts
+++ b/sdk/tests/ui-slider-dropdown.test.ts
@@ -10,7 +10,6 @@ import {
createDropdownSystem,
UIEventQueue,
UIEventType,
- UIRect,
UINode,
UIVisual,
DimensionUnit,
@@ -151,6 +150,46 @@ describe('createSlider', () => {
expect(node.insetLeft).toEqual({ value: 50, unit: DimensionUnit.Percent });
});
+ it('refreshes visuals when the range changes while value stays the same', () => {
+ const onChange = vi.fn(), slider = make({ value: .5, onChange });
+ tick();
+ const d = world.get(slider.entity, UISlider) as Record<string, unknown>;
+ world.insert(slider.entity, UISlider, { ...d, max: 2 });
+ tick();
+ expect((world.get(slider.fillEntity, UIVisual) as { fillAmount: number }).fillAmount).toBe(.25);
+ expect((world.get(slider.handleEntity, UINode) as { insetLeft: { value: number } }).insetLeft.value).toBe(25);
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('paints replacement fill and handle targets without changing the value', () => {
+ const onChange = vi.fn(), slider = make({ value: .5, onChange });
+ tick();
+ const fill = world.spawn(), handle = world.spawn();
+ world.insert(fill, UIVisual, { ...world.get(slider.fillEntity, UIVisual) as object, fillAmount: 0 });
+ world.insert(handle, UINode, { ...world.get(slider.handleEntity, UINode) as object, insetLeft: { unit: 0, value: 0 } });
+ world.insert(slider.entity, UISlider, { ...world.get(slider.entity, UISlider) as object, fill, handle });
+ tick();
+ expect((world.get(fill, UIVisual) as { fillAmount: number }).fillAmount).toBe(.5);
+ expect((world.get(handle, UINode) as { insetLeft: { value: number } }).insetLeft.value).toBe(50);
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('does not rewrite stable visuals on consecutive frames', () => {
+ make({ value: .5 }); tick();
+ const write = vi.spyOn(world, 'insert');
+ for (let i = 0; i < 100; i++) tick();
+ expect(write).not.toHaveBeenCalled();
+ });
+
+ it('a change callback may remove a later slider in the same update', () => {
+ let later: ReturnType<typeof createSlider>;
+ const first = make({ onChange: () => later.dispose() });
+ later = make(); tick();
+ first.setValue(.5);
+ expect(() => tick()).not.toThrow();
+ expect(world.valid(later.entity)).toBe(false);
+ });
+
it('arrow keys nudge the focused slider (1% of range when continuous)', () => {
const onChange = vi.fn();
const slider = make({ min: 0, max: 100, value: 50, onChange });
diff --git a/sdk/tests/ui-slider-visual-wasm.test.ts b/sdk/tests/ui-slider-visual-wasm.test.ts
new file mode 100644
index 000000000..06b452c85
--- /dev/null
+++ b/sdk/tests/ui-slider-visual-wasm.test.ts
@@ -0,0 +1,41 @@
+// 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 { UIEventQueue } from '../src/ui/core/events';
+import { UINode } from '../src/ui/core/ui-node';
+import { UIVisual } from '../src/ui/core/ui-visual';
+import { UISlider, createSliderSystem } from '../src/ui/behavior/slider';
+import { createSlider } from '../src/ui/widgets/slider';
+import type { CppRegistry, ESEngineModule } from '../src/wasm';
+import { HAS_WASM, loadWasmModule } from './helpers/loadWasm';
+
+describe.skipIf(!HAS_WASM)('slider visual reconciliation 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, createSliderSystem(app.world, events));
+ });
+ afterEach(() => { app.quit({ keepRenderer: true }); registry.delete(); });
+ it('range edits repaint fill and thumb without reporting a value change', async () => {
+ const changed = vi.fn(), slider = createSlider({ world: app.world, events, value: .5, onChange: changed });
+ await app.tick(1 / 60);
+ app.world.update(slider.entity, UISlider, d => { d.max = 2; }); await app.tick(1 / 60);
+ expect(app.world.get(slider.fillEntity, UIVisual).fillAmount).toBe(.25);
+ expect(app.world.get(slider.handleEntity, UINode).insetLeft.value).toBe(25);
+ expect(changed).not.toHaveBeenCalled();
+ });
+ it('new visual targets receive the current fraction immediately', async () => {
+ const slider = createSlider({ world: app.world, events, value: .5 }); await app.tick(1 / 60);
+ const fill = app.world.spawn(), handle = app.world.spawn();
+ app.world.insert(fill, UIVisual); app.world.insert(handle, UINode);
+ app.world.update(slider.entity, UISlider, d => { d.fill = fill; d.handle = handle; });
+ await app.tick(1 / 60);
+ expect(app.world.get(fill, UIVisual).fillAmount).toBe(.5);
+ expect(app.world.get(handle, UINode).insetLeft.value).toBe(50);
+ });
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 7a80b4c1e..66af6c4ad 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 slider visuals after range and target edits':
+ { note: 'Slider visuals follow range and target edits.' },
'fix(ui): retire object drag capture before cancelled movement':
{ note: 'Object drags stop when their input or target becomes unavailable.' },
'fix(ui): cancel scroll capture and motion when input ownership changes':