Файли
openidea/src/platform/initialFocus.test.ts
T
84ae5bb179 fix(windows): open every auxiliary window with keyboard focus in its content (#8113)
A diff opened with Show Diff in New Window came up with nothing focused, so the first F7 / Shift+F7 did nothing until a click; the comparison window's file tree took no keys either. installInitialFocus now runs for every auxiliary window: it tries focus candidates ([data-autofocus], editor hosts, trees, grids, lists, editor text) until one accepts focus, restores focus when the focused content is replaced (the next diff), skips disabled, hidden and inert content, and stops once the user acts. The diff window's body forwards focus to the diff editor and the comparison tree is marked as its window's focus target.

Also forgets a closed window's cached menu state.

Refs #7250. Supersedes #7282.

Co-authored-by: albince-mi <[email protected]>
Co-authored-by: arookieofc-ai <[email protected]>
2026-12-05 02:39:21 +08:00

132 рядки
4.7 KiB
TypeScript

import { afterEach, expect, it, vi } from "vitest";
import { initialFocusTarget, installInitialFocus } from "./initialFocus";
let uninstall: (() => void) | undefined;
afterEach(() => {
uninstall?.();
uninstall = undefined;
document.body.innerHTML = "";
vi.useRealTimers();
});
const install = (warn = vi.fn()) => {
uninstall = installInitialFocus(window, warn);
return warn;
};
/** Lets the mutation observer and microtasks run. */
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
function add(html: string): HTMLElement {
const holder = document.createElement("div");
holder.innerHTML = html;
document.body.append(holder);
return holder;
}
it("focuses the window's content once it has loaded, not before", async () => {
install();
expect(document.activeElement).toBe(document.body);
add('<div role="status">Loading…</div>');
await settle();
expect(document.activeElement).toBe(document.body);
add('<div data-autofocus tabindex="-1" id="target"></div>');
await settle();
expect(document.activeElement?.id).toBe("target");
});
it("prefers the window's data-autofocus target over a tree or an editor before it", () => {
add('<div class="cm-content" contenteditable="true"></div><div role="tree" tabindex="0"></div>');
add('<input data-autofocus id="search">');
install();
expect(document.activeElement?.id).toBe("search");
});
it("falls back to a tree, then to editor text, when the window marks nothing", () => {
add('<div class="cm-content" contenteditable="true" id="text"></div><div role="tree" tabindex="0" id="tree"></div>');
expect(initialFocusTarget(document)?.id).toBe("tree");
document.getElementById("tree")!.remove();
expect(initialFocusTarget(document)?.id).toBe("text");
});
it("skips hidden and inert content", () => {
add(
'<div hidden><div data-autofocus tabindex="-1"></div></div><div inert><div role="tree" tabindex="0"></div></div>',
);
add('<div role="listbox" tabindex="0" id="list"></div>');
expect(initialFocusTarget(document)?.id).toBe("list");
});
it("keeps focus restoration pending until a delayed replacement accepts it", async () => {
const host = add(
'<div data-autofocus tabindex="-1" id="host"><div class="cm-content" contenteditable="true" id="old"></div></div>',
);
install();
(host.querySelector("#old") as HTMLElement).focus();
host.innerHTML = "";
await settle();
expect(document.activeElement).toBe(document.body);
host.innerHTML = '<div class="cm-content" contenteditable="true" id="replacement"></div>';
await settle();
expect(document.activeElement?.id).toBe("replacement");
});
it("tries fallback candidates when the first is disabled or CSS-hidden", async () => {
install();
add(
'<button data-autofocus disabled id="disabled"></button><div data-autofocus tabindex="-1" id="css-hidden" style="display:none"></div><div role="tree" tabindex="0" id="tree"></div>',
);
await settle();
expect(document.activeElement?.id).toBe("tree");
});
it("never takes focus from what has it", async () => {
const holder = add('<input id="typed">');
(holder.querySelector("#typed") as HTMLInputElement).focus();
install();
add('<div data-autofocus tabindex="-1"></div>');
await settle();
expect(document.activeElement?.id).toBe("typed");
});
it("focuses the replacement when the focused content is removed (the window's next diff)", async () => {
const host = add(
'<div data-autofocus tabindex="-1" id="host"><div class="cm-content" contenteditable="true" id="old"></div></div>',
);
install();
(host.querySelector("#old") as HTMLElement).focus();
host.querySelector("#old")!.remove();
await settle();
await settle();
expect(document.activeElement?.id).toBe("host");
});
it("leaves focus alone after a click on empty space", async () => {
add('<div data-autofocus tabindex="-1" id="host"></div><div id="gap"></div>');
install();
expect(document.activeElement?.id).toBe("host");
document.getElementById("gap")!.dispatchEvent(new Event("pointerdown", { bubbles: true }));
(document.activeElement as HTMLElement).blur();
add("<span>later content</span>");
await settle();
await settle();
expect(document.activeElement).toBe(document.body);
});
it("stops waiting once the user does something", async () => {
install();
document.dispatchEvent(new KeyboardEvent("keydown", { key: "a" }));
add('<div data-autofocus tabindex="-1"></div>');
await settle();
expect(document.activeElement).toBe(document.body);
});
it("warns about a window that never offers anything to focus", () => {
vi.useFakeTimers();
const warn = install();
add("<p>nothing focusable</p>");
vi.advanceTimersByTime(15_000);
expect(warn).toHaveBeenCalledWith(expect.stringContaining("data-autofocus"));
});