Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions apps/desktop/e2e/menu-hints.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { test, expect, type ElectronApplication, type Page } from "@playwright/test";
import fs from "node:fs";
import { launch, seedHome, tid } from "./support";

/**
* The composer's menus keep each row to one line. The sentence that used to sit under a row's title is
* the row's accessible description, and shows once, at the foot of the menu, for the row under the
* pointer or the keyboard focus. A menu that passes no hints keeps its rows and has no foot.
*/
let app: ElectronApplication;
let page: Page;
let home: string;
let repo: string;

const ATTACH = "Add files or images to your next message.";
const PLAN = "Investigate read-only and return a plan instead of editing.";
const AUTO = "A fast judge picks the model, effort and speed for each turn, within your limits.";

test.beforeAll(async () => {
({ home, repo } = seedHome());
({ app, page } = await launch(home));
// A draft has a composer and no thread: the seeded mode is Chat and the backend is the mock.
await page.keyboard.press("Meta+n");
await expect(tid(page, "draft-view")).toBeVisible();
});

test.afterAll(async () => {
await app?.close();
fs.rmSync(home, { recursive: true, force: true });
fs.rmSync(repo, { recursive: true, force: true });
});

test("the access menu keeps each choice to one line and describes the focused or hovered one at its foot", async () => {
await tid(page, "access-picker").click();
const menu = tid(page, "access-menu");
const option = (mode: string) => menu.locator(`[data-testid="access-option"][data-mode="${mode}"]`);
const foot = tid(menu, "menu-hint");
// Opened without touching a row: the checked choice has focus, and the foot already describes it.
await expect(option("chat")).toBeFocused();
await expect(foot).toHaveText("Read-only. Asks before any command or edit.");
// The foot is the visible copy; assistive technology reads the description from the row itself.
await expect(foot).toHaveAttribute("aria-hidden", "true");
await expect(option("chat")).toHaveText("Read only");
await expect(option("chat")).toHaveAttribute("aria-description", "Read-only. Asks before any command or edit.");
await expect(option("full-access")).toHaveText("Full access");
await expect(option("full-access")).toHaveAttribute("aria-description", "No sandbox, no prompts. Only in a trusted environment.");
expect(Math.round((await option("agent").boundingBox())!.height)).toBe(32);
await page.keyboard.press("ArrowDown");
await expect(option("agent")).toBeFocused();
await expect(foot).toHaveText("Edits and runs commands inside the project; asks to go outside it.");
await option("full-access").hover();
await expect(foot).toHaveText("No sandbox, no prompts. Only in a trusted environment.");
await page.keyboard.press("Escape");
await expect(menu).toHaveCount(0);
await expect(tid(page, "access-picker")).toBeFocused();
// Nothing was chosen.
await expect(tid(page, "access-picker")).toHaveAttribute("data-mode", "chat");
});

test("the + menu shows Plan and Auto as switches and describes each row", async () => {
await tid(page, "composer-plus").click();
const menu = tid(page, "composer-plus-menu");
const foot = tid(menu, "menu-hint");
await expect(page.getByRole("menuitem", { name: "Attach files", exact: true })).toBeFocused();
await expect(foot).toHaveText(ATTACH);
await page.keyboard.press("ArrowDown");
const plan = tid(menu, "plan-toggle");
await expect(plan).toBeFocused();
await expect(foot).toHaveText(PLAN);
await expect(plan).toHaveRole("menuitemcheckbox");
await expect(plan).toHaveAttribute("aria-checked", "false");
await expect(plan).toHaveAttribute("aria-description", PLAN);
// The row is its title and key hint; the sentence is no longer part of the row's text.
await expect(plan).not.toContainText("Investigate");
expect(Math.round((await plan.boundingBox())!.height)).toBe(32);
await tid(menu, "auto-toggle").hover();
await expect(foot).toHaveText(AUTO);
const backend = tid(menu, "backend-picker").getByRole("menuitemradio", { checked: true });
await expect(backend).toHaveText("Mock");
// A disabled row may not report the pointer; its description is still on the row.
await expect(backend).toHaveAttribute("aria-description", "Offline scripted engine (demo)");
await page.keyboard.press("Escape");
await expect(menu).toHaveCount(0);
await expect(tid(page, "plan-chip")).toHaveCount(0);
});

test("a menu without hints has no foot", async () => {
await tid(page, "model-picker").click();
const menu = tid(page, "model-menu");
await expect(menu).toBeVisible();
await expect(tid(menu, "menu-hint")).toHaveCount(0);
await page.keyboard.press("Escape");
await expect(menu).toHaveCount(0);
});
44 changes: 17 additions & 27 deletions apps/desktop/src/renderer/components/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,33 +261,27 @@ function PlusMenu({ busy, pinned, backend, plan, auto, onBackend, onPlan, onAuto
return (
<span className="composer-menu">
<IconButton ref={trigger} icon="plus" label="Plan, Auto and backend" size="md" className="composer-plus-btn" data-testid="composer-plus" aria-haspopup="menu" aria-expanded={open} tooltipSide="top" onClick={() => setOpen((v) => !v)} />
<Menu open={open} onClose={() => setOpen(false)} anchorRef={trigger} label="Composer options" placement="top-start" testId="composer-plus-menu" className="composer-options">
<MenuItem disabled={busy} onClick={pick(onAttach)}><Icon name="paperclip" size={16} /><span className="menu-body"><span className="menu-title">Attach files</span></span></MenuItem>
<Menu open={open} onClose={() => setOpen(false)} anchorRef={trigger} label="Composer options" placement="top-start" testId="composer-plus-menu" className="composer-options" hint={ATTACH_HINT}>
<MenuItem hint={ATTACH_HINT} disabled={busy} onClick={pick(onAttach)}><Icon name="paperclip" size={14} /><span className="menu-title">Attach files</span></MenuItem>
<MenuSeparator />
{pinned && <div className="menu-note" data-testid="local-pinned-note">This thread uses a local model, so Plan, Auto and the backend are fixed. Choose cloud models in the model menu to change them.</div>}
<MenuItem toggle selected={plan} disabled={busy || pinned} data-testid="plan-toggle" onClick={pick(() => onPlan(!plan))}>
<span className="menu-body">
<span className="menu-title">Plan mode <Kbd>⇧⌘P</Kbd></span>
<span className="menu-desc">{pinned ? "Plan mode is not qualified for local models." : "Investigate read-only and return a plan instead of editing."}</span>
</span>
<MenuItem toggle selected={plan} disabled={busy || pinned} data-testid="plan-toggle" hint={pinned ? "Plan mode is not qualified for local models." : "Investigate read-only and return a plan instead of editing."} onClick={pick(() => onPlan(!plan))}>
<span className="menu-title">Plan mode <Kbd>⇧⌘P</Kbd></span>
</MenuItem>
<MenuItem toggle selected={auto} disabled={busy || pinned} data-testid="auto-toggle" onClick={pick(() => onAuto(!auto))}>
<span className="menu-body">
<span className="menu-title">Auto routing</span>
<span className="menu-desc">{pinned ? "Local threads keep the model you explicitly selected." : "A fast judge picks the model, effort and speed for each turn, within your limits."}</span>
</span>
<MenuItem toggle selected={auto} disabled={busy || pinned} data-testid="auto-toggle" hint={pinned ? "Local threads keep the model you explicitly selected." : "A fast judge picks the model, effort and speed for each turn, within your limits."} onClick={pick(() => onAuto(!auto))}>
<span className="menu-title">Auto routing</span>
</MenuItem>
<MenuSeparator />
<MenuLabel>Backend</MenuLabel>
<div role="group" aria-label="Backend" data-testid="backend-picker">
{BACKENDS.map((b) => (
<MenuItem key={b.id} checkable selected={backend === b.id} disabled={busy || pinned} title={b.hint} onClick={pick(() => onBackend(b.id))}>
<span className="menu-body"><span className="menu-title">{b.label}</span></span>
<MenuItem key={b.id} checkable selected={backend === b.id} disabled={busy || pinned} hint={b.hint} onClick={pick(() => onBackend(b.id))}>
<span className="menu-title">{b.label}</span>
</MenuItem>
))}
{backend === "mock" && (
<MenuItem checkable selected disabled title="Offline scripted engine (demo)">
<span className="menu-body"><span className="menu-title">Mock</span></span>
<MenuItem checkable selected disabled hint="Offline scripted engine (demo)">
<span className="menu-title">Mock</span>
</MenuItem>
)}
</div>
Expand All @@ -296,6 +290,7 @@ function PlusMenu({ busy, pinned, backend, plan, auto, onBackend, onPlan, onAuto
);
}

const ATTACH_HINT = "Add files or images to your next message.";
const ACCESS_LABEL: Record<Mode, string> = { chat: "Read only", agent: "Agent", "full-access": "Full access" };

/** The access pill: how much the agent may do without asking (the thread's mode). Orange only for full access. */
Expand All @@ -316,27 +311,22 @@ function AccessMenu({ busy, mode, onMode, approvals, onApprovals }: { busy: bool
<Icon name="info" size={14} />
{ACCESS_LABEL[mode]}
</button>
<Menu open={open} onClose={() => setOpen(false)} anchorRef={trigger} label="Access" placement="top-start" testId="access-menu" className="access-options">
<Menu open={open} onClose={() => setOpen(false)} anchorRef={trigger} label="Access" placement="top-start" testId="access-menu" className="access-options" focusSelected hint={info?.hint ?? ""}>
<MenuLabel>Access</MenuLabel>
{MODES.map((m) => (
<MenuItem key={m.id} checkable selected={m.id === mode} data-testid="access-option" data-mode={m.id} onClick={() => { onMode(m.id); setOpen(false); }}>
<span className="menu-body">
<span className="menu-title">{ACCESS_LABEL[m.id]}</span>
<span className="menu-desc">{m.hint}</span>
</span>
<MenuItem key={m.id} checkable selected={m.id === mode} hint={m.hint} data-testid="access-option" data-mode={m.id} onClick={() => { onMode(m.id); setOpen(false); }}>
<span className="menu-title">{ACCESS_LABEL[m.id]}</span>
</MenuItem>
))}
{onApprovals && <MenuSeparator />}
{onApprovals && <MenuLabel>Approvals</MenuLabel>}
{onApprovals && APPROVAL_POLICIES.map((p) => (
<MenuItem key={p} checkable selected={p === approvals} data-testid="approvals-option" data-policy={p} onClick={() => {
<MenuItem key={p} checkable selected={p === approvals} hint={APPROVAL_HINT[p]} data-testid="approvals-option" data-policy={p} onClick={() => {
setOpen(false);
if (p !== "yolo") onApprovals(p);
else void confirmDialog(YOLO_CONFIRM).then((ok) => { if (ok) onApprovals(p); });
}}>
<span className="menu-body">
<span className="menu-title">{APPROVAL_LABEL[p]}</span>
<span className="menu-desc">{APPROVAL_HINT[p]}</span>
</span>
<span className="menu-title">{APPROVAL_LABEL[p]}</span>
</MenuItem>
))}
</Menu>
Expand Down
35 changes: 29 additions & 6 deletions apps/desktop/src/renderer/components/ui/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createContext, useContext, useEffect, useLayoutEffect, useRef, type ButtonHTMLAttributes, type ReactNode, type RefObject } from "react";
import { createContext, useContext, useEffect, useLayoutEffect, useRef, useState, type ButtonHTMLAttributes, type ReactNode, type RefObject } from "react";
import { Icon } from "./Icon";

type MenuRole = "menu" | "listbox";
Expand All @@ -21,6 +21,11 @@ interface MenuProps {
fitViewport?: boolean;
className?: string;
testId?: string;
/**
* Gives the menu a description area at its foot. It shows the `hint` of the row under the pointer
* or the keyboard focus, and this text until one is. Rows in such a menu stay on one line.
*/
hint?: string;
children: ReactNode;
}

Expand All @@ -35,8 +40,15 @@ const MenuContext = createContext<MenuRole>("menu");
* - a pointer press outside the menu and trigger closes it.
* Position it by placing it inside a `position: relative` wrapper next to the trigger.
*/
export function Menu({ open, onClose, anchorRef, label, role = "menu", focusSelected = role === "listbox", initialFocusRef, placement = "top-start", fitViewport = false, className, testId, children }: MenuProps) {
export function Menu({ open, onClose, anchorRef, label, role = "menu", focusSelected = role === "listbox", initialFocusRef, placement = "top-start", fitViewport = false, className, testId, hint, children }: MenuProps) {
const ref = useRef<HTMLDivElement>(null);
// The description at the foot follows the pointer and the focus; each opening starts from `hint`.
const [described, setDescribed] = useState(hint);
useEffect(() => { if (open) setDescribed(hint); }, [open, hint]);
const describe = (target: EventTarget) => {
const text = target instanceof Element ? target.closest<HTMLElement>("[data-hint]")?.dataset.hint : undefined;
if (text !== undefined) setDescribed(text);
};
useLayoutEffect(() => {
if (!open || !fitViewport) return;
const fit = () => {
Expand Down Expand Up @@ -114,8 +126,11 @@ export function Menu({ open, onClose, anchorRef, label, role = "menu", focusSele
if (!open) return null;
return (
<MenuContext.Provider value={role}>
<div ref={ref} role={role} aria-label={label} data-testid={testId} className={`menu ${placement}${className ? ` ${className}` : ""}`}>
<div ref={ref} role={role} aria-label={label} data-testid={testId} className={`menu ${placement}${hint !== undefined ? " has-hint" : ""}${className ? ` ${className}` : ""}`}
onMouseOver={hint !== undefined ? (e) => describe(e.target) : undefined} onFocus={hint !== undefined ? (e) => describe(e.target) : undefined}>
{children}
{/* Each row carries its own description for assistive technology; this is the visible copy. */}
{hint !== undefined && <div className="menu-hint" data-testid="menu-hint" aria-hidden="true">{described}</div>}
</div>
</MenuContext.Provider>
);
Expand All @@ -128,20 +143,28 @@ interface MenuItemProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "r
checkable?: boolean;
/** An on/off setting (menuitemcheckbox); `selected` is its state. */
toggle?: boolean;
/**
* What choosing this row does, in a sentence. The row then stays on one line: the sentence is its
* accessible description and shows at the foot of a `Menu` that has a `hint`.
*/
hint?: string;
children: ReactNode;
}

/** One row of a Menu. Its ARIA role follows the parent (option in a listbox, menuitem/menuitemradio in a menu). */
export function MenuItem({ selected, checkable, toggle, className, children, type = "button", ...rest }: MenuItemProps) {
export function MenuItem({ selected, checkable, toggle, hint, className, children, type = "button", ...rest }: MenuItemProps) {
const parent = useContext(MenuContext);
const aria =
parent === "listbox" ? { role: "option", "aria-selected": Boolean(selected) }
: toggle ? { role: "menuitemcheckbox", "aria-checked": Boolean(selected) }
: checkable ? { role: "menuitemradio", "aria-checked": Boolean(selected) }
: { role: "menuitem" };
const compact = hint !== undefined;
const described = compact ? { "data-hint": hint, "aria-description": hint } : {};
return (
<button type={type} tabIndex={-1} className={`menu-item${selected ? " selected" : ""}${className ? ` ${className}` : ""}`} {...aria} {...rest}>
{(checkable || toggle) && <span className="menu-check">{selected ? <Icon name="check" size={14} /> : null}</span>}
<button type={type} tabIndex={-1} className={`menu-item${compact ? " compact" : ""}${selected ? " selected" : ""}${className ? ` ${className}` : ""}`} {...aria} {...described} {...rest}>
{/* A one-line row shows an on/off setting as a switch; its state is the row's aria-checked. */}
{toggle && compact ? <span className="menu-switch" aria-hidden="true" /> : (checkable || toggle) && <span className="menu-check">{selected ? <Icon name="check" size={14} /> : null}</span>}
{children}
</button>
);
Expand Down
26 changes: 26 additions & 0 deletions apps/desktop/src/renderer/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,32 @@ a.thread-pr:hover { text-decoration: underline; }
.menu-separator { height: 1px; margin: 4px 6px; background: var(--border-pane); }
.menu-item:disabled { opacity: .5; cursor: default; background: transparent; }
.menu-title .kbd { margin-left: auto; }
/* Menus (components/ui/Menu.tsx). Every menu shares the shell. A menu with a `hint` keeps each row to
one line and describes the row under the pointer or focus once, at its foot. */
.menu { border-color: var(--hairline); background: var(--bg-elevated); box-shadow: inset 0 1px 0 color-mix(in srgb, var(--text-1) 5%, transparent), 0 24px 60px -12px rgba(0, 0, 0, .6); animation: menu-in var(--duration-fast) var(--ease-out) both; }
@keyframes menu-in { from { opacity: 0; } }
/* On the elevated surface a hovered row takes the light tint and the selected row the stronger one. */
.menu-item:hover:not(:disabled), .menu-item:focus-visible { background: var(--tint); }
.menu-item.selected, .menu-item.selected:hover:not(:disabled) { background: var(--tint-strong); }
.menu.has-hint { width: 264px; min-width: 0; max-width: calc(100vw - 24px); }
.menu.has-hint .menu-label { padding: 8px 10px 5px; color: var(--text-3); font: var(--weight-medium) 11px/14px var(--font-sans); letter-spacing: .09em; text-transform: uppercase; }
.menu.has-hint .menu-separator { margin: 6px 10px; background: var(--hairline); }
.menu-item.compact { align-items: center; height: 32px; padding: 0 10px; color: var(--text-2); font-size: var(--text-sm); }
.menu-item.compact.selected { background: transparent; color: var(--text-1); }
.menu-item.compact:hover:not(:disabled), .menu-item.compact:focus-visible { background: var(--tint-strong); color: var(--text-1); }
.menu-item.compact > .icon-svg { flex: none; color: var(--text-3); }
.menu-item.compact .menu-title { flex: 1; min-width: 0; overflow: hidden; font-weight: var(--weight-regular); white-space: nowrap; }
.menu-item.compact.selected .menu-title { font-weight: var(--weight-medium); }
/* The check and the switch sit at the row's right edge, after its title. */
.menu-item.compact .menu-check { order: 9; height: 14px; color: var(--text-1); }
.menu-item.compact .kbd { margin: 0 0 0 auto; padding: 0; border: 0; background: none; color: var(--text-3); font: 11.5px/1 var(--font-sans); }
.menu-switch { order: 9; position: relative; width: 26px; height: 16px; flex: none; border-radius: var(--radius-xs); background: var(--tint-strong); box-shadow: inset 0 0 0 1px var(--hairline); transition: background var(--duration-fast) var(--ease-out); }
.menu-switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 10px; height: 10px; border-radius: 2px; background: var(--text-3); transition: transform var(--duration-fast) var(--ease-out), background var(--duration-fast) var(--ease-out); }
.menu-item[aria-checked="true"] .menu-switch { background: var(--text-1); box-shadow: none; }
.menu-item[aria-checked="true"] .menu-switch::after { transform: translateX(10px); background: var(--bg-elevated); }
.menu-hint { min-height: 58px; margin-top: 6px; padding: 10px 10px 4px; border-top: 1px solid var(--hairline); color: var(--text-info); font-size: var(--text-xs); line-height: 1.45; }
@media (prefers-reduced-motion: reduce) { .menu { animation: none; } }
@media (forced-colors: active) { .menu-switch { border: 1px solid ButtonText; } .menu-item[aria-checked="true"] .menu-switch { background: Highlight; } }
.icon-btn.composer-plus-btn, .icon-btn.composer-attach-btn { color: var(--text-1); }
.model-trigger:focus-visible, .access-pill:focus-visible, .composer-chip:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

Expand Down
Loading
Loading