storebound/apps/desktop/src/traymenu/main.tsx
CRT 5236f002f1 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 13:44:08 +08:00

70 lines
2.9 KiB
TypeScript

// The tray menu panel window's entry (tray-menu.html; one window per panel, made by menu_panel.rs). Theme and language
// from the same prefs as the main window; the tree and every live update from Rust (`menu-model`). In a plain dev
// browser it shows a demo with fixture data instead (demo.tsx, never in the release bundle).
import { useEffect, useState } from 'react';
import { createRoot } from 'react-dom/client';
import { applyPrefs, loadPrefs, resolveLang } from '../prefs';
import { bridge, inTauri, menuState, onModel, onPanel } from './menu-api';
import type { MenuNode, PanelInfo } from './model';
import { type MenuLang, TrayMenu } from './TrayMenu';
import '../styles.css';
import '../design.css';
import './traymenu.css';
const sys = () => (typeof navigator !== 'undefined' ? navigator.language : 'en');
function prefs(): MenuLang {
const p = loadPrefs();
if (import.meta.env.DEV) {
const q = new URLSearchParams(location.search);
const th = q.get('theme'), lg = q.get('lang');
if (th === 'light' || th === 'dark') p.theme = th;
if (lg === 'en' || lg === 'tr') p.lang = lg;
}
applyPrefs(p, document.documentElement, sys());
return resolveLang(p.lang, sys());
}
/** One panel window: its state from Rust, the live tree, pin changes. */
function Panel({ lang }: { lang: MenuLang }) {
const [info, setInfo] = useState<PanelInfo | null>(null);
const [nodes, setNodes] = useState<MenuNode[]>([]);
const [pinned, setPinned] = useState(false);
const [focusTick, setFocusTick] = useState(0);
useEffect(() => {
let live = true;
const offs = [
onModel(setNodes),
onPanel(word => {
if (word === 'pinned') setPinned(true);
else if (word === 'unpinned') setPinned(false);
else if (word === 'focus') setFocusTick(n => n + 1);
}),
];
menuState().then((s: PanelInfo | null) => {
if (!live || !s) return;
setInfo(s);
setNodes(s.nodes);
setPinned(s.pinned);
}, () => {});
return () => { live = false; for (const off of offs) off(); };
}, []);
useEffect(() => { if (focusTick) document.querySelector<HTMLElement>('.tm-panel')?.focus(); }, [focusTick]);
if (!info) return null;
return <TrayMenu nodes={nodes} path={info.path} pinned={pinned} hasParent={info.hasParent && !pinned} focusFirst={info.focusFirst} bridge={bridge} lang={lang} />;
}
void (async () => {
let lang = prefs();
const root = createRoot(document.getElementById('menu')!);
if (!inTauri() && import.meta.env.DEV) {
const { Demo } = await import('./demo');
document.body.classList.add('tm-demo');
root.render(<Demo lang={lang} />);
return;
}
const render = () => root.render(<Panel key={lang} lang={lang} />);
render();
// theme or language changed in the main window (same origin: shared localStorage)
window.addEventListener('storage', () => { const l = prefs(); if (l !== lang) { lang = l; render(); } });
})();