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): isolate text input cleanup per app

yhh4e5855a55ab5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 82005c27e..8a41a7196 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Text input surfaces stay with their own apps.** Closing an older app or a preview removes its own text-editor listeners and surface, preserving typing in other running apps. Repeated cleanup releases each surface once.
+
- **Tilemap atlas and grid edits refresh cached meshes.** Changing columns, texture dimensions, atlas borders or grid layout updates UVs and geometry without repainting tile data. Cached meshes now compare their resolved layout inputs, while unchanged inputs retain their geometry; editor pixel checks compare live edits with a fresh repaint.
- **Static siblings reuse their parent transform snapshot.** Unchanged static bodies sharing a parent avoid repeated child-world-pose reads while dynamic bodies keep composition active. Parent movement and direct child edits still update their poses in place, and cached parent snapshots are removed when no static body references them.
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index b97dc3af8..f9cb1c175 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -8575,7 +8575,7 @@ world: World
## TextInputPlugin — class @experimental
```
build: (app: App) => void
-cleanup: () => void
+cleanup: (app?: App) => void
dependencies: "focus"[]
name: "textInput"
static new (): TextInputPlugin
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 64faa5d72..1f4943165 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -8779,7 +8779,7 @@ world: World
## TextInputPlugin — class @experimental
```
build: (app: App) => void
-cleanup: () => void
+cleanup: (app?: App) => void
dependencies: "focus"[]
name: "textInput"
static new (): TextInputPlugin
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 50e772d56..e3872f10e 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -8570,7 +8570,7 @@ world: World
## TextInputPlugin — class @experimental
```
build: (app: App) => void
-cleanup: () => void
+cleanup: (app?: App) => void
dependencies: "focus"[]
name: "textInput"
static new (): TextInputPlugin
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 9af477fc1..a593086a4 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -8843,7 +8843,7 @@ world: World
## TextInputPlugin — class @experimental
```
build: (app: App) => void
-cleanup: () => void
+cleanup: (app?: App) => void
dependencies: "focus"[]
name: "textInput"
static new (): TextInputPlugin
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index bee310953..3a5d1d5dc 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -8577,7 +8577,7 @@ world: World
## TextInputPlugin — class @experimental
```
build: (app: App) => void
-cleanup: () => void
+cleanup: (app?: App) => void
dependencies: "focus"[]
name: "textInput"
static new (): TextInputPlugin
diff --git a/sdk/src/ui/text/text-input-plugin.ts b/sdk/src/ui/text/text-input-plugin.ts
index 10c298942..97a81b4a5 100644
--- a/sdk/src/ui/text/text-input-plugin.ts
+++ b/sdk/src/ui/text/text-input-plugin.ts
@@ -48,16 +48,22 @@ export class TextInputPlugin implements Plugin {
name = PluginName.TextInput;
dependencies = [PluginName.Focus];
- private cleanupListeners_: (() => void) | null = null;
-
- cleanup(): void {
- if (this.cleanupListeners_) {
- this.cleanupListeners_();
- this.cleanupListeners_ = null;
+ private cleanupListeners_ = new Map<App, () => void>();
+
+ cleanup(app?: App): void {
+ if (app) {
+ const cleanup = this.cleanupListeners_.get(app);
+ this.cleanupListeners_.delete(app);
+ cleanup?.();
+ } else {
+ const cleanups = [...this.cleanupListeners_.values()];
+ this.cleanupListeners_.clear();
+ for (const cleanup of cleanups) cleanup();
}
}
build(app: App): void {
+ this.cleanup(app);
registerComponent('TextInput', TextInput);
if (!playModeOnly()) return;
@@ -150,7 +156,7 @@ export class TextInputPlugin implements Plugin {
}
});
- this.cleanupListeners_ = () => {
+ this.cleanupListeners_.set(app, () => {
unsubscribe();
editor.dispose();
for (const ch of childrenOf.values()) {
@@ -159,7 +165,7 @@ export class TextInputPlugin implements Plugin {
if (world.valid(ch.caret)) world.despawn(ch.caret);
}
childrenOf.clear();
- };
+ });
function syncFromEditor(): void {
const focused = getFocusedTextInput();
diff --git a/sdk/tests/text-input-plugin-lifecycle.test.ts b/sdk/tests/text-input-plugin-lifecycle.test.ts
new file mode 100644
index 000000000..048ef9c9c
--- /dev/null
+++ b/sdk/tests/text-input-plugin-lifecycle.test.ts
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { afterEach, expect, it, vi } from 'vitest';
+import type { TextEditorEvent } from '../src/platform/types';
+
+const surfaces: ReturnType<typeof createSurface>[] = [];
+function createSurface() {
+ const listeners = new Set<(event: TextEditorEvent) => void>();
+ return {
+ listeners,
+ focus: vi.fn(), blur: vi.fn(), write: vi.fn(),
+ read: () => ({ value: 'typed', selectionStart: 5, selectionEnd: 5, backward: false }),
+ subscribe: (listener: (event: TextEditorEvent) => void) => {
+ listeners.add(listener);
+ return () => listeners.delete(listener);
+ },
+ dispose: vi.fn(),
+ };
+}
+vi.mock('../src/platform/base', async original => ({
+ ...await original<typeof import('../src/platform/base')>(),
+ platformCreateTextEditor: () => {
+ const surface = createSurface();
+ surfaces.push(surface);
+ return surface;
+ },
+}));
+const { App } = await import('../src/app/app');
+const { TextInputPlugin } = await import('../src/ui/text/text-input-plugin');
+const { FocusManager, FocusManagerState } = await import('../src/ui/input/focusable');
+const { UIEvents, UIEventQueue } = await import('../src/ui/core/events');
+const { TextInput } = await import('../src/ui/text/text-input');
+const apps: ReturnType<typeof App.new>[] = [];
+function build(plugin: InstanceType<typeof TextInputPlugin>) {
+ const app = App.new();
+ apps.push(app);
+ app.addPlugin({ name: 'focus', build(app) {
+ app.insertResource(FocusManager, new FocusManagerState());
+ app.insertResource(UIEvents, new UIEventQueue());
+ } }).addPlugin(plugin);
+ return app;
+}
+afterEach(() => {
+ for (const app of apps.splice(0)) app.quit();
+ surfaces.length = 0;
+});
+
+it('quitting an older app releases only its text surface and preserves newer input', () => {
+ 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);
+ first.quit();
+ expect(surfaces[0].dispose).toHaveBeenCalledOnce();
+ expect(surfaces[0].listeners.size).toBe(0);
+ expect(surfaces[1].dispose).not.toHaveBeenCalled();
+ for (const listener of surfaces[1].listeners) listener({ kind: 'change' });
+ expect(second.world.get(field, TextInput).value).toBe('typed');
+ second.quit();
+ expect(surfaces[1].dispose).toHaveBeenCalledOnce();
+ expect(surfaces[1].listeners.size).toBe(0);
+});
+
+it('releases repeated preview surfaces without losing the main surface', () => {
+ const plugin = new TextInputPlugin(), main = build(plugin);
+ for (let i = 0; i < 100; i++) build(plugin).quit();
+ expect(surfaces[0].dispose).not.toHaveBeenCalled();
+ expect(surfaces[0].listeners.size).toBe(1);
+ for (const surface of surfaces.slice(1)) {
+ expect(surface.dispose).toHaveBeenCalledOnce();
+ expect(surface.listeners.size).toBe(0);
+ }
+ main.quit();
+ expect(surfaces[0].dispose).toHaveBeenCalledOnce();
+});
+
+it('explicit cleanup is idempotent and leaves another app subscribed', () => {
+ const plugin = new TextInputPlugin(), first = build(plugin), second = build(plugin);
+ plugin.cleanup(first);
+ plugin.cleanup(first);
+ first.quit();
+ expect(surfaces[0].dispose).toHaveBeenCalledOnce();
+ expect(surfaces[1].listeners.size).toBe(1);
+ second.quit();
+ expect(surfaces[1].dispose).toHaveBeenCalledOnce();
+});
+
+it('cleanup without an app releases every remaining surface exactly once', () => {
+ const plugin = new TextInputPlugin();
+ build(plugin);
+ build(plugin);
+ plugin.cleanup();
+ plugin.cleanup();
+ for (const surface of surfaces) {
+ expect(surface.dispose).toHaveBeenCalledOnce();
+ expect(surface.listeners.size).toBe(0);
+ }
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 6afe84276..8886424b7 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): isolate text input cleanup per app':
+ { note: 'Text input surfaces stay with their own apps.' },
'fix(render): refresh tilemap meshes after layout changes':
{ note: 'Tilemap atlas and grid edits refresh cached meshes.' },
'perf(physics): reuse static parent transform snapshots':