@liberation-data/desk 0.43.0 → 0.45.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 (197) hide show
  1. package/README.md +238 -10
  2. package/dist/core/desk.d.ts +2 -0
  3. package/dist/core/desk.d.ts.map +1 -1
  4. package/dist/core/desk.js +11 -2
  5. package/dist/core/desk.js.map +1 -1
  6. package/dist/core/hosted.d.ts +222 -0
  7. package/dist/core/hosted.d.ts.map +1 -0
  8. package/dist/core/hosted.js +166 -0
  9. package/dist/core/hosted.js.map +1 -0
  10. package/dist/core/index.d.ts +9 -1
  11. package/dist/core/index.d.ts.map +1 -1
  12. package/dist/core/index.js +5 -1
  13. package/dist/core/index.js.map +1 -1
  14. package/dist/core/location.d.ts +8 -1
  15. package/dist/core/location.d.ts.map +1 -1
  16. package/dist/core/location.js +18 -2
  17. package/dist/core/location.js.map +1 -1
  18. package/dist/core/preferences.d.ts +27 -0
  19. package/dist/core/preferences.d.ts.map +1 -0
  20. package/dist/core/preferences.js +121 -0
  21. package/dist/core/preferences.js.map +1 -0
  22. package/dist/core/scheme.d.ts +9 -3
  23. package/dist/core/scheme.d.ts.map +1 -1
  24. package/dist/core/scheme.js +13 -4
  25. package/dist/core/scheme.js.map +1 -1
  26. package/dist/core/theme.d.ts +41 -0
  27. package/dist/core/theme.d.ts.map +1 -0
  28. package/dist/core/theme.js +56 -0
  29. package/dist/core/theme.js.map +1 -0
  30. package/dist/core/themeChange.d.ts +25 -0
  31. package/dist/core/themeChange.d.ts.map +1 -0
  32. package/dist/core/themeChange.js +49 -0
  33. package/dist/core/themeChange.js.map +1 -0
  34. package/dist/core/types.d.ts +5 -0
  35. package/dist/core/types.d.ts.map +1 -1
  36. package/dist/css/apps.css +52 -1
  37. package/dist/css/shell.css +100 -0
  38. package/dist/css/tokens.css +10 -0
  39. package/dist/css/windows.css +28 -0
  40. package/dist/desk.css +90 -1
  41. package/dist/fx/fx.d.ts.map +1 -1
  42. package/dist/fx/fx.js +7 -31
  43. package/dist/fx/fx.js.map +1 -1
  44. package/dist/host/checkChrome.d.ts +20 -0
  45. package/dist/host/checkChrome.d.ts.map +1 -0
  46. package/dist/host/checkChrome.js +219 -0
  47. package/dist/host/checkChrome.js.map +1 -0
  48. package/dist/host/checkTheme.d.ts +35 -0
  49. package/dist/host/checkTheme.d.ts.map +1 -0
  50. package/dist/host/checkTheme.js +128 -0
  51. package/dist/host/checkTheme.js.map +1 -0
  52. package/dist/host/index.d.ts +7 -0
  53. package/dist/host/index.d.ts.map +1 -0
  54. package/dist/host/index.js +8 -0
  55. package/dist/host/index.js.map +1 -0
  56. package/dist/host/router.d.ts +99 -0
  57. package/dist/host/router.d.ts.map +1 -0
  58. package/dist/host/router.js +279 -0
  59. package/dist/host/router.js.map +1 -0
  60. package/dist/react/Desktop.d.ts.map +1 -1
  61. package/dist/react/Desktop.js +11 -14
  62. package/dist/react/Desktop.js.map +1 -1
  63. package/dist/react/Dock.d.ts +23 -4
  64. package/dist/react/Dock.d.ts.map +1 -1
  65. package/dist/react/Dock.js +17 -29
  66. package/dist/react/Dock.js.map +1 -1
  67. package/dist/react/MenuBar.d.ts +1 -1
  68. package/dist/react/MenuBar.d.ts.map +1 -1
  69. package/dist/react/MenuBar.js +20 -4
  70. package/dist/react/MenuBar.js.map +1 -1
  71. package/dist/react/app.d.ts +65 -12
  72. package/dist/react/app.d.ts.map +1 -1
  73. package/dist/react/app.js +79 -21
  74. package/dist/react/app.js.map +1 -1
  75. package/dist/react/appFrame.d.ts +11 -7
  76. package/dist/react/appFrame.d.ts.map +1 -1
  77. package/dist/react/appFrame.js +24 -6
  78. package/dist/react/appFrame.js.map +1 -1
  79. package/dist/react/appPanels.d.ts +4 -1
  80. package/dist/react/appPanels.d.ts.map +1 -1
  81. package/dist/react/appPanels.js +76 -28
  82. package/dist/react/appPanels.js.map +1 -1
  83. package/dist/react/appearance.d.ts +7 -1
  84. package/dist/react/appearance.d.ts.map +1 -1
  85. package/dist/react/appearance.js +32 -53
  86. package/dist/react/appearance.js.map +1 -1
  87. package/dist/react/context.d.ts +25 -1
  88. package/dist/react/context.d.ts.map +1 -1
  89. package/dist/react/context.js +73 -14
  90. package/dist/react/context.js.map +1 -1
  91. package/dist/react/dockStatus.d.ts +19 -0
  92. package/dist/react/dockStatus.d.ts.map +1 -0
  93. package/dist/react/dockStatus.js +25 -0
  94. package/dist/react/dockStatus.js.map +1 -0
  95. package/dist/react/frameMenus.d.ts +28 -0
  96. package/dist/react/frameMenus.d.ts.map +1 -0
  97. package/dist/react/frameMenus.js +85 -0
  98. package/dist/react/frameMenus.js.map +1 -0
  99. package/dist/react/hostApp.d.ts +15 -0
  100. package/dist/react/hostApp.d.ts.map +1 -0
  101. package/dist/react/hostApp.js +77 -0
  102. package/dist/react/hostApp.js.map +1 -0
  103. package/dist/react/hostDock.d.ts +25 -0
  104. package/dist/react/hostDock.d.ts.map +1 -0
  105. package/dist/react/hostDock.js +98 -0
  106. package/dist/react/hostDock.js.map +1 -0
  107. package/dist/react/hostMenu.d.ts +2 -36
  108. package/dist/react/hostMenu.d.ts.map +1 -1
  109. package/dist/react/hostMenu.js.map +1 -1
  110. package/dist/react/hosted.d.ts +28 -0
  111. package/dist/react/hosted.d.ts.map +1 -0
  112. package/dist/react/hosted.js +53 -0
  113. package/dist/react/hosted.js.map +1 -0
  114. package/dist/react/index.d.ts +10 -5
  115. package/dist/react/index.d.ts.map +1 -1
  116. package/dist/react/index.js +6 -3
  117. package/dist/react/index.js.map +1 -1
  118. package/dist/react/mediaQuery.d.ts +3 -0
  119. package/dist/react/mediaQuery.d.ts.map +1 -0
  120. package/dist/react/mediaQuery.js +11 -0
  121. package/dist/react/mediaQuery.js.map +1 -0
  122. package/dist/react/shell.d.ts +13 -1
  123. package/dist/react/shell.d.ts.map +1 -1
  124. package/dist/react/shell.js +19 -3
  125. package/dist/react/shell.js.map +1 -1
  126. package/dist/react/sidebar.d.ts +6 -1
  127. package/dist/react/sidebar.d.ts.map +1 -1
  128. package/dist/react/sidebar.js +6 -3
  129. package/dist/react/sidebar.js.map +1 -1
  130. package/dist/react/windowDescription.d.ts +7 -0
  131. package/dist/react/windowDescription.d.ts.map +1 -1
  132. package/dist/react/windowDescription.js +9 -0
  133. package/dist/react/windowDescription.js.map +1 -1
  134. package/dist/react/windowMenus.d.ts +19 -0
  135. package/dist/react/windowMenus.d.ts.map +1 -0
  136. package/dist/react/windowMenus.js +88 -0
  137. package/dist/react/windowMenus.js.map +1 -0
  138. package/dist/shell/HostShell.d.ts +53 -0
  139. package/dist/shell/HostShell.d.ts.map +1 -0
  140. package/dist/shell/HostShell.js +116 -0
  141. package/dist/shell/HostShell.js.map +1 -0
  142. package/dist/shell/index.d.ts +7 -0
  143. package/dist/shell/index.d.ts.map +1 -0
  144. package/dist/shell/index.js +9 -0
  145. package/dist/shell/index.js.map +1 -0
  146. package/dist/shell/keys.d.ts +47 -0
  147. package/dist/shell/keys.d.ts.map +1 -0
  148. package/dist/shell/keys.js +68 -0
  149. package/dist/shell/keys.js.map +1 -0
  150. package/dist/shell/motion.d.ts +72 -0
  151. package/dist/shell/motion.d.ts.map +1 -0
  152. package/dist/shell/motion.js +118 -0
  153. package/dist/shell/motion.js.map +1 -0
  154. package/llms.txt +57 -20
  155. package/package.json +10 -1
  156. package/src/core/desk.ts +11 -2
  157. package/src/core/hosted.ts +351 -0
  158. package/src/core/index.ts +28 -1
  159. package/src/core/location.ts +23 -3
  160. package/src/core/preferences.ts +132 -0
  161. package/src/core/scheme.ts +16 -5
  162. package/src/core/theme.ts +91 -0
  163. package/src/core/themeChange.ts +59 -0
  164. package/src/core/types.ts +5 -0
  165. package/src/css/apps.css +52 -1
  166. package/src/css/shell.css +99 -0
  167. package/src/css/tokens.css +10 -0
  168. package/src/css/windows.css +28 -0
  169. package/src/fx/fx.ts +7 -25
  170. package/src/host/checkChrome.ts +257 -0
  171. package/src/host/checkTheme.ts +134 -0
  172. package/src/host/index.ts +10 -0
  173. package/src/host/router.ts +374 -0
  174. package/src/react/Desktop.tsx +16 -21
  175. package/src/react/Dock.tsx +34 -28
  176. package/src/react/MenuBar.tsx +22 -4
  177. package/src/react/app.tsx +116 -22
  178. package/src/react/appFrame.tsx +39 -14
  179. package/src/react/appPanels.tsx +102 -31
  180. package/src/react/appearance.tsx +36 -45
  181. package/src/react/context.tsx +99 -13
  182. package/src/react/dockStatus.ts +42 -0
  183. package/src/react/frameMenus.ts +100 -0
  184. package/src/react/hostApp.tsx +89 -0
  185. package/src/react/hostDock.tsx +118 -0
  186. package/src/react/hostMenu.ts +3 -38
  187. package/src/react/hosted.tsx +65 -0
  188. package/src/react/index.ts +12 -4
  189. package/src/react/mediaQuery.ts +18 -0
  190. package/src/react/shell.tsx +30 -3
  191. package/src/react/sidebar.tsx +10 -3
  192. package/src/react/windowDescription.ts +12 -1
  193. package/src/react/windowMenus.ts +107 -0
  194. package/src/shell/HostShell.tsx +220 -0
  195. package/src/shell/index.ts +11 -0
  196. package/src/shell/keys.ts +101 -0
  197. package/src/shell/motion.ts +163 -0
@@ -1,7 +1,10 @@
1
1
  import type { CommandId } from '../core/commands.js'
2
+ import type { HostMenu, HostMenuItem } from '../core/hosted.js'
2
3
  import { parseShortcut } from '../core/shortcuts.js'
3
4
  import type { Menu, MenuItem } from './MenuBar.js'
4
5
 
6
+ export type { HostMenu, HostMenuItem }
7
+
5
8
  /*
6
9
  * A MENU BAR SOMEBODY ELSE DRAWS.
7
10
  *
@@ -11,44 +14,6 @@ import type { Menu, MenuItem } from './MenuBar.js'
11
14
  * under an id. When someone picks a row the host sends the id back, and the page runs the item.
12
15
  */
13
16
 
14
- /** One row of a menu a host draws. Plain data, so it can cross a process boundary. */
15
- export interface HostMenuItem {
16
- readonly type: 'item' | 'separator' | 'header'
17
- /**
18
- * Present on 'item'; unique within one publish. Opaque to a host, which only sends it back: it
19
- * names the item's menu, place and label, so a choice from a menu that has since changed runs
20
- * nothing rather than whatever took that place.
21
- */
22
- readonly id?: string
23
- /** Present on 'item' and 'header'. */
24
- readonly label?: string
25
- readonly detail?: string
26
- /** In desk's format: 'mod+k', 'mod+alt+a', 'alt+]'. */
27
- readonly shortcut?: string
28
- /**
29
- * The page handles this item's shortcut itself, so a host draws it beside the item and gives it
30
- * no key (Electron: `registerAccelerator: false`). Set on an action's shortcut, which the drawn
31
- * bar only shows too: binding it as well would fire the action twice.
32
- */
33
- readonly showOnly?: boolean
34
- /** Defaults to true. */
35
- readonly enabled?: boolean
36
- /** Absent means the item is not checkable. */
37
- readonly checked?: boolean
38
- }
39
-
40
- /** A whole menu as a host sees it. */
41
- export interface HostMenu {
42
- readonly id: string
43
- /** The menu's `label`: a host draws text, so a status menu's richer `title` stays behind. */
44
- readonly label: string
45
- /** The app's own menu, which a `Menu` calls `emphasis`. */
46
- readonly primary?: boolean
47
- /** One of the bar's status menus, which a host puts after the others. */
48
- readonly status?: boolean
49
- readonly items: readonly HostMenuItem[]
50
- }
51
-
52
17
  /** What a native shell provides so a `MenuBar` can live in its menu bar. */
53
18
  export interface MenuHost {
54
19
  /** Replaces every menu the page published before. An empty list takes them all away. */
@@ -0,0 +1,65 @@
1
+ import { createContext, useContext, useEffect, useMemo, useRef } from 'react'
2
+ import { THEME_TOPIC } from '../core/hosted.js'
3
+ import type { Broadcast, HostClient, Publisher } from '../core/hosted.js'
4
+ import type { DeskTheme } from '../core/theme.js'
5
+ import type { MenuHost } from './hostMenu.js'
6
+
7
+ /*
8
+ * An app in hosted mode has its host in context: `MenuBar` and `Dock` find it there and send their
9
+ * contents to the host instead of drawing them, and the app's own code reaches it through `useHost`
10
+ * to publish and listen. Outside a host the context is empty and nothing changes.
11
+ *
12
+ * The client connects in an effect, so for the first render an app knows it is hosted before it has
13
+ * a client: `client` is null then, and the chrome draws nothing rather than flash its own.
14
+ */
15
+
16
+ export const HostContext = createContext<{ readonly client: HostClient | null } | null>(null)
17
+
18
+ /** Whether this app runs in a host, connected yet or not. */
19
+ export const useHosted = (): boolean => useContext(HostContext) !== null
20
+
21
+ /** The host this app runs in, or null when it runs on its own (or has not connected yet). */
22
+ export const useHost = (): HostClient | null => useContext(HostContext)?.client ?? null
23
+
24
+ /**
25
+ * The host's menu bar as a `MenuHost`, for a `MenuBar` in hosted mode; null until connected. Shortcuts
26
+ * are shared as with any host's menu bar: the host gives those with `mod` a key and sends the item
27
+ * back, as a native menu's key equivalents do whichever app has focus, and the app binds the rest.
28
+ */
29
+ export function useHostMenu(): MenuHost | null {
30
+ const host = useHost()
31
+ return useMemo(
32
+ () =>
33
+ host && {
34
+ publish: menus => host.setChrome({ menus }),
35
+ onSelect: listener => host.onCommand('menu', listener),
36
+ },
37
+ [host],
38
+ )
39
+ }
40
+
41
+ /**
42
+ * Hears `publisher`'s broadcasts on `topic` while mounted, starting with the last one. `publisher` is
43
+ * an app's id, or `'primary'`. Does nothing when not hosted.
44
+ */
45
+ export function useHostBroadcast<T = unknown>(publisher: Publisher, topic: string, handler: (broadcast: Broadcast<T>) => void): void {
46
+ const host = useHost()
47
+ const latest = useRef(handler)
48
+ latest.current = handler
49
+ useEffect(() => host?.subscribe<T>(publisher, topic, broadcast => latest.current(broadcast)), [host, publisher, topic])
50
+ }
51
+
52
+ /**
53
+ * Publishes this app's theme to the host, again whenever it changes: what the primary does so that
54
+ * every other app wears its look. The host passes it on only from the primary. Does nothing when not
55
+ * hosted, or while `theme` is null.
56
+ */
57
+ export function usePublishTheme(theme: DeskTheme | null): void {
58
+ const host = useHost()
59
+ const key = theme ? JSON.stringify(theme) : ''
60
+ const latest = useRef(theme)
61
+ latest.current = theme
62
+ useEffect(() => {
63
+ if (host && latest.current) host.publish(THEME_TOPIC, latest.current)
64
+ }, [host, key])
65
+ }
@@ -15,16 +15,19 @@ 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,
23
25
  SIDEBAR_LIMITS,
26
+ SIDEBAR_SIZE,
24
27
  SYSTEM_GROUP,
25
28
  useDeskApp,
26
29
  } from './app.js'
27
- export type { AboutFact, AboutSection, AppLink, AppSettings, DeskApp, SettingsSection } from './app.js'
30
+ export type { AboutFact, AboutSection, AppDockMarker, AppLink, AppSettings, DeskApp, SettingsSection } from './app.js'
28
31
  export { AboutContent, aboutText, SettingsContent, useOpenSettings } from './appPanels.js'
29
32
  export {
30
33
  AppearanceSettings,
@@ -37,11 +40,11 @@ export {
37
40
  } from './appearance.js'
38
41
  export type { AppearanceRows, AppearanceSettingsProps } from './appearance.js'
39
42
  // 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'
43
+ export { COLOR_SCHEME_KEY, COLOR_SCHEME_SCRIPT, colorSchemeScript, LOOK_KEY } from '../core/scheme.js'
41
44
  export type { ColorScheme } from '../core/scheme.js'
42
45
  export type { DeskShellProps } from './shell.js'
43
46
  export { DeskProvider, useDesk, useDeskState, useLook, useWindowId, useWindowRoute } from './context.js'
44
- export type { DeskProviderProps } from './context.js'
47
+ export type { DeskProviderProps, WindowBack } from './context.js'
45
48
  export { useCanPerform, useCommand, usePerform, useShortcuts } from './commands.js'
46
49
  export type { UseCommandOptions } from './commands.js'
47
50
  export { isWidgetInFront, setWidgetInFront, useWidgetsInFront } from './widgets.js'
@@ -73,7 +76,7 @@ export type { SoundsToggleProps } from './soundsToggle.js'
73
76
  export { Loading, Spinner } from './progress.js'
74
77
  export { useWindowProgress } from './windowProgress.js'
75
78
  export type { WindowProgress } from './windowProgress.js'
76
- export { useWindowInfo, useWindowNote } from './windowDescription.js'
79
+ export { useWindowBack, useWindowInfo, useWindowNote } from './windowDescription.js'
77
80
  export type { WindowDescription } from './windowDescription.js'
78
81
  export type { LoadingProps, SpinnerProps, SpinnerSize } from './progress.js'
79
82
  export { Led } from './led.js'
@@ -117,6 +120,7 @@ export type { ComposerProps, Message, ThreadProps } from './conversation.js'
117
120
  export { Alert, Popover, Sheet, ToastProvider, useToast } from './overlays.js'
118
121
  export type { AlertProps, PopoverProps, PopoverTriggerProps, SheetProps, Toast, ToastInput } from './overlays.js'
119
122
  export { MenuBar, menuAction, menuCommand, menuHeader, menuSeparator, widgetMenuItems, windowMenuItems } from './MenuBar.js'
123
+ export { useWindowMenus } from './windowMenus.js'
120
124
  export { useContextMenu } from './contextMenu.js'
121
125
  export type { ContextMenuOptions, ContextMenuResult, ContextMenuTargetProps } from './contextMenu.js'
122
126
  export type { Menu, MenuBarProps, MenuItem } from './MenuBar.js'
@@ -125,4 +129,8 @@ export type { HostMenu, HostMenuItem, HostMenuModel, MenuHost } from './hostMenu
125
129
  export { IconView } from './iconView.js'
126
130
  export type { IconViewItem, IconViewProps } from './iconView.js'
127
131
  export { Dock, DOCK_ITEM, dockItem, dockSeparator, dockStack } from './Dock.js'
132
+ export { hostDockModel } from './hostDock.js'
133
+ export type { HostDockModel } from './hostDock.js'
134
+ export { useHost, useHostBroadcast, useHosted, usePublishTheme } from './hosted.js'
128
135
  export type { DockEntry, DockItem, DockPins, DockProps, DockSide, DockStack } from './Dock.js'
136
+ export type { DockStatus } from './dockStatus.js'
@@ -0,0 +1,18 @@
1
+ import { useCallback, useSyncExternalStore } from 'react'
2
+
3
+ /** Whether a CSS media query matches now, re-read when it changes. False where there is no `matchMedia`. */
4
+ export function useMediaQuery(query: string): boolean {
5
+ const subscribe = useCallback(
6
+ (onChange: () => void) => {
7
+ const list = globalThis.matchMedia?.(query)
8
+ list?.addEventListener('change', onChange)
9
+ return () => list?.removeEventListener('change', onChange)
10
+ },
11
+ [query],
12
+ )
13
+ return useSyncExternalStore(
14
+ subscribe,
15
+ () => globalThis.matchMedia?.(query).matches ?? false,
16
+ () => false,
17
+ )
18
+ }
@@ -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
  }
@@ -27,9 +27,14 @@ export interface SidebarProps {
27
27
  readonly label: string
28
28
  readonly footer?: ReactNode
29
29
  readonly className?: string
30
+ /**
31
+ * `true` when the list is shown on its own and choosing a place takes you away from it, as on a phone: the
32
+ * arrow keys then only move between the items, and Enter or a click chooses one. Otherwise moving chooses.
33
+ */
34
+ readonly focusOnly?: boolean
30
35
  }
31
36
 
32
- export function Sidebar({ sections, value, onChange, label, footer, className }: SidebarProps) {
37
+ export function Sidebar({ sections, value, onChange, label, footer, className, focusOnly }: SidebarProps) {
33
38
  const nav = useRef<HTMLElement>(null)
34
39
  const items = sections.flatMap(section => section.items).filter(item => !item.disabled)
35
40
 
@@ -38,13 +43,15 @@ export function Sidebar({ sections, value, onChange, label, footer, className }:
38
43
  const delta = keys[event.key as keyof typeof keys]
39
44
  if (!delta && event.key !== 'Home' && event.key !== 'End') return
40
45
  event.preventDefault()
41
- const at = items.findIndex(item => item.id === value)
46
+ // Moving without choosing, it moves from the item that has focus, not the one chosen.
47
+ const focused = (document.activeElement as HTMLElement | null)?.dataset?.item
48
+ const at = items.findIndex(item => item.id === (focusOnly && focused !== undefined ? focused : value))
42
49
  const next =
43
50
  event.key === 'Home' ? items[0]
44
51
  : event.key === 'End' ? items.at(-1)
45
52
  : items[(at + (delta ?? 1) + items.length) % items.length]
46
53
  if (!next) return
47
- onChange(next.id)
54
+ if (!focusOnly) onChange(next.id)
48
55
  nav.current?.querySelector<HTMLButtonElement>(`[data-item="${CSS.escape(next.id)}"]`)?.focus()
49
56
  }
50
57
 
@@ -1,6 +1,7 @@
1
1
  import { useContext, useEffect } from 'react'
2
2
  import type { ReactNode } from 'react'
3
3
  import { WindowContext } from './context.js'
4
+ import type { WindowBack } from './context.js'
4
5
 
5
6
  /*
6
7
  * WHAT THIS VIEW IS LOOKING AT, IN THE TITLE BAR.
@@ -40,7 +41,17 @@ export function useWindowInfo(info: WindowDescription): boolean {
40
41
  return useReport(setInfo, info)
41
42
  }
42
43
 
43
- function useReport(set: ((value: WindowDescription) => void) | undefined, value: WindowDescription): boolean {
44
+ /**
45
+ * A view one step in, as Settings' section on a phone: the title row gets a back button named for the
46
+ * parent ("‹ Settings") and the title becomes the view's, until the component unmounts or reports nothing.
47
+ * Returns whether a title row took it; outside a window the caller draws its own way back.
48
+ */
49
+ export function useWindowBack(back: WindowBack | null | undefined): boolean {
50
+ const setBack = useContext(WindowContext)?.setBack
51
+ return useReport(setBack, back ?? undefined)
52
+ }
53
+
54
+ function useReport<T>(set: ((value: T | undefined) => void) | undefined, value: T | undefined): boolean {
44
55
  useEffect(() => {
45
56
  set?.(value)
46
57
  }, [set, value])
@@ -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'