2023-08-28 20:31:49 -04:00
|
|
|
import { assign, createMachine } from 'xstate'
|
2023-08-31 09:34:13 -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-02-16 09:09:58 -05:00
|
|
|
import { Models } from '@kittycad/lib'
|
|
|
|
|
2024-02-20 17:55:06 -08:00
|
|
|
const kclManagerPromise = import('lang/KclSingleton').then(
|
|
|
|
(module) => module.kclManager
|
|
|
|
)
|
|
|
|
|
2024-02-16 09:09:58 -05:00
|
|
|
export const DEFAULT_PROJECT_NAME = 'project-$nnn'
|
|
|
|
|
|
|
|
export enum UnitSystem {
|
|
|
|
Imperial = 'imperial',
|
|
|
|
Metric = 'metric',
|
|
|
|
}
|
|
|
|
|
|
|
|
export const baseUnits = {
|
|
|
|
imperial: ['in', 'ft', 'yd'],
|
|
|
|
metric: ['mm', 'cm', 'm'],
|
|
|
|
} as const
|
|
|
|
|
|
|
|
export type BaseUnit = Models['UnitLength_type']
|
|
|
|
|
|
|
|
export const baseUnitsUnion = Object.values(baseUnits).flatMap((v) => v)
|
|
|
|
|
|
|
|
export type Toggle = 'On' | 'Off'
|
|
|
|
|
|
|
|
export const SETTINGS_PERSIST_KEY = 'SETTINGS_PERSIST_KEY'
|
2023-08-28 20:31:49 -04:00
|
|
|
|
2024-03-08 17:59:14 -05:00
|
|
|
type SettingsMachineContext = {
|
|
|
|
baseUnit: BaseUnit
|
|
|
|
cameraControls: CameraSystem
|
|
|
|
defaultDirectory: string
|
|
|
|
defaultProjectName: string
|
|
|
|
onboardingStatus: string
|
|
|
|
showDebugPanel: boolean
|
|
|
|
textWrapping: Toggle
|
|
|
|
theme: Themes
|
|
|
|
unitSystem: UnitSystem
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:31:49 -04:00
|
|
|
export const settingsMachine = createMachine(
|
|
|
|
{
|
|
|
|
/** @xstate-layout N4IgpgJg5mDOIC5QGUwBc0EsB2VYDpMIAbMAYlTQAIAVACzAFswBtABgF1FQAHAe1iYsfbNxAAPRAA42+AEwB2KQFYAzGznKAnADZli1QBoQAT2kBGKfm37lOned3nzqgL6vjlLLgJFSFdCoAETAAMwBDAFdiagAFACc+ACswAGNqADlw5nYuJBB+QWFRfMkEABY5fDYa2rra83LjMwQdLWV8BXLyuxlVLU1Ld090bzxCEnJKYLComODMeLS0PniTXLFCoUwRMTK7fC1zNql7NgUjtnKjU0RlBSqpLVUVPVUda60tYZAvHHG-FNAgBVbBCKjIEywNBMDb5LbFPaILqdfRSORsS4qcxXZqIHqyK6qY4XOxsGTKco-P4+Cb+aYAIXCsDAVFBQjhvAE212pWkskUKnUml0+gUNxaqkU+EccnKF1UCnucnMcjcHl+o3+vkmZBofCgUFIMwARpEoFRYuFsGBiJyCtzEXzWrJlGxlKdVFKvfY1XiEBjyvhVOVzBdzu13pYFNStbTAQFqAB5bAmvjheIQf4QtDhNCRWD2hE7EqgfayHTEh7lHQNSxSf1Scz4cpHHFyFVujTKczuDXYPgQOBiGl4TaOktIhAAWg6X3nC4Xp39050sYw2rpYHHRUnztVhPJqmUlIGbEriv9WhrLZ6uibHcqUr7riAA */
|
|
|
|
id: 'Settings',
|
|
|
|
predictableActionArguments: true,
|
2024-02-16 09:09:58 -05:00
|
|
|
context: {
|
2024-03-08 17:59:14 -05:00
|
|
|
baseUnit: 'mm',
|
|
|
|
cameraControls: 'KittyCAD',
|
2024-02-16 09:09:58 -05:00
|
|
|
defaultDirectory: '',
|
|
|
|
defaultProjectName: DEFAULT_PROJECT_NAME,
|
|
|
|
onboardingStatus: '',
|
|
|
|
showDebugPanel: false,
|
2024-03-08 17:59:14 -05:00
|
|
|
textWrapping: 'On',
|
2024-02-16 09:09:58 -05:00
|
|
|
theme: Themes.System,
|
2024-02-20 17:55:06 -08:00
|
|
|
unitSystem: UnitSystem.Metric,
|
2024-03-08 17:59:14 -05:00
|
|
|
} as SettingsMachineContext,
|
2023-08-28 20:31:49 -04:00
|
|
|
initial: 'idle',
|
|
|
|
states: {
|
|
|
|
idle: {
|
2023-08-31 09:34:13 -04:00
|
|
|
entry: ['setThemeClass'],
|
2023-08-28 20:31:49 -04:00
|
|
|
on: {
|
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-02-20 17:55:06 -08:00
|
|
|
async () => {
|
|
|
|
;(await kclManagerPromise).executeAst()
|
|
|
|
},
|
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-02-20 17:55:06 -08:00
|
|
|
async () => {
|
|
|
|
;(await kclManagerPromise).executeAst()
|
|
|
|
},
|
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
|
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) => {
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|