@liberation-data/desk 0.43.0 → 0.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/README.md +225 -8
  2. package/dist/core/hosted.d.ts +222 -0
  3. package/dist/core/hosted.d.ts.map +1 -0
  4. package/dist/core/hosted.js +166 -0
  5. package/dist/core/hosted.js.map +1 -0
  6. package/dist/core/index.d.ts +9 -1
  7. package/dist/core/index.d.ts.map +1 -1
  8. package/dist/core/index.js +5 -1
  9. package/dist/core/index.js.map +1 -1
  10. package/dist/core/location.d.ts +8 -1
  11. package/dist/core/location.d.ts.map +1 -1
  12. package/dist/core/location.js +18 -2
  13. package/dist/core/location.js.map +1 -1
  14. package/dist/core/preferences.d.ts +27 -0
  15. package/dist/core/preferences.d.ts.map +1 -0
  16. package/dist/core/preferences.js +121 -0
  17. package/dist/core/preferences.js.map +1 -0
  18. package/dist/core/scheme.d.ts +9 -3
  19. package/dist/core/scheme.d.ts.map +1 -1
  20. package/dist/core/scheme.js +13 -4
  21. package/dist/core/scheme.js.map +1 -1
  22. package/dist/core/theme.d.ts +41 -0
  23. package/dist/core/theme.d.ts.map +1 -0
  24. package/dist/core/theme.js +56 -0
  25. package/dist/core/theme.js.map +1 -0
  26. package/dist/core/themeChange.d.ts +25 -0
  27. package/dist/core/themeChange.d.ts.map +1 -0
  28. package/dist/core/themeChange.js +49 -0
  29. package/dist/core/themeChange.js.map +1 -0
  30. package/dist/css/apps.css +35 -1
  31. package/dist/css/shell.css +100 -0
  32. package/dist/css/tokens.css +10 -0
  33. package/dist/desk.css +45 -1
  34. package/dist/fx/fx.d.ts.map +1 -1
  35. package/dist/fx/fx.js +7 -31
  36. package/dist/fx/fx.js.map +1 -1
  37. package/dist/host/checkChrome.d.ts +20 -0
  38. package/dist/host/checkChrome.d.ts.map +1 -0
  39. package/dist/host/checkChrome.js +219 -0
  40. package/dist/host/checkChrome.js.map +1 -0
  41. package/dist/host/checkTheme.d.ts +35 -0
  42. package/dist/host/checkTheme.d.ts.map +1 -0
  43. package/dist/host/checkTheme.js +128 -0
  44. package/dist/host/checkTheme.js.map +1 -0
  45. package/dist/host/index.d.ts +7 -0
  46. package/dist/host/index.d.ts.map +1 -0
  47. package/dist/host/index.js +8 -0
  48. package/dist/host/index.js.map +1 -0
  49. package/dist/host/router.d.ts +99 -0
  50. package/dist/host/router.d.ts.map +1 -0
  51. package/dist/host/router.js +279 -0
  52. package/dist/host/router.js.map +1 -0
  53. package/dist/react/Dock.d.ts +23 -4
  54. package/dist/react/Dock.d.ts.map +1 -1
  55. package/dist/react/Dock.js +17 -29
  56. package/dist/react/Dock.js.map +1 -1
  57. package/dist/react/MenuBar.d.ts +1 -1
  58. package/dist/react/MenuBar.d.ts.map +1 -1
  59. package/dist/react/MenuBar.js +20 -4
  60. package/dist/react/MenuBar.js.map +1 -1
  61. package/dist/react/app.d.ts +50 -9
  62. package/dist/react/app.d.ts.map +1 -1
  63. package/dist/react/app.js +59 -13
  64. package/dist/react/app.js.map +1 -1
  65. package/dist/react/appFrame.d.ts +11 -7
  66. package/dist/react/appFrame.d.ts.map +1 -1
  67. package/dist/react/appFrame.js +24 -6
  68. package/dist/react/appFrame.js.map +1 -1
  69. package/dist/react/appPanels.d.ts +4 -1
  70. package/dist/react/appPanels.d.ts.map +1 -1
  71. package/dist/react/appPanels.js +32 -23
  72. package/dist/react/appPanels.js.map +1 -1
  73. package/dist/react/appearance.d.ts +7 -1
  74. package/dist/react/appearance.d.ts.map +1 -1
  75. package/dist/react/appearance.js +32 -53
  76. package/dist/react/appearance.js.map +1 -1
  77. package/dist/react/context.d.ts +15 -1
  78. package/dist/react/context.d.ts.map +1 -1
  79. package/dist/react/context.js +73 -14
  80. package/dist/react/context.js.map +1 -1
  81. package/dist/react/dockStatus.d.ts +19 -0
  82. package/dist/react/dockStatus.d.ts.map +1 -0
  83. package/dist/react/dockStatus.js +25 -0
  84. package/dist/react/dockStatus.js.map +1 -0
  85. package/dist/react/frameMenus.d.ts +28 -0
  86. package/dist/react/frameMenus.d.ts.map +1 -0
  87. package/dist/react/frameMenus.js +85 -0
  88. package/dist/react/frameMenus.js.map +1 -0
  89. package/dist/react/hostApp.d.ts +15 -0
  90. package/dist/react/hostApp.d.ts.map +1 -0
  91. package/dist/react/hostApp.js +77 -0
  92. package/dist/react/hostApp.js.map +1 -0
  93. package/dist/react/hostDock.d.ts +25 -0
  94. package/dist/react/hostDock.d.ts.map +1 -0
  95. package/dist/react/hostDock.js +98 -0
  96. package/dist/react/hostDock.js.map +1 -0
  97. package/dist/react/hostMenu.d.ts +2 -36
  98. package/dist/react/hostMenu.d.ts.map +1 -1
  99. package/dist/react/hostMenu.js.map +1 -1
  100. package/dist/react/hosted.d.ts +28 -0
  101. package/dist/react/hosted.d.ts.map +1 -0
  102. package/dist/react/hosted.js +53 -0
  103. package/dist/react/hosted.js.map +1 -0
  104. package/dist/react/index.d.ts +8 -3
  105. package/dist/react/index.d.ts.map +1 -1
  106. package/dist/react/index.js +5 -2
  107. package/dist/react/index.js.map +1 -1
  108. package/dist/react/shell.d.ts +13 -1
  109. package/dist/react/shell.d.ts.map +1 -1
  110. package/dist/react/shell.js +19 -3
  111. package/dist/react/shell.js.map +1 -1
  112. package/dist/react/windowMenus.d.ts +19 -0
  113. package/dist/react/windowMenus.d.ts.map +1 -0
  114. package/dist/react/windowMenus.js +88 -0
  115. package/dist/react/windowMenus.js.map +1 -0
  116. package/dist/shell/HostShell.d.ts +53 -0
  117. package/dist/shell/HostShell.d.ts.map +1 -0
  118. package/dist/shell/HostShell.js +116 -0
  119. package/dist/shell/HostShell.js.map +1 -0
  120. package/dist/shell/index.d.ts +7 -0
  121. package/dist/shell/index.d.ts.map +1 -0
  122. package/dist/shell/index.js +9 -0
  123. package/dist/shell/index.js.map +1 -0
  124. package/dist/shell/keys.d.ts +47 -0
  125. package/dist/shell/keys.d.ts.map +1 -0
  126. package/dist/shell/keys.js +68 -0
  127. package/dist/shell/keys.js.map +1 -0
  128. package/dist/shell/motion.d.ts +72 -0
  129. package/dist/shell/motion.d.ts.map +1 -0
  130. package/dist/shell/motion.js +118 -0
  131. package/dist/shell/motion.js.map +1 -0
  132. package/llms.txt +48 -14
  133. package/package.json +10 -1
  134. package/src/core/hosted.ts +351 -0
  135. package/src/core/index.ts +28 -1
  136. package/src/core/location.ts +23 -3
  137. package/src/core/preferences.ts +132 -0
  138. package/src/core/scheme.ts +16 -5
  139. package/src/core/theme.ts +91 -0
  140. package/src/core/themeChange.ts +59 -0
  141. package/src/css/apps.css +35 -1
  142. package/src/css/shell.css +99 -0
  143. package/src/css/tokens.css +10 -0
  144. package/src/fx/fx.ts +7 -25
  145. package/src/host/checkChrome.ts +257 -0
  146. package/src/host/checkTheme.ts +134 -0
  147. package/src/host/index.ts +10 -0
  148. package/src/host/router.ts +374 -0
  149. package/src/react/Dock.tsx +34 -28
  150. package/src/react/MenuBar.tsx +22 -4
  151. package/src/react/app.tsx +84 -14
  152. package/src/react/appFrame.tsx +39 -14
  153. package/src/react/appPanels.tsx +38 -25
  154. package/src/react/appearance.tsx +36 -45
  155. package/src/react/context.tsx +88 -13
  156. package/src/react/dockStatus.ts +42 -0
  157. package/src/react/frameMenus.ts +100 -0
  158. package/src/react/hostApp.tsx +89 -0
  159. package/src/react/hostDock.tsx +118 -0
  160. package/src/react/hostMenu.ts +3 -38
  161. package/src/react/hosted.tsx +65 -0
  162. package/src/react/index.ts +9 -2
  163. package/src/react/shell.tsx +30 -3
  164. package/src/react/windowMenus.ts +107 -0
  165. package/src/shell/HostShell.tsx +220 -0
  166. package/src/shell/index.ts +11 -0
  167. package/src/shell/keys.ts +101 -0
  168. package/src/shell/motion.ts +163 -0
@@ -15,8 +15,10 @@ export { DeskShell } from './shell.js'
15
15
  export {
16
16
  ABOUT_WINDOW,
17
17
  appWindowTitle,
18
+ dockAbout,
18
19
  dockSettings,
19
20
  openAbout,
21
+ openSettings,
20
22
  PANEL_LIMITS,
21
23
  SETTINGS_WINDOW,
22
24
  SettingsCommand,
@@ -24,7 +26,7 @@ export {
24
26
  SYSTEM_GROUP,
25
27
  useDeskApp,
26
28
  } from './app.js'
27
- export type { AboutFact, AboutSection, AppLink, AppSettings, DeskApp, SettingsSection } from './app.js'
29
+ export type { AboutFact, AboutSection, AppDockMarker, AppLink, AppSettings, DeskApp, SettingsSection } from './app.js'
28
30
  export { AboutContent, aboutText, SettingsContent, useOpenSettings } from './appPanels.js'
29
31
  export {
30
32
  AppearanceSettings,
@@ -37,7 +39,7 @@ export {
37
39
  } from './appearance.js'
38
40
  export type { AppearanceRows, AppearanceSettingsProps } from './appearance.js'
39
41
  // Also in the core entry, with no React, for a server-rendered <head> and for moving a choice kept elsewhere.
40
- export { COLOR_SCHEME_KEY, COLOR_SCHEME_SCRIPT, LOOK_KEY } from '../core/scheme.js'
42
+ export { COLOR_SCHEME_KEY, COLOR_SCHEME_SCRIPT, colorSchemeScript, LOOK_KEY } from '../core/scheme.js'
41
43
  export type { ColorScheme } from '../core/scheme.js'
42
44
  export type { DeskShellProps } from './shell.js'
43
45
  export { DeskProvider, useDesk, useDeskState, useLook, useWindowId, useWindowRoute } from './context.js'
@@ -117,6 +119,7 @@ export type { ComposerProps, Message, ThreadProps } from './conversation.js'
117
119
  export { Alert, Popover, Sheet, ToastProvider, useToast } from './overlays.js'
118
120
  export type { AlertProps, PopoverProps, PopoverTriggerProps, SheetProps, Toast, ToastInput } from './overlays.js'
119
121
  export { MenuBar, menuAction, menuCommand, menuHeader, menuSeparator, widgetMenuItems, windowMenuItems } from './MenuBar.js'
122
+ export { useWindowMenus } from './windowMenus.js'
120
123
  export { useContextMenu } from './contextMenu.js'
121
124
  export type { ContextMenuOptions, ContextMenuResult, ContextMenuTargetProps } from './contextMenu.js'
122
125
  export type { Menu, MenuBarProps, MenuItem } from './MenuBar.js'
@@ -125,4 +128,8 @@ export type { HostMenu, HostMenuItem, HostMenuModel, MenuHost } from './hostMenu
125
128
  export { IconView } from './iconView.js'
126
129
  export type { IconViewItem, IconViewProps } from './iconView.js'
127
130
  export { Dock, DOCK_ITEM, dockItem, dockSeparator, dockStack } from './Dock.js'
131
+ export { hostDockModel } from './hostDock.js'
132
+ export type { HostDockModel } from './hostDock.js'
133
+ export { useHost, useHostBroadcast, useHosted, usePublishTheme } from './hosted.js'
128
134
  export type { DockEntry, DockItem, DockPins, DockProps, DockSide, DockStack } from './Dock.js'
135
+ export type { DockStatus } from './dockStatus.js'
@@ -1,11 +1,15 @@
1
- import { useState } from 'react'
1
+ import { useLayoutEffect, useState } from 'react'
2
2
  import type { ReactNode } from 'react'
3
3
  import { createDesk } from '../core/desk.js'
4
4
  import type { Desk } from '../core/desk.js'
5
5
  import type { Bus } from '../core/events.js'
6
+ import type { DeskHostBridge } from '../core/hosted.js'
6
7
  import type { LookChoice } from '../core/look.js'
7
8
  import type { Holdable } from '../core/services.js'
9
+ import { setPreferenceStore } from '../core/preferences.js'
10
+ import type { PreferenceStore } from '../core/preferences.js'
8
11
  import type { DeskApp } from './app.js'
12
+ import type { DeskTheme } from '../core/theme.js'
9
13
  import type { DeskOptions } from '../core/types.js'
10
14
  import { DeskProvider } from './context.js'
11
15
  import { BusProvider } from './events.js'
@@ -25,12 +29,35 @@ export interface DeskShellProps {
25
29
  readonly services?: readonly Holdable[]
26
30
  /** What the app says about itself, for its About and its Settings. See `DeskProviderProps.app`. */
27
31
  readonly app?: DeskApp
32
+ /** Hosted mode: see `DeskProviderProps.host`. Found on `globalThis.deskHost` when left out. */
33
+ readonly host?: DeskHostBridge | null
34
+ /** How a hosted app wears the theme it is sent. See `DeskProviderProps.onTheme`. */
35
+ readonly onTheme?: (theme: DeskTheme) => void
36
+ /**
37
+ * Where every choice Desk remembers is kept: scheme, look, sounds. This browser's localStorage when left
38
+ * out. Desk copies the store's values to localStorage too, for the head script's first paint.
39
+ */
40
+ readonly preferences?: PreferenceStore
28
41
  readonly children: ReactNode
29
42
  }
30
43
 
44
+ /* The stores mounted shells were given, the latest last: it is the one in use, until its shell goes. */
45
+ const givenStores: PreferenceStore[] = []
46
+
31
47
  /** Everything an app needs around it, in one component: the desk, its bus, and toasts. */
32
- export function DeskShell({ desk, options, bus, toasts = true, look = 'auto', services = [], app, children }: DeskShellProps) {
48
+ export function DeskShell({ desk, options, bus, toasts = true, look = 'auto', services = [], app, host, onTheme, preferences, children }: DeskShellProps) {
33
49
  const [made] = useState(() => desk ?? createDesk(options))
50
+ useLayoutEffect(() => {
51
+ if (!preferences) return
52
+ givenStores.push(preferences)
53
+ setPreferenceStore(preferences)
54
+ return () => {
55
+ const inUse = givenStores.at(-1)
56
+ givenStores.splice(givenStores.lastIndexOf(preferences), 1)
57
+ if (givenStores.at(-1) !== inUse) setPreferenceStore(givenStores.at(-1))
58
+ }
59
+ }, [preferences])
34
60
  const inner = toasts ? <ToastProvider>{children}</ToastProvider> : children
35
- return <DeskProvider desk={made} look={look} services={services} {...(app ? { app } : {})}>{bus ? <BusProvider bus={bus}>{inner}</BusProvider> : inner}</DeskProvider>
61
+ const given = { ...(app ? { app } : {}), ...(host === undefined ? {} : { host }), ...(onTheme === undefined ? {} : { onTheme }) }
62
+ return <DeskProvider desk={made} look={look} services={services} {...given}>{bus ? <BusProvider bus={bus}>{inner}</BusProvider> : inner}</DeskProvider>
36
63
  }
@@ -0,0 +1,107 @@
1
+ import { useEffect, useMemo, useSyncExternalStore } from 'react'
2
+ import { focusedId } from '../core/desk.js'
3
+ import type { Desk } from '../core/desk.js'
4
+ import type { WindowId } from '../core/types.js'
5
+ import { parseShortcut, isApplePlatform } from '../core/shortcuts.js'
6
+ import { warnOnce } from '../core/location.js'
7
+ import { DESK_SHORTCUTS } from './app.js'
8
+ import { useOptionalDesk, useWindowId } from './context.js'
9
+ import type { Menu, MenuItem } from './MenuBar.js'
10
+
11
+ /*
12
+ * MENUS FROM THE WINDOW IN FRONT. As on a Mac, the bar follows what is in front: a window's menus join it
13
+ * while the window has focus, after the app's own and before Window and Help, in the drawn bar or a host's.
14
+ * A window's item never takes a shortcut the app or Desk uses: it keeps its label, loses the key, and the
15
+ * clash is said once on the console.
16
+ */
17
+
18
+ interface Registry {
19
+ readonly sources: Set<{ readonly window: WindowId; readonly menus: readonly Menu[] }>
20
+ readonly listeners: Set<() => void>
21
+ version: number
22
+ }
23
+
24
+ const registries = new WeakMap<Desk, Registry>()
25
+ const registryOf = (desk: Desk): Registry => {
26
+ let registry = registries.get(desk)
27
+ if (!registry) registries.set(desk, (registry = { sources: new Set(), listeners: new Set(), version: 0 }))
28
+ return registry
29
+ }
30
+ const changed = (registry: Registry) => {
31
+ registry.version++
32
+ registry.listeners.forEach(listener => listener())
33
+ }
34
+
35
+ /** Adds `menus` to the bar while `window` is in front, each id prefixed with the window's. Returns a remover. */
36
+ export function addWindowMenus(desk: Desk, window: WindowId, menus: readonly Menu[]): () => void {
37
+ const registry = registryOf(desk)
38
+ const source = { window, menus: menus.map(menu => ({ ...menu, id: `${window}/${menu.id}` })) }
39
+ registry.sources.add(source)
40
+ changed(registry)
41
+ return () => {
42
+ if (registry.sources.delete(source)) changed(registry)
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Menus this window adds to the bar while it has focus. Call it inside a window's content (outside one it
48
+ * does nothing), with a stable array (`useMemo`).
49
+ */
50
+ export function useWindowMenus(menus: readonly Menu[]): void {
51
+ const desk = useOptionalDesk()
52
+ const window = useWindowId()
53
+ useEffect(() => (desk && window !== null && menus.length ? addWindowMenus(desk, window, menus) : undefined), [desk, window, menus])
54
+ }
55
+
56
+ /** The menus the window in front has added; none when nothing has focus. */
57
+ export function useFrontMenus(desk: Desk): readonly Menu[] {
58
+ const registry = registryOf(desk)
59
+ const subscribe = (listener: () => void) => {
60
+ registry.listeners.add(listener)
61
+ const stop = desk.subscribe(listener)
62
+ return () => {
63
+ registry.listeners.delete(listener)
64
+ stop()
65
+ }
66
+ }
67
+ const front = focusedId(useSyncExternalStore(desk.subscribe, desk.getState))
68
+ const version = useSyncExternalStore(subscribe, () => registry.version)
69
+ return useMemo(() => [...registry.sources].filter(s => s.window === front).flatMap(s => s.menus), [registry, front, version])
70
+ }
71
+
72
+ /** The keys a shortcut stands for, as `matchesShortcut` reads it here: `MOD+E`, and `ctrl+e` off a Mac, are `mod+e`. */
73
+ const keysOf = (shortcut: string) => {
74
+ const s = parseShortcut(shortcut)
75
+ const apple = isApplePlatform()
76
+ return [s.key, s.shift, s.alt, apple && s.mod, s.ctrl || (!apple && s.mod)].join('|')
77
+ }
78
+
79
+ const shortcutsOf = (menus: readonly Menu[]) =>
80
+ new Set([
81
+ ...Object.keys(DESK_SHORTCUTS),
82
+ ...menus.flatMap(m => (typeof m.items === 'function' ? m.items() : m.items)).flatMap(i => ('shortcut' in i && i.shortcut ? [i.shortcut] : [])),
83
+ ].map(keysOf))
84
+
85
+ /**
86
+ * The app's menus with the front window's joined in, before the ones with ids `window` and `help` (or at
87
+ * the end). A front item asking for a shortcut the app's menus, its `status` menus or Desk use loses it.
88
+ * Function items stay so.
89
+ */
90
+ export function joinFrontMenus(own: readonly Menu[], front: readonly Menu[], status: readonly Menu[] = []): readonly Menu[] {
91
+ if (front.length === 0) return own
92
+ const yielding = (menu: Menu, items: readonly MenuItem[]) => {
93
+ const taken = shortcutsOf([...own, ...status])
94
+ return items.map(item => {
95
+ if (!('shortcut' in item) || !item.shortcut || !taken.has(keysOf(item.shortcut))) return item
96
+ warnOnce(`desk: "${item.label}" in the ${menu.label} menu asks for ${item.shortcut}, which the app uses. The app's keeps it.`)
97
+ const { shortcut: _taken, ...rest } = item
98
+ return rest as MenuItem
99
+ })
100
+ }
101
+ const joined = front.map(menu => {
102
+ const { items } = menu
103
+ return { ...menu, items: typeof items === 'function' ? () => yielding(menu, items()) : yielding(menu, items) }
104
+ })
105
+ const at = own.findIndex(menu => menu.id === 'window' || menu.id === 'help')
106
+ return at < 0 ? [...own, ...joined] : [...own.slice(0, at), ...joined, ...own.slice(at)]
107
+ }
@@ -0,0 +1,220 @@
1
+ import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
2
+ import type { RefObject } from 'react'
3
+ import type { CommandTarget, DeskChrome, HostDockEntry, HostDockItem } from '../core/hosted.js'
4
+ import { applyTheme } from '../core/theme.js'
5
+ import type { DeskTheme } from '../core/theme.js'
6
+ import { DeskProvider } from '../react/context.js'
7
+ import { Dock, roveFocus } from '../react/Dock.js'
8
+ import type { DockEntry, DockItem, DockSide } from '../react/Dock.js'
9
+ import { neighbour, platformOf, switchKeyAction, switchKeys } from './keys.js'
10
+ import type { SwitchKeys } from './keys.js'
11
+
12
+ /*
13
+ * ONE DOCK AND ONE SWITCHER FOR SEVERAL APPS.
14
+ *
15
+ * A host that runs several Desk apps side by side, each in a view of its own, draws the chrome they
16
+ * share once: the dock, and a switcher between them. This is that chrome, for the host's own page.
17
+ * It draws; it moves nothing. The apps' views are the host's, and so is moving them: the shell says
18
+ * what the person asked for (`onSwitch`, `onCommand`) and the host does it, then says where it landed
19
+ * (`current`).
20
+ *
21
+ * The page sits under the apps' views, which the host bounds to the desktop area that `onInsets`
22
+ * reports, so the chrome around them shows and the apps cover the rest.
23
+ */
24
+
25
+ /** One app the host runs, as the switcher shows it. */
26
+ export interface ShellWorkspace {
27
+ readonly id: string
28
+ readonly title: string
29
+ /** A CSS colour: the switcher entry's mark. */
30
+ readonly accent?: string
31
+ /** An image URL or `data:` URI. */
32
+ readonly icon?: string
33
+ /** The app whose theme every workspace wears. */
34
+ readonly primary?: boolean
35
+ /** Running and drawn; asleep, with a snapshot; or closed, with a snapshot, until it is visited again. */
36
+ readonly state: 'live' | 'suspended' | 'discarded'
37
+ }
38
+
39
+ /** How the host should show a switch: the desktops slide, or (from the overview) zoom. */
40
+ export type SwitchHow = 'slide' | 'zoom'
41
+
42
+ /** The room the chrome takes on each side of the window, in CSS pixels: the apps go inside it. */
43
+ export interface ShellInsets {
44
+ readonly top: number
45
+ readonly right: number
46
+ readonly bottom: number
47
+ readonly left: number
48
+ }
49
+
50
+ export interface HostShellProps {
51
+ /** In the switcher's order: the primary first, then as the host registered them. */
52
+ readonly workspaces: readonly ShellWorkspace[]
53
+ /** The workspace on screen. Changes when the host has finished moving to another. */
54
+ readonly current: string
55
+ /** What each app last sent of its chrome, by workspace id; missing or null until it has. */
56
+ readonly chrome: Readonly<Record<string, DeskChrome | null | undefined>>
57
+ /** The primary's theme, applied to this page. */
58
+ readonly theme?: DeskTheme | null
59
+ /** A picture of each workspace that isn't live, for the overview. */
60
+ readonly snapshots?: Readonly<Record<string, string>>
61
+ /** The person asked for another workspace. */
62
+ readonly onSwitch: (id: string, how: SwitchHow) => void
63
+ /** The person chose one of an app's items: `target` says from where, `itemId` is the app's own id for it. */
64
+ readonly onCommand: (appId: string, target: CommandTarget, itemId: string) => void
65
+ /** The room the chrome takes, whenever it changes: bound the apps' views inside it. */
66
+ readonly onInsets?: (insets: ShellInsets) => void
67
+ /** The switching keys; the platform's defaults if left out, none at all with null. */
68
+ readonly keys?: SwitchKeys | null
69
+ /** How a click in the switcher switches. */
70
+ readonly clickSwitch?: SwitchHow
71
+ readonly dockSide?: DockSide
72
+ }
73
+
74
+ const imageIcon = (label: string, image: string | undefined) =>
75
+ image ? <img src={image} alt="" draggable={false} /> : <span className="desk-shell-initial">{label.slice(0, 1).toUpperCase()}</span>
76
+
77
+ /** An app's dock entries, as data, turned back into entries the drawn dock draws; choosing one asks the host. */
78
+ export function dockFromHost(entries: readonly HostDockEntry[], choose: (itemId: string) => void): DockEntry[] {
79
+ const item = (entry: HostDockItem): DockItem => ({
80
+ id: entry.id,
81
+ label: entry.label,
82
+ icon: imageIcon(entry.label, entry.image),
83
+ ...(entry.description === undefined ? {} : { description: entry.description }),
84
+ ...(entry.badge === undefined ? {} : { badge: entry.badge === '' ? <span className="desk-shell-dot" /> : entry.badge }),
85
+ ...(entry.disabled ? { disabled: true } : {}),
86
+ status: { running: entry.running, focused: entry.focused, minimized: entry.minimized },
87
+ onSelect: () => choose(entry.id),
88
+ })
89
+ return entries.map((entry): DockEntry => {
90
+ if (entry.type === 'separator') return { type: 'separator', id: entry.id }
91
+ if (entry.type === 'item') return { type: 'item', ...item(entry) }
92
+ return { type: 'stack', id: entry.id, label: entry.label, icon: imageIcon(entry.label, entry.image), items: entry.items.map(item) }
93
+ })
94
+ }
95
+
96
+ const defaultKeys = (): SwitchKeys => switchKeys(platformOf(typeof navigator === 'undefined' ? '' : navigator.platform || navigator.userAgent))
97
+
98
+ export function HostShell(props: HostShellProps) {
99
+ // Its own desk, never hosted: this page is the host's, drawing for the apps.
100
+ return (
101
+ <DeskProvider host={null} look={props.theme?.look ?? 'auto'}>
102
+ <Shell {...props} />
103
+ </DeskProvider>
104
+ )
105
+ }
106
+
107
+ function Shell({ workspaces, current, chrome, theme, onSwitch, onCommand, onInsets, keys, clickSwitch = 'slide', dockSide = 'left' }: HostShellProps) {
108
+ useLayoutEffect(() => {
109
+ if (theme) applyTheme(theme)
110
+ }, [theme])
111
+
112
+ const entries = useMemo(
113
+ () => dockFromHost(chrome[current]?.dock ?? [], itemId => onCommand(current, 'dock', itemId)),
114
+ [chrome, current, onCommand],
115
+ )
116
+
117
+ // The arrived workspace's title, said once, politely: only on a change, never on first showing.
118
+ const [arrived, setArrived] = useState('')
119
+ const shown = useRef(current)
120
+ useEffect(() => {
121
+ if (shown.current === current) return
122
+ shown.current = current
123
+ setArrived(workspaces.find(w => w.id === current)?.title ?? '')
124
+ }, [current, workspaces])
125
+
126
+ // The switching keys, when this page has focus; a host catches them first when an app has it.
127
+ const bound = useMemo(() => (keys === null ? null : keys ?? defaultKeys()), [keys])
128
+ useEffect(() => {
129
+ if (!bound) return
130
+ const onKey = (event: globalThis.KeyboardEvent) => {
131
+ const action = switchKeyAction(event, bound)
132
+ if (action !== 'previous' && action !== 'next') return
133
+ const to = neighbour(workspaces, current, action)
134
+ event.preventDefault()
135
+ if (to) onSwitch(to.id, 'slide')
136
+ }
137
+ window.addEventListener('keydown', onKey)
138
+ return () => window.removeEventListener('keydown', onKey)
139
+ }, [bound, workspaces, current, onSwitch])
140
+
141
+ const root = useRef<HTMLDivElement>(null)
142
+ useInsets(root, dockSide, onInsets)
143
+
144
+ return (
145
+ <div ref={root} className="desk-shell" data-dock-side={dockSide}>
146
+ <div className="desk-shell-bar">
147
+ <Switcher workspaces={workspaces} current={current} onChoose={id => id !== current && onSwitch(id, clickSwitch)} />
148
+ </div>
149
+ <Dock entries={entries} side={dockSide} label={`${workspaces.find(w => w.id === current)?.title ?? 'App'} dock`} />
150
+ <div className="desk-shell-announce" role="status" aria-live="polite">
151
+ {arrived}
152
+ </div>
153
+ </div>
154
+ )
155
+ }
156
+
157
+ function Switcher({ workspaces, current, onChoose }: { readonly workspaces: readonly ShellWorkspace[]; readonly current: string; readonly onChoose: (id: string) => void }) {
158
+ // One Tab stop: the current workspace's, or the first while the current one is not listed.
159
+ const stop = workspaces.some(w => w.id === current) ? current : workspaces[0]?.id
160
+ const toolbar = useRef<HTMLDivElement>(null)
161
+ // Arrowing moves the stop by hand; once the host has moved, it is the current workspace's again.
162
+ useLayoutEffect(() => {
163
+ toolbar.current?.querySelectorAll<HTMLButtonElement>('button').forEach(b => (b.tabIndex = b.dataset.workspace === stop ? 0 : -1))
164
+ }, [stop, workspaces])
165
+ return (
166
+ <div ref={toolbar} role="toolbar" aria-label="Workspaces" className="desk-shell-switcher" onKeyDown={event => {
167
+ // A held modifier makes the arrow a switching key, which the page's listener handles: don't rove too.
168
+ if (!event.ctrlKey && !event.altKey && !event.metaKey) roveFocus(event, { next: ['ArrowRight'], previous: ['ArrowLeft'] })
169
+ }}
170
+ >
171
+ {workspaces.map(w => (
172
+ <button
173
+ key={w.id}
174
+ type="button"
175
+ className="desk-shell-workspace"
176
+ data-rove
177
+ data-workspace={w.id}
178
+ tabIndex={w.id === stop ? 0 : -1}
179
+ aria-current={w.id === current ? 'true' : undefined}
180
+ data-state={w.state}
181
+ onClick={() => onChoose(w.id)}
182
+ >
183
+ {w.icon ? <img src={w.icon} alt="" draggable={false} /> : <span className="desk-shell-mark" style={w.accent ? { background: w.accent } : undefined} aria-hidden="true" />}
184
+ <span>{w.title}</span>
185
+ </button>
186
+ ))}
187
+ </div>
188
+ )
189
+ }
190
+
191
+ /** Reports the room the bar and the dock take, whenever either changes size. */
192
+ function useInsets(root: RefObject<HTMLDivElement | null>, side: DockSide, onInsets: ((insets: ShellInsets) => void) | undefined) {
193
+ const last = useRef('')
194
+ useLayoutEffect(() => {
195
+ const el = root.current
196
+ if (!el || !onInsets) return
197
+ const measure = () => {
198
+ const bounds = el.getBoundingClientRect()
199
+ const bar = el.querySelector('.desk-shell-bar')?.getBoundingClientRect()
200
+ const dock = el.querySelector('.desk-dock')?.getBoundingClientRect()
201
+ const gap = dock && dock.width > 0 ? 1 : 0
202
+ const insets: ShellInsets = {
203
+ top: Math.ceil(bar ? bar.bottom - bounds.top : 0),
204
+ left: side === 'left' && gap ? Math.ceil(dock!.right - bounds.left) : 0,
205
+ right: side === 'right' && gap ? Math.ceil(bounds.right - dock!.left) : 0,
206
+ bottom: side === 'bottom' && gap ? Math.ceil(bounds.bottom - dock!.top) : 0,
207
+ }
208
+ const key = JSON.stringify(insets)
209
+ if (key === last.current) return
210
+ last.current = key
211
+ onInsets(insets)
212
+ }
213
+ measure()
214
+ if (typeof ResizeObserver === 'undefined') return
215
+ const observer = new ResizeObserver(measure)
216
+ observer.observe(el)
217
+ el.querySelectorAll('.desk-shell-bar, .desk-dock').forEach(node => observer.observe(node))
218
+ return () => observer.disconnect()
219
+ })
220
+ }
@@ -0,0 +1,11 @@
1
+ /*
2
+ * The shell of a host that runs several Desk apps side by side: one dock and one switcher drawn for
3
+ * all of them, and the curve the host moves their views on. Only the host's own page loads it; no
4
+ * app does, and nothing in core or react imports it.
5
+ */
6
+ export { dockFromHost, HostShell } from './HostShell.js'
7
+ export type { HostShellProps, ShellInsets, ShellWorkspace, SwitchHow } from './HostShell.js'
8
+ export { neighbour, platformOf, switchKeyAction, switchKeys } from './keys.js'
9
+ export type { KeyInput, ShellPlatform, SwitchKey, SwitchKeys } from './keys.js'
10
+ export { animate, FADE_MS, linear, pageClock, SLIDE_MS, slideOffsets, slidePlan, spring, timerClock } from './motion.js'
11
+ export type { Animation, AnimateOptions, FrameClock, SlideOffsets, SlidePlan } from './motion.js'
@@ -0,0 +1,101 @@
1
+ /*
2
+ * THE KEYS THAT SWITCH WORKSPACES, PER PLATFORM.
3
+ *
4
+ * Every OS has keys of its own for its own workspaces, and a host's must never fight them: macOS takes
5
+ * Ctrl+←/→ for Spaces and Ctrl+↑ for Mission Control; GNOME takes Ctrl+Alt+←/→ (and Super+PageUp/Down),
6
+ * KDE Meta+Ctrl+←/→; Windows takes Ctrl+Win+←/→ for its virtual desktops. So the defaults step around
7
+ * each, and a host can offer its own in Settings.
8
+ *
9
+ * A key reaches whichever view has focus, usually an app's. So a host catches these before the app
10
+ * does (in Electron, `before-input-event`) and asks `switchKeyAction` what the key means; the shell's
11
+ * own page uses the same function for the keys it sees itself.
12
+ */
13
+
14
+ /** One key with its modifiers. `meta` is ⌘ on a Mac, Super (the Windows key) elsewhere. */
15
+ export interface SwitchKey {
16
+ readonly key: string
17
+ readonly ctrl?: boolean
18
+ readonly alt?: boolean
19
+ readonly meta?: boolean
20
+ readonly shift?: boolean
21
+ }
22
+
23
+ /** What each switching key does. */
24
+ export interface SwitchKeys {
25
+ readonly previous: SwitchKey
26
+ readonly next: SwitchKey
27
+ /** Opens the overview, once there is one to open. */
28
+ readonly overview: SwitchKey
29
+ }
30
+
31
+ export type ShellPlatform = 'mac' | 'linux' | 'windows'
32
+
33
+ /**
34
+ * The platform, from a browser's `navigator.platform` or user agent, or a Node `process.platform`
35
+ * ('darwin', 'linux', 'win32').
36
+ */
37
+ export function platformOf(name: string): ShellPlatform {
38
+ if (/mac|darwin|iphone|ipad/i.test(name)) return 'mac'
39
+ if (/win/i.test(name)) return 'windows'
40
+ return 'linux'
41
+ }
42
+
43
+ const arrows = (mods: Omit<SwitchKey, 'key'>): SwitchKeys => ({
44
+ previous: { key: 'ArrowLeft', ...mods },
45
+ next: { key: 'ArrowRight', ...mods },
46
+ overview: { key: 'ArrowUp', ...mods },
47
+ })
48
+
49
+ /** The default keys on a platform: Ctrl+Option on a Mac, Super+Alt on Linux, Ctrl+Alt on Windows. */
50
+ export function switchKeys(platform: ShellPlatform): SwitchKeys {
51
+ switch (platform) {
52
+ case 'mac':
53
+ return arrows({ ctrl: true, alt: true })
54
+ case 'linux':
55
+ return arrows({ meta: true, alt: true })
56
+ case 'windows':
57
+ return arrows({ ctrl: true, alt: true })
58
+ }
59
+ }
60
+
61
+ /** A key event from a page (`ctrlKey`…) or from Electron's `before-input-event` (`control`…). */
62
+ export interface KeyInput {
63
+ readonly key: string
64
+ /** `keydown`/`keyup` from a page, `keyDown`/`keyUp` from Electron: only a key going down switches. */
65
+ readonly type?: string
66
+ readonly ctrlKey?: boolean
67
+ readonly altKey?: boolean
68
+ readonly metaKey?: boolean
69
+ readonly shiftKey?: boolean
70
+ readonly control?: boolean
71
+ readonly alt?: boolean
72
+ readonly meta?: boolean
73
+ readonly shift?: boolean
74
+ }
75
+
76
+ const matches = (input: KeyInput, key: SwitchKey): boolean =>
77
+ input.key === key.key &&
78
+ Boolean(input.ctrlKey ?? input.control) === Boolean(key.ctrl) &&
79
+ Boolean(input.altKey ?? input.alt) === Boolean(key.alt) &&
80
+ Boolean(input.metaKey ?? input.meta) === Boolean(key.meta) &&
81
+ Boolean(input.shiftKey ?? input.shift) === Boolean(key.shift)
82
+
83
+ /** What a key does, if it is one of the switching keys: modifiers must match exactly. */
84
+ export function switchKeyAction(input: KeyInput, keys: SwitchKeys): 'previous' | 'next' | 'overview' | null {
85
+ // Electron's before-input-event sends the key's release too: one press must switch once.
86
+ if (/up$/i.test(input.type ?? '')) return null
87
+ if (matches(input, keys.previous)) return 'previous'
88
+ if (matches(input, keys.next)) return 'next'
89
+ if (matches(input, keys.overview)) return 'overview'
90
+ return null
91
+ }
92
+
93
+ /**
94
+ * The workspace one step from `current`, in the order given (the switcher's order), or null at
95
+ * either end: like the OS's own workspaces, switching does not wrap round.
96
+ */
97
+ export function neighbour<T extends { readonly id: string }>(workspaces: readonly T[], current: string, step: 'previous' | 'next'): T | null {
98
+ const index = workspaces.findIndex(w => w.id === current)
99
+ if (index < 0) return null
100
+ return workspaces[index + (step === 'next' ? 1 : -1)] ?? null
101
+ }