fix(workbench): restore focus before running main-menu actions (#4310)

The custom main menu returns focus to the editor or field that opened it before running an action, so Edit › Undo / Redo reach the editor and workspace history (e.g. after Rename) instead of running while the menu button holds focus.
This commit is contained in:
l0ng-ai
2026-11-18 22:54:23 +08:00
committed by GitHub
parent 466556603d
commit 7ec0d3dbec
2 changed files with 75 additions and 7 deletions
+40 -1
View File
@@ -2,7 +2,7 @@ import { act, fireEvent, render, screen, within } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { commands, type Settings } from "@/platform/ipc";
import { useProject } from "@/platform/project";
import { registerActions } from "@/platform/actions";
import { getAction, registerActions } from "@/platform/actions";
import { usePlatform } from "@/platform/platformInfo";
import { MainMenu } from "./MainMenu";
@@ -60,6 +60,45 @@ afterEach(() => {
const titles = () => within(screen.getByRole("menubar"));
describe("main menu action focus", () => {
it.each([
{ style: "hamburger", narrow: false, row: false },
{ style: "merged", narrow: false, row: false },
{ style: "merged", narrow: true, row: false },
{ style: "toolbar", narrow: false, row: true },
])("restores the invoking field before Undo runs ($style, narrow=$narrow)", async ({ style, narrow, row }) => {
if (narrow) available = 100;
setStyle(style);
const focusAtUndo = vi.fn(() => document.activeElement);
vi.mocked(getAction("test.undo")!.run).mockImplementation(() => {
focusAtUndo();
});
render(
<>
<input aria-label="Editor field" />
<MainMenu row={row} />
</>,
);
const field = screen.getByRole("textbox", { name: "Editor field" });
field.focus();
const open = (button: HTMLElement) => {
fireEvent.pointerDown(button);
button.focus();
fireEvent.click(button);
};
if (style === "hamburger" || narrow) open(screen.getByRole("button", { name: "Main Menu" }));
if (narrow) {
const root = await screen.findByRole("menu", { name: "Main Menu" });
fireEvent.click(within(root).getByRole("menuitem", { name: /Edit/ }));
} else open(within(await screen.findByRole("menubar")).getByRole("menuitem", { name: "Edit" }));
const undo = await screen.findByRole("menuitem", { name: "Undo" });
expect(undo).toHaveFocus();
fireEvent.click(undo);
expect(focusAtUndo).toHaveReturnedWith(field);
expect(field).toHaveFocus();
});
});
describe("main menu presentation", () => {
it("keeps the hamburger default, expanding on click and collapsing on Escape", async () => {
const expanded = vi.fn();
+35 -6
View File
@@ -108,6 +108,17 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
const persistent = (merged && fits) || row;
const showTitles = persistent || (!merged && expanded);
const rememberFocus = (target: EventTarget | null) => {
if (
target instanceof HTMLElement &&
target !== document.body &&
target !== hamburger.current &&
!bar?.contains(target) &&
!target.closest('[role="menu"]')
)
previousFocus.current = target;
};
useEffect(() => {
let disposed = false;
if (merged || row)
@@ -155,6 +166,13 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
setOpen(null);
setExpanded(false);
};
// MenuList runs the action synchronously after onDone, before Popup unmounts.
const closeForAction = () => {
const target = previousFocus.current;
previousFocus.current = null;
close();
if (target?.isConnected) target.focus({ preventScroll: true });
};
const expand = async () => {
const top = await loadMenus();
if (!top.length) return;
@@ -188,10 +206,13 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
} else if (persistent) {
if (bar?.contains(document.activeElement)) previousFocus.current?.focus();
else {
previousFocus.current = document.activeElement as HTMLElement;
rememberFocus(document.activeElement);
focusTitle(0);
}
} else void expand();
} else {
rememberFocus(document.activeElement);
void expand();
}
});
});
const onKeyDown = (e: KeyboardEvent) => {
@@ -219,9 +240,13 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
title={t("menu.mainMenu")}
aria-haspopup="menu"
aria-expanded={expanded}
onFocus={(event) => rememberFocus(event.relatedTarget)}
onClick={() => {
if (expanded) close();
else void expand();
else {
rememberFocus(document.activeElement);
void expand();
}
}}
>
<Icon name="main-menu" size={20} />
@@ -238,7 +263,10 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
aria-haspopup="menu"
aria-expanded={open === i}
className={clsx(styles.menuTitle, open === i && styles.menuTitleOpen)}
onFocus={() => setFocused(i)}
onFocus={(event) => {
rememberFocus(event.relatedTarget);
setFocused(i);
}}
onPointerDown={() => {
pressedTitle.current = true;
setTimeout(() => {
@@ -252,6 +280,7 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
if (open !== null) setOpen(i);
}}
onClick={() => {
rememberFocus(document.activeElement);
if (open === i) close();
else setOpen(i);
}}
@@ -274,7 +303,7 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
>
<MenuList
items={menuItems(menus[open].children)}
onDone={close}
onDone={closeForAction}
aria-label={label(menus[open].id, menus[open].label)}
/>
</Popup>
@@ -302,7 +331,7 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
autoFocus={false}
className={menuStyles.popup}
>
<MenuList items={menuItems(menus)} onDone={close} aria-label={t("menu.mainMenu")} />
<MenuList items={menuItems(menus)} onDone={closeForAction} aria-label={t("menu.mainMenu")} />
</Popup>
)}
</div>