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

@ -15,6 +15,7 @@ import {
import { fileMachine } from 'machines/fileMachine'
import { isDesktop } from 'lib/isDesktop'
import {
DEFAULT_DEFAULT_LENGTH_UNIT,
DEFAULT_FILE_NAME,
DEFAULT_PROJECT_KCL_FILE,
FILE_EXT,
@ -29,11 +30,12 @@ import {
} from 'lib/getKclSamplesManifest'
import { markOnce } from 'lib/performance'
import { commandBarActor } from 'machines/commandBarMachine'
import { settingsActor, useSettings } from 'machines/appMachine'
import { useSettings } from 'machines/appMachine'
import { createRouteCommands } from 'lib/commandBarConfigs/routeCommandConfig'
import { useToken } from 'machines/appMachine'
import { createNamedViewsCommand } from 'lib/commandBarConfigs/namedViewsConfig'
import { reportRejection } from 'lib/trap'
import { err, reportRejection } from 'lib/trap'
import { newKclFile } from 'lang/project'
type MachineContext<T extends AnyStateMachine> = {
state: StateFrom<T>
@ -237,7 +239,12 @@ export const FileMachineProvider = ({
createdPath
)
} else {
await window.electron.writeFile(createdPath, input.content ?? '')
const codeToWrite = newKclFile(
input.content,
settings.modeling.defaultUnit.current
)
if (err(codeToWrite)) return Promise.reject(codeToWrite)
await window.electron.writeFile(createdPath, codeToWrite)
}
}
@ -266,7 +273,12 @@ export const FileMachineProvider = ({
})
createdName = name
createdPath = path
await window.electron.writeFile(createdPath, input.content ?? '')
const codeToWrite = newKclFile(
input.content,
settings.modeling.defaultUnit.current
)
if (err(codeToWrite)) return Promise.reject(codeToWrite)
await window.electron.writeFile(createdPath, codeToWrite)
}
return {
@ -357,10 +369,16 @@ export const FileMachineProvider = ({
const hasKclEntries =
entries.filter((e: string) => e.endsWith('.kcl')).length !== 0
if (!hasKclEntries) {
await window.electron.writeFile(
window.electron.path.join(project.path, DEFAULT_PROJECT_KCL_FILE),
''
const codeToWrite = newKclFile(
undefined,
settings.modeling.defaultUnit.current
)
if (err(codeToWrite)) return Promise.reject(codeToWrite)
const path = window.electron.path.join(
project.path,
DEFAULT_PROJECT_KCL_FILE
)
await window.electron.writeFile(path, codeToWrite)
// Refresh the route selected above because it's possible we're on
// the same path on the navigate, which doesn't cause anything to
// refresh, leaving a stale execution state.
@ -401,7 +419,9 @@ export const FileMachineProvider = ({
authToken: token ?? '',
projectData,
settings: {
defaultUnit: settings.modeling.defaultUnit.current ?? 'mm',
defaultUnit:
settings.modeling.defaultUnit.current ??
DEFAULT_DEFAULT_LENGTH_UNIT,
},
specialPropsForSampleCommand: {
onSubmit: async (data) => {
@ -419,18 +439,6 @@ export const FileMachineProvider = ({
},
})
}
// Either way, we want to overwrite the defaultUnit project setting
// with the sample's setting.
if (data.sampleUnits) {
settingsActor.send({
type: 'set.modeling.defaultUnit',
data: {
level: 'project',
value: data.sampleUnits,
},
})
}
},
providedOptions: kclSamples.map((sample) => ({
value: sample.pathFromProjectDirectoryToFirstFile,