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): retire text sessions when focus eligibility changes

yhhbc48595d4655
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e4ce05efb..13ff378c4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Ineligible text fields reject late editing callbacks immediately.** Hidden fields and controls outside a newly opened modal cannot commit delayed changes, composition endings or submissions. The rejected session releases its OS editing surface and preedit state, so eligibility returning before another frame reattaches the committed value instead of replaying stale input; fields inside the active dialog remain editable.
+
- **Keyboard focus observes authored visibility between layout passes.** Focus traversal and single-target checks read current display ancestry, so hiding a tree or opening a previously hidden modal immediately changes eligibility without waiting for the cached layout bit. Per-query ancestry memoization and borrowed component reads share work across controls; hosts without authored UI ancestry retain their existing visibility fallback.
- **Text-input length limits preserve complete supplementary characters.** Oversized host input still uses UTF-16 length limits, but truncation backs off when the boundary would split a valid surrogate pair. The corrected value and selection return to the editing surface together, preventing malformed emoji in committed input while preserving BMP limits and existing cursor semantics.
diff --git a/sdk/src/ui/text/text-input-plugin.ts b/sdk/src/ui/text/text-input-plugin.ts
index d532f948e..ae0033d19 100644
--- a/sdk/src/ui/text/text-input-plugin.ts
+++ b/sdk/src/ui/text/text-input-plugin.ts
@@ -13,6 +13,7 @@ import { Text, TextAlign, TextVerticalAlign, type TextData } from '../core/text'
import { Interactable, UIInteraction, type UIInteractionData } from '../input/interactable';
import { Focusable, FocusManager, FocusManagerState } from '../input/focusable';
import type { FocusableData } from '../input/focusable';
+import { isFocusEligible } from '../input/focus-inspection';
import { UICameraInfo, type UICameraData } from '../core/ui-camera-info';
import { Transform, type TransformData } from '../../ecs/component';
import { UIEvents, UIEventQueue, type UIEvent } from '../core/events';
@@ -114,19 +115,38 @@ export class TextInputPlugin implements Plugin {
const fm = app.getResource(FocusManager) as FocusManagerState | null;
if (!fm || fm.focusedEntity === null) return null;
const entity = fm.focusedEntity;
- if (!world.isEntityActive(entity) || !world.has(entity, TextInput)) return null;
+ if (!world.valid(entity) || !world.has(entity, TextInput)) return null;
if (world.get(entity, TextInput).readOnly) return null;
- if (world.has(entity, Interactable) && !world.get(entity, Interactable).enabled) return null;
- return entity;
+ return isFocusEligible(world, entity) ? entity : null;
}
function getEditingTextInput(): Entity | null {
return editingEntity !== null && getFocusedTextInput() === editingEntity ? editingEntity : null;
}
+ function detachEditor(): void {
+ const owner = editingEntity;
+ editingEntity = null;
+ composing = false;
+ if (owner === null) return;
+ if (world.valid(owner) && world.has(owner, TextInput) && world.get(owner, TextInput).focused) {
+ world.update(owner, TextInput, ti => { ti.focused = false; });
+ }
+ editor!.blur();
+ }
+
const unsubscribe = editor.subscribe((event) => {
const focused = getEditingTextInput();
- if (focused === null) return;
+ if (focused === null) {
+ // A hidden field or an opened modal can invalidate the owner
+ // between frames. Retire its preedit even if eligibility returns
+ // before the next focus tick; never detach a newly assigned field.
+ if (editingEntity !== null
+ && (app.getResource(FocusManager) as FocusManagerState).focusedEntity === editingEntity) {
+ detachEditor();
+ }
+ return;
+ }
switch (event.kind) {
case 'change':
if (!composing && focused !== null) syncFromEditor();
@@ -379,9 +399,7 @@ 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();
+ detachEditor();
}
if (currentFocused !== null) {
diff --git a/sdk/tests/text-input-focus-session.test.ts b/sdk/tests/text-input-focus-session.test.ts
index eccb4c23a..717ac69f1 100644
--- a/sdk/tests/text-input-focus-session.test.ts
+++ b/sdk/tests/text-input-focus-session.test.ts
@@ -7,6 +7,8 @@ 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 { UINode, UIDisplay } from '../src/ui/core/ui-node';
+import { UIDialog } from '../src/ui/behavior/dialog';
import type { App } from '../src/app/app';
vi.mock('../src/platform/base', async original => ({
@@ -97,3 +99,52 @@ it('releases focus after submission when the callback leaves the session unchang
expect(app.world.get(first, TextInput).focused).toBe(false);
expect(document.activeElement).not.toBe(textarea);
});
+
+it.each(['hidden', 'ancestor', 'modal'] as const)('rejects late editing callbacks after %s and restores the committed value', async mutation => {
+ const { app, first, textarea } = build();
+ const world = app.world, fm = app.getResource(FocusManager);
+ world.insert(first, UINode);
+ const root = world.spawn();
+ world.insert(root, UINode);
+ if (mutation === 'ancestor') world.setParent(first, root);
+ fm.focus(first);
+ await app.tick(1 / 60);
+ const changed = vi.fn(), submitted = vi.fn();
+ app.getResource(UIEvents).on(first, 'change', changed);
+ app.getResource(UIEvents).on(first, 'submit', submitted);
+ textarea.dispatchEvent(new CompositionEvent('compositionstart'));
+ if (mutation === 'modal') world.insert(root, UIDialog);
+ else world.update(mutation === 'hidden' ? first : root, UINode, n => { n.display = UIDisplay.None; });
+ textarea.value = 'late preedit';
+ textarea.dispatchEvent(new CompositionEvent('compositionend'));
+ textarea.dispatchEvent(new Event('input'));
+ textarea.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', cancelable: true }));
+ expect(world.get(first, TextInput).value).toBe('first');
+ expect(world.get(first, TextInput).focused).toBe(false);
+ expect(changed).not.toHaveBeenCalled();
+ expect(submitted).not.toHaveBeenCalled();
+ expect(document.activeElement).not.toBe(textarea);
+ // Eligibility returns before another focus tick. A rejected composition
+ // must not leave a composing latch or stale value attached to this field.
+ if (mutation === 'modal') world.remove(root, UIDialog);
+ else world.update(mutation === 'hidden' ? first : root, UINode, n => { n.display = UIDisplay.Flex; });
+ await app.tick(1 / 60);
+ expect(textarea.value).toBe('first');
+ textarea.value = 'resumed';
+ textarea.dispatchEvent(new Event('input'));
+ expect(world.get(first, TextInput).value).toBe('resumed');
+ expect(changed).toHaveBeenCalledOnce();
+});
+
+it('accepts editing inside the active modal subtree', async () => {
+ const { app, first, textarea } = build();
+ const root = app.world.spawn();
+ app.world.insert(root, UINode);
+ app.world.insert(root, UIDialog);
+ app.world.setParent(first, root);
+ app.getResource(FocusManager).focus(first);
+ await app.tick(1 / 60);
+ textarea.value = 'modal text';
+ textarea.dispatchEvent(new Event('input'));
+ expect(app.world.get(first, TextInput).value).toBe('modal text');
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index ae2320281..52c98024a 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): retire text sessions when focus eligibility changes':
+ { note: 'Ineligible text fields reject late editing callbacks immediately.' },
'fix(ui): resolve live display ancestry for keyboard focus':
{ note: 'Keyboard focus observes authored visibility between layout passes.' },
'fix(ui): preserve surrogate pairs when clamping host input':