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): bind text input events to the active session
yhhf120b6798c9a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 42ea6a7a7..81375e5d1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Text input events stay with the active editing session.** Switching fields no longer lets the previous surface's blur clear the new focus or its queued input overwrite the next value. Web and native editors reset interrupted composition state when focus restarts, so normal typing resumes without requiring a composition-end event.+- **Text caret picking measures long strings once.** Pointer placement in single-line, multiline and password fields accumulates glyph advances in linear time instead of repeatedly measuring every prefix. UTF-16 caret boundaries, glyph scaling, missing glyphs and masked password widths retain their existing positions.- **Disabled text fields reject queued input.** Text input checks effective entity activity and interaction state before accepting editor events, including disabled ancestors, and releases the editing surface on the next update.diff --git a/sdk/src/platform/native/textEditor.ts b/sdk/src/platform/native/textEditor.tsindex aefec3f49..d1c1aea4e 100644--- a/sdk/src/platform/native/textEditor.ts+++ b/sdk/src/platform/native/textEditor.ts@@ -57,6 +57,7 @@ export function createNativeTextEditor(bridge: NativeTextEditorBridge): Platformreturn {focus(next: TextEditorState, options: TextEditorOptions): void {+ composingNow = false;state = { ...next };focused = true;bridge.focus(next.value, next.selectionStart, next.selectionEnd,@@ -65,6 +66,7 @@ export function createNativeTextEditor(bridge: NativeTextEditorBridge): Platformblur(): void {if (!focused) return;focused = false;+ composingNow = false;bridge.blur();},read(): TextEditorState {diff --git a/sdk/src/platform/webTextEditor.ts b/sdk/src/platform/webTextEditor.tsindex 8eea1483d..8688ce9b8 100644--- a/sdk/src/platform/webTextEditor.ts+++ b/sdk/src/platform/webTextEditor.ts@@ -84,7 +84,10 @@ export function createWebTextEditor(): PlatformTextEditor | null {// field mirrors its selection each frame. This only reports the motion.emit({ kind: 'change' });};- const onBlur = (): void => emit({ kind: 'blur' });+ const onBlur = (): void => {+ composing = false;+ emit({ kind: 'blur' });+ };textarea.addEventListener('input', onInput);textarea.addEventListener('compositionstart', onCompositionStart);@@ -102,6 +105,7 @@ export function createWebTextEditor(): PlatformTextEditor | null {return {focus(state: TextEditorState, options: TextEditorOptions): void {+ composing = false;multiline = options.multiline;// No limit is the ABSENCE of the attribute — assigning a negative// maxLength is an IndexSizeError, not "unlimited".diff --git a/sdk/src/ui/text/text-input-plugin.ts b/sdk/src/ui/text/text-input-plugin.tsindex 52ac410de..ccc5b59a7 100644--- a/sdk/src/ui/text/text-input-plugin.ts+++ b/sdk/src/ui/text/text-input-plugin.ts@@ -104,6 +104,7 @@ export class TextInputPlugin implements Plugin {};let composing = false;+ let editingEntity: Entity | null = null;let cursorVisible = true;let cursorTimer = 0;let lastTime = 0;@@ -117,8 +118,13 @@ export class TextInputPlugin implements Plugin {return entity;}+ function getEditingTextInput(): Entity | null {+ return editingEntity !== null && getFocusedTextInput() === editingEntity ? editingEntity : null;+ }+const unsubscribe = editor.subscribe((event) => {- const focused = getFocusedTextInput();+ const focused = getEditingTextInput();+ if (focused === null) return;switch (event.kind) {case 'change':if (!composing && focused !== null) syncFromEditor();@@ -141,6 +147,8 @@ export class TextInputPlugin implements Plugin {if (focused !== null) blurCurrent();break;case 'blur':+ editingEntity = null;+ composing = false;if (focused !== null) {world.update(focused, TextInput, (ti) => { ti.focused = false; });(app.getResource(FocusManager) as FocusManagerState).blur();@@ -158,6 +166,7 @@ export class TextInputPlugin implements Plugin {});this.cleanupListeners_.set(app, () => {+ editingEntity = null;unsubscribe();editor.dispose();for (const ch of childrenOf.values()) {@@ -169,7 +178,7 @@ export class TextInputPlugin implements Plugin {});function syncFromEditor(): void {- const focused = getFocusedTextInput();+ const focused = getEditingTextInput();if (focused === null) return;const ti = world.get(focused, TextInput) as TextInputData;if (ti.readOnly) return;@@ -252,6 +261,8 @@ export class TextInputPlugin implements Plugin {const ti = world.get(entity, TextInput) as TextInputData;if (ti.readOnly) return;+ composing = false;+ editingEntity = entity;world.update(entity, TextInput, (d) => { d.focused = true; });editor!.focus(@@ -268,6 +279,8 @@ export class TextInputPlugin implements Plugin {}const fm = app.getResource(FocusManager) as FocusManagerState;fm.blur();+ editingEntity = null;+ composing = false;editor!.blur();}@@ -339,6 +352,8 @@ export class TextInputPlugin implements Plugin {if (world.valid(prevFocusedTextInput) && world.has(prevFocusedTextInput, TextInput)) {world.update(prevFocusedTextInput, TextInput, (ti) => { ti.focused = false; });}+ editingEntity = null;+ composing = false;editor.blur();}diff --git a/sdk/tests/native-text-editor-session.test.ts b/sdk/tests/native-text-editor-session.test.tsnew file mode 100644index 000000000..c60b1a0dc--- /dev/null+++ b/sdk/tests/native-text-editor-session.test.ts@@ -0,0 +1,24 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { expect, it, vi } from 'vitest';+import { createNativeTextEditor } from '../src/platform/native/textEditor';+import type { NativeTextEditorPush } from '../src/platform/native/bridge';++it('starts a fresh composition session when refocusing the native editor', () => {+ let push!: (event: NativeTextEditorPush) => void;+ const editor = createNativeTextEditor({+ focus: vi.fn(), blur: vi.fn(), write: vi.fn(),+ subscribe(handler) { push = handler; return () => {}; },+ });+ const listener = vi.fn();+ editor.subscribe(listener);+ const state = { value: '', selectionStart: 0, selectionEnd: 0, backward: false };+ const options = { multiline: false, maxLength: 0, password: false };+ editor.focus(state, options);+ push({ kind: 'state', value: 'preedit', selectionStart: 7, selectionEnd: 7, composing: true });+ editor.blur();+ editor.focus(state, options);+ push({ kind: 'state', value: 'next', selectionStart: 4, selectionEnd: 4, composing: true });+ expect(listener.mock.calls.filter(([event]) => event.kind === 'composition' && event.composing)).toHaveLength(2);+ editor.dispose();+});diff --git a/sdk/tests/text-input-focus-session.test.ts b/sdk/tests/text-input-focus-session.test.tsnew file mode 100644index 000000000..fa6f8d1fc--- /dev/null+++ b/sdk/tests/text-input-focus-session.test.ts@@ -0,0 +1,72 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { afterEach, expect, it, vi } from 'vitest';+import { bootMockApp } from './helpers/mockApp';+import { createWebTextEditor } from '../src/platform/webTextEditor';+import { TextInputPlugin } from '../src/ui/text/text-input-plugin';+import { TextInput } from '../src/ui/text/text-input';+import { FocusManager, FocusManagerState } from '../src/ui/input/focusable';+import { UIEvents, UIEventQueue } from '../src/ui/core/events';+import type { App } from '../src/app/app';++vi.mock('../src/platform/base', async original => ({+ ...await original<typeof import('../src/platform/base')>(),+ platformCreateTextEditor: () => createWebTextEditor(),+}));+const apps: App[] = [];+function build() {+ const app = bootMockApp().app;+ apps.push(app);+ app.addPlugin({ name: 'focus', build(app) {+ app.insertResource(FocusManager, new FocusManagerState());+ app.insertResource(UIEvents, new UIEventQueue());+ } }).addPlugin(new TextInputPlugin());+ const first = app.world.spawn(), second = app.world.spawn();+ app.world.insert(first, TextInput, { value: 'first' });+ app.world.insert(second, TextInput, { value: 'second' });+ return { app, first, second, textarea: document.querySelector<HTMLTextAreaElement>('textarea[data-estella-text-editor]')! };+}+afterEach(() => { for (const app of apps.splice(0)) app.quit(); });++it.each([false, true])('keeps the new field focused when switching, composing=%j', async composing => {+ const { app, first, second, textarea } = build();+ const fm = app.getResource(FocusManager);+ fm.focus(first);+ await app.tick(1 / 60);+ if (composing) textarea.dispatchEvent(new CompositionEvent('compositionstart'));+ fm.focus(second);+ await app.tick(1 / 60);+ expect(fm.focusedEntity).toBe(second);+ expect(app.world.get(first, TextInput).focused).toBe(false);+ expect(app.world.get(second, TextInput).focused).toBe(true);+ textarea.value = 'new text';+ textarea.dispatchEvent(new Event('input'));+ expect(app.world.get(second, TextInput).value).toBe('new text');+ expect(app.world.get(first, TextInput).value).toBe('first');+});++it('rejects old surface input while a new field is awaiting activation', async () => {+ const { app, first, second, textarea } = build();+ const fm = app.getResource(FocusManager);+ fm.focus(first);+ await app.tick(1 / 60);+ fm.focus(second);+ textarea.value = 'old pending input';+ textarea.dispatchEvent(new Event('input'));+ expect(app.world.get(second, TextInput).value).toBe('second');+});++it('accepts normal input after an interrupted composition loses and regains focus', async () => {+ const { app, first, textarea } = build();+ const fm = app.getResource(FocusManager);+ fm.focus(first);+ await app.tick(1 / 60);+ textarea.dispatchEvent(new CompositionEvent('compositionstart'));+ textarea.blur();+ expect(fm.focusedEntity).toBe(null);+ fm.focus(first);+ await app.tick(1 / 60);+ textarea.value = 'resumed';+ textarea.dispatchEvent(new Event('input'));+ expect(app.world.get(first, TextInput).value).toBe('resumed');+});diff --git a/sdk/tests/text-input-plugin-lifecycle.test.ts b/sdk/tests/text-input-plugin-lifecycle.test.tsindex 7905cc0f2..a0dd65ad0 100644--- a/sdk/tests/text-input-plugin-lifecycle.test.ts+++ b/sdk/tests/text-input-plugin-lifecycle.test.ts@@ -48,11 +48,12 @@ afterEach(() => {surfaces.length = 0;});-it('quitting an older app releases only its text surface and preserves newer input', () => {+it('quitting an older app releases only its text surface and preserves newer input', async () => {const plugin = new TextInputPlugin(), first = build(plugin), second = build(plugin);const field = second.world.spawn();second.world.insert(field, TextInput, { value: '' });second.getResource(FocusManager).focus(field);+ await second.tick(1 / 60);first.quit();expect(surfaces[0].dispose).toHaveBeenCalledOnce();expect(surfaces[0].listeners.size).toBe(0);diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex fcd1ed441..121fa31a7 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): bind text input events to the active session':+ { note: 'Text input events stay with the active editing session.' },'perf(ui): measure text caret prefixes in one pass':{ note: 'Text caret picking measures long strings once.' },'fix(ui): reject input on disabled text fields':