Skip to content

Commit 4371090

Browse files
improve(ui): make agent switching faster and more visual (#126968)
* improve(ui): refine agent switching * test(ui): cover agent switcher behavior * fix(ui): preserve agent menu keyboard navigation * fix(ui): keep full agent roster reachable
1 parent 946aa39 commit 4371090

9 files changed

Lines changed: 695 additions & 133 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3996,7 +3996,7 @@ ui/src/app/update-success-notice.ts 1
39963996
ui/src/app/user-profile.ts 2
39973997
ui/src/app/web-push.runtime.ts 3
39983998
ui/src/components/agent-select.ts 3
3999-
ui/src/components/app-sidebar-agent-menu.ts 4
3999+
ui/src/components/app-sidebar-agent-menu.ts 3
40004000
ui/src/components/app-sidebar-nav-menus.ts 6
40014001
ui/src/components/app-sidebar-render.ts 4
40024002
ui/src/components/app-sidebar-session-catalog-render.ts 4
@@ -4063,7 +4063,6 @@ ui/src/components/session-menu.ts 1
40634063
ui/src/components/session-organizer-controller.ts 5
40644064
ui/src/components/settings-sidebar.ts 3
40654065
ui/src/components/settings-ui.ts 7
4066-
ui/src/components/sidebar-agent-card.ts 1
40674066
ui/src/components/sidebar-attention-dismissals.ts 1
40684067
ui/src/components/sidebar-attention.ts 4
40694068
ui/src/components/terminal/terminal-connection.ts 2

‎ui/src/components/app-sidebar-agent-menu.ts‎

Lines changed: 152 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,32 @@ const IDENTITY_MENU_LINKS: ReadonlyArray<{
4646
},
4747
];
4848

49-
/** Above this roster size the chip menu switches to pinned agents + filter. */
50-
const QUICK_SWITCH_AGENT_LIMIT = 10;
49+
/** Above this roster size the chip menu adds filtering and scrolls the grid. */
50+
const QUICK_SWITCH_VISIBLE_AGENT_LIMIT = 6;
5151
const AGENT_VALUE_PREFIX = "agent:";
5252
const COMMAND_VALUE_PREFIX = "command:";
5353
const LINK_VALUE_PREFIX = "link:";
54+
const sidebarMenuTypeahead = new WeakMap<
55+
HTMLElement,
56+
{ query: string; timeout: ReturnType<typeof setTimeout> }
57+
>();
58+
59+
function sidebarMenuItems(dropdown: Element | null) {
60+
return [
61+
...(dropdown?.querySelectorAll<HTMLElement & { active: boolean }>(
62+
":scope > wa-dropdown-item:not([disabled]), :scope > .sidebar-agent-menu__agent-grid > wa-dropdown-item:not([disabled])",
63+
) ?? []),
64+
];
65+
}
66+
67+
function focusSidebarMenuItem(
68+
items: Array<HTMLElement & { active: boolean }>,
69+
target: HTMLElement,
70+
) {
71+
items.forEach((item) => (item.active = item === target));
72+
target.focus({ preventScroll: true });
73+
target.scrollIntoView?.({ block: "nearest" });
74+
}
5475

5576
// Nested overlays bubble lifecycle events through the dropdown. Only the
5677
// owner's completed hide may remove its menu or consume its Escape state.
@@ -62,15 +83,14 @@ function closeMenuAfterOwnDropdownHide(event: Event, onClose: (restoreFocus?: bo
6283
}
6384

6485
function moveSidebarMenuFocus(event: KeyboardEvent): boolean {
65-
if (event.key !== "ArrowDown" && event.key !== "ArrowUp") {
86+
if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
87+
return false;
88+
}
89+
if (event.target instanceof HTMLInputElement && (event.key === "Home" || event.key === "End")) {
6690
return false;
6791
}
6892
const dropdown = (event.currentTarget as HTMLElement).closest("wa-dropdown");
69-
const items = [
70-
...(dropdown?.querySelectorAll<HTMLElement & { active: boolean }>(
71-
":scope > wa-dropdown-item:not([disabled])",
72-
) ?? []),
73-
];
93+
const items = sidebarMenuItems(dropdown);
7494
const footer = dropdown?.querySelector<HTMLElement>(".sidebar-identity-menu__footer");
7595
const controls = [
7696
...items,
@@ -83,21 +103,65 @@ function moveSidebarMenuFocus(event: KeyboardEvent): boolean {
83103
}
84104
const direction = event.key === "ArrowDown" ? 1 : -1;
85105
const target =
86-
index < 0
87-
? items.at(direction === 1 ? 0 : -1)
88-
: controls[(index + direction + controls.length) % controls.length];
106+
event.key === "Home"
107+
? items[0]
108+
: event.key === "End"
109+
? items.at(-1)
110+
: index < 0
111+
? items.at(direction === 1 ? 0 : -1)
112+
: controls[(index + direction + controls.length) % controls.length];
89113
if (!target || (footer && !footer.contains(current) && !footer.contains(target))) {
90114
return false;
91115
}
92116
event.preventDefault();
93117
event.stopPropagation();
94118
// Native footer actions are outside Web Awesome's roving item list; reset
95119
// its active row on both crossings so reverse navigation cannot skip one.
96-
items.forEach((item) => (item.active = item === target));
97-
target.focus({ preventScroll: true });
120+
focusSidebarMenuItem(items, target);
121+
return true;
122+
}
123+
124+
function typeaheadSidebarMenuFocus(event: KeyboardEvent): boolean {
125+
if (event.key.length !== 1 || event.metaKey || event.ctrlKey || event.altKey) {
126+
return false;
127+
}
128+
const dropdown = event.currentTarget;
129+
if (!(dropdown instanceof HTMLElement)) {
130+
return false;
131+
}
132+
const previous = sidebarMenuTypeahead.get(dropdown);
133+
if (event.key === " " && !previous?.query) {
134+
return false;
135+
}
136+
event.preventDefault();
137+
event.stopPropagation();
138+
if (previous) {
139+
clearTimeout(previous.timeout);
140+
}
141+
const query = `${previous?.query ?? ""}${event.key}`.trim().toLowerCase();
142+
const timeout = setTimeout(() => sidebarMenuTypeahead.delete(dropdown), 1_000);
143+
sidebarMenuTypeahead.set(dropdown, { query, timeout });
144+
const items = sidebarMenuItems(dropdown);
145+
const target = items.find((item) =>
146+
(item.textContent ?? "").trim().toLowerCase().startsWith(query),
147+
);
148+
if (target) {
149+
focusSidebarMenuItem(items, target);
150+
}
98151
return true;
99152
}
100153

154+
function focusActiveAgentMenuItem(dropdown: HTMLElement) {
155+
const items = sidebarMenuItems(dropdown);
156+
const target =
157+
items.find((item) => item.classList.contains("sidebar-agent-menu__agent-switch--active")) ??
158+
items.find((item) => item.classList.contains("sidebar-agent-menu__agent-switch"));
159+
if (!target) {
160+
return;
161+
}
162+
focusSidebarMenuItem(items, target);
163+
}
164+
101165
type AgentMenuAgent = {
102166
id: string;
103167
name?: string;
@@ -114,9 +178,13 @@ type SidebarAgentMenuParams = {
114178
filter: string;
115179
pinnedAgentIds: readonly string[];
116180
connected: boolean;
181+
openMode: "hover" | "click";
117182
agentUnreadCount: (agentId: string) => number;
118183
agentApprovalCount: (agentId: string) => number;
119184
onFilterChange: (next: string) => void;
185+
onPointerEnter: () => void;
186+
onPointerLeave: () => void;
187+
onAfterShow: () => void;
120188
onSwitchAgent: (agentId: string) => void;
121189
onAskCapabilities: (agentId: string) => void;
122190
onTabAway: () => void;
@@ -145,17 +213,16 @@ function isApplePlatform(): boolean {
145213
return /Mac|iPhone|iPad|iPod/u.test(globalThis.navigator?.platform ?? "");
146214
}
147215

148-
/** Rows for the chip switcher. Small rosters list everything; past
149-
QUICK_SWITCH_AGENT_LIMIT the menu shows pinned agents (plus the active
150-
one) and the filter searches the full roster. */
216+
/** Rows for the chip switcher. Small rosters list everything; larger rosters
217+
add filtering while keeping every unpinned option reachable by scrolling. */
151218
function sidebarAgentMenuRows(params: {
152219
agents: readonly AgentMenuAgent[];
153220
activeId: string;
154221
filter: string;
155222
pinnedAgentIds: readonly string[];
156223
identities: ReadonlyMap<string, AgentIdentityResult>;
157224
}) {
158-
const { agents, activeId } = params;
225+
const { agents } = params;
159226
const availableIds = new Set(agents.map((agent) => normalizeAgentId(agent.id)));
160227
const pinnedIds = new Set(
161228
params.pinnedAgentIds
@@ -167,7 +234,7 @@ function sidebarAgentMenuRows(params: {
167234
const bPinned = pinnedIds.has(normalizeAgentId(b.id)) ? 0 : 1;
168235
return aPinned - bPinned;
169236
});
170-
if (agents.length <= QUICK_SWITCH_AGENT_LIMIT) {
237+
if (agents.length <= QUICK_SWITCH_VISIBLE_AGENT_LIMIT) {
171238
return { rows: sorted, showFilter: false };
172239
}
173240
const query = params.filter.trim().toLowerCase();
@@ -181,23 +248,7 @@ function sidebarAgentMenuRows(params: {
181248
});
182249
return { rows, showFilter: true };
183250
}
184-
if (pinnedIds.size > 0) {
185-
return {
186-
rows: sorted.filter((entry) => {
187-
const agentId = normalizeAgentId(entry.id);
188-
return pinnedIds.has(agentId) || agentId === activeId;
189-
}),
190-
showFilter: true,
191-
};
192-
}
193-
let rows = sorted.slice(0, QUICK_SWITCH_AGENT_LIMIT);
194-
if (!rows.some((entry) => normalizeAgentId(entry.id) === activeId)) {
195-
const activeAgent = sorted.find((entry) => normalizeAgentId(entry.id) === activeId);
196-
if (activeAgent) {
197-
rows = [...rows.slice(0, QUICK_SWITCH_AGENT_LIMIT - 1), activeAgent];
198-
}
199-
}
200-
return { rows, showFilter: true };
251+
return { rows: sorted, showFilter: true };
201252
}
202253

203254
function renderAgentRow(agent: AgentMenuAgent, params: SidebarAgentMenuParams) {
@@ -214,37 +265,38 @@ function renderAgentRow(agent: AgentMenuAgent, params: SidebarAgentMenuParams) {
214265
const option = { value: agentId, label, agent };
215266
return html`
216267
<wa-dropdown-item
217-
class="sidebar-customize-menu__item sidebar-agent-menu__agent-switch agent-select__option"
268+
class="sidebar-customize-menu__item sidebar-agent-menu__agent-switch agent-select__option ${active
269+
? "sidebar-agent-menu__agent-switch--active"
270+
: ""}"
218271
value=${`${AGENT_VALUE_PREFIX}${encodeURIComponent(agentId)}`}
219272
type="checkbox"
220273
role="menuitemradio"
221274
aria-checked=${String(active)}
222275
${ref((element) => syncDropdownItemRadio(element, active))}
223276
>
224-
<span slot="icon">${renderAgentSelectAvatar(option, identity)}</span>
225-
${renderAgentSelectCopy(option)}
226-
${approvals > 0
227-
? html`<span
228-
slot="details"
229-
class="sidebar-agent-approval-count"
230-
aria-label=${approvalLabel}
231-
title=${approvalLabel}
232-
>${approvals}</span
233-
>`
234-
: nothing}
235-
${active
236-
? html`<span slot="details" class="session-menu__check" aria-hidden="true"
237-
>${icons.check}</span
238-
>`
239-
: nothing}
240-
${unread > 0
241-
? html`<span
242-
slot="details"
243-
class="session-unread-dot"
244-
role="img"
245-
aria-label=${t("sessionsView.unread")}
246-
></span>`
247-
: nothing}
277+
<span class="sidebar-agent-menu__agent-tile">
278+
<span class="sidebar-agent-menu__agent-avatar">
279+
${renderAgentSelectAvatar(option, identity)}
280+
</span>
281+
${renderAgentSelectCopy(option)}
282+
<span class="sidebar-agent-menu__agent-status">
283+
${approvals > 0
284+
? html`<span
285+
class="sidebar-agent-approval-count"
286+
aria-label=${approvalLabel}
287+
title=${approvalLabel}
288+
>${approvals}</span
289+
>`
290+
: nothing}
291+
${unread > 0
292+
? html`<span
293+
class="session-unread-dot"
294+
role="img"
295+
aria-label=${t("sessionsView.unread")}
296+
></span>`
297+
: nothing}
298+
</span>
299+
</span>
248300
</wa-dropdown-item>
249301
`;
250302
}
@@ -294,6 +346,8 @@ export function renderSidebarAgentMenu(params: SidebarAgentMenuParams) {
294346
placement="bottom-start"
295347
.distance=${0}
296348
aria-label=${t("agentChip.menuLabel")}
349+
@pointerenter=${params.onPointerEnter}
350+
@pointerleave=${params.onPointerLeave}
297351
@wa-select=${(event: CustomEvent<{ item: HTMLElement & { value?: string } }>) => {
298352
event.preventDefault();
299353
const item = event.detail.item;
@@ -326,14 +380,42 @@ export function renderSidebarAgentMenu(params: SidebarAgentMenuParams) {
326380
}
327381
}}
328382
@wa-after-show=${(event: Event) => {
383+
if (!(event.currentTarget instanceof HTMLElement)) {
384+
return;
385+
}
386+
params.onAfterShow();
387+
if (params.openMode === "hover") {
388+
return;
389+
}
329390
if (showFilter) {
330-
(event.currentTarget as HTMLElement)
391+
event.currentTarget
331392
.querySelector<HTMLInputElement>(".sidebar-agent-menu__filter input")
332393
?.focus();
394+
} else {
395+
focusActiveAgentMenuItem(event.currentTarget);
333396
}
334397
}}
335-
@keydown=${(event: KeyboardEvent) =>
336-
trackDropdownKeyboardDismissal(event, params.onTabAway)}
398+
@keydown=${(event: KeyboardEvent) => {
399+
if (moveSidebarMenuFocus(event)) {
400+
return;
401+
}
402+
if (typeaheadSidebarMenuFocus(event)) {
403+
return;
404+
}
405+
const item =
406+
event.target instanceof HTMLElement
407+
? event.target.closest<HTMLElement>(
408+
".sidebar-agent-menu__agent-grid > wa-dropdown-item:not([disabled])",
409+
)
410+
: null;
411+
if ((event.key === "Enter" || event.key === " ") && item) {
412+
event.preventDefault();
413+
event.stopPropagation();
414+
item.click();
415+
return;
416+
}
417+
trackDropdownKeyboardDismissal(event, params.onTabAway);
418+
}}
337419
@wa-after-hide=${(event: Event) => closeMenuAfterOwnDropdownHide(event, params.onClose)}
338420
>
339421
<button
@@ -371,19 +453,20 @@ export function renderSidebarAgentMenu(params: SidebarAgentMenuParams) {
371453
</div>
372454
`
373455
: nothing}
374-
${rows.map((entry) => renderAgentRow(entry, params))}
375-
${rows.length === 0
376-
? html`<div class="sidebar-agent-menu__empty">
377-
${t("agentChip.noAgentMatches")}
456+
${rows.length > 0
457+
? html`<div class="sidebar-agent-menu__agent-grid">
458+
${rows.map((entry) => renderAgentRow(entry, params))}
378459
</div>`
379-
: nothing}
460+
: html`<div class="sidebar-agent-menu__empty">
461+
${t("agentChip.noAgentMatches")}
462+
</div>`}
380463
`
381464
: nothing}
465+
<div class="sidebar-customize-menu__separator" role="separator"></div>
382466
<wa-dropdown-item class="sidebar-customize-menu__item" value="command:new-agent">
383467
<span slot="icon" class="nav-item__icon" aria-hidden="true">${icons.users}</span>
384468
<span class="sidebar-customize-menu__text">${t("custodian.newAgent")}</span>
385469
</wa-dropdown-item>
386-
<div class="sidebar-customize-menu__separator" role="separator"></div>
387470
<wa-dropdown-item
388471
class="sidebar-customize-menu__item"
389472
value="command:capabilities"

‎ui/src/components/app-sidebar-render.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ export function renderAppSidebarBrand(host: AppSidebarRenderHost) {
136136
.approvalCount=${approvalCount}
137137
.switcherAvailable=${cardAgents.length > 1}
138138
.onToggleMenu=${(trigger: HTMLElement) => host.sidebarMenus.toggleAgentMenu(trigger)}
139+
.onMenuPointerEnter=${(trigger: HTMLElement, event: PointerEvent) =>
140+
host.sidebarMenus.scheduleAgentMenuHoverOpen(trigger, event)}
141+
.onMenuPointerLeave=${() => host.sidebarMenus.handleAgentMenuTriggerPointerLeave()}
139142
@contextmenu=${(event: MouseEvent) => {
140143
event.preventDefault();
141144
if (host.sidebarMenus.agentMenuPosition !== null) {

0 commit comments

Comments
 (0)