Compare commits

...
Author SHA1 Message Date
l0ng-ai e0490d5c43 fix(terminal): don't take focus that moved elsewhere while the view loaded
A focus request made before xterm opened was applied whenever it opened.
If the user clicked another view meanwhile (the Terminal still loading), the
terminal took the caret back. 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.
2026-11-21 04:22:51 +08:00
2 changed files with 59 additions and 1 deletions

No files matched your search

+45
View File
@@ -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();
+14 -1
View File
@@ -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;
}