/** * The app menu's model (M3.9): the commands that have a menu path, grouped * by the path's first segment. * * - **Groups** come in Lutra's order (File, Edit, View, Go, Help); a group * Lutra does not know goes before Help, sorted by name. Empty groups are * left out. Known group names come from the catalog. * - **Items** in a group follow {@link ITEM_ORDER}; others come after it, * sorted by label. * - **Labels:** a UI command shows its catalog label; an engine command * shows the rest of its menu path (`File/Close Tab` → `Close Tab`), since * the engine has no catalog and its path is written for menus. Deeper * paths are joined with ` › `. */ import type { PaletteEntry } from "../../commands/store"; import { t } from "../../i18n"; /** One entry of the menu. */ export interface MenuItem { /** Command id. */ id: string; /** Text shown. */ label: string; /** Formatted shortcut, or null. */ shortcut: string | null; /** Whether it can run now; disabled items are dimmed and do nothing. */ enabled: boolean; } /** One group of the menu. */ export interface MenuGroup { /** The first segment of the menu path, e.g. `File`. */ key: string; /** Heading shown. */ label: string; /** Items in display order. */ items: MenuItem[]; } /** Known groups in display order. */ const GROUP_ORDER = ["File", "Edit", "View", "Go", "Help"]; /** Order of known commands inside their group. */ const ITEM_ORDER = [ "project.openDialog", "file.save", "file.saveAll", "buffer.close", "project.close", "app.quit", "palette.open", "view.toggleProjectTree", "view.toggleStructure", "find.open", "terminal.toggle", "settings.open", "goto.fileOpen", "goto.classOpen", "goto.symbolOpen", "app.about", ]; /** * The heading of a group: catalog text for Lutra's groups, else the name. * * @param key - the first segment of the menu path. */ function groupLabel(key: string): string { switch (key) { case "File": return t("menu.group.file"); case "Edit": return t("menu.group.edit"); case "View": return t("menu.group.view"); case "Go": return t("menu.group.go"); case "Help": return t("menu.group.help"); default: return key; } } /** * The sort rank of a group. * * @param key - the group name. */ function groupRank(key: string): number { const index = GROUP_ORDER.indexOf(key); if (index >= 0) { return key === "Help" ? 1000 : index; } return 500; } /** * The sort rank of an item. * * @param id - the command id. */ function itemRank(id: string): number { const index = ITEM_ORDER.indexOf(id); return index >= 0 ? index : ITEM_ORDER.length; } /** * Splits a menu path into its non-empty segments. * * @param path - for example `File/Save`. */ function segments(path: string): string[] { return path .split("/") .map((part) => part.trim()) .filter((part) => part !== ""); } /** * Builds the menu from the commands the palette knows. * * @param entries - the commands; those without a menu path are left out. * @returns the groups in display order, none empty. */ export function buildMenu(entries: readonly PaletteEntry[]): MenuGroup[] { const groups = new Map(); for (const entry of entries) { if (entry.menuPath === null) { continue; } const [group, ...rest] = segments(entry.menuPath); if (group === undefined) { continue; } const label = entry.ui || rest.length === 0 ? entry.label : rest.join(" › "); const items = groups.get(group) ?? []; items.push({ id: entry.id, label, shortcut: entry.shortcut, enabled: entry.enabled }); groups.set(group, items); } return [...groups.entries()] .sort(([a], [b]) => groupRank(a) - groupRank(b) || a.localeCompare(b)) .map(([key, items]) => ({ key, label: groupLabel(key), items: items.sort((a, b) => itemRank(a.id) - itemRank(b.id) || a.label.localeCompare(b.label)), })); } /** * Finds the next item whose label starts with the typed text, searching * from the item after `from` and wrapping around; when the text is one * character typed again, this cycles through the items with that initial. * * @param labels - item labels in display order. * @param typed - what was typed, case-insensitive. * @param from - index of the focused item, -1 for none. * @returns the index, or -1 when no label matches. */ export function typeAhead(labels: readonly string[], typed: string, from: number): number { const needle = typed.toLocaleLowerCase(); if (needle === "" || labels.length === 0) { return -1; } const start = needle.length > 1 ? Math.max(from, 0) : from + 1; for (let step = 0; step < labels.length; step += 1) { const index = (start + step + labels.length) % labels.length; if ((labels[index] ?? "").toLocaleLowerCase().startsWith(needle)) { return index; } } return -1; }