From 9e7f842caff97da23861c9e0db42e9aef658a8fe Mon Sep 17 00:00:00 2001 From: SwiftChen <34408816+swift98@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:30:00 +0800 Subject: [PATCH] fix(toolbar): keep editor focus when clicking toolbar action buttons (#7792) Editor actions on the main toolbar (Collapse All, Expand All) and plugin toolbar slots did nothing when clicked: the button took focus on mousedown, so editorFocus was false by the time the click dispatched and the action's when clause rejected it. A press on a toolbar action now leaves focus where it is, except while a tool window has focus, where actions gated on !toolWindowFocus (Reformat Code, Select Next Tab) still need the click to leave the tool window. Covers the main toolbar and contributed toolbars. Closes #7758 Co-authored-by: swift98 <34408816+swift98@users.noreply.github.com> Co-authored-by: og-orca <301901286+og-orca@users.noreply.github.com> --- e2e/toolbarCustomization.spec.ts | 57 ++++++++++++++++++++++- src/workbench/ContributedToolbar.test.tsx | 25 ++++++++++ src/workbench/ContributedToolbar.tsx | 2 + src/workbench/TitleBar.test.tsx | 49 +++++++++++++++++++ src/workbench/TitleBar.tsx | 2 + src/workbench/focus.ts | 10 +++- 6 files changed, 143 insertions(+), 2 deletions(-) diff --git a/e2e/toolbarCustomization.spec.ts b/e2e/toolbarCustomization.spec.ts index 725f497bfc..748b54a8f6 100644 --- a/e2e/toolbarCustomization.spec.ts +++ b/e2e/toolbarCustomization.spec.ts @@ -1,4 +1,4 @@ -import { expect, test } from "./fixtures"; +import { expect, openFromTree, test } from "./fixtures"; test("Customize Main Toolbar keeps layout actions above the groups and stacks them in a narrow window", async ({ workbench: page, @@ -136,3 +136,58 @@ test("the project colour gradient follows the Project widget", async ({ workbenc await expect(icon).toHaveCount(0); await expect.poll(anchor).toBe(""); }); + +test("main toolbar editor actions keep editor focus and run Collapse All / Expand All", async ({ workbench: page }) => { + const editor = await openFromTree(page, "App.tsx"); + const content = editor.locator(".cm-content"); + await content.click(); + await page.keyboard.press("Meta+a"); + await page.keyboard.insertText( + [ + "export function formatOrder(total: number) {", + ' const currency = "USD";', + " return currency + total.toFixed(2);", + "}", + ].join("\n"), + ); + await expect(content).toBeFocused(); + + await page.evaluate(async () => { + const path = "/src/platform/project.ts"; + const { useProject } = await import(/* @vite-ignore */ path); + const settings = useProject.getState().settings; + useProject.setState({ + settings: { + ...settings, + appearance: { + ...settings?.appearance, + mainToolbar: { + left: ["project"], + center: ["action:code.foldAll", "action:code.unfoldAll", "action:editor.nextTab"], + right: ["action:navigate.searchEverywhere"], + hidden: [], + }, + }, + }, + }); + }); + + const bar = page.locator("header").first(); + await bar.getByRole("button", { name: "Collapse All", exact: true }).click({ delay: 80 }); + await expect(editor.locator(".cm-foldPlaceholder")).toHaveCount(1); + await expect(content).toBeFocused(); + + await bar.getByRole("button", { name: "Expand All", exact: true }).click({ delay: 80 }); + await expect(editor.locator(".cm-foldPlaceholder")).toHaveCount(0); + await expect(content).toBeFocused(); + + // With focus in a tool window, a toolbar click still leaves it so `!toolWindowFocus` actions run. + await openFromTree(page, "README.md"); + const activeTab = page.locator("[role=tab][aria-selected=true]"); + await expect(activeTab).toContainText("README.md"); + const projectTree = page.getByRole("tree", { name: "Project" }); + await projectTree.click(); + await expect(projectTree).toBeFocused(); + await bar.getByRole("button", { name: "Select Next Tab", exact: true }).click({ delay: 80 }); + await expect(activeTab).toContainText("App.tsx"); +}); diff --git a/src/workbench/ContributedToolbar.test.tsx b/src/workbench/ContributedToolbar.test.tsx index 9a7fe97858..795a36a6c3 100644 --- a/src/workbench/ContributedToolbar.test.tsx +++ b/src/workbench/ContributedToolbar.test.tsx @@ -1,5 +1,6 @@ import { afterEach, expect, it, vi } from "vitest"; import { render, screen, fireEvent } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; import { ContributedToolbar } from "./ContributedToolbar"; import { registerMenuSlotItem } from "./contributions"; import { registerAction } from "@/platform/actions"; @@ -31,3 +32,27 @@ it("renders toolbar slots with command icons and honors when clauses", () => { render(); expect(screen.queryByRole("button")).toBeNull(); }); +it("takes focus on a click from a tool window only", async () => { + const focused: (Element | null)[] = []; + offs.push( + registerAction({ id: "test.focus", title: "Act", run: () => void focused.push(document.activeElement) }), + registerMenuSlotItem("editor.title", { action: "test.focus", label: "Act", icon: "run" }), + ); + render( + <> +