79 lines
4.5 KiB
TypeScript
79 lines
4.5 KiB
TypeScript
import React, { useState } from "react";
|
|
import type { DesktopManifest, DesktopMenu, DesktopMenuItem, DesktopWorkbench } from "./command-registry.js";
|
|
import { commandLabel, commandTooltip } from "./command-registry.js";
|
|
|
|
const STANDARD_MENUS: Array<{ label: string; items: string[] }> = [
|
|
{ label: "File", items: ["Std_New", "Std_Open", "Std_Save", "Std_Export", "Std_Close"] },
|
|
{ label: "Edit", items: ["Std_Undo", "Std_Redo"] },
|
|
{ label: "View", items: ["Std_ViewFitAll", "Std_ViewIsometric", "Std_ViewTop", "Std_ViewFront"] },
|
|
{ label: "Tools", items: ["Std_DlgPreferences", "Std_DlgCustomize", "Std_UnitsCalculator"] },
|
|
{ label: "Macro", items: ["Std_MacroStartDebug", "Std_MacroStopDebug"] },
|
|
{ label: "Windows", items: ["Std_DockViewMenu", "Std_TileWindows"] },
|
|
{ label: "Help", items: ["Std_About", "Std_OnlineHelp"] },
|
|
];
|
|
|
|
function MenuItems({ items, manifest, onCommand }: {
|
|
items: DesktopMenuItem[];
|
|
manifest: DesktopManifest;
|
|
onCommand?: (id: string) => void;
|
|
}) {
|
|
return <div className="shell-menu-items">
|
|
{items.map((item, index) => {
|
|
if (item.type === "separator") return <hr key={`sep-${index}`} />;
|
|
if (item.type === "submenu") return <details key={`sub-${index}`} className="shell-submenu">
|
|
<summary>{item.label ?? "Submenu"}</summary>
|
|
<MenuItems items={item.items ?? []} manifest={manifest} onCommand={onCommand} />
|
|
</details>;
|
|
if (!item.id) return null;
|
|
return <button key={item.id} type="button" title={commandTooltip(manifest, item.id)}
|
|
onClick={() => onCommand?.(item.id!)}>
|
|
<span>{commandLabel(manifest, item.id)}</span>
|
|
{manifest.commandMetadata[item.id]?.accel
|
|
? <kbd>{manifest.commandMetadata[item.id].accel}</kbd> : null}
|
|
</button>;
|
|
})}
|
|
</div>;
|
|
}
|
|
|
|
export function DesktopMenuBar({ manifest, workbench, onCommand, initialOpen }: {
|
|
manifest: DesktopManifest;
|
|
workbench: DesktopWorkbench;
|
|
onCommand?: (id: string) => void;
|
|
initialOpen?: string;
|
|
}) {
|
|
const [open, setOpen] = useState(initialOpen ?? "");
|
|
const menus: DesktopMenu[] = workbench.menus;
|
|
return <nav className="shell-menubar" role="menubar" aria-label="Desktop menu bar">
|
|
<style>{`
|
|
.shell-menubar{display:flex;align-items:stretch;gap:2px;background:#2b2b2b;border-bottom:1px solid #3f3f3f;color:#e7edf2;font:13px/1.3 system-ui,"Noto Sans SC",sans-serif;position:relative;z-index:20}
|
|
.shell-menubar>button{background:transparent;border:0;color:inherit;padding:5px 10px;cursor:pointer;border-radius:3px}
|
|
.shell-menubar>button:hover,.shell-menubar>button[aria-expanded=true]{background:#3d4a52}
|
|
.shell-dropdown{position:absolute;top:100%;left:0;min-width:260px;max-height:70vh;overflow:auto;background:#0f1419;border:1px solid #3f3f3f;box-shadow:0 12px 32px #0009;padding:6px;z-index:30;border-radius:0 0 6px 6px}
|
|
.shell-menu-items>button{display:flex;width:100%;justify-content:space-between;gap:18px;background:transparent;border:0;color:#dbe6ee;padding:4px 8px;text-align:left;cursor:pointer;border-radius:3px;font-size:12.5px}
|
|
.shell-menu-items>button:hover{background:#29444b}
|
|
.shell-menu-items kbd{color:#93a6b3;font-size:11px}
|
|
.shell-submenu>summary{cursor:pointer;padding:4px 8px;border-radius:3px;color:#dbe6ee;font-size:12.5px}
|
|
.shell-submenu>summary:hover{background:#22313b}
|
|
.shell-submenu .shell-menu-items{padding-left:12px;border-left:1px solid #2a3a45;margin-left:8px}
|
|
.shell-menu-items hr{border:0;border-top:1px solid #2a3a45;margin:4px 0}
|
|
`}</style>
|
|
{STANDARD_MENUS.map(menu => <button key={menu.label} type="button" aria-expanded={open === menu.label}
|
|
onClick={() => setOpen(previous => previous === menu.label ? "" : menu.label)}>
|
|
{menu.label}
|
|
{open === menu.label && <div className="shell-dropdown" role="menu" aria-label={menu.label}>
|
|
<div className="shell-menu-items">{menu.items.map(id => <button key={id} type="button"
|
|
title={commandTooltip(manifest, id)} onClick={() => onCommand?.(id)}>
|
|
<span>{commandLabel(manifest, id)}</span><kbd>{id}</kbd></button>)}</div>
|
|
</div>}
|
|
</button>)}
|
|
<button type="button" aria-expanded={open === workbench.id}
|
|
onClick={() => setOpen(previous => previous === workbench.id ? "" : workbench.id)}>
|
|
{workbench.id}
|
|
{open === workbench.id && <div className="shell-dropdown" role="menu" aria-label={`${workbench.id} menu`}>
|
|
<MenuItems items={menus.map(menu => ({ type: "submenu" as const, label: menu.label, items: menu.items }))}
|
|
manifest={manifest} onCommand={onCommand} />
|
|
</div>}
|
|
</button>
|
|
</nav>;
|
|
}
|