mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-25 16:04:33 +00:00
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.
This commit is contained in:
@@ -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);
|
||||
});
|
||||
@@ -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")}
|
||||
>
|
||||
<Icon name={problem ? "warning" : "project-flutter"} size={16} />
|
||||
<span className={styles.widgetName}>{label}</span>
|
||||
<span className={styles.widgetName} data-toolbar-label="">
|
||||
{label}
|
||||
</span>
|
||||
<Icon name="chevron-down" size={14} className={styles.widgetChevron} />
|
||||
</BarButton>
|
||||
<Menu
|
||||
|
||||
@@ -71,6 +71,7 @@ export function RunWidget() {
|
||||
aria-haspopup="listbox"
|
||||
aria-expanded={chooserOpen}
|
||||
data-run-chooser=""
|
||||
data-toolbar-icon-dropdown={selected ? "" : undefined}
|
||||
onClick={() => void executeAction("run.chooseConfig", "select")}
|
||||
>
|
||||
{selected && (
|
||||
@@ -82,7 +83,7 @@ export function RunWidget() {
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
<span className={styles.widgetName}>
|
||||
<span className={styles.widgetName} data-toolbar-label="">
|
||||
{selected ? label(selected) : tx(currentFile ? "Current File" : "Select Configuration")}
|
||||
</span>
|
||||
<Icon name="chevron-down" size={14} className={styles.widgetChevron} />
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
}
|
||||
.repo {
|
||||
flex: none;
|
||||
--toolbar-label-max: 120px;
|
||||
max-width: 120px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
|
||||
@@ -148,6 +148,7 @@ export function BranchWidget() {
|
||||
<>
|
||||
<BarButton
|
||||
data-branches-widget
|
||||
data-toolbar-icon-dropdown=""
|
||||
variant="title"
|
||||
className={styles.widget}
|
||||
tooltip={tooltip}
|
||||
@@ -156,8 +157,12 @@ export function BranchWidget() {
|
||||
onClick={() => useBranchesPopup.setState({ open: !open })}
|
||||
>
|
||||
<Icon name={presented?.icon ?? (branch.head ? "branch" : "commit")} size={16} className={styles.icon} />
|
||||
{repo && <span className={styles.repo}>{repo}</span>}
|
||||
<span className={styles.name}>
|
||||
{repo && (
|
||||
<span className={styles.repo} data-toolbar-label="">
|
||||
{repo}
|
||||
</span>
|
||||
)}
|
||||
<span className={styles.name} data-toolbar-label="">
|
||||
{inProgress(branch.operation)
|
||||
? operationTitle(inProgress(branch.operation)!, branch.head)
|
||||
: (presented?.text ?? headLabel(branch))}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<TitleItemContribution>) {
|
||||
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() {
|
||||
<span className={styles.avatar} aria-hidden="true">
|
||||
{projectInitials(label)}
|
||||
</span>
|
||||
<span className={styles.widgetText}>{label}</span>
|
||||
<span className={styles.widgetText} data-toolbar-label="">
|
||||
{label}
|
||||
</span>
|
||||
<Icon name="chevron-down" size={12} className={styles.chevron} />
|
||||
</BarButton>
|
||||
{Popup ? (
|
||||
@@ -118,7 +122,7 @@ function VcsFallback() {
|
||||
aria-expanded={open}
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
>
|
||||
<span className={styles.widgetText}>
|
||||
<span className={styles.widgetText} data-toolbar-label="">
|
||||
<T>Version Control</T>
|
||||
</span>
|
||||
<Icon name="chevron-down" size={12} className={styles.chevron} />
|
||||
@@ -319,6 +323,8 @@ export function TitleBar({
|
||||
const separateMenu = useProject((s) => s.settings?.appearance?.mainMenuStyle === "toolbar");
|
||||
const [bar, setBar] = useState<HTMLElement | null>(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({
|
||||
</header>
|
||||
);
|
||||
const entry = (id: string) => <ToolbarEntry key={id} id={id} items={map} />;
|
||||
const toolbar = !minimal && !lightEdit;
|
||||
const menuRow = frameless && !minimal && separateMenu;
|
||||
const header = (
|
||||
<header
|
||||
@@ -364,7 +369,7 @@ export function TitleBar({
|
||||
</span>
|
||||
)}
|
||||
{toolbar && !menuExpanded && shown.left.map(entry)}
|
||||
<div className={styles.drag} {...windowDragRegion} />
|
||||
<div className={styles.drag} {...windowDragRegion} data-toolbar-spacer="" />
|
||||
{!minimal && (
|
||||
<>
|
||||
{toolbar && !menuExpanded && shown.right.map(entry)}
|
||||
|
||||
@@ -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<HTMLElement>("[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]);
|
||||
}
|
||||
Reference in New Issue
Block a user