From 97f78908a1b0f061a290d8a448c82b87d7992000 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sat, 21 Nov 2026 04:23:06 +0800 Subject: [PATCH] fix(terminal): don't take focus that moved elsewhere while the view loaded (#4899) A focus request made before xterm opened was applied whenever it opened, so a terminal still loading took the caret back from a view the user had clicked meanwhile. The deferred focus now applies only while focus is where it was when requested, on the page, or inside the terminal's own tool window. --- src/features/terminal/session.test.ts | 45 +++++++++++++++++++++++++++ src/features/terminal/session.ts | 15 ++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/features/terminal/session.test.ts b/src/features/terminal/session.test.ts index be8405e22..d93f6e92f 100644 --- a/src/features/terminal/session.test.ts +++ b/src/features/terminal/session.test.ts @@ -422,6 +422,51 @@ describe("terminal open", () => { }); }); +describe("deferred terminal focus", () => { + function openable(key: string) { + const container = document.createElement("div"); + document.body.appendChild(container); + const session = new TerminalSession({ key, cwd: "/tmp", onChange: () => undefined, onFocus: () => undefined }); + Object.defineProperty(session.host, "offsetWidth", { configurable: true, get: () => 400 }); + Object.defineProperty(session.host, "offsetHeight", { configurable: true, get: () => 300 }); + const textarea = document.createElement("textarea"); + vi.spyOn(session.term, "open").mockImplementation(() => undefined); + Object.defineProperty(session.term, "textarea", { configurable: true, get: () => textarea }); + const focus = vi.spyOn(session.term, "focus").mockImplementation(() => undefined); + return { container, session, focus }; + } + + afterEach(() => vi.useRealTimers()); + + it("takes the caret once xterm opens when focus stayed put", () => { + vi.useFakeTimers(); + const { container, session, focus } = openable("s-stay"); + session.focus(); + session.attach(container); + vi.advanceTimersByTime(FIT_SETTLE_MS); + expect(focus).toHaveBeenCalledTimes(1); + session.dispose(); + container.remove(); + }); + + it("leaves focus that moved elsewhere before xterm opened", () => { + vi.useFakeTimers(); + const { container, session, focus } = openable("s-moved"); + const tree = document.createElement("div"); + tree.tabIndex = 0; + document.body.appendChild(tree); + session.focus(); + tree.focus(); + session.attach(container); + vi.advanceTimersByTime(FIT_SETTLE_MS); + expect(focus).not.toHaveBeenCalled(); + expect(document.activeElement).toBe(tree); + session.dispose(); + container.remove(); + tree.remove(); + }); +}); + describe("terminal font zoom", () => { afterEach(() => { unbindTerminals(); diff --git a/src/features/terminal/session.ts b/src/features/terminal/session.ts index 2b179fcee..ad987e20a 100644 --- a/src/features/terminal/session.ts +++ b/src/features/terminal/session.ts @@ -254,6 +254,8 @@ export class TerminalSession { private opened = false; /** `focus()` arrived before the textarea existed (the pane is focused while xterm is still closed). */ private pendingFocus = false; + /** What had focus when `pendingFocus` was set: a later move elsewhere cancels it. */ + private pendingFocusFrom: Element | null = null; /** Bounds retries when the pane has no box yet, or xterm cannot measure a cell. */ private fitAttempts = 0; private disposed = false; @@ -679,7 +681,17 @@ export class TerminalSession { this.opts.onFocus(this.key); }); this.watchImeCommit(); - if (this.pendingFocus) this.focus(); + if (this.pendingFocus) { + this.pendingFocus = false; + if (this.focusStayedHere()) this.focus(); + } + } + + /** Whether focus is still where the deferred `focus()` left it, or in this terminal's own window. */ + private focusStayedHere(): boolean { + const active = document.activeElement; + if (!active || active === document.body || active === this.pendingFocusFrom) return true; + return this.host.contains(active) || !!this.host.closest("[data-toolwindow]")?.contains(active); } detach(container: HTMLElement) { @@ -990,6 +1002,7 @@ export class TerminalSession { this.clearUnseenDone(); if (!this.opened || !this.term.textarea) { this.pendingFocus = true; + this.pendingFocusFrom = document.activeElement; this.scheduleFit(); return; }