2023-08-28 20:31:49 -04:00
|
|
|
import { assign, createMachine } from 'xstate'
|
2024-03-14 15:56:45 -04:00
|
|
|
import { Themes, getSystemTheme, setThemeClass } from 'lib/theme'
|
Command bar: add extrude command, nonlinear editing, etc (#1204)
* Tweak toaster look and feel
* Add icons, tweak plus icon names
* Rename commandBarMeta to commandBarConfig
* Refactor command bar, add support for icons
* Create a tailwind plugin for aria-pressed button state
* Remove overlay from behind command bar
* Clean up toolbar
* Button and other style tweaks
* Icon tweaks follow-up: make old icons work with new sizing
* Delete unused static icons
* More CSS tweaks
* Small CSS tweak to project sidebar
* Add command bar E2E test
* fumpt
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* fix typo in a comment
* Fix icon padding (built version only)
* Update onboarding and warning banner icons padding
* Misc minor style fixes
* Get Extrude opening and canceling from command bar
* Iconography tweaks
* Get extrude kind of working
* Refactor command bar config types and organization
* Move command bar configs to be co-located with each other
* Start building a state machine for the command bar
* Start converting command bar to state machine
* Add support for multiple args, confirmation step
* Submission behavior, hotkeys, code organization
* Add new test for extruding from command bar
* Polish step back and selection hotkeys, CSS tweaks
* Loading style tweaks
* Validate selection inputs, polish UX of args re-editing
* Prevent submission with multiple selection on singlular arg
* Remove stray console logs
* Tweak test, CSS nit, remove extrude "result" argument
* Fix linting warnings
* Show Ctrl+/ instead of ⌘K on all platforms but Mac
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* Add "Enter sketch" to command bar
* fix command bar test
* Fix flaky cmd bar extrude test by waiting for engine select response
* Cover both button labels '⌘K' and 'Ctrl+/' in test
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
|
|
|
import { CameraSystem } from 'lib/cameraControls'
|
2024-03-14 15:56:45 -04:00
|
|
|
import { isTauri } from 'lib/isTauri'
|
|
|
|
import { writeToSettingsFile } from 'lib/tauriFS'
|
|
|
|
import { DEFAULT_PROJECT_NAME, SETTINGS_PERSIST_KEY } from 'lib/constants'
|
2024-03-12 10:37:35 -04:00
|
|
|
import {
|
|
|
|
UnitSystem,
|
2024-03-14 15:56:45 -04:00
|
|
|
type BaseUnit,
|
|
|
|
type SettingsMachineContext,
|
|
|
|
type Toggle,
|
|
|
|
} from 'lib/settings/settingsTypes'
|
2024-02-20 17:55:06 -08:00
|
|
|
|
2023-08-28 20:31:49 -04:00
|
|
|
export const settingsMachine = createMachine(
|
|
|
|
{
|
2024-03-14 15:56:45 -04:00
|
|
|
/** @xstate-layout N4IgpgJg5mDOIC5QGUwBc0EsB2VYDpMIAbMAYlTQAIBBY4qyrXWAbQAYBdRUABwHtYmLP2w8QAD0TsANCACe0gL5K5THHkIlylKgCEAhrDBUAqtmEduSEAKEixNqQgCM7AJz52AJgAsAVg8AZgAOEIA2fxd3XzlFBCCXf3xfdlS0kN9vGIiVNXRmTSJSCnQqAGEDAFswACcDCtE0Wv5iNi5xO2FMUXFnWQVlVRB1Fi0S3QARMAAzAwBXYmpJzFqwAGM0flr5K07Bbt6nRH9w-HcXcPcI8PYAdgu0oLiTu+98EPdQ0-8g8N8gu53HkRgUNARijoytM5otqAAFFoAKw21AActUwHsbF0HH1EFkvOxiSTScSXLFBggrsk7r4AuEQuxAd4oiEQaMitpStQAPLYABG-AMtQgGkYaAMaHm7WsfAOeOOCEC+HCiTevlu5JcYReCBCLhSFzc3m8SWJrJcHLBY0hPKoABUwBJqAB1eq8XgabHy+w9Rygfp69jWjDg8ZQ6gOgAWYBqPtsCv9+P17Hw3juIV+Pn87kiGeeVINIXwuf8rPC4WiVZcQVDhQh3N05mEjHksDQcYTuOTSrp+Du5ZC3g8bizbkp8QCaaelwep3YTP8vnr4btDv4UCgpCo0wF8ygVHhBmwYGI3aTR0DiFupfY-giQSC3iflZfepHvnwQV8Lge93cX4qxCO4VGGbB+AgOBxE5eAcUvANJEQABaXwQj1ZCQLvUkmXpFwzStYZYIjfY-SvJDXBHLxa01Stc0yIE7j1NwKW-NUAl8a4-DuZkwKUIA */
|
2023-08-28 20:31:49 -04:00
|
|
|
id: 'Settings',
|
|
|
|
predictableActionArguments: true,
|
2024-03-14 15:56:45 -04:00
|
|
|
context: {} as SettingsMachineContext,
|
2023-08-28 20:31:49 -04:00
|
|
|
initial: 'idle',
|
|
|
|
states: {
|
|
|
|
idle: {
|
2024-03-14 15:56:45 -04:00
|
|
|
entry: ['setThemeClass', 'setClientSideSceneUnits', 'persistSettings'],
|
2023-08-28 20:31:49 -04:00
|
|
|
on: {
|
2024-03-14 15:56:45 -04:00
|
|
|
'Set All Settings': {
|
|
|
|
actions: [
|
|
|
|
assign((context, event) => {
|
|
|
|
return {
|
|
|
|
...context,
|
|
|
|
...event.data,
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
'persistSettings',
|
|
|
|
'setThemeClass',
|
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
2023-09-08 10:13:35 -04:00
|
|
|
'Set Base Unit': {
|
2023-08-28 20:31:49 -04:00
|
|
|
actions: [
|
2023-12-06 14:44:13 -05:00
|
|
|
assign({
|
2024-02-20 17:55:06 -08:00
|
|
|
baseUnit: (_, event) => event.data.baseUnit,
|
2023-12-06 14:44:13 -05:00
|
|
|
}),
|
2023-08-28 20:31:49 -04:00
|
|
|
'persistSettings',
|
|
|
|
'toastSuccess',
|
2024-03-14 15:56:45 -04:00
|
|
|
'setClientSideSceneUnits',
|
|
|
|
'Execute AST',
|
2023-08-28 20:31:49 -04:00
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
2023-09-08 10:13:35 -04:00
|
|
|
'Set Camera Controls': {
|
2023-08-28 20:31:49 -04:00
|
|
|
actions: [
|
|
|
|
assign({
|
2023-09-08 10:13:35 -04:00
|
|
|
cameraControls: (_, event) => event.data.cameraControls,
|
2023-08-28 20:31:49 -04:00
|
|
|
}),
|
|
|
|
'persistSettings',
|
|
|
|
'toastSuccess',
|
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
|
|
|
'Set Default Directory': {
|
|
|
|
actions: [
|
|
|
|
assign({
|
|
|
|
defaultDirectory: (_, event) => event.data.defaultDirectory,
|
|
|
|
}),
|
|
|
|
'persistSettings',
|
|
|
|
'toastSuccess',
|
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
2023-09-08 10:13:35 -04:00
|
|
|
'Set Default Project Name': {
|
2023-08-28 20:31:49 -04:00
|
|
|
actions: [
|
|
|
|
assign({
|
2023-09-08 10:13:35 -04:00
|
|
|
defaultProjectName: (_, event) =>
|
|
|
|
event.data.defaultProjectName.trim() || DEFAULT_PROJECT_NAME,
|
2023-08-28 20:31:49 -04:00
|
|
|
}),
|
|
|
|
'persistSettings',
|
|
|
|
'toastSuccess',
|
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
2023-09-08 10:13:35 -04:00
|
|
|
'Set Onboarding Status': {
|
2023-08-28 20:31:49 -04:00
|
|
|
actions: [
|
2023-09-08 10:13:35 -04:00
|
|
|
assign({
|
|
|
|
onboardingStatus: (_, event) => event.data.onboardingStatus,
|
|
|
|
}),
|
2023-08-28 20:31:49 -04:00
|
|
|
'persistSettings',
|
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
2023-09-06 21:27:30 -04:00
|
|
|
'Set Text Wrapping': {
|
|
|
|
actions: [
|
|
|
|
assign({
|
|
|
|
textWrapping: (_, event) => event.data.textWrapping,
|
|
|
|
}),
|
|
|
|
'persistSettings',
|
|
|
|
'toastSuccess',
|
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
2023-09-08 10:13:35 -04:00
|
|
|
'Set Theme': {
|
2023-08-28 20:31:49 -04:00
|
|
|
actions: [
|
|
|
|
assign({
|
2023-09-08 10:13:35 -04:00
|
|
|
theme: (_, event) => event.data.theme,
|
2023-08-28 20:31:49 -04:00
|
|
|
}),
|
|
|
|
'persistSettings',
|
|
|
|
'toastSuccess',
|
2023-09-08 10:13:35 -04:00
|
|
|
'setThemeClass',
|
2023-08-28 20:31:49 -04:00
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
2023-09-08 10:13:35 -04:00
|
|
|
'Set Unit System': {
|
2023-08-28 20:31:49 -04:00
|
|
|
actions: [
|
|
|
|
assign({
|
2023-09-08 10:13:35 -04:00
|
|
|
unitSystem: (_, event) => event.data.unitSystem,
|
|
|
|
baseUnit: (_, event) =>
|
|
|
|
event.data.unitSystem === 'imperial' ? 'in' : 'mm',
|
|
|
|
}),
|
|
|
|
'persistSettings',
|
|
|
|
'toastSuccess',
|
2024-03-14 15:56:45 -04:00
|
|
|
'Execute AST',
|
2023-09-08 10:13:35 -04:00
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
|
|
|
'Toggle Debug Panel': {
|
|
|
|
actions: [
|
|
|
|
assign({
|
|
|
|
showDebugPanel: (context) => {
|
|
|
|
return !context.showDebugPanel
|
|
|
|
},
|
2023-08-28 20:31:49 -04:00
|
|
|
}),
|
|
|
|
'persistSettings',
|
2023-09-08 10:13:35 -04:00
|
|
|
'toastSuccess',
|
2023-08-28 20:31:49 -04:00
|
|
|
],
|
|
|
|
target: 'idle',
|
|
|
|
internal: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
tsTypes: {} as import('./settingsMachine.typegen').Typegen0,
|
|
|
|
schema: {
|
|
|
|
events: {} as
|
2024-03-14 15:56:45 -04:00
|
|
|
| { type: 'Set All Settings'; data: Partial<SettingsMachineContext> }
|
2023-09-08 10:13:35 -04:00
|
|
|
| { type: 'Set Base Unit'; data: { baseUnit: BaseUnit } }
|
2023-09-11 16:21:23 -04:00
|
|
|
| {
|
|
|
|
type: 'Set Camera Controls'
|
|
|
|
data: { cameraControls: CameraSystem }
|
|
|
|
}
|
2023-09-08 10:13:35 -04:00
|
|
|
| { type: 'Set Default Directory'; data: { defaultDirectory: string } }
|
2023-08-28 20:31:49 -04:00
|
|
|
| {
|
|
|
|
type: 'Set Default Project Name'
|
|
|
|
data: { defaultProjectName: string }
|
|
|
|
}
|
2023-09-08 10:13:35 -04:00
|
|
|
| { type: 'Set Onboarding Status'; data: { onboardingStatus: string } }
|
|
|
|
| { type: 'Set Text Wrapping'; data: { textWrapping: Toggle } }
|
|
|
|
| { type: 'Set Theme'; data: { theme: Themes } }
|
2023-08-28 20:31:49 -04:00
|
|
|
| {
|
|
|
|
type: 'Set Unit System'
|
2023-08-31 16:08:15 -04:00
|
|
|
data: { unitSystem: UnitSystem }
|
2023-08-28 20:31:49 -04:00
|
|
|
}
|
|
|
|
| { type: 'Toggle Debug Panel' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
actions: {
|
|
|
|
persistSettings: (context) => {
|
2024-03-14 15:56:45 -04:00
|
|
|
if (isTauri()) {
|
|
|
|
writeToSettingsFile(context).catch((err) => {
|
|
|
|
console.error('Error writing settings:', err)
|
|
|
|
})
|
|
|
|
}
|
2023-08-28 20:31:49 -04:00
|
|
|
try {
|
|
|
|
localStorage.setItem(SETTINGS_PERSIST_KEY, JSON.stringify(context))
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
|
|
|
},
|
2023-08-31 09:34:13 -04:00
|
|
|
setThemeClass: (context, event) => {
|
|
|
|
const currentTheme =
|
|
|
|
event.type === 'Set Theme' ? event.data.theme : context.theme
|
|
|
|
setThemeClass(
|
|
|
|
currentTheme === Themes.System ? getSystemTheme() : currentTheme
|
|
|
|
)
|
|
|
|
},
|
2023-08-28 20:31:49 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|