fix(workbench): center and label Quick Switch Scheme (#4897)

Quick Switch Scheme now opens in the center of the window and stays there when the window is resized. A title identifies the popup, and a numeric column shows shortcuts 1–4 for opening its categories. Theme previews and editor focus still restore when the popup closes.
This commit is contained in:
l0ng-ai
2026-11-21 07:23:50 +08:00
committed by GitHub
parent a17af582f3
commit f1bfd5db52
9 changed files with 98 additions and 29 deletions
+41
View File
@@ -0,0 +1,41 @@
import { expect, openFromTree, test } from "./fixtures";
test("Quick Switch Scheme stays centered and opens its numbered choices without leaving the editor", async ({
workbench: page,
}) => {
const editor = await openFromTree(page, "build.gradle.kts");
const content = editor.locator(".cm-content");
await content.focus();
const root = page.locator("html");
const theme = await root.getAttribute("data-theme");
const colorTheme = await root.getAttribute("data-color-theme");
await page.keyboard.press("Control+Backquote");
const menu = page.getByRole("menu", { name: /^(Switch Scheme|Context menu)$/ });
await expect(menu).toBeVisible();
for (const { width, height } of [
{ width: 1120, height: 680 },
{ width: 620, height: 540 },
]) {
await page.setViewportSize({ width, height });
await expect(async () => {
const bounds = await menu.locator("xpath=..").boundingBox();
expect(Math.abs(bounds!.x + bounds!.width / 2 - width / 2)).toBeLessThan(1);
expect(Math.abs(bounds!.y + bounds!.height / 2 - height / 2)).toBeLessThan(1);
}).toPass({ timeout: 3000 });
}
await expect(menu.getByText("Switch Scheme", { exact: true })).toBeVisible();
await expect(menu.getByRole("menuitem", { name: "Editor Color Scheme" })).toBeFocused();
await expect(menu.locator("[data-menu-number]")).toHaveText(["1", "2", "3", "4"]);
await page.keyboard.press("4");
const light = page.getByRole("menuitemcheckbox", { name: "Solarized Light" });
await light.hover();
await expect(root).toHaveAttribute("data-color-theme", "solarized-light");
await page.keyboard.press("Escape");
await expect(light).toBeHidden();
await expect(menu).toBeVisible();
await page.keyboard.press("Escape");
await expect(menu).toBeHidden();
await expect(content).toBeFocused();
await expect.poll(() => root.getAttribute("data-theme")).toBe(theme);
await expect.poll(() => root.getAttribute("data-color-theme")).toBe(colorTheme);
});
+1
View File
@@ -476,6 +476,7 @@
"Files": "Files",
"Advanced options": "Advanced options",
"Synchronization": "Synchronization",
"Switch Scheme": "Switch Scheme",
"Files opened in associated applications": "Files opened in associated applications",
"Files matching these patterns open in their system application instead of an editor.": "Files matching these patterns open in their system application instead of an editor.",
"Show in Finder": "Show in Finder",
+1
View File
@@ -4205,6 +4205,7 @@
"Project Widget": "项目微件",
"Editor Color Scheme": "编辑器配色方案",
"Quick Switch Scheme…": "快速切换方案…",
"Switch Scheme": "切换方案",
"No saved layouts. Use Save Current Layout as… first.": "没有已保存的布局。请先使用“将当前布局另存为…”。",
"Enter a layout name": "请输入布局名称",
"A layout with this name exists": "已存在同名的布局",
+31 -19
View File
@@ -507,34 +507,41 @@ export function Menu({
// ---------------------------------------------------------------------------
// Context menus
type ContextMenuAnchor = { x: number; y: number } | "center";
type ContextMenuPosition = { x: number; y: number; centered?: false } | { centered: true };
interface ContextMenuState {
menu: {
x: number;
y: number;
items: MenuItem[];
numbers: boolean;
id: number;
/** The text selection the menu was opened on (see `restoreSelection`). */
selection: Range[] | null;
owner: Element | null;
} | null;
menu:
| (ContextMenuPosition & {
items: MenuItem[];
title?: string;
numbers: boolean;
id: number;
/** The text selection the menu was opened on (see `restoreSelection`). */
selection: Range[] | null;
owner: Element | null;
})
| null;
}
export const useContextMenuStore = create<ContextMenuState>(() => ({ menu: null }));
let menuSeq = 0;
export function showContextMenu(
at: { clientX: number; clientY: number } | { x: number; y: number },
at: { clientX: number; clientY: number } | ContextMenuAnchor,
items: MenuItem[],
options?: { numbers?: boolean; owner?: Element | null },
options?: { numbers?: boolean; owner?: Element | null; title?: string },
) {
const x = "clientX" in at ? at.clientX : at.x;
const y = "clientY" in at ? at.clientY : at.y;
const position: ContextMenuPosition =
at === "center"
? { centered: true }
: { x: "clientX" in at ? at.clientX : at.x, y: "clientY" in at ? at.clientY : at.y };
const title = options?.title;
useContextMenuStore.setState({
menu: {
x,
y,
items,
...position,
items: title ? [{ kind: "header", label: title }, ...items] : items,
title,
numbers: !!options?.numbers,
id: ++menuSeq,
selection: textSelection(),
@@ -592,7 +599,7 @@ export function ContextMenuHost() {
<Popup
key={menu.id}
open
anchor={{ x: menu.x, y: menu.y }}
anchor={menu.centered ? "center" : { x: menu.x, y: menu.y }}
placement="bottom-start"
offset={2}
role="presentation"
@@ -600,7 +607,12 @@ export function ContextMenuHost() {
onClose={hideContextMenu}
className={styles.popup}
>
<MenuList items={menu.items} numbers={menu.numbers} onDone={hideContextMenu} aria-label="Context menu" />
<MenuList
items={menu.items}
numbers={menu.numbers}
onDone={hideContextMenu}
aria-label={menu.title ? tx(menu.title) : "Context menu"}
/>
</Popup>
);
}
+7
View File
@@ -10,6 +10,13 @@
opacity: 1;
animation: popIn var(--dur-fast) var(--ease);
}
.centered {
position: fixed;
inset: 50% auto auto 50%;
translate: -50% -50%;
max-width: calc(100vw - 12px);
max-height: calc(100vh - 12px);
}
.popup::-webkit-scrollbar {
width: 10px;
height: 10px;
+12 -5
View File
@@ -37,7 +37,13 @@ import styles from "./Popup.module.css";
/** Layer ids of the popups a component is rendered inside: a dialog opened there leaves them open. */
export const PopupAncestry = createContext<readonly RefObject<number | null>[]>([]);
export type PopupAnchor = Element | VirtualElement | RefObject<Element | null> | { x: number; y: number } | null;
export type PopupAnchor =
| Element
| VirtualElement
| RefObject<Element | null>
| { x: number; y: number }
| "center"
| null;
export interface PopupProps {
open: boolean;
@@ -90,7 +96,7 @@ const measureNaturalHeight: Middleware = {
};
function toReference(anchor: PopupAnchor): Element | VirtualElement | null {
if (!anchor) return null;
if (!anchor || anchor === "center") return null;
if ("current" in anchor) return anchor.current;
if ("getBoundingClientRect" in anchor) return anchor;
const { x, y } = anchor;
@@ -127,6 +133,7 @@ function PopupInner({
onResized,
...aria
}: PopupProps) {
const centered = anchor === "center";
const reference = toReference(anchor);
const hasInlineMaxHeight = style?.maxHeight != null && style.maxHeight !== "";
const middleware = useMemo(
@@ -261,10 +268,10 @@ function PopupInner({
role={role}
aria-label={aria["aria-label"]}
tabIndex={-1}
className={clsx(styles.popup, !unstyled && styles.surface, className)}
data-positioned={isPositioned || undefined}
className={clsx(styles.popup, centered && styles.centered, !unstyled && styles.surface, className)}
data-positioned={centered || isPositioned || undefined}
data-resized={size ? "" : undefined}
style={{ ...floatingStyles, ...(size && { width: size.w, height: size.h }), ...style }}
style={{ ...(!centered && floatingStyles), ...(size && { width: size.w, height: size.h }), ...style }}
>
<PopupAncestry.Provider value={ancestry}>{children}</PopupAncestry.Provider>
{resizable && (
+2 -2
View File
@@ -74,7 +74,7 @@ describe("Quick Switch theme preview", () => {
it("previews from the Theme submenu and takes the preview back when the menu closes", async () => {
const before = shown();
render(<ContextMenuHost />);
act(() => showQuickSwitch({ x: 10, y: 10 }));
act(showQuickSwitch);
fireEvent.pointerEnter(screen.getByRole("menuitem", { name: "Theme" }));
act(() => vi.advanceTimersByTime(200));
fireEvent.pointerEnter(screen.getByRole("menuitemcheckbox", { name: "Solarized Light" }));
@@ -95,7 +95,7 @@ describe("Quick Switch theme preview", () => {
it("selects a theme clicked in the Theme submenu", async () => {
render(<ContextMenuHost />);
act(() => showQuickSwitch({ x: 10, y: 10 }));
act(showQuickSwitch);
fireEvent.pointerEnter(screen.getByRole("menuitem", { name: "Theme" }));
act(() => vi.advanceTimersByTime(200));
fireEvent.click(screen.getByRole("menuitemcheckbox", { name: "Solarized Dark" }));
+2 -2
View File
@@ -139,9 +139,9 @@ export function quickSwitchItems(preview?: ThemePreview): MenuItem[] {
}
/** Opens Quick Switch Scheme; a theme previewed there goes away with the menu unless chosen. */
export function showQuickSwitch(at: { x: number; y: number }) {
export function showQuickSwitch() {
const preview = themePreview();
showContextMenu(at, quickSwitchItems(preview));
showContextMenu("center", quickSwitchItems(preview), { title: "Switch Scheme", numbers: true });
const menu = useContextMenuStore.getState().menu;
const unsubscribe = useContextMenuStore.subscribe((s) => {
if (s.menu === menu) return;
+1 -1
View File
@@ -101,7 +101,7 @@ export function registerShellActions(): () => void {
title: "Quick Switch Scheme…",
category: "View",
keywords: ["theme", "color scheme", "keymap", "view mode"],
run: () => showQuickSwitch({ x: window.innerWidth / 2 - 120, y: 96 }),
run: showQuickSwitch,
},
{
id: "toolbar.customize",