From bd37494ee89adc8fb994b54ad1e99d22e4858fdd Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:17:20 +0800 Subject: [PATCH] fix(agents): keep composer toolbar on one row with dynamic overflow Co-authored-by: Cursor --- src/features/agents/Composer.module.css | 103 ++++--- src/features/agents/Composer.tsx | 4 +- src/features/agents/ConfigPickers.test.tsx | 115 +++++++- src/features/agents/ConfigPickers.tsx | 313 +++++++++++++++++---- src/features/agents/HomeEditor.test.tsx | 4 +- 5 files changed, 420 insertions(+), 119 deletions(-) diff --git a/src/features/agents/Composer.module.css b/src/features/agents/Composer.module.css index 725ea7a437..ae0784e534 100644 --- a/src/features/agents/Composer.module.css +++ b/src/features/agents/Composer.module.css @@ -86,26 +86,30 @@ } .bar { display: flex; - flex-wrap: wrap; + flex-wrap: nowrap; align-items: center; - gap: var(--space-0-5); + gap: var(--space-1); padding: 4px 6px 6px; cursor: default; } .toolbar { display: flex; - flex: 1; - flex-wrap: wrap; + flex: 1 1 0; + flex-wrap: nowrap; align-items: center; min-width: 0; + overflow: hidden; gap: var(--space-0-5); } +.toolbar > button { + flex: 0 1 auto; + min-width: 0; +} .actions { display: flex; flex: none; align-items: center; gap: var(--space-1); - margin-left: auto; } /* Attach/reference buttons share the send button's 24px footprint and chrome. */ /* Plain toolbar icons, framed only on hover like the rest of the IDE's toolbars. */ @@ -114,21 +118,60 @@ height: var(--h-control); } .spacer { - flex: 1; + display: none; } .configPicker, .modelControl { display: flex; + flex: 0 1 auto; min-width: 0; } .modelControl { align-items: center; } +.modelControl > .chip { + flex: 0 1 auto; + min-width: 0; +} .configPicker > button { + min-width: 0; max-width: 100%; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.inlineSetting { + flex: none; +} +.collapsed { + display: none; +} +.compactDetail { + display: none; } .settingsMenu { display: none; + flex: none; +} +.settingsMenuVisible { + display: flex; +} +/* Applied only synchronously during JS overflow measurement (never painted). */ +.toolbarMeasuring > *, +.toolbarMeasuring .configPicker, +.toolbarMeasuring .modelControl, +.toolbarMeasuring .modelControl > .chip, +.toolbarMeasuring .chip, +.toolbarMeasuring .configPicker > button { + flex: none; + max-width: none; +} +.toolbarMeasuring .collapsed, +.toolbarMeasuring .settingsMenu { + display: flex; +} +.toolbarMeasuring .compactDetail { + display: inline-grid; } .settingsPopup { width: min(320px, calc(100vw - 24px)); @@ -336,54 +379,6 @@ padding: 8px 14px 2px; border-top: 1px solid var(--border); } -@container agent-composer (max-width: 600px) { - /* Narrow: keep one row of icons next to the agent picker. The model/mode pickers - collapse into the settings menu, so nothing has to wrap onto a second line. */ - .bar { - flex-direction: row; - align-items: center; - gap: var(--space-1-5); - } - .toolbar { - flex-wrap: nowrap; - } - .toolbar > button, - .toolbar > .configPicker { - flex: 0 1 auto; - min-width: 0; - } - .configPicker > button { - min-width: 0; - max-width: 100%; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; - } - .chipDetail { - display: none; - } - .modelDetail { - flex-shrink: 0; - max-width: 65%; - } - .inlineSetting { - display: none; - } - .settingsMenu { - display: flex; - flex: none; - margin-left: auto; - } - .actions { - width: auto; - flex: none; - margin-left: 0; - justify-content: flex-end; - } - .actions .spacer { - display: none; - } -} .pickerValue { display: inline-flex; align-items: baseline; diff --git a/src/features/agents/Composer.tsx b/src/features/agents/Composer.tsx index 437df6b8f8..1a7dd8ef86 100644 --- a/src/features/agents/Composer.tsx +++ b/src/features/agents/Composer.tsx @@ -1323,7 +1323,9 @@ export function Composer(props: ComposerProps) { )}
e.stopPropagation()}> -
{props.toolbar}
+
+ {props.toolbar} +
{ @@ -95,7 +95,7 @@ describe("configPickerOptions", () => { }); }); -it("shows Cursor approvals separately from its execution mode and sends the choice", () => { +it("tucks Cursor approvals into the mode popup as a switch and sends the choice", () => { const option = (id: string, category: string, values: string[]): AgentConfigOption => ({ id, category, @@ -110,19 +110,19 @@ it("shows Cursor approvals separately from its execution mode and sends the choi })), }); const onChange = vi.fn(); - render( - , - ); - expect(screen.getByText("Agent")).toBeVisible(); - fireEvent.click(screen.getByLabelText("Approvals")); - fireEvent.click(screen.getByRole("option", { name: "Bypass approvals" })); + const cfg = [ + option("mode", "mode", ["Agent", "Plan", "Ask"]), + option("openidea.cursor.approval", "approval", ["ask", "bypass"]), + ]; + const { rerender } = render(); + expect(screen.getByRole("button", { name: "Mode" })).toHaveTextContent("Agent"); + expect(screen.queryByRole("button", { name: "Approvals" })).not.toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "Mode" })); + fireEvent.click(screen.getByRole("switch", { name: "Bypass approvals" })); expect(onChange).toHaveBeenCalledWith("openidea.cursor.approval", "bypass"); + + rerender(); + expect(screen.getByRole("button", { name: "Mode" })).toHaveTextContent("AgentBypass"); }); it("shows Allow All as a switch and sends on/off values", () => { @@ -501,4 +501,91 @@ describe("model settings", () => { render(); expect(screen.getByRole("button", { name: "Model" })).toHaveAttribute("aria-haspopup", "listbox"); }); + + it("omits off switches from multi-setting summaries and shows them concisely when on", () => { + const cfg = copilotConfig(); + cfg.push( + { + id: "context", + name: "Context", + category: "model_config", + description: null, + current: "500k", + via: "option", + values: [value("500k", "500K")], + }, + { + id: "fast", + name: "Fast", + category: "model_config", + description: null, + current: "false", + via: "option", + values: [value("false", "Off"), value("true", "Fast")], + }, + ); + const { rerender } = render(); + const settings = screen.getByRole("button", { name: /^Model settings:/ }); + expect(settings).toHaveTextContent("medium · 500K"); + expect(settings).toHaveAttribute("title", expect.stringContaining("Fast: Off")); + + rerender(); + expect(screen.getByRole("button", { name: /^Model settings:/ })).toHaveTextContent("medium · 500K · Fast"); + }); +}); + +describe("computeOverflowLayout", () => { + const base = { + gap: 2, + fixedWidths: [38, 220], // agent icon + model/settings + secondaryWidths: [120, 90], // mode (with detail) + collaboration mode + detailWidth: 45, + detailIndex: 0, + menuWidth: 24, + }; + + it("keeps everything visible when the toolbar fits", () => { + // 38 + 220 + 120 + 90 + 3*2 = 474 + expect(computeOverflowLayout({ ...base, available: 500 })).toEqual({ + compactDetail: false, + collapsedCount: 0, + }); + }); + + it("hides chip detail before collapsing any secondary picker", () => { + // 474 - 45 = 429 + expect(computeOverflowLayout({ ...base, available: 440 })).toEqual({ + compactDetail: true, + collapsedCount: 0, + }); + }); + + it("collapses trailing secondary pickers one by one as space shrinks", () => { + // Keep mode (full 120) + menu (24): 38 + 220 + 120 + 24 + 3*2 = 408 + expect(computeOverflowLayout({ ...base, available: 410 })).toEqual({ + compactDetail: false, + collapsedCount: 1, + }); + // Keep mode (compact 75) + menu (24): 38 + 220 + 75 + 24 + 3*2 = 363 + expect(computeOverflowLayout({ ...base, available: 370 })).toEqual({ + compactDetail: true, + collapsedCount: 1, + }); + // Collapse all secondary pickers into the menu: + expect(computeOverflowLayout({ ...base, available: 320 })).toEqual({ + compactDetail: false, + collapsedCount: 2, + }); + }); + + it("does nothing when unmeasured (available <= 0) or when there are no secondary items", () => { + expect(computeOverflowLayout({ ...base, available: 0 })).toEqual({ + compactDetail: false, + collapsedCount: 0, + }); + expect(computeOverflowLayout({ ...base, available: 100, secondaryWidths: [] })).toEqual({ + compactDetail: false, + collapsedCount: 0, + }); + }); }); diff --git a/src/features/agents/ConfigPickers.tsx b/src/features/agents/ConfigPickers.tsx index c3ce3e35d0..1cab7a7e87 100644 --- a/src/features/agents/ConfigPickers.tsx +++ b/src/features/agents/ConfigPickers.tsx @@ -1,7 +1,7 @@ /** * Model and settings controls shared by the New Task and session composers. */ -import { useEffect, useRef, useState } from "react"; +import { useEffect, useLayoutEffect, useRef, useState } from "react"; import clsx from "clsx"; import type { AgentConfigOption } from "@/bindings"; import { useTx } from "@/platform/i18n"; @@ -74,6 +74,103 @@ function switchValues(option: AgentConfigOption): { on: string; off: string } | return on && off ? { on, off } : null; } +interface ModeApprovalSwitch { + on: string; + off: string; + /** Switch label inside the Mode popup footer (e.g. "Allow All", "Bypass approvals"). */ + label: string; + /** Concise detail shown on the Mode chip when enabled (e.g. "Allow All", "Bypass"). */ + chipLabel: string; +} + +/** A two-state permissions or approval option tucked as a switch inside the Mode popup. */ +function modeApprovalSwitch(option: AgentConfigOption): ModeApprovalSwitch | null { + if (option.category === "permissions") { + const sw = switchValues(option); + return sw ? { ...sw, label: option.name, chipLabel: option.name } : null; + } + if (option.category === "approval" && option.values.length === 2) { + const on = option.values.find((v) => v.value.toLowerCase() === "bypass"); + const off = option.values.find((v) => v !== on); + if (on && off) { + return { on: on.value, off: off.value, label: on.name || option.name, chipLabel: "Bypass" }; + } + } + return null; +} + +export interface OverflowInput { + available: number; + gap: number; + fixedWidths: readonly number[]; + secondaryWidths: readonly number[]; + detailWidth: number; + detailIndex: number; + menuWidth: number; +} + +export interface OverflowLayout { + /** Hide secondary detail text inside chips (e.g. "Allow All" / "Bypass") before collapsing items. */ + compactDetail: boolean; + /** Number of trailing secondary items moved into the overflow settings menu. */ + collapsedCount: number; +} + +/** + * Decides how many secondary pickers fit on a single toolbar row before spilling into the + * settings menu: first drops optional chip detail labels, then collapses trailing secondary + * items one by one from right to left. + */ +export function computeOverflowLayout({ + available, + gap, + fixedWidths, + secondaryWidths, + detailWidth, + detailIndex, + menuWidth, +}: OverflowInput): OverflowLayout { + if (available <= 0 || secondaryWidths.length === 0) { + return { compactDetail: false, collapsedCount: 0 }; + } + + const fixedCount = fixedWidths.length; + const fixedTotal = fixedWidths.reduce((sum, w) => sum + w, 0); + + const widthFor = (kept: number, compactDetail: boolean): number => { + const hasMenu = kept < secondaryWidths.length; + const itemCount = fixedCount + kept + (hasMenu ? 1 : 0); + if (itemCount === 0) return 0; + let total = fixedTotal; + for (let i = 0; i < kept; i++) total += secondaryWidths[i]!; + if (compactDetail && detailWidth > 0 && detailIndex >= 0 && detailIndex < kept) { + total -= detailWidth; + } + if (hasMenu) total += menuWidth; + return total + gap * (itemCount - 1); + }; + + if (widthFor(secondaryWidths.length, false) <= available) { + return { compactDetail: false, collapsedCount: 0 }; + } + if (detailWidth > 0 && detailIndex >= 0 && widthFor(secondaryWidths.length, true) <= available) { + return { compactDetail: true, collapsedCount: 0 }; + } + + for (let collapsed = 1; collapsed <= secondaryWidths.length; collapsed++) { + const kept = secondaryWidths.length - collapsed; + if (kept === 0) return { compactDetail: false, collapsedCount: collapsed }; + if (widthFor(kept, false) <= available) { + return { compactDetail: false, collapsedCount: collapsed }; + } + if (detailWidth > 0 && detailIndex >= 0 && detailIndex < kept && widthFor(kept, true) <= available) { + return { compactDetail: true, collapsedCount: collapsed }; + } + } + + return { compactDetail: false, collapsedCount: secondaryWidths.length }; +} + const selected = (option: AgentConfigOption, value: string | null) => option.values.find((v) => v.value === value) ?? null; @@ -103,7 +200,9 @@ export function ConfigPickers({ }) { const tx = useTx(); const [open, setOpen] = useState(false); + const [overflow, setOverflow] = useState({ compactDetail: false, collapsedCount: 0 }); const anchor = useRef(null); + const menuRef = useRef(null); const options = mainOptions(config); if (modelRefresh?.draft && !options.some((o) => o.category === "model")) options.unshift({ @@ -124,53 +223,144 @@ export function ConfigPickers({ const tuckedEffort = combinedModel ? (effort ?? null) : null; const tuckedModelConfig = new Set(modelConfigs.map((o) => o.id)); const mode = options.find((o) => o.category === "mode"); - const allowAll = mode && options.find((o) => switchValues(o)); + const allowAll = mode && options.find((o) => !!modeApprovalSwitch(o)); const inline = options.filter( (o) => o !== effort && o !== allowAll && !(combinedModel && tuckedModelConfig.has(o.id)), ); if (effort && !tuckedEffort) inline.splice(model ? inline.indexOf(model) + 1 : 0, 0, effort); - // The compact menu holds everything but the model, effort, and model-specific parameters. + const collapsible = inline.filter((o) => o.category !== "model" && o !== effort); + // The overflow menu holds everything except the model, effort, and model-specific parameters. const secondary = options.filter( (o) => o.category !== "model" && o !== effort && !(combinedModel && tuckedModelConfig.has(o.id)), ); + const signature = [ + ...inline.map((o) => `${o.id}:${valueOf(o) ?? ""}`), + ...(tuckedEffort ? [`${tuckedEffort.id}:${valueOf(tuckedEffort) ?? ""}`] : []), + ...modelConfigs.map((o) => `${o.id}:${valueOf(o) ?? ""}`), + allowAll ? `${allowAll.id}:${valueOf(allowAll) ?? ""}` : "", + modelRefresh?.loadingSettings ? "loading" : "", + ].join("|"); + + useLayoutEffect(() => { + const menuEl = menuRef.current; + const toolbar = menuEl?.closest("[data-composer-toolbar]"); + if (!toolbar) return; + + const measure = () => { + const style = getComputedStyle(toolbar); + const padX = (parseFloat(style.paddingLeft) || 0) + (parseFloat(style.paddingRight) || 0); + const available = toolbar.getBoundingClientRect().width - padX; + if (available <= 0) return; + + toolbar.classList.add(styles.toolbarMeasuring!); + const gap = parseFloat(style.columnGap) || 0; + const fixedWidths: number[] = []; + const secondaryWidths: number[] = []; + let detailWidth = 0; + let detailIndex = -1; + let menuWidth = 0; + + for (const child of Array.from(toolbar.children) as HTMLElement[]) { + if (child.hasAttribute("data-config-menu")) { + menuWidth = child.getBoundingClientRect().width; + continue; + } + if (child.hasAttribute("data-config-secondary")) { + const idx = secondaryWidths.length; + secondaryWidths.push(child.getBoundingClientRect().width); + const detail = child.querySelector("[data-chip-detail]"); + if (detail) { + const chipGap = parseFloat(getComputedStyle(detail.parentElement!).columnGap) || 0; + detailWidth = detail.getBoundingClientRect().width + chipGap; + detailIndex = idx; + } + continue; + } + const w = child.getBoundingClientRect().width; + if (w > 0) fixedWidths.push(w); + } + + toolbar.classList.remove(styles.toolbarMeasuring!); + + const next = computeOverflowLayout({ + available, + gap, + fixedWidths, + secondaryWidths, + detailWidth, + detailIndex, + menuWidth, + }); + setOverflow((prev) => { + if (prev.compactDetail === next.compactDetail && prev.collapsedCount === next.collapsedCount) return prev; + if (prev.collapsedCount > 0 && next.collapsedCount === 0) setOpen(false); + return next; + }); + }; + + measure(); + const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(measure); + ro?.observe(toolbar); + const mo = typeof MutationObserver === "undefined" ? null : new MutationObserver(measure); + mo?.observe(toolbar, { childList: true }); + return () => { + ro?.disconnect(); + mo?.disconnect(); + }; + }, [signature, tx]); + + const collapsedCount = Math.min(overflow.collapsedCount, collapsible.length); + const modeIndex = mode ? collapsible.indexOf(mode) : -1; + const modeVisible = modeIndex >= 0 && modeIndex < collapsible.length - collapsedCount; const approval = secondary.find((o) => o.category === "approval" || o.category === "mode"); const summary = secondary.map((o) => `${tx(o.name)}: ${selected(o, valueOf(o))?.name ?? tx("Default")}`).join(" · "); return ( <> - {inline.map((o) => ( - - {o === model && combinedModel ? ( - opt.values.length > 0) - .map((opt) => ({ ...opt, current: valueOf(opt) }))} - onChange={onChange} - modelRefresh={modelRefresh} - /> - ) : o === mode && allowAll ? ( - - ) : ( - - )} - {o === model && !combinedModel && modelRefresh?.loadingSettings && } - - ))} + {inline.map((o) => { + const secondaryIndex = collapsible.indexOf(o); + const isSecondary = secondaryIndex >= 0; + const collapsed = isSecondary && secondaryIndex >= collapsible.length - collapsedCount; + return ( + + {o === model && combinedModel ? ( + opt.values.length > 0) + .map((opt) => ({ ...opt, current: valueOf(opt) }))} + onChange={onChange} + modelRefresh={modelRefresh} + /> + ) : o === mode && allowAll ? ( + + ) : ( + + )} + {o === model && !combinedModel && modelRefresh?.loadingSettings && } + + ); + })} {secondary.length > 0 && ( -
+
0 && styles.settingsMenuVisible)} + > ) { - return options - .map((o) => { - const label = settingValue(o)?.name ?? tx("Default"); - return o.category === "thought_level" || isContextOption(o.id) ? label : `${tx(o.name)}: ${label}`; - }) - .join(" · "); +function settingsSummary(options: AgentConfigOption[], tx: ReturnType, includeInactiveSwitches = false) { + const format = (o: AgentConfigOption) => { + const current = settingValue(o); + const label = current?.name ?? tx("Default"); + const sw = switchValues(o); + if (sw) { + const isOn = current?.value === sw.on; + if (!includeInactiveSwitches && !isOn && options.length > 1) return null; + return isOn ? tx(o.name) : `${tx(o.name)}: ${label}`; + } + return o.category === "thought_level" || o.category === "context_tier" || isContextOption(o.id) + ? label + : `${tx(o.name)}: ${label}`; + }; + const parts = options.map(format).filter((p): p is string => p !== null); + if (parts.length > 0) return parts.join(" · "); + return options.map((o) => `${tx(o.name)}: ${settingValue(o)?.name ?? tx("Default")}`).join(" · "); } function ModelPicker({ @@ -298,6 +498,7 @@ function ModelPicker({ : (selectedModel?.name ?? (modelRefresh ? modelValue : null) ?? tx("Default")); const shown = settings.filter((o) => o.values.length > 0); const summary = settingsSummary(shown, tx); + const fullSummary = settingsSummary(shown, tx, true); const toggle = (which: "models" | "settings") => { if (open === which) return setOpen(null); if (which === "models") modelRefresh?.onOpen?.(); @@ -327,7 +528,11 @@ function ModelPicker({ type="button" className={clsx(styles.chip, styles.modelDetail)} aria-label={tx`Model settings: ${summary}`} - title={tx`Configure ${modelName}…`} + title={ + fullSummary && fullSummary !== summary + ? `${tx`Configure ${modelName}…`} (${fullSummary})` + : tx`Configure ${modelName}…` + } aria-haspopup="dialog" aria-expanded={open === "settings"} onClick={() => toggle("settings")} @@ -507,27 +712,35 @@ function CostBadge({ category }: { category: string | null | undefined }) { ); } -/** The mode menu, with the agent's on/off permissions option (Allow All) as a switch under it. */ +/** The mode menu, with the agent's two-state permissions or approval option as a switch under it. */ function ModePicker({ mode, modeValue, allowAll, allowAllValue, + compactDetail = false, onChange, }: { mode: AgentConfigOption; modeValue: string | null; allowAll: AgentConfigOption; allowAllValue: string | null; + compactDetail?: boolean; onChange: (option: string, value: string) => void; }) { const tx = useTx(); const [open, setOpen] = useState(false); const anchor = useRef(null); - const toggle = switchValues(allowAll)!; + const toggle = modeApprovalSwitch(allowAll)!; const current = selected(mode, modeValue); const on = allowAllValue === toggle.on; - const summary = `${tx(mode.name)}: ${current?.name ?? tx("Default")} · ${tx(allowAll.name)}: ${on ? tx("On") : tx("Off")}`; + const approvalText = + allowAll.category === "approval" + ? (selected(allowAll, allowAllValue)?.name ?? (on ? tx("On") : tx("Off"))) + : on + ? tx("On") + : tx("Off"); + const summary = `${tx(mode.name)}: ${current?.name ?? tx("Default")} · ${tx(allowAll.name)}: ${approvalText}`; return ( <>
- {tx(allowAll.name)} + {tx(toggle.label)} onChange(allowAll.id, checked ? toggle.on : toggle.off)} />
diff --git a/src/features/agents/HomeEditor.test.tsx b/src/features/agents/HomeEditor.test.tsx index 6c82856ac6..afedae60c4 100644 --- a/src/features/agents/HomeEditor.test.tsx +++ b/src/features/agents/HomeEditor.test.tsx @@ -249,8 +249,8 @@ it("switches Cursor per-model context and fast options cleanly without sending s }; useAgents.setState({ providers: [cursor], draft: savedDraft({ provider: "cursor", config: {} }) }); render(); - // Cursor defaults fast off even though the agent reported true, and formats context concisely. - expect(settingsButton()).toHaveTextContent("high · 256k · Fast: Off"); + // Cursor defaults fast off even though the agent reported true, and omits off switches from multi-setting summaries. + expect(settingsButton()).toHaveTextContent("high · 256k"); await chooseModel("gemini-4-neon"); expect(settingsButton()).toHaveTextContent("high · 300k"); fireEvent.click(screen.getByRole("button", { name: "Start" }));