Some checks failed
FreeCAD master CI / Prepare (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
FreeCAD master CI / Pixi (push) Has been cancelled
FreeCAD master CI / Ubuntu (push) Has been cancelled
FreeCAD master CI / Windows (push) Has been cancelled
FreeCAD master CI / Lint (push) Has been cancelled
FreeCAD master CI / WrapUp (push) Has been cancelled
Stale Issues / stale (push) Has been cancelled
CodeQL Advanced (c-cpp) / Analyze (c-cpp) (push) Has been cancelled
Push Crowdin Translations / update-crowdin (push) Has been cancelled
Fetch Crowdin Translations / update-crowdin (push) Has been cancelled
Build Release / upload_src (push) Has been cancelled
Build Release / build (10.13, macos-15-intel, osx-64) (push) Has been cancelled
Build Release / build (11.0, macos-latest, osx-arm64) (push) Has been cancelled
Build Release / build (15.0, macos-latest, osx-arm64) (push) Has been cancelled
Build Release / build (ubuntu-22.04, linux-64) (push) Has been cancelled
Build Release / build (ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Build Release / build (windows-latest, win-64) (push) Has been cancelled
Monthly issue metrics / issue metrics (push) Has been cancelled
205 lines
15 KiB
TypeScript
205 lines
15 KiB
TypeScript
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<PartUiAction>?]> = [
|
|
["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<string, PartUiAction>;
|
|
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<HTMLDetailsElement>('.part-menu, .part-tool-group, .application-menus > details');
|
|
root?.querySelectorAll<HTMLDetailsElement>('details[open]').forEach(item => { item.open = false; });
|
|
if (root) { root.open = false; if (focus) root.querySelector<HTMLElement>('summary')?.focus(); }
|
|
}
|
|
function menuItems(menu: HTMLElement) {
|
|
return [...menu.querySelectorAll<HTMLElement>('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<HTMLDetailsElement>('details[open]').forEach(child => { child.open = false; });
|
|
sibling.open = false;
|
|
}
|
|
}
|
|
group.open = true;
|
|
const menu = group.querySelector<HTMLElement>('.part-command-menu');
|
|
if (focus && menu) {
|
|
const items = menuItems(menu);
|
|
items[focus === 'last' ? items.length - 1 : 0]?.focus();
|
|
}
|
|
}
|
|
function menuKeys(event: React.KeyboardEvent<HTMLElement>) {
|
|
const target = event.target as HTMLElement;
|
|
const menu = target.closest<HTMLElement>('.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<HTMLDetailsElement>('details[open]').forEach(child => { child.open = false; });
|
|
parent.open = false; parent.querySelector<HTMLElement>('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 <button type="button" className={toolbar ? 'part-tool-button' : undefined} data-command={command}
|
|
disabled={props.disabled(item)} aria-label={item.label} title={item.label} aria-pressed={toolbar ? props.currentTask === item.task : undefined}
|
|
onClick={event => { closeMenu(event.currentTarget, true); props.activate(item); }}>
|
|
{item.icon && <img src={`/icons/part/${item.icon}.svg`} alt="" aria-hidden="true" />}
|
|
{!item.icon && !toolbar && <span className="part-command-icon" aria-hidden="true" />}
|
|
{(!toolbar || !item.icon) && item.label}
|
|
</button>;
|
|
}
|
|
function Group({ name, commands, props, toolbar = false }: { name: string; commands: string[]; props: Props; toolbar?: boolean }) {
|
|
return <details className={toolbar ? 'part-tool-group' : 'part-submenu'} onKeyDown={toolbar ? menuKeys : undefined}>
|
|
<summary onClick={event => {
|
|
event.preventDefault();
|
|
const group = event.currentTarget.parentElement as HTMLDetailsElement;
|
|
if (group.open) group.open = false; else openGroup(group);
|
|
}}>{name}</summary><div className="part-command-menu">{commands.map(command => <Action key={command} command={command} props={props} />)}</div>
|
|
</details>;
|
|
}
|
|
export function PartWorkbenchMenu(props: Props) {
|
|
const root = useRef<HTMLDetailsElement>(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 <details ref={root} className="part-menu" onKeyDown={menuKeys}><summary>Part</summary><div className="part-command-menu">
|
|
<Action command="Part_BoxSelection" props={props} /><hr />
|
|
<Group name="Primitives" commands={family('Primitives')} props={props} />
|
|
<Action command="Part_Primitives" props={props} />
|
|
<Action command="Part_Builder" props={props} /><hr />
|
|
{['Part_ShapeFromMesh', 'Part_PointsFromMesh'].map(command => <Action key={command} command={command} props={props} />)}
|
|
{['Part_MakeSolid', 'Part_ReverseShape'].map(command => <Action key={command} command={command} props={props} />)}
|
|
<Group name="Copy" commands={family('Copy')} props={props} /><hr />
|
|
{['Boolean', 'Join', 'Split', 'Compound'].map(name => <Group key={name} name={name} commands={family(name)} props={props} />)}<hr />
|
|
<Action command="Sketcher_NewSketch" props={props} />
|
|
{[...tools, 'Part_Offset', 'Part_Offset2D', 'Part_Thickness', 'Part_ProjectionOnSurface', 'Part_SectionCut'].map(command => <Action key={command} command={command} props={props} />)}<hr />
|
|
<Action command="Part_EditAttachment" props={props} /><hr />
|
|
{family('Inspection').map(command => <Action key={command} command={command} props={props} />)}
|
|
</div></details>;
|
|
}
|
|
export function PartWorkbenchViewItems(props: Props) {
|
|
return <Action command="Part_ColorPerFace" props={props} />;
|
|
}
|
|
export function PartWorkbenchToolbars(props: Props) {
|
|
useEffect(() => {
|
|
const dismiss = (event: PointerEvent) => document.querySelectorAll<HTMLDetailsElement>('.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 <>
|
|
<ToolbarGroup name="Solids">{family('Solids').map(command => <Action key={command} command={command} props={props} toolbar />)}</ToolbarGroup>
|
|
<ToolbarGroup name="Part Tools">{['Sketcher_NewSketch', ...tools].map(command => <Action key={command} command={command} props={props} toolbar />)}
|
|
<Group name="Offset" commands={['Part_Offset', 'Part_Offset2D']} props={props} toolbar />
|
|
{['Part_Thickness', 'Part_ProjectionOnSurface', 'Part_ColorPerFace'].map(command => <Action key={command} command={command} props={props} toolbar />)}
|
|
</ToolbarGroup>
|
|
<ToolbarGroup name="Boolean Tools"><Group name="Compound Tools" commands={family('Compound')} props={props} toolbar />
|
|
{family('Boolean').map(command => <Action key={command} command={command} props={props} toolbar />)}
|
|
<Group name="Join Shapes" commands={family('Join')} props={props} toolbar /><Group name="Split Shapes" commands={family('Split')} props={props} toolbar />
|
|
{['Part_CheckGeometry', 'Part_Defeaturing'].map(command => <Action key={command} command={command} props={props} toolbar />)}
|
|
</ToolbarGroup>
|
|
</>;
|
|
}
|