Add manifest-driven desktop shell preview
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
5
web/src/shell-preview-main.tsx
Normal file
5
web/src/shell-preview-main.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import React from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { ShellPreview } from "./shell/shell-preview.js";
|
||||
|
||||
createRoot(document.querySelector<HTMLDivElement>("#app")!).render(<ShellPreview />);
|
||||
68
web/src/shell/command-registry.ts
Normal file
68
web/src/shell/command-registry.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
export type DesktopCommandMetadata = {
|
||||
menuText: string;
|
||||
toolTip: string;
|
||||
icon: string;
|
||||
accel: string;
|
||||
};
|
||||
|
||||
export type DesktopMenuItem = {
|
||||
type: "command" | "submenu" | "separator";
|
||||
id?: string;
|
||||
label?: string;
|
||||
ref?: string;
|
||||
items?: DesktopMenuItem[];
|
||||
};
|
||||
|
||||
export type DesktopMenu = { label: string; items: DesktopMenuItem[] };
|
||||
export type DesktopToolbar = { name: string; items: string[] };
|
||||
export type DesktopWorkbench = {
|
||||
id: string;
|
||||
source: string;
|
||||
menus: DesktopMenu[];
|
||||
toolbars: DesktopToolbar[];
|
||||
docks: string[];
|
||||
};
|
||||
export type DesktopManifest = {
|
||||
schema: string;
|
||||
generatedAt: string;
|
||||
workbenchCount: number;
|
||||
workbenches: DesktopWorkbench[];
|
||||
commandMetadata: Record<string, DesktopCommandMetadata>;
|
||||
commandCount: number;
|
||||
menuCommandCount: number;
|
||||
toolbarCommandCount: number;
|
||||
};
|
||||
|
||||
let cache: DesktopManifest | null = null;
|
||||
|
||||
export async function loadDesktopManifest(url = "/api/desktop-gui-manifest.json"): Promise<DesktopManifest> {
|
||||
if (cache) return cache;
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`desktop manifest HTTP ${response.status}`);
|
||||
cache = (await response.json()) as DesktopManifest;
|
||||
return cache;
|
||||
}
|
||||
|
||||
export function workbenchById(manifest: DesktopManifest, id: string): DesktopWorkbench | undefined {
|
||||
return manifest.workbenches.find(item => item.id === id);
|
||||
}
|
||||
|
||||
export function commandLabel(manifest: DesktopManifest, command: string): string {
|
||||
return manifest.commandMetadata[command]?.menuText?.trim() || command;
|
||||
}
|
||||
|
||||
export function commandTooltip(manifest: DesktopManifest, command: string): string {
|
||||
return manifest.commandMetadata[command]?.toolTip?.trim() || command;
|
||||
}
|
||||
|
||||
export function menuCommandIds(menu: DesktopMenu): string[] {
|
||||
const out: string[] = [];
|
||||
const walk = (items: DesktopMenuItem[] = []) => {
|
||||
for (const item of items) {
|
||||
if (item.type === "command" && item.id) out.push(item.id);
|
||||
if (item.items) walk(item.items);
|
||||
}
|
||||
};
|
||||
walk(menu.items);
|
||||
return out;
|
||||
}
|
||||
78
web/src/shell/menu-bar.tsx
Normal file
78
web/src/shell/menu-bar.tsx
Normal file
@@ -0,0 +1,78 @@
|
||||
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>;
|
||||
}
|
||||
110
web/src/shell/shell-preview.tsx
Normal file
110
web/src/shell/shell-preview.tsx
Normal file
@@ -0,0 +1,110 @@
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import * as THREE from "three";
|
||||
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
||||
import { DesktopMenuBar } from "./menu-bar.js";
|
||||
import { DesktopToolbars } from "./toolbar.js";
|
||||
import { loadDesktopManifest, workbenchById, type DesktopManifest } from "./command-registry.js";
|
||||
|
||||
function PreviewViewport() {
|
||||
const host = useRef<HTMLDivElement>(null);
|
||||
useEffect(() => {
|
||||
const container = host.current;
|
||||
if (!container) return;
|
||||
const scene = new THREE.Scene();
|
||||
scene.background = new THREE.Color(0x0d1318);
|
||||
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 10000);
|
||||
camera.up.set(0, 0, 1);
|
||||
camera.position.set(160, -220, 160);
|
||||
const renderer = new THREE.WebGLRenderer({ antialias: true });
|
||||
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
|
||||
container.appendChild(renderer.domElement);
|
||||
renderer.domElement.style.width = "100%";
|
||||
renderer.domElement.style.height = "100%";
|
||||
renderer.domElement.style.display = "block";
|
||||
scene.add(new THREE.HemisphereLight(0xffffff, 0x223344, 1.8));
|
||||
const light = new THREE.DirectionalLight(0xffffff, 1.6); light.position.set(200, -300, 400); scene.add(light);
|
||||
const grid = new THREE.GridHelper(400, 20, 0x3f5a66, 0x223039);
|
||||
grid.rotation.x = Math.PI / 2; scene.add(grid);
|
||||
const axes = new THREE.AxesHelper(80); scene.add(axes);
|
||||
const box = new THREE.Mesh(new THREE.BoxGeometry(80, 60, 20), new THREE.MeshStandardMaterial({ color: 0x73c6d4, metalness: 0.2, roughness: 0.6 }));
|
||||
box.position.set(0, 0, 10); scene.add(box);
|
||||
const controls = new OrbitControls(camera, renderer.domElement);
|
||||
controls.enableDamping = true; controls.target.set(0, 0, 0); controls.update();
|
||||
const resize = () => {
|
||||
const width = container.clientWidth || 600; const height = container.clientHeight || 400;
|
||||
renderer.setSize(width, height, false); camera.aspect = width / height; camera.updateProjectionMatrix();
|
||||
};
|
||||
resize(); const observer = new ResizeObserver(resize); observer.observe(container);
|
||||
let frame = 0;
|
||||
const animate = () => { frame = requestAnimationFrame(animate); controls.update(); renderer.render(scene, camera); };
|
||||
animate();
|
||||
return () => { cancelAnimationFrame(frame); observer.disconnect(); controls.dispose(); renderer.dispose(); if (renderer.domElement.parentElement === container) container.removeChild(renderer.domElement); };
|
||||
}, []);
|
||||
return <div ref={host} className="shell-viewport" aria-label="Preview 3D viewport" />;
|
||||
}
|
||||
|
||||
export function ShellPreview() {
|
||||
const [manifest, setManifest] = useState<DesktopManifest | null>(null);
|
||||
const [workbenchId, setWorkbenchId] = useState("Part");
|
||||
const [lastCommand, setLastCommand] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
useEffect(() => {
|
||||
loadDesktopManifest().then(setManifest).catch(loadError => setError(String(loadError?.message ?? loadError)));
|
||||
}, []);
|
||||
const workbench = manifest ? workbenchById(manifest, workbenchId) : undefined;
|
||||
return <div className="shell-preview">
|
||||
<style>{`
|
||||
html,body,#app{margin:0;height:100%;background:#0f1419;color:#dbe6ee;font:13px/1.5 system-ui,"Noto Sans SC",sans-serif}
|
||||
.shell-preview{display:flex;flex-direction:column;height:100%}
|
||||
.shell-title{display:flex;align-items:center;gap:10px;padding:6px 10px;background:#1b242c;border-bottom:1px solid #3f3f3f}
|
||||
.shell-title strong{font-size:14px}
|
||||
.shell-title select{background:#0b1014;color:#dbe6ee;border:1px solid #3f3f3f;border-radius:4px;padding:3px 6px}
|
||||
.shell-body{display:grid;grid-template-columns:240px minmax(0,1fr) 300px;flex:1;min-height:0}
|
||||
.shell-tree,.shell-props{background:#17212b;border-right:1px solid #2a3a45;padding:8px;overflow:auto}
|
||||
.shell-props{border-right:0;border-left:1px solid #2a3a45}
|
||||
.shell-tree h3,.shell-props h3{margin:2px 0 8px;font-size:12px;color:#93a6b3;text-transform:uppercase}
|
||||
.shell-tree button,.shell-props button{display:block;width:100%;text-align:left;background:transparent;border:0;color:#dbe6ee;padding:3px 4px;border-radius:3px;font-size:12px}
|
||||
.shell-tree button:hover{background:#22313b}
|
||||
.shell-viewport{min-width:0;min-height:0}
|
||||
.shell-status{display:flex;gap:14px;padding:4px 10px;background:#1b242c;border-top:1px solid #3f3f3f;color:#93a6b3;font-size:11.5px}
|
||||
.shell-log{font-family:ui-monospace,monospace;color:#9fd6de;font-size:11px}
|
||||
`}</style>
|
||||
<div className="shell-title">
|
||||
<strong>Web FreeCAD · Desktop Shell Preview</strong>
|
||||
<select aria-label="Workbench" value={workbenchId} onChange={event => setWorkbenchId(event.target.value)}>
|
||||
{(manifest?.workbenches ?? []).map(item => <option key={item.id} value={item.id}>{item.id}</option>)}
|
||||
</select>
|
||||
<span className="shell-log">{manifest ? `${manifest.workbenches.length} workbenches · ${manifest.commandCount} commands` : "loading manifest…"}</span>
|
||||
<span className="shell-log">{lastCommand ? `last: ${lastCommand}` : ""}</span>
|
||||
</div>
|
||||
{error && <p style={{ color: "#e0b15c", padding: "6px 10px" }}>{error}</p>}
|
||||
{manifest && workbench && <DesktopMenuBar manifest={manifest} workbench={workbench}
|
||||
initialOpen="Part" onCommand={command => setLastCommand(command)} />}
|
||||
{manifest && workbench && <DesktopToolbars manifest={manifest} workbench={workbench}
|
||||
onCommand={command => setLastCommand(command)} />}
|
||||
<div className="shell-body">
|
||||
<aside className="shell-tree" aria-label="Model tree">
|
||||
<h3>Model</h3>
|
||||
{workbench?.docks?.length ? workbench.docks.map(dock => <button key={dock} type="button">{dock}</button>)
|
||||
: <button type="button">Model</button>}
|
||||
<button type="button">Tasks</button>
|
||||
</aside>
|
||||
<PreviewViewport />
|
||||
<aside className="shell-props" aria-label="Properties">
|
||||
<h3>Properties</h3>
|
||||
<p style={{ color: "#93a6b3", fontSize: 12 }}>
|
||||
{workbench ? `${workbench.id} · ${workbench.menus.length} menus · ${workbench.toolbars.length} toolbars` : ""}
|
||||
</p>
|
||||
<p style={{ color: "#93a6b3", fontSize: 12 }}>
|
||||
GUI 清单来自 desktop-gui-manifest.json;菜单/工具栏已由 React 渲染。
|
||||
</p>
|
||||
</aside>
|
||||
</div>
|
||||
<div className="shell-status">
|
||||
<span>Ready</span>
|
||||
<span>{workbench ? `${workbench.menus.length} menus` : ""}</span>
|
||||
<span>{workbench ? `${workbench.toolbars.length} toolbars` : ""}</span>
|
||||
<span>{workbench?.source ?? ""}</span>
|
||||
</div>
|
||||
</div>;
|
||||
}
|
||||
26
web/src/shell/toolbar.tsx
Normal file
26
web/src/shell/toolbar.tsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import React from "react";
|
||||
import type { DesktopManifest, DesktopWorkbench } from "./command-registry.js";
|
||||
import { commandLabel, commandTooltip } from "./command-registry.js";
|
||||
|
||||
export function DesktopToolbars({ manifest, workbench, onCommand }: {
|
||||
manifest: DesktopManifest;
|
||||
workbench: DesktopWorkbench;
|
||||
onCommand?: (id: string) => void;
|
||||
}) {
|
||||
return <div className="shell-toolbars" role="toolbar" aria-label="Workbench toolbars">
|
||||
<style>{`
|
||||
.shell-toolbars{display:flex;flex-wrap:wrap;gap:10px;padding:6px 10px;background:#333;border-bottom:1px solid #3f3f3f;color:#e7edf2;font:12px/1.3 system-ui,"Noto Sans SC",sans-serif}
|
||||
.shell-toolbar{display:flex;align-items:center;gap:4px;padding:2px 6px;border:1px solid #4a4a4a;border-radius:4px;background:#2b2b2b}
|
||||
.shell-toolbar strong{font-weight:600;color:#b9c7d0;margin-right:4px;white-space:nowrap}
|
||||
.shell-toolbar button{background:#3a3a3a;border:1px solid #555;color:#e7edf2;border-radius:3px;padding:2px 6px;cursor:pointer;font-size:11.5px;max-width:150px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||
.shell-toolbar button:hover{background:#4a5962;border-color:#73c6d4}
|
||||
`}</style>
|
||||
{workbench.toolbars.map(toolbar => <div className="shell-toolbar" key={toolbar.name}>
|
||||
<strong>{toolbar.name}</strong>
|
||||
{toolbar.items.slice(0, 18).map(command => <button key={command} type="button"
|
||||
title={commandTooltip(manifest, command)} onClick={() => onCommand?.(command)}>
|
||||
{commandLabel(manifest, command)}
|
||||
</button>)}
|
||||
</div>)}
|
||||
</div>;
|
||||
}
|
||||
Reference in New Issue
Block a user