BREAKING: KCL @settings are the source of truth for units (#5808)

This commit is contained in:
Jonathan Tran
2025-03-31 10:56:03 -04:00
committed by GitHub
parent eac5abba79
commit efc8c82d8b
237 changed files with 820 additions and 2146 deletions

View File

@ -1,6 +1,11 @@
import { Popover } from '@headlessui/react'
import { settingsActor, useSettings } from 'machines/appMachine'
import { changeKclSettings, unitLengthToUnitLen } from 'lang/wasm'
import {
changeKclSettings,
unitAngleToUnitAng,
unitLengthToUnitLen,
} from 'lang/wasm'
import { DEFAULT_DEFAULT_ANGLE_UNIT } from 'lib/constants'
import { DEFAULT_DEFAULT_LENGTH_UNIT } from 'lib/constants'
import { baseUnitLabels, baseUnitsUnion } from 'lib/settings/settingsTypes'
import { codeManager, kclManager } from 'lib/singletons'
import { err, reportRejection } from 'lib/trap'
@ -8,24 +13,10 @@ import { useEffect, useState } from 'react'
import toast from 'react-hot-toast'
export function UnitsMenu() {
const settings = useSettings()
const [hasPerFileLengthUnit, setHasPerFileLengthUnit] = useState(
Boolean(kclManager.fileSettings.defaultLengthUnit)
)
const [lengthSetting, setLengthSetting] = useState(
kclManager.fileSettings.defaultLengthUnit ||
settings.modeling.defaultUnit.current
)
const [fileSettings, setFileSettings] = useState(kclManager.fileSettings)
useEffect(() => {
setHasPerFileLengthUnit(Boolean(kclManager.fileSettings.defaultLengthUnit))
setLengthSetting(
kclManager.fileSettings.defaultLengthUnit ||
settings.modeling.defaultUnit.current
)
}, [
kclManager.fileSettings.defaultLengthUnit,
settings.modeling.defaultUnit.current,
])
setFileSettings(kclManager.fileSettings)
}, [kclManager.fileSettings])
return (
<Popover className="relative pointer-events-auto">
@ -41,7 +32,7 @@ export function UnitsMenu() {
<div className="absolute w-[1px] h-[1em] bg-primary right-0 top-1/2 -translate-y-1/2"></div>
</div>
<span className="sr-only">Current units are:&nbsp;</span>
{lengthSetting}
{fileSettings.defaultLengthUnit ?? DEFAULT_DEFAULT_LENGTH_UNIT}
</Popover.Button>
<Popover.Panel
className={`absolute bottom-full right-0 mb-2 w-48 bg-chalkboard-10 dark:bg-chalkboard-90
@ -54,40 +45,35 @@ export function UnitsMenu() {
<button
className="flex items-center gap-2 m-0 py-1.5 px-2 cursor-pointer hover:bg-chalkboard-20 dark:hover:bg-chalkboard-80 border-none text-left"
onClick={() => {
if (hasPerFileLengthUnit) {
const newCode = changeKclSettings(codeManager.code, {
defaultLengthUnits: unitLengthToUnitLen(unit),
defaultAngleUnits: { type: 'Degrees' },
})
if (err(newCode)) {
toast.error(
`Failed to set per-file units: ${newCode.message}`
)
} else {
codeManager.updateCodeStateEditor(newCode)
Promise.all([
codeManager.writeToFile(),
kclManager.executeCode(),
])
.then(() => {
toast.success(`Updated per-file units to ${unit}`)
})
.catch(reportRejection)
}
const newCode = changeKclSettings(codeManager.code, {
defaultLengthUnits: unitLengthToUnitLen(unit),
defaultAngleUnits: unitAngleToUnitAng(
fileSettings.defaultAngleUnit ??
DEFAULT_DEFAULT_ANGLE_UNIT
),
})
if (err(newCode)) {
toast.error(
`Failed to set per-file units: ${newCode.message}`
)
} else {
settingsActor.send({
type: 'set.modeling.defaultUnit',
data: {
level: 'project',
value: unit,
},
})
codeManager.updateCodeStateEditor(newCode)
Promise.all([
codeManager.writeToFile(),
kclManager.executeCode(),
])
.then(() => {
toast.success(`Updated per-file units to ${unit}`)
})
.catch(reportRejection)
}
close()
}}
>
<span className="flex-1">{baseUnitLabels[unit]}</span>
{unit === lengthSetting && (
{unit ===
(fileSettings.defaultLengthUnit ??
DEFAULT_DEFAULT_LENGTH_UNIT) && (
<span className="text-chalkboard-60">current</span>
)}
</button>