import React, { useEffect, useRef } from "react"; import { PART_DESKTOP_MENUS } from "./part-desktop-adapter.js"; import { ToolbarGroup } from "./persistent-toolbar-workspace.js"; export interface PartUiAction { command: string; label: string; task: string; icon?: string; primitive?: "Cylinder" | "Sphere" | "Cone" | "Torus"; boolean?: "Cut" | "Fuse" | "Common"; join?: "connect" | "embed" | "cutout"; split?: "fragments" | "slice-apart" | "slice" | "xor"; } // Interaction mapping only. Geometry and document mutations stay in the existing adapters. // Menu families come from the existing Part desktop adapter. Native command labels/icons // correspond to Part/Gui/Command.cpp, CommandParametric.cpp and Resources/Part.qrc. const rows: Array<[string, string, string, string?, Partial?]> = [ ["Part_Box", "Cube", "box", "Part_Box_Parametric"], ["Part_Cylinder", "Cylinder", "primitive", "Part_Cylinder_Parametric", { primitive: "Cylinder" }], ["Part_Sphere", "Sphere", "primitive", "Part_Sphere_Parametric", { primitive: "Sphere" }], ["Part_Cone", "Cone", "primitive", "Part_Cone_Parametric", { primitive: "Cone" }], ["Part_Torus", "Torus", "primitive", "Part_Torus_Parametric", { primitive: "Torus" }], ["Part_Tube", "Tube", "tube", "Part_Tube"], ["Part_Primitives", "Primitive", "primitives", "Part_Primitives"], ["Part_ToleranceSet", "Set Tolerance", "tolerance", "Part_ToleranceSet"], ["Part_Builder", "Shape Builder", "shape-builder", "Part_Shapebuilder"], ["Part_SimpleCopy", "Simple Copy", "simple-copy", "Part_3D_object"], ["Part_TransformedCopy", "Transformed Copy", "transformed-copy", "Part_Transformed_Copy"], ["Part_ElementCopy", "Element Copy", "element-copy", "Part_Element_Copy"], ["Part_RefineShape", "Refine Shape", "refine-shape", "Part_Refine_Shape"], ["Part_Boolean", "Boolean Operation", "boolean", "Part_Booleans"], ["Part_Cut", "Cut", "boolean", "Part_Cut", { boolean: "Cut" }], ["Part_Fuse", "Union", "boolean", "Part_Fuse", { boolean: "Fuse" }], ["Part_Common", "Intersection", "boolean", "Part_Common", { boolean: "Common" }], ["Part_JoinConnect", "Connect Shapes", "join", "Part_JoinConnect", { join: "connect" }], ["Part_JoinEmbed", "Embed Shapes", "join", "Part_JoinEmbed", { join: "embed" }], ["Part_JoinCutout", "Cutout for Object", "join", "Part_JoinCutout", { join: "cutout" }], ["Part_BooleanFragments", "Boolean Fragments", "split", "Part_BooleanFragments", { split: "fragments" }], ["Part_SliceApart", "Slice Apart", "split", "Part_SliceApart", { split: "slice-apart" }], ["Part_Slice", "Slice to Compound", "split", "Part_Slice", { split: "slice" }], ["Part_XOR", "Boolean XOR", "split", "Part_XOR", { split: "xor" }], ["Part_Compound", "Compound", "compound", "Part_Compound"], ["Part_ExplodeCompound", "Explode Compound", "explode-compound", "Part_ExplodeCompound"], ["Part_CompoundFilter", "Compound Filter", "compound-filter", "Part_CompoundFilter"], ["Part_Extrude", "Extrude", "extrude", "Part_Extrude"], ["Part_Revolve", "Revolve", "revolve", "Part_Revolve"], ["Part_Mirror", "Mirror", "mirror", "Part_Mirror"], ["Part_Scale", "Scale", "scale", "Part_Scale"], ["Part_Fillet", "Fillet", "fillet", "Part_Fillet"], ["Part_Chamfer", "Chamfer", "chamfer", "Part_Chamfer"], ["Part_MakeFace", "Face From Wires", "make-face", "Part_MakeFace"], ["Part_RuledSurface", "Ruled Surface", "ruled-surface", "Part_RuledSurface"], ["Part_Loft", "Loft", "loft", "Part_Loft"], ["Part_Sweep", "Sweep", "sweep", "Part_Sweep"], ["Part_Section", "Section", "section", "Part_Section"], ["Part_CrossSections", "Cross-Sections", "cross-sections", "Part_CrossSections"], ["Part_Offset", "3D Offset", "offset", "Part_Offset"], ["Part_Offset2D", "2D Offset", "offset2d", "Part_Offset2D"], ["Part_Thickness", "Thickness", "thickness", "Part_Thickness"], ["Part_ProjectionOnSurface", "Project on Surface", "projection-on-surface", "Part_ProjectionOnSurface"], ["Part_SectionCut", "Persistent Section Cut", "section-cut", "Part_SectionCut"], ["Part_CheckGeometry", "Check Geometry", "check-geometry", "Part_CheckGeometry"], ["Part_Defeaturing", "Defeaturing", "defeaturing", "Part_Defeaturing"], ["Part_MakeSolid", "Convert to Solid", "make-solid", "Part_MakeSolid"], ["Part_ReverseShape", "Reverse Shapes", "reverse-shapes", "Part_Reverse_Shape"], ["Part_ColorPerFace", "Appearance per Face", "face-colors", "Part_ColorFace"], ["Part_BoxSelection", "Box Selection", "box-selection", "Part_BoxSelection"], ["Part_ShapeFromMesh", "Shape From Mesh", "shape-from-mesh", "Part_Shape_from_Mesh"], ["Part_PointsFromMesh", "Points From Shape", "points-from-mesh", "Part_PointsFromMesh"], ["Part_EditAttachment", "Attachment", "edit-attachment", "Part_Attachment"], ["Sketcher_NewSketch", "New Sketch", "sketcher-new-sketch", "Sketcher_NewSketch"], ["Materials_InspectAppearance", "Inspect Appearance", "inspect-appearance"], ["Materials_InspectMaterial", "Inspect Material", "inspect-material"], ]; export const partUiActions = Object.fromEntries(rows.map(([command, label, task, icon, options]) => [command, { command, label, task, icon, ...options }])) as Record; const family = (name: string) => (PART_DESKTOP_MENUS[name] ?? []).filter(command => partUiActions[command]); const tools = ["Part_Extrude", "Part_Revolve", "Part_Mirror", "Part_Scale", "Part_Fillet", "Part_Chamfer", "Part_MakeFace", "Part_RuledSurface", "Part_Loft", "Part_Sweep", "Part_Section", "Part_CrossSections"]; export const partUiCss = ` .part-menu{position:relative}.part-menu>summary{list-style:none}.part-menu>summary::-webkit-details-marker{display:none} .part-command-menu{position:absolute;left:0;top:100%;z-index:50;min-width:240px;padding:3px;background:#17212b;color:#e7edf3;border:1px solid #435464;box-shadow:0 4px 12px #0006} .part-command-menu .part-submenu{position:relative}.part-command-menu .part-submenu>.part-command-menu{left:100%;top:0} .part-command-menu button,.part-command-menu summary{font:inherit;font-size:13px;display:flex;align-items:center;gap:8px;width:100%;padding:4px 8px;border:0;border-radius:0;background:transparent;color:inherit;white-space:nowrap;text-align:left;cursor:default} .part-command-menu summary{list-style:none}.part-command-menu summary::after{content:'▸';margin-left:auto} .part-command-menu button:hover,.part-command-menu button:focus-visible,.part-command-menu summary:hover,.part-command-menu summary:focus-visible{background:#264b56;outline:1px solid #66c7d5;outline-offset:-1px} .part-command-menu button:disabled{opacity:.45}.part-command-menu hr{border:0;border-top:1px solid #435464;margin:3px 2px} .part-command-menu img,.part-command-icon{width:20px;height:20px;object-fit:contain;flex-shrink:0}.part-command-menu summary{padding-left:36px}.part-tool-button{display:inline-flex;align-items:center;justify-content:center;background:transparent;border:1px solid transparent;border-radius:2px;color:inherit;padding:3px;min-width:30px;height:30px;cursor:pointer} .part-tool-button img{width:24px;height:24px}.part-tool-button:hover,.part-tool-button:focus-visible{border-color:#66c7d5;background:#264b56}.part-tool-button[aria-pressed=true]{border-color:#66c7d5}.part-tool-button:disabled{opacity:.4;cursor:default} .part-tool-group{position:relative}.part-tool-group>summary{list-style:none;font-size:12px;padding:4px;cursor:pointer}.part-tool-group>summary::after{content:' ▾'}.part-tool-group>.part-command-menu{top:100%} @media(max-width:650px){.part-command-menu .part-submenu>.part-command-menu{position:static;box-shadow:none;border:0;padding-left:14px}.part-menu>.part-command-menu{max-height:70vh;overflow:auto}} `; type Props = { disabled(action: PartUiAction): boolean; activate(action: PartUiAction): void; currentTask: string }; function closeMenu(node: HTMLElement, focus = false) { const root = node.closest('.part-menu, .part-tool-group, .application-menus > details'); root?.querySelectorAll('details[open]').forEach(item => { item.open = false; }); if (root) { root.open = false; if (focus) root.querySelector('summary')?.focus(); } } function menuItems(menu: HTMLElement) { return [...menu.querySelectorAll('button:not(:disabled), summary')] .filter(item => item.closest('.part-command-menu') === menu); } function openGroup(group: HTMLDetailsElement, focus: 'first' | 'last' | false = false) { const siblings = group.parentElement?.children ?? []; for (const sibling of siblings) { if (sibling !== group && sibling instanceof HTMLDetailsElement) { sibling.querySelectorAll('details[open]').forEach(child => { child.open = false; }); sibling.open = false; } } group.open = true; const menu = group.querySelector('.part-command-menu'); if (focus && menu) { const items = menuItems(menu); items[focus === 'last' ? items.length - 1 : 0]?.focus(); } } function menuKeys(event: React.KeyboardEvent) { const target = event.target as HTMLElement; const menu = target.closest('.part-command-menu'); if (event.key === 'Tab') { closeMenu(target, true); return; } if (event.key === 'Escape' || event.key === 'ArrowLeft' && menu?.parentElement?.classList.contains('part-submenu')) { event.preventDefault(); event.stopPropagation(); const parent = menu?.parentElement; if (parent instanceof HTMLDetailsElement && parent.classList.contains('part-submenu')) { parent.querySelectorAll('details[open]').forEach(child => { child.open = false; }); parent.open = false; parent.querySelector('summary')?.focus(); } else closeMenu(target, true); return; } const submenu = target.parentElement instanceof HTMLDetailsElement ? target.parentElement : undefined; if (target.tagName === 'SUMMARY' && submenu && (['ArrowRight', 'Enter', ' '].includes(event.key) || !menu && ['ArrowDown', 'ArrowUp'].includes(event.key))) { event.preventDefault(); event.stopPropagation(); openGroup(submenu, event.key === 'ArrowUp' ? 'last' : 'first'); return; } if (!menu) return; if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return; event.preventDefault(); event.stopPropagation(); const items = menuItems(menu); if (!items.length) return; const index = items.indexOf(target); const next = event.key === 'Home' ? 0 : event.key === 'End' ? items.length - 1 : (index + (event.key === 'ArrowUp' ? -1 : 1) + items.length) % items.length; items[next]?.focus(); } function Action({ command, props, toolbar = false }: { command: string; props: Props; toolbar?: boolean }) { const item = partUiActions[command]; if (!item) return null; return ; } function Group({ name, commands, props, toolbar = false }: { name: string; commands: string[]; props: Props; toolbar?: boolean }) { return
{ event.preventDefault(); const group = event.currentTarget.parentElement as HTMLDetailsElement; if (group.open) group.open = false; else openGroup(group); }}>{name}
{commands.map(command => )}
; } export function PartWorkbenchMenu(props: Props) { const root = useRef(null); useEffect(() => { const dismiss = (event: PointerEvent) => { if (root.current && !root.current.contains(event.target as Node)) closeMenu(root.current); }; document.addEventListener('pointerdown', dismiss); return () => document.removeEventListener('pointerdown', dismiss); }, []); return
Part


{['Part_ShapeFromMesh', 'Part_PointsFromMesh'].map(command => )} {['Part_MakeSolid', 'Part_ReverseShape'].map(command => )}
{['Boolean', 'Join', 'Split', 'Compound'].map(name => )}
{[...tools, 'Part_Offset', 'Part_Offset2D', 'Part_Thickness', 'Part_ProjectionOnSurface', 'Part_SectionCut'].map(command => )}

{family('Inspection').map(command => )}
; } export function PartWorkbenchViewItems(props: Props) { return ; } export function PartWorkbenchToolbars(props: Props) { useEffect(() => { const dismiss = (event: PointerEvent) => document.querySelectorAll('.part-tool-group[open]').forEach(node => { if (!node.contains(event.target as Node)) closeMenu(node); }); document.addEventListener('pointerdown', dismiss); return () => document.removeEventListener('pointerdown', dismiss); }, []); return <> {family('Solids').map(command => )} {['Sketcher_NewSketch', ...tools].map(command => )} {['Part_Thickness', 'Part_ProjectionOnSurface', 'Part_ColorPerFace'].map(command => )} {family('Boolean').map(command => )} {['Part_CheckGeometry', 'Part_Defeaturing'].map(command => )} ; }