storebound/apps/desktop/src/traymenu/model.ts
CRT a67af1ba57 desktop: tray menu panels and pinnable context menus
The tray's right-click menu is drawn by the app: one tree from tray.rs
(stable submenu keys), one frameless always-on-top menu-N window per
panel rendered by tray-menu.html with the design system, placed inside
the monitor's work area. A pure panel book keeps transient and pinned
panels apart; pinned panels (pin button, Ctrl+click, middle click) are
draggable, step aside from each other and are saved in state.json
(menuPins) and restored at start. tray_update pushes the tree to the
open panels, which update in place; closed panels are destroyed. The
native menu, rendered from the same tree, stays as the fallback and on
Linux. Only menu-* windows get the menu_* commands (menu.json).

In-app context menus get the same behaviour through a pure menu stack
(ui/menu-stack.ts): pin a menu, tear off a submenu, pinned panels
survive the next menu, are draggable and re-read their entries from
live sources so data changes update them in place.

Also drops a stray diff3 marker line from CHANGELOG.md.
2026-10-24 05:44:08 +00:00

75 lines
3.5 KiB
TypeScript

// 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<MenuNode, { kind: 'sep' }>;
/** 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<MenuNode, { kind: 'sub' }> => 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);