Apply unit preference to property display

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
2026-11-13 22:53:15 +08:00
parent 26c0963363
commit a13b1f221d
4 changed files with 37 additions and 6 deletions

View File

@@ -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
View 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}³`;
}