"use client"; import { Grid3X3, Palette, Ruler, X } from "lucide-react"; import { useEffect, useState } from "react"; import { normalizeScaleForUnits, parseMeasurementInput, scaleOptionsForUnits, WORKSPACE_UNIT_OPTIONS } from "@/lib/measurementUnits"; import { DEFAULT_WORKPLANE_WORKSPACE } from "@/lib/workplaneSettings"; import type { GridSize, WorkplaneWorkspaceSettings } from "@/types/sketchforge"; type WorkspaceSettings = WorkplaneWorkspaceSettings; type WorkspaceSettingsSection = "appearance" | "measurement" | "workplane"; const GRID_SIZES: GridSize[] = ["Off", "0.1 mm", "0.25 mm", "0.5 mm", "1.0 mm", "2.0 mm", "5.0 mm", "Brick"]; const MIN_WORKSPACE_SIZE = 60; const MAX_WORKSPACE_SIZE = 2000; const MIN_GRID_BLOCK_SIZE = 1; const MAX_GRID_BLOCK_SIZE = 200; const WORKSPACE_SIZE_PRESETS = [ { label: "200 x 200 mm", width: 200, depth: 200 }, { label: "300 x 300 mm", width: 300, depth: 300 }, { label: "500 x 500 mm", width: 500, depth: 500 }, { label: "1000 x 1000 mm", width: 1000, depth: 1000 }, { label: "2000 x 2000 mm", width: 2000, depth: 2000 }, { label: "Custom", width: 200, depth: 200 }, ]; const GRID_BLOCK_PRESETS = ["1 mm", "2.5 mm", "5 mm", "10 mm", "20 mm", "50 mm", "100 mm", "Custom"] as const; function clamp(value: number, min: number, max: number) { return Math.min(max, Math.max(min, value)); } function gridBlockSizeForPreset(preset: string, fallback: number) { if (preset === "Custom") { return clamp(fallback, MIN_GRID_BLOCK_SIZE, MAX_GRID_BLOCK_SIZE); } return clamp(Number.parseFloat(preset) || DEFAULT_WORKPLANE_WORKSPACE.gridBlockSize, MIN_GRID_BLOCK_SIZE, MAX_GRID_BLOCK_SIZE); } export function WorkspaceSettingsModal({ workspace, snap, onWorkspaceChange, onSnapChange, onMakeDefault, onClose, }: { workspace: WorkspaceSettings; snap: GridSize; onWorkspaceChange: (next: WorkspaceSettings) => void; onSnapChange: (next: GridSize) => void; onMakeDefault: () => void; onClose: () => void; }) { const [defaultSaved, setDefaultSaved] = useState(false); const [activeSection, setActiveSection] = useState("appearance"); const [dimensionDrafts, setDimensionDrafts] = useState(() => ({ width: workspace.width.toFixed(workspace.accuracy), depth: workspace.depth.toFixed(workspace.accuracy), })); const [gridBlockSizeDraft, setGridBlockSizeDraft] = useState(() => workspace.gridBlockSize.toFixed(workspace.accuracy)); const scaleOptions = scaleOptionsForUnits(workspace.units); const scaleValue = normalizeScaleForUnits(workspace.units, workspace.scale); useEffect(() => { setDimensionDrafts({ width: workspace.width.toFixed(workspace.accuracy), depth: workspace.depth.toFixed(workspace.accuracy), }); }, [workspace.accuracy, workspace.depth, workspace.width]); useEffect(() => { setGridBlockSizeDraft(workspace.gridBlockSize.toFixed(workspace.accuracy)); }, [workspace.accuracy, workspace.gridBlockSize]); const patchWorkspace = (patch: Partial) => { setDefaultSaved(false); const next = { ...workspace, ...patch }; onWorkspaceChange({ ...next, scale: normalizeScaleForUnits(next.units, next.scale) }); }; const setDimension = (key: "width" | "depth", value: string) => { const parsed = parseMeasurementInput(value); const next = clamp(Number.isFinite(parsed) ? parsed : workspace[key], MIN_WORKSPACE_SIZE, MAX_WORKSPACE_SIZE); setDimensionDrafts((current) => ({ ...current, [key]: next.toFixed(workspace.accuracy) })); patchWorkspace({ [key]: next, sizePreset: "Custom" } as Partial); }; const setWorkspaceSizePreset = (sizePreset: string) => { const preset = WORKSPACE_SIZE_PRESETS.find((entry) => entry.label === sizePreset); if (!preset || sizePreset === "Custom") { patchWorkspace({ sizePreset: "Custom" }); return; } patchWorkspace({ sizePreset, width: preset.width, depth: preset.depth }); }; const setGridBlockPreset = (gridBlockPreset: string) => { patchWorkspace({ gridBlockPreset, gridBlockSize: gridBlockSizeForPreset(gridBlockPreset, workspace.gridBlockSize) }); }; const setGridBlockSize = (value: string) => { const parsed = parseMeasurementInput(value); const next = clamp(Number.isFinite(parsed) ? parsed : workspace.gridBlockSize, MIN_GRID_BLOCK_SIZE, MAX_GRID_BLOCK_SIZE); setGridBlockSizeDraft(next.toFixed(workspace.accuracy)); patchWorkspace({ gridBlockPreset: "Custom", gridBlockSize: next }); }; return (
event.stopPropagation()}>
Workspace settings
{activeSection === "appearance" ? ( <>
Appearance Adjust the canvas and navigation behavior.
patchWorkspace({ showShadows })} /> patchWorkspace({ showGrid })} /> patchWorkspace({ cruiseShapes })} /> ) : null} {activeSection === "measurement" ? ( <>
Measurement Choose units, precision, scale, and snapping.
patchWorkspace({ units })} /> patchWorkspace({ scale })} /> patchWorkspace({ accuracy: accuracy.slice(2).length as WorkspaceSettings["accuracy"] })} /> { setDefaultSaved(false); onSnapChange(next as GridSize); }} /> ) : null} {activeSection === "workplane" ? ( <>
Workplane Set the plate dimensions and visible grid spacing.
preset.label)} onChange={setWorkspaceSizePreset} />
{workspace.gridBlockPreset === "Custom" ? (
) : null} ) : null}
Save the current settings for this project.
); } function WorkspaceToggle({ label, checked, onChange }: { label: string; checked: boolean; onChange: (checked: boolean) => void }) { return ( ); } function WorkspaceSelect({ label, value, options, onChange, }: { label: string; value: string; options: readonly string[]; onChange: (value: string) => void; }) { return ( ); }