From f1bfd5db52a5f04d207e5247295ed4075d07b2ca Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sat, 21 Nov 2026 07:23:50 +0800 Subject: [PATCH] fix(workbench): center and label Quick Switch Scheme (#4897) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- e2e/quickSwitch.spec.ts | 41 ++++++++++++++++++++++++ src/platform/locales/text/en.json | 1 + src/platform/locales/text/zh.json | 1 + src/ui/Menu.tsx | 50 ++++++++++++++++++------------ src/ui/Popup.module.css | 7 +++++ src/ui/Popup.tsx | 17 +++++++--- src/workbench/quickSwitch.test.tsx | 4 +-- src/workbench/quickSwitch.ts | 4 +-- src/workbench/shellActions.ts | 2 +- 9 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 e2e/quickSwitch.spec.ts diff --git a/e2e/quickSwitch.spec.ts b/e2e/quickSwitch.spec.ts new file mode 100644 index 000000000..dde8f848f --- /dev/null +++ b/e2e/quickSwitch.spec.ts @@ -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); +}); diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index fb6bc89d4..e74f1610f 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -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", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 73aee8453..50ffdcc79 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -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": "已存在同名的布局", diff --git a/src/ui/Menu.tsx b/src/ui/Menu.tsx index 530d517de..94070bd1b 100644 --- a/src/ui/Menu.tsx +++ b/src/ui/Menu.tsx @@ -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(() => ({ 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() { - + ); } diff --git a/src/ui/Popup.module.css b/src/ui/Popup.module.css index 8a2ab8bfb..5e56134cd 100644 --- a/src/ui/Popup.module.css +++ b/src/ui/Popup.module.css @@ -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; diff --git a/src/ui/Popup.tsx b/src/ui/Popup.tsx index c2e89dc9e..d126858ec 100644 --- a/src/ui/Popup.tsx +++ b/src/ui/Popup.tsx @@ -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[]>([]); -export type PopupAnchor = Element | VirtualElement | RefObject | { x: number; y: number } | null; +export type PopupAnchor = + | Element + | VirtualElement + | RefObject + | { 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 }} > {children} {resizable && ( diff --git a/src/workbench/quickSwitch.test.tsx b/src/workbench/quickSwitch.test.tsx index 908f47a7e..84330e90a 100644 --- a/src/workbench/quickSwitch.test.tsx +++ b/src/workbench/quickSwitch.test.tsx @@ -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(); - 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(); - 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" })); diff --git a/src/workbench/quickSwitch.ts b/src/workbench/quickSwitch.ts index 8f280bac0..c86989de1 100644 --- a/src/workbench/quickSwitch.ts +++ b/src/workbench/quickSwitch.ts @@ -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; diff --git a/src/workbench/shellActions.ts b/src/workbench/shellActions.ts index b2c9c9a87..ee3a0d091 100644 --- a/src/workbench/shellActions.ts +++ b/src/workbench/shellActions.ts @@ -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",