Apply unit preference to property display
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -36,6 +36,7 @@ export function PreferencesDialog({ onClose }: { onClose: () => void }) {
|
||||
useEffect(() => {
|
||||
localStorage.setItem(KEY, JSON.stringify(preferences));
|
||||
if (hydrated.current) void saveSettings("web-preferences.json", preferences);
|
||||
window.dispatchEvent(new Event("freecad-preferences-changed"));
|
||||
}, [preferences]);
|
||||
const update = <K extends keyof WebPreferences>(key: K, value: WebPreferences[K]) =>
|
||||
setPreferences(previous => ({ ...previous, [key]: value }));
|
||||
|
||||
19
web/src/shell/units.ts
Normal file
19
web/src/shell/units.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export type UnitInfo = { symbol: string; lengthFactor: number; volumeFactor: number };
|
||||
|
||||
export function unitInfo(unitSystem: string): UnitInfo {
|
||||
if (unitSystem.startsWith("MKS")) return { symbol: "m", lengthFactor: 0.001, volumeFactor: 1e-9 };
|
||||
if (unitSystem.startsWith("Imperial")) return { symbol: "in", lengthFactor: 1 / 25.4, volumeFactor: 1 / (25.4 ** 3) };
|
||||
if (unitSystem.startsWith("Building US")) return { symbol: "in", lengthFactor: 1 / 25.4, volumeFactor: 1 / (25.4 ** 3) };
|
||||
if (unitSystem.startsWith("Building cm")) return { symbol: "cm", lengthFactor: 0.1, volumeFactor: 0.001 };
|
||||
return { symbol: "mm", lengthFactor: 1, volumeFactor: 1 };
|
||||
}
|
||||
|
||||
export function formatLength(valueMm: number, unitSystem: string, digits = 3) {
|
||||
const info = unitInfo(unitSystem);
|
||||
return `${(valueMm * info.lengthFactor).toFixed(digits)} ${info.symbol}`;
|
||||
}
|
||||
|
||||
export function formatVolume(valueMm3: number, unitSystem: string, digits = 4) {
|
||||
const info = unitInfo(unitSystem);
|
||||
return `${(valueMm3 * info.volumeFactor).toFixed(digits)} ${info.symbol}³`;
|
||||
}
|
||||
Reference in New Issue
Block a user