// The tray menu panels' pure half (tested in tests/tray-menu.test.ts): the tree Rust sends (menu_panel.rs `Node`), // walking it by stable submenu keys, splitting a row into its text and live value, type-to-filter, keyboard moves and // keeping the highlighted row on the same item when a live update arrives. export type MenuNode = | { kind: 'item'; id: string; label: string; enabled: boolean } | { kind: 'sep' } | { kind: 'sub'; key: string; label: string; items: MenuNode[] }; export type Row = Exclude; /** What menu_state returns for one panel window. */ export interface PanelInfo { label: string; path: string[]; pinned: boolean; hasParent: boolean; focusFirst: boolean; nodes: MenuNode[]; } /** "First · App Store: In review" -> { text: 'First', value: 'App Store: In review' } (the value is right-aligned). */ export function split(label: string): { text: string; value?: string } { const at = label.indexOf('·'); if (at <= 0) return { text: label.trim() }; const value = label.slice(at + 1).trim(); return value ? { text: label.slice(0, at).trim(), value } : { text: label.slice(0, at).trim() }; } /** The items at `path` (submenu keys) and the submenu's label, or null when that submenu is gone. */ export function findPath(nodes: readonly MenuNode[], path: readonly string[]): { items: MenuNode[]; label: string | null } | null { let items = nodes as MenuNode[]; let label: string | null = null; for (const key of path) { const next = items.find((n): n is Extract => n.kind === 'sub' && n.key === key); if (!next) return null; items = next.items; label = next.label; } return { items, label }; } /** Type-to-filter: rows whose label holds every word; separators drop out while filtering. */ export function filterNodes(items: readonly MenuNode[], query: string): MenuNode[] { const words = query.toLocaleLowerCase().split(/\s+/).filter(Boolean); if (!words.length) return items as MenuNode[]; return items.filter(n => n.kind !== 'sep' && words.every(w => n.label.toLocaleLowerCase().includes(w))); } export const rows = (items: readonly MenuNode[]): Row[] => items.filter((n): n is Row => n.kind !== 'sep'); export const isEnabled = (n: Row): boolean => n.kind === 'sub' || n.enabled; export const rowId = (n: Row): string => (n.kind === 'sub' ? `sub:${n.key}` : n.id); /** The next enabled row from `current` in `dir`, wrapping; -1 when none. `current` -1 starts before the first. */ export function nextIndex(current: number, dir: 1 | -1, enabled: readonly boolean[]): number { const n = enabled.length; if (!n) return -1; const from = current < 0 ? (dir > 0 ? -1 : n) : current; for (let step = 1; step <= n; step += 1) { const i = (((from + dir * step) % n) + n) % n; if (enabled[i]) return i; } return -1; } /** A live update must not move the highlight: the same id / key in the new rows, else the same place (clamped). */ export function keepActive(before: readonly Row[], after: readonly Row[], active: number): number { if (active < 0 || !after.length) return -1; const was = before[active]; if (!was) return Math.min(active, after.length - 1); const found = after.findIndex(n => rowId(n) === rowId(was)); return found >= 0 ? found : Math.min(active, after.length - 1); } /** Ctrl / Cmd + click or a middle click on a submenu row tears it off as a pinned panel. */ export const isTearOff = (e: { button: number; ctrlKey: boolean; metaKey: boolean }): boolean => e.button === 1 || ((e.ctrlKey || e.metaKey) && e.button === 0);