From b30b30311f3f8bfab04f4372bcfd7d344fd51f03 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 22 Nov 2026 06:47:40 +0800 Subject: [PATCH] fix(workbench): keep toolbar controls readable in narrow windows (#5087) The main toolbar shares the available text width between labels, so long project and run configuration names give up space before short branch names. At the minimum window width, dropdowns with leading icons show just those icons with their tooltips, while Run, Debug and the window buttons keep their hit targets. --- e2e/mainToolbarLayout.spec.ts | 97 +++++++++++++++++++++++ src/features/run/FlutterDeviceChooser.tsx | 5 +- src/features/run/RunWidget.tsx | 3 +- src/features/vcs/BranchWidget.module.css | 1 + src/features/vcs/BranchWidget.tsx | 9 ++- src/workbench/TitleBar.module.css | 26 +++++- src/workbench/TitleBar.tsx | 13 ++- src/workbench/toolbarLabelWidth.ts | 77 ++++++++++++++++++ 8 files changed, 222 insertions(+), 9 deletions(-) create mode 100644 e2e/mainToolbarLayout.spec.ts create mode 100644 src/workbench/toolbarLabelWidth.ts diff --git a/e2e/mainToolbarLayout.spec.ts b/e2e/mainToolbarLayout.spec.ts new file mode 100644 index 000000000..611b59485 --- /dev/null +++ b/e2e/mainToolbarLayout.spec.ts @@ -0,0 +1,97 @@ +import { expect, stubDesktop, test } from "./fixtures"; + +const projectName = "Regional Order Fulfillment and Payments"; +const runName = "Fulfillment Integration Checkout Suite"; + +test("the main toolbar compresses long labels before a short branch name", async ({ workbench: page }) => { + await stubDesktop(page); + await page.evaluate( + async ({ projectName, runName }) => { + const platform = "/src/platform/platformInfo.ts"; + const project = "/src/platform/project.ts"; + const vcs = "/src/features/vcs/store.ts"; + const run = "/src/features/run/store.ts"; + const { usePlatform } = await import(/* @vite-ignore */ platform); + const { useProject } = await import(/* @vite-ignore */ project); + const { useVcs } = await import(/* @vite-ignore */ vcs); + const { useRun } = await import(/* @vite-ignore */ run); + usePlatform.setState({ os: "windows", framelessTitleBar: true, overlayTitleBar: false }); + useProject.setState({ project: { ...useProject.getState().project, displayName: projectName } }); + useVcs.setState({ + available: true, + branch: { head: "main", headCommit: "a".repeat(40), upstream: null, ahead: 0, behind: 0, operation: null }, + }); + useRun.setState({ + loaded: true, + selectedId: "test", + configs: [ + { + id: "test", + name: runName, + kind: "shell", + command: "node", + args: [], + cwd: null, + env: {}, + detected: false, + isTest: false, + debugAdapter: null, + }, + ], + }); + }, + { projectName, runName }, + ); + + const toolbar = page.locator("header").first(); + const project = toolbar.getByText(projectName, { exact: true }); + const branch = toolbar.getByText("main", { exact: true }); + const run = toolbar.getByText(runName, { exact: true }); + const width = (label: typeof project) => + label.evaluate((element) => ({ shown: element.clientWidth, preferred: element.scrollWidth })); + const labels = [project, branch, run]; + + await page.setViewportSize({ width: 1150, height: 620 }); + const expanded = await Promise.all(labels.map(width)); + await page.setViewportSize({ width: 690, height: 620 }); + await expect.poll(async () => (await width(branch)).shown).toBe((await width(branch)).preferred); + const narrowed = await Promise.all([width(project), width(run)]); + for (const { shown, preferred } of narrowed) { + expect(shown).toBeGreaterThan(35); + expect(shown).toBeLessThan(preferred); + } + expect(Math.abs(narrowed[0]!.shown - narrowed[1]!.shown)).toBeLessThanOrEqual(2); + + await page.setViewportSize({ width: 400, height: 550 }); + const buttons = toolbar.locator("button:visible"); + await expect + .poll(async () => + buttons.evaluateAll((elements) => { + const bounds = elements + .filter((element) => getComputedStyle(element).visibility === "visible") + .map((element) => ({ + label: element.getAttribute("aria-label") ?? element.textContent, + rect: element.getBoundingClientRect(), + })) + .filter(({ rect }) => rect.width > 0) + .sort((a, b) => a.rect.left - b.rect.left); + return bounds + .filter( + ({ rect }, i) => + rect.left < 0 || rect.right > innerWidth || rect.left < (bounds[i - 1]?.rect.right ?? 0) - 1, + ) + .map(({ label, rect }) => [label, rect.left, rect.right]); + }), + ) + .toEqual([]); + await expect.poll(async () => (await Promise.all(labels.map(width))).map(({ shown }) => shown)).toEqual([0, 0, 0]); + + const chooser = toolbar.locator("[data-run-chooser]"); + await chooser.press("Enter"); + const configs = page.getByRole("dialog", { name: "Run/Debug Configurations" }); + await expect(configs.getByRole("option", { name: new RegExp(`^${runName}`), selected: true })).toBeVisible(); + await page.keyboard.press("Escape"); + + await page.setViewportSize({ width: 1150, height: 620 }); + await expect.poll(() => Promise.all(labels.map(width))).toEqual(expanded); +}); diff --git a/src/features/run/FlutterDeviceChooser.tsx b/src/features/run/FlutterDeviceChooser.tsx index 5b01558dd..6133dd033 100644 --- a/src/features/run/FlutterDeviceChooser.tsx +++ b/src/features/run/FlutterDeviceChooser.tsx @@ -75,6 +75,7 @@ function Chooser({ root }: { root: string }) { ref={anchor} variant="title" className={styles.widgetDevice} + data-toolbar-icon-dropdown="" tooltip={pinned ? tx`Flutter device, pinned by “${selected?.name ?? ""}”` : tx("Flutter Device")} aria-label={tx`Flutter device: ${label}`} aria-haspopup="menu" @@ -82,7 +83,9 @@ function Chooser({ root }: { root: string }) { onClick={() => void executeAction("run.flutterChooseDevice")} > - {label} + + {label} + void executeAction("run.chooseConfig", "select")} > {selected && ( @@ -82,7 +83,7 @@ export function RunWidget() { )} )} - + {selected ? label(selected) : tx(currentFile ? "Current File" : "Select Configuration")} diff --git a/src/features/vcs/BranchWidget.module.css b/src/features/vcs/BranchWidget.module.css index 2051f3b5f..04b5b8971 100644 --- a/src/features/vcs/BranchWidget.module.css +++ b/src/features/vcs/BranchWidget.module.css @@ -20,6 +20,7 @@ } .repo { flex: none; + --toolbar-label-max: 120px; max-width: 120px; overflow: hidden; text-overflow: ellipsis; diff --git a/src/features/vcs/BranchWidget.tsx b/src/features/vcs/BranchWidget.tsx index 37e08d4d0..68442411f 100644 --- a/src/features/vcs/BranchWidget.tsx +++ b/src/features/vcs/BranchWidget.tsx @@ -148,6 +148,7 @@ export function BranchWidget() { <> useBranchesPopup.setState({ open: !open })} > - {repo && {repo}} - + {repo && ( + + {repo} + + )} + {inProgress(branch.operation) ? operationTitle(inProgress(branch.operation)!, branch.head) : (presented?.text ?? headLabel(branch))} diff --git a/src/workbench/TitleBar.module.css b/src/workbench/TitleBar.module.css index f11c40441..024f3002a 100644 --- a/src/workbench/TitleBar.module.css +++ b/src/workbench/TitleBar.module.css @@ -62,6 +62,14 @@ gap: var(--space-1); min-width: 0; } +.bar > .widget, +.item:has([data-toolbar-label]) { + flex: none; +} +.bar [data-toolbar-label] { + min-inline-size: 0; + max-inline-size: min(var(--toolbar-label-width, 100vw), var(--toolbar-label-max, 100vw)); +} /* Main toolbar dropdown widgets. */ .widget { min-width: 0; @@ -256,7 +264,7 @@ color: var(--fg-caption-close-hover); } -/* A narrow window: tighter spacing, and the widgets give way (ellipsis) before anything overlaps. */ +/* A narrow window leaves more room for widgets and window controls. */ @media (max-width: 560px) { .bar { gap: var(--space-0-5); @@ -264,6 +272,10 @@ .widget { padding: 0 4px; } + .item button:has(> [data-toolbar-label]) { + margin-inline: 0; + padding-inline: var(--space-1); + } .tools { margin-left: 2px; } @@ -283,3 +295,15 @@ width: 28px; } } + +/* The leading icons identify these dropdowns when the toolbar has to use its smallest spacing. */ +@media (max-width: 480px) { + .bar [data-toolbar-icon-dropdown] { + gap: var(--space-0-5); + min-inline-size: var(--h-control-small); + padding-inline: var(--space-0-5); + } + .bar [data-toolbar-icon-dropdown] > svg:last-child { + display: none; + } +} diff --git a/src/workbench/TitleBar.tsx b/src/workbench/TitleBar.tsx index 28ddfe0e7..a96f78e2e 100644 --- a/src/workbench/TitleBar.tsx +++ b/src/workbench/TitleBar.tsx @@ -33,6 +33,7 @@ import styles from "./TitleBar.module.css"; import { useNativeTitlebar } from "./nativeTitlebar"; import { useDocumentTitle } from "./documentTitle"; import { windowDragRegion } from "./window-drag"; +import { useToolbarLabelWidth } from "./toolbarLabelWidth"; export function orderTitleItems(items: Iterable) { const all = [...items]; @@ -76,6 +77,7 @@ function ProjectButton() { variant="title" className={styles.widget} tooltip={root} + data-toolbar-icon-dropdown="" aria-label={label} aria-haspopup={Popup ? "dialog" : "menu"} aria-expanded={open} @@ -84,7 +86,9 @@ function ProjectButton() { - {label} + + {label} + {Popup ? ( @@ -118,7 +122,7 @@ function VcsFallback() { aria-expanded={open} onClick={() => setOpen((v) => !v)} > - + Version Control @@ -319,6 +323,8 @@ export function TitleBar({ const separateMenu = useProject((s) => s.settings?.appearance?.mainMenuStyle === "toolbar"); const [bar, setBar] = useState(null); useNativeTitlebar(bar); + const toolbar = !minimal && !lightEdit && !compact && !bare; + useToolbarLabelWidth(toolbar ? bar : null); const className = clsx( styles.bar, overlay && styles.overlay, @@ -343,7 +349,6 @@ export function TitleBar({ ); const entry = (id: string) => ; - const toolbar = !minimal && !lightEdit; const menuRow = frameless && !minimal && separateMenu; const header = (
)} {toolbar && !menuExpanded && shown.left.map(entry)} -
+
{!minimal && ( <> {toolbar && !menuExpanded && shown.right.map(entry)} diff --git a/src/workbench/toolbarLabelWidth.ts b/src/workbench/toolbarLabelWidth.ts new file mode 100644 index 000000000..d7967dce4 --- /dev/null +++ b/src/workbench/toolbarLabelWidth.ts @@ -0,0 +1,77 @@ +import { useLayoutEffect } from "react"; + +const widthProperty = "--toolbar-label-width"; + +/** Short labels keep their full width. Divide the remaining space between the longer ones. */ +function labelLimit(widths: number[], available: number): number | undefined { + widths.sort((a, b) => a - b); + for (const [index, width] of widths.entries()) { + const limit = Math.max(0, Math.floor(available / (widths.length - index))); + if (width > limit) return limit; + available -= width; + } +} + +const pixels = (value: string) => Number.parseFloat(value) || 0; + +function measureLabelWidth(toolbar: HTMLElement) { + if (!toolbar.clientWidth) return; + const children = Array.from(toolbar.children).filter((child) => child.getClientRects().length > 0); + const labels = Array.from(toolbar.querySelectorAll("[data-toolbar-label]")).filter( + (label) => label.getClientRects().length > 0, + ); + const style = getComputedStyle(toolbar); + const padding = pixels(style.paddingLeft) + pixels(style.paddingRight); + const gaps = Math.max(0, children.length - 1) * pixels(style.columnGap); + const occupied = children.reduce((sum, child) => { + const childStyle = getComputedStyle(child); + const width = child.hasAttribute("data-toolbar-spacer") + ? pixels(childStyle.minWidth) + : child.getBoundingClientRect().width; + return sum + width + pixels(childStyle.marginLeft) + pixels(childStyle.marginRight); + }, 0); + const labelWidth = labels.reduce((sum, label) => sum + label.getBoundingClientRect().width, 0); + const available = toolbar.clientWidth - padding - gaps - occupied + labelWidth; + const preferredWidths = labels.map((label) => { + const localMax = getComputedStyle(label).getPropertyValue("--toolbar-label-max"); + return Math.min(label.scrollWidth, pixels(localMax) || Infinity); + }); + const limit = labelLimit(preferredWidths, available); + // Below one em, the icon and tooltip identify the control more clearly than clipped text. + const value = limit === undefined ? "" : `${limit < pixels(style.fontSize) ? 0 : limit}px`; + if (toolbar.style.getPropertyValue(widthProperty) !== value) toolbar.style.setProperty(widthProperty, value); +} + +export function useToolbarLabelWidth(toolbar: HTMLElement | null) { + useLayoutEffect(() => { + if (!toolbar || typeof ResizeObserver === "undefined") return; + let frame = 0; + const schedule = () => { + if (frame) return; + frame = requestAnimationFrame(() => { + frame = 0; + measureLabelWidth(toolbar); + }); + }; + const sizes = new ResizeObserver(schedule); + const observeEntries = () => { + sizes.disconnect(); + sizes.observe(toolbar); + for (const child of toolbar.children) sizes.observe(child); + }; + // A renamed label can stay the same size while clipped, so watch its text as well. + const content = new MutationObserver((changes) => { + if (changes.some((change) => change.target === toolbar)) observeEntries(); + schedule(); + }); + observeEntries(); + content.observe(toolbar, { childList: true, characterData: true, subtree: true }); + measureLabelWidth(toolbar); + return () => { + sizes.disconnect(); + content.disconnect(); + cancelAnimationFrame(frame); + toolbar.style.removeProperty(widthProperty); + }; + }, [toolbar]); +}