@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
@@ -13,6 +13,8 @@ import { hasModKey, hostMenuModel, sameHostModel } from './hostMenu.js'
13
13
  import { isWidgetInFront, setWidgetInFront, useWidgetsInFront } from './widgets.js'
14
14
  import type { Widget } from './widgets.js'
15
15
  import type { HostMenu, MenuHost } from './hostMenu.js'
16
+ import { useHosted, useHostMenu } from './hosted.js'
17
+ import { joinFrontMenus, useFrontMenus } from './windowMenus.js'
16
18
 
17
19
  /*
18
20
  * A menu bar in the AppKit manner. Menus never take keyboard focus from the
@@ -195,11 +197,22 @@ const ownedBy = (host: MenuHost) => (shortcut: string) => (host.ownsShortcut ? h
195
197
  export function MenuBar(given: MenuBarProps) {
196
198
  const desk = useDesk()
197
199
  const app = useDeskApp()
200
+ const hosted = useHosted()
201
+ const hostMenu = useHostMenu()
198
202
  // About and Settings at the top of the app menu, before either bar sees the menus: drawn or in a host's
199
- // menu bar, they are there once. The item shows ⌘,; Desk binds it on the command, or the host owns it.
200
- const menus = useMemo(() => placeAppMenuItems(given.menus, app, () => openPanel(desk, ABOUT_WINDOW)), [given.menus, app, desk])
201
- const props = app ? { ...given, menus } : given
202
- return props.host ? <HostedMenuBar {...props} host={props.host} /> : <DrawnMenuBar {...props} />
203
+ // menu bar, they are there once. The item shows ⌘,; Desk binds it on the command, or the host owns it. In
204
+ // hosted mode they are the host's, which has one About and one Settings for every app it runs.
205
+ // The window in front adds its menus, after the app's own and before Window and Help, in either bar.
206
+ const front = useFrontMenus(desk)
207
+ const menus = useMemo(
208
+ () => joinFrontMenus(hosted ? given.menus : placeAppMenuItems(given.menus, app, () => openPanel(desk, ABOUT_WINDOW)), front, given.status),
209
+ [given.menus, given.status, app, desk, hosted, front],
210
+ )
211
+ const props = menus === given.menus ? given : { ...given, menus }
212
+ // In hosted mode the host's menu bar is the default `host`: one given here still wins.
213
+ const host = props.host ?? hostMenu
214
+ if (host) return <HostedMenuBar {...props} host={host} />
215
+ return hosted ? null : <DrawnMenuBar {...props} />
203
216
  }
204
217
 
205
218
  /**
@@ -338,6 +351,11 @@ function DrawnMenuBar({ menus, status = [], leading, trailing, label = 'Menu bar
338
351
  useEffect(() => {
339
352
  if (!open) return
340
353
  const menuIndex = all.findIndex(m => m.id === open.id)
354
+ // Its menu left the bar while open (its window left the front): close it, or it takes keys unseen.
355
+ if (menuIndex < 0) {
356
+ setOpen(null)
357
+ return
358
+ }
341
359
  const move = (active: number) => {
342
360
  setOpen({ ...open, active })
343
361
  revealMenuItem(`${baseId}-menu-${open.id}-${active}`)
package/src/react/app.tsx CHANGED
@@ -27,12 +27,16 @@ import type { Menu, MenuItem } from './MenuBar.js'
27
27
  export interface AppLink {
28
28
  readonly label: string
29
29
  readonly url: string
30
+ /** A heading the link is listed under on About, such as "Social". Links with none come first, unheaded. */
31
+ readonly group?: string
30
32
  }
31
33
 
32
34
  /** One line of the About panel's facts: what something is, and its value. */
33
35
  export interface AboutFact {
34
36
  readonly name: string
35
37
  readonly value: string
38
+ /** What Copy copies in place of `value`, which is what is shown: a short commit shown, the full one copied. */
39
+ readonly copyValue?: string
36
40
  /** What the person might want to know of it. Shown under the value, never copied. */
37
41
  readonly note?: ReactNode
38
42
  /** Where to read more, shown after the note as a link: the guide to a setup that could be faster. Never copied. */
@@ -48,6 +52,11 @@ export interface AboutFact {
48
52
  export interface AboutSection {
49
53
  readonly title?: string
50
54
  readonly facts: readonly AboutFact[]
55
+ /**
56
+ * Still being worked out: the section shows "Checking" under its title, while the others are shown,
57
+ * and Copy leaves it out until it is ready.
58
+ */
59
+ readonly pending?: boolean
51
60
  }
52
61
 
53
62
  /** One section of Settings: a heading and the controls under it. */
@@ -94,6 +103,11 @@ export interface DeskApp {
94
103
  readonly name: string
95
104
  /** The app's mark, drawn large on the About panel. */
96
105
  readonly icon?: ReactNode
106
+ /**
107
+ * The app's name as a wide mark, for a brand that is a wordmark: drawn under the icon at its own width
108
+ * rather than squeezed into a square, in place of the name as text, which stays for screen readers.
109
+ */
110
+ readonly wordmark?: ReactNode
97
111
  readonly version?: string
98
112
  /** A line under the name: what the app is for. */
99
113
  readonly description?: ReactNode
@@ -135,7 +149,7 @@ export const SettingsCommand = 'desk.settings'
135
149
  * these shows the shortcut and leaves the binding to this, so one press is one command. A host's native menu
136
150
  * bar that gives the shortcut a key of its own takes it over while it is up, so the page does not do it too.
137
151
  */
138
- const DESK_SHORTCUTS: Keymap = { 'mod+comma': SettingsCommand }
152
+ export const DESK_SHORTCUTS: Keymap = { 'mod+comma': SettingsCommand }
139
153
  const hostOwns = new WeakMap<Desk, (shortcut: string) => boolean>()
140
154
 
141
155
  /** Whether a menu item's shortcut is one Desk binds on the command already, so the menu only shows it. */
@@ -210,13 +224,31 @@ export function openPanel(desk: Desk, id: WindowId, route?: string): void {
210
224
  */
211
225
  export const openAbout = (desk: Desk): void => openPanel(desk, ABOUT_WINDOW)
212
226
 
213
- /** Opens Settings, in the app's own window if it has one, on `route` if given. */
214
- export function openSettings(desk: Desk, app: DeskApp | null, route?: string): void {
227
+ /** Opens Settings for this declaration, in the app's own window if it has one, on `route` if given. */
228
+ export function openAppSettings(desk: Desk, app: DeskApp | null, route?: string): void {
215
229
  if (!hasSettings(app)) return
216
230
  if (app?.settings?.open) app.settings.open(route)
217
231
  else openPanel(desk, SETTINGS_WINDOW, route)
218
232
  }
219
233
 
234
+ /* The declaration each desk's provider holds, as shown (Appearance in place, less what a host themes). */
235
+ const declarations = new WeakMap<Desk, () => DeskApp | null>()
236
+
237
+ /** The provider says what the app on this desk declared. Returns a function that forgets it. */
238
+ export function declareOn(desk: Desk, app: () => DeskApp | null): () => void {
239
+ declarations.set(desk, app)
240
+ return () => {
241
+ if (declarations.get(desk) === app) declarations.delete(desk)
242
+ }
243
+ }
244
+
245
+ /**
246
+ * Opens Settings as the gear, the menu item and ⌘, do, on the section `route` names: Desk's panel, or the
247
+ * app's own window. For a caller outside React, such as a search result or a link handler; inside, use
248
+ * `useOpenSettings()`. Nothing when the app has no Settings to show, or no provider has declared it.
249
+ */
250
+ export const openSettings = (desk: Desk, route?: string): void => openAppSettings(desk, declarations.get(desk)?.() ?? null, route)
251
+
220
252
  const glyph = (path: string) => (
221
253
  <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
222
254
  <path d={path} />
@@ -230,29 +262,67 @@ const INFO = glyph('M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18zM12 11v5M12 8h.01')
230
262
 
231
263
  /**
232
264
  * Where the gear goes, when the app wants it somewhere other than just before About: put this among its own
233
- * system items. It is dropped when the app has no settings.
265
+ * system items, or among the items of a stack, a System stack. It is dropped when the app has no settings.
266
+ */
267
+ export const dockSettings = (): AppDockMarker => ({ type: 'settings', id: SETTINGS_WINDOW, label: 'Settings', icon: GEAR })
268
+
269
+ /**
270
+ * Where About goes, when the app wants it somewhere other than last in the system group: among its own items,
271
+ * or in a stack. Placed either way, the app's own place is the only one: Desk adds no loose About.
234
272
  */
235
- export const dockSettings = (): DockEntry => ({ type: 'settings', id: SETTINGS_WINDOW })
273
+ export const dockAbout = (): AppDockMarker => ({ type: 'about', id: ABOUT_WINDOW, label: 'About', icon: INFO })
274
+
275
+ /**
276
+ * The marker `dockSettings()` and `dockAbout()` give: a dock entry, and a stack item too, so either can stand
277
+ * in a stack's items. Desk puts the real item in its place.
278
+ */
279
+ export type AppDockMarker = { readonly type: 'settings' | 'about'; readonly id: string } & DockItem
236
280
 
237
281
  /** The separator the system group starts after. An app that has none gets one before Settings and About. */
238
282
  export const SYSTEM_GROUP = 'system'
239
283
 
240
- type Placed = Exclude<DockEntry, { readonly type: 'settings' }>
284
+ type Placed = Exclude<DockEntry, { readonly type: 'settings' | 'about' }>
285
+ type Marker = AppDockMarker['type']
286
+
287
+ /** Which marker an entry or stack item is, `dockSettings()` or `dockAbout()`, or null for any other. */
288
+ export const markerOf = (entry: object): Marker | null => {
289
+ const type = (entry as { readonly type?: unknown }).type
290
+ return type === 'settings' || type === 'about' ? type : null
291
+ }
241
292
 
242
293
  /**
243
- * The app's dock entries with Settings and About in place: About last in the system group (the entries after
244
- * the separator with id `system`, up to the next separator), Settings where `dockSettings()` stands or else
245
- * just before About. The app's own items keep their order. Without a declaration, only the marker goes.
294
+ * The app's dock entries with Settings and About in place. Where the app put `dockSettings()` or `dockAbout()`,
295
+ * among its entries or in a stack, each goes there. What it did not place goes at the end of the system group
296
+ * (the entries after the separator with id `system`, up to the next separator): Settings, then About. The
297
+ * app's own items keep their order. Without a declaration, only the markers go, and a stack they leave empty.
246
298
  */
247
299
  export function placeAppDockItems(entries: readonly DockEntry[], app: DeskApp | null, open: { settings: () => void; about: () => void }): Placed[] {
248
300
  const settings: DockItem | null = hasSettings(app)
249
301
  ? { id: SETTINGS_WINDOW, label: 'Settings', icon: GEAR, onSelect: open.settings, ...(app?.settings?.badge != null ? { badge: app.settings.badge } : {}) }
250
302
  : null
251
- const marked = entries.some(e => e.type === 'settings')
252
- const placed: Placed[] = entries.flatMap(e => (e.type !== 'settings' ? [e] : settings && app ? [{ type: 'item' as const, ...settings }] : []))
253
- if (!app) return placed
254
- const about: Placed = { type: 'item', id: ABOUT_WINDOW, label: 'About', icon: INFO, onSelect: open.about }
255
- const ending = [...(settings && !marked ? [{ type: 'item' as const, ...settings }] : []), about]
303
+ const about: DockItem | null = app ? { id: ABOUT_WINDOW, label: 'About', icon: INFO, onSelect: open.about } : null
304
+ const marked = new Set<Marker>()
305
+ // A marker placed twice gives one item, where it first stands.
306
+ const replace = (marker: Marker): DockItem[] => {
307
+ const item = marked.has(marker) ? null : marker === 'settings' ? settings : about
308
+ marked.add(marker)
309
+ return item ? [item] : []
310
+ }
311
+ const placed: Placed[] = entries.flatMap((entry): Placed[] => {
312
+ const marker = markerOf(entry)
313
+ if (marker) return replace(marker).map(item => ({ type: 'item' as const, ...item }))
314
+ if (entry.type !== 'stack') return [entry as Placed]
315
+ if (!entry.items.some(markerOf)) return [entry]
316
+ const items = entry.items.flatMap(item => {
317
+ const inStack = markerOf(item)
318
+ return inStack ? replace(inStack) : [item]
319
+ })
320
+ return items.length ? [{ ...entry, items }] : []
321
+ })
322
+ const ending = [...(settings && !marked.has('settings') ? [settings] : []), ...(about && !marked.has('about') ? [about] : [])].map(
323
+ item => ({ type: 'item' as const, ...item }),
324
+ )
325
+ if (!ending.length) return placed
256
326
  const start = placed.findIndex(e => e.type === 'separator' && e.id === SYSTEM_GROUP)
257
327
  if (start < 0) return [...placed, ...(placed.length ? [{ type: 'separator' as const, id: SYSTEM_GROUP }] : []), ...ending]
258
328
  const next = placed.findIndex((e, i) => i > start && e.type === 'separator')
@@ -1,11 +1,15 @@
1
- import { useEffect, useRef } from 'react'
1
+ import { useEffect, useId, useRef, useState } from 'react'
2
2
  import type { CSSProperties } from 'react'
3
3
  import { topicMatches } from '../core/events.js'
4
+ import { DESK_PROTOCOL } from '../core/hosted.js'
5
+ import type { DeskChrome } from '../core/hosted.js'
6
+ import type { AppTheme } from '../core/theme.js'
4
7
  import type { DeskEvent } from '../core/events.js'
5
8
  import { useDragging, useDropTarget } from './dnd.js'
6
9
  import type { Accepts } from './dnd.js'
7
10
  import { useDesk, useWindowId } from './context.js'
8
11
  import { useBus } from './events.js'
12
+ import { frameMenus, useFrameMenus } from './frameMenus.js'
9
13
 
10
14
  /*
11
15
  * A generated app — a self-contained page — running inside a window, and taking
@@ -17,8 +21,8 @@ import { useBus } from './events.js'
17
21
  * which it may say, and what may be dropped on it. Nothing else crosses.
18
22
  */
19
23
 
20
- /** The protocol version. Every message carries it, so a mismatch is ignored rather than misread. */
21
- const PROTOCOL = 1
24
+ /** Every message carries the protocol version, shared with hosted mode, so a mismatch is ignored rather than misread. */
25
+ const PROTOCOL = DESK_PROTOCOL
22
26
 
23
27
  /*
24
28
  * THE HOST'S LOOK, OFFERED TO THE APP.
@@ -27,31 +31,30 @@ const PROTOCOL = 1
27
31
  * page inside a dark desk, a dark one inside a light desk. Switching the desk's theme should carry
28
32
  * the app too — the person switched the whole thing, not the chrome around it.
29
33
  *
30
- * So the host offers its palette, and the app takes it or ignores it. `mode` says which way round
31
- * the page is, for an app that decides its own colours but wants to know; `tokens` are custom
32
- * properties the bridge sets on the app's own root, so an app that reads them gets the desk's
33
- * colours with nothing to write.
34
+ * So the host offers its palette (an `AppTheme`), and the app takes it or ignores it. `mode` says
35
+ * which way round the page is, for an app that decides its own colours but wants to know; `tokens`
36
+ * are custom properties the bridge sets on the app's own root, so an app that reads them gets the
37
+ * desk's colours with nothing to write.
34
38
  *
35
39
  * The mode is stamped as `data-theme`, which is the attribute desk's own stylesheet reads and the
36
40
  * one the HIG tells app authors to honour — so a page already written that way follows the desk
37
41
  * with nothing added to it.
38
42
  */
39
- export interface AppTheme {
40
- readonly mode: 'light' | 'dark'
41
- /** Custom properties, named as they will be set: `{ '--sb-bg-dark': '#0b0f1a' }`. */
42
- readonly tokens: Readonly<Record<string, string>>
43
- }
44
43
 
45
44
  type HostMessage =
46
45
  | { readonly desk: 1; readonly kind: 'hello'; readonly window: string | null; readonly listens: readonly string[]; readonly says: readonly string[]; readonly theme: AppTheme | null }
47
46
  | { readonly desk: 1; readonly kind: 'theme'; readonly theme: AppTheme }
48
47
  | { readonly desk: 1; readonly kind: 'event'; readonly topic: string; readonly payload: unknown; readonly from: string | null }
49
48
  | { readonly desk: 1; readonly kind: 'drop'; readonly type: string; readonly payload: unknown; readonly from: string | null }
49
+ /** A menu item the app sent was chosen: hosted mode's command, with the app's own id. */
50
+ | { readonly desk: 1; readonly kind: 'command'; readonly target: 'menu'; readonly id: string }
50
51
 
51
52
  type AppMessage =
52
53
  | { readonly desk: 1; readonly kind: 'ready' }
53
54
  | { readonly desk: 1; readonly kind: 'publish'; readonly topic: string; readonly payload: unknown }
54
55
  | { readonly desk: 1; readonly kind: 'open'; readonly window: string }
56
+ /** The app's menus, as hosted mode's apps send theirs. Only `menus` is read. */
57
+ | { readonly desk: 1; readonly kind: 'chrome'; readonly chrome: Partial<DeskChrome> }
55
58
 
56
59
  const isAppMessage = (data: unknown): data is AppMessage =>
57
60
  typeof data === 'object' && data !== null && (data as { desk?: unknown }).desk === PROTOCOL && typeof (data as { kind?: unknown }).kind === 'string'
@@ -72,6 +75,12 @@ export interface AppFrameProps {
72
75
  readonly accepts?: Accepts
73
76
  /** Windows the app may ask to open. Default: none. */
74
77
  readonly opens?: readonly string[]
78
+ /**
79
+ * The app may add menus to the bar, which join it while this frame's window is in front. Its items
80
+ * go back to it alone when chosen, and never take a shortcut the page uses, nor one without a modifier
81
+ * (only `mod`, `ctrl`, `alt` or F1–F24 bind). Default: false.
82
+ */
83
+ readonly menus?: boolean
75
84
  /**
76
85
  * The frame's sandbox. The default lets the app run its own scripts and forms, and
77
86
  * nothing more: no same-origin access, no top-level navigation, no pop-ups.
@@ -89,6 +98,7 @@ export function AppFrame({
89
98
  theme,
90
99
  accepts,
91
100
  opens = [],
101
+ menus: addsMenus = false,
92
102
  sandbox = 'allow-scripts allow-forms',
93
103
  className,
94
104
  }: AppFrameProps) {
@@ -105,8 +115,13 @@ export function AppFrame({
105
115
  frame.current?.contentWindow?.postMessage(message, '*')
106
116
  }
107
117
 
108
- const latest = useRef({ listens, says, opens, windowId, theme })
109
- latest.current = { listens, says, opens, windowId, theme }
118
+ const latest = useRef({ listens, says, opens, windowId, theme, addsMenus })
119
+ latest.current = { listens, says, opens, windowId, theme, addsMenus }
120
+
121
+ // The app's menus, named for this frame so they never meet the page's or another frame's.
122
+ const prefix = `frame${useId()}`
123
+ const [menus, setMenus] = useState<ReturnType<typeof frameMenus>>(null)
124
+ useFrameMenus(desk, addsMenus ? windowId : null, menus, id => post({ desk: PROTOCOL, kind: 'command', target: 'menu', id }))
110
125
 
111
126
  // What the app says: only from this frame, only on topics it was granted.
112
127
  useEffect(() => {
@@ -115,11 +130,15 @@ export function AppFrame({
115
130
  const message = event.data
116
131
  const granted = latest.current
117
132
  if (message.kind === 'ready') {
133
+ // A page loading in the frame, perhaps a new one: the menus it had are gone with the old.
134
+ setMenus(null)
118
135
  post({ desk: PROTOCOL, kind: 'hello', window: granted.windowId, listens: granted.listens, says: granted.says, theme: granted.theme ?? null })
119
136
  } else if (message.kind === 'publish') {
120
137
  if (typeof message.topic === 'string' && granted.says.some(pattern => topicMatches(pattern, message.topic))) {
121
138
  bus.publish(message.topic, message.payload, { from: granted.windowId })
122
139
  }
140
+ } else if (message.kind === 'chrome') {
141
+ if (granted.addsMenus) setMenus(frameMenus(message.chrome?.menus, prefix))
123
142
  } else if (message.kind === 'open') {
124
143
  if (typeof message.window === 'string' && granted.opens.includes(message.window)) desk.open(message.window)
125
144
  }
@@ -182,11 +201,14 @@ export function AppFrame({
182
201
  * desk.onDrop(drop => …) take what is dropped on the window
183
202
  * desk.publish('ride.selected', ride) say something, if it was granted
184
203
  * desk.open('map') ask for a window, if it was granted
204
+ * desk.setMenus([{ id, label, items }]) add menus to the bar, if it was granted
205
+ * desk.onCommand(id => …) hear which of its menu items was chosen
185
206
  * desk.window which window this is, once connected
186
207
  */
187
208
  export const APP_BRIDGE_SCRIPT = `(() => {
188
209
  const handlers = new Map();
189
210
  const drops = [];
211
+ const commands = [];
190
212
  const send = message => parent.postMessage(Object.assign({ desk: ${PROTOCOL} }, message), '*');
191
213
  const applyTheme = theme => {
192
214
  if (!theme) return;
@@ -208,6 +230,8 @@ export const APP_BRIDGE_SCRIPT = `(() => {
208
230
  onDrop(handler) { drops.push(handler); },
209
231
  publish(topic, payload) { send({ kind: 'publish', topic, payload }); },
210
232
  open(window) { send({ kind: 'open', window }); },
233
+ setMenus(menus) { send({ kind: 'chrome', chrome: { menus } }); },
234
+ onCommand(handler) { commands.push(handler); },
211
235
  };
212
236
  const matches = (pattern, topic) =>
213
237
  pattern === '*' || pattern === topic || (pattern.endsWith('.*') && (topic === pattern.slice(0, -2) || topic.startsWith(pattern.slice(0, -1))));
@@ -218,6 +242,7 @@ export const APP_BRIDGE_SCRIPT = `(() => {
218
242
  if (message.kind === 'theme') applyTheme(message.theme);
219
243
  if (message.kind === 'event') for (const [pattern, list] of handlers) if (matches(pattern, message.topic)) list.forEach(h => h(message));
220
244
  if (message.kind === 'drop') drops.forEach(h => h(message));
245
+ if (message.kind === 'command' && message.target === 'menu') commands.forEach(h => h(message.id));
221
246
  });
222
247
  window.desk = desk;
223
248
  send({ kind: 'ready' });
@@ -1,6 +1,6 @@
1
1
  import { useCallback, useEffect, useRef } from 'react'
2
2
  import type { ReactNode } from 'react'
3
- import { openSettings, useDeskApp } from './app.js'
3
+ import { openAppSettings, useDeskApp } from './app.js'
4
4
  import type { AboutSection, DeskApp, SettingsSection } from './app.js'
5
5
  import { useOptionalDesk, useWindowRoute } from './context.js'
6
6
  import { Button } from './controls.js'
@@ -16,11 +16,15 @@ import { Split } from './split.js'
16
16
  * the desk, so a section added at runtime, or a fact that changes, shows without the window being rebuilt.
17
17
  */
18
18
 
19
- /** The app's name and version, then every fact that may be copied, by section: what About's Copy copies. */
19
+ /**
20
+ * The app's name and version, then every fact that may be copied, by section, each as its `copyValue` if it
21
+ * has one: what About's Copy copies. A section still pending is left out.
22
+ */
20
23
  export function aboutText(app: Pick<DeskApp, 'name' | 'version'>, sections: readonly AboutSection[]): string {
21
24
  const blocks = [app.version ? `${app.name} ${app.version}` : app.name]
22
25
  for (const section of sections) {
23
- const lines = section.facts.filter(fact => fact.copy !== false).map(({ name, value }) => `${name}: ${value}`)
26
+ if (section.pending) continue
27
+ const lines = section.facts.filter(fact => fact.copy !== false).map(({ name, value, copyValue }) => `${name}: ${copyValue ?? value}`)
24
28
  if (lines.length) blocks.push([...(section.title ? [section.title] : []), ...lines].join('\n'))
25
29
  }
26
30
  return blocks.join('\n\n')
@@ -35,7 +39,7 @@ export function useOpenSettings(): (route?: string) => void {
35
39
  const app = useDeskApp()
36
40
  const desk = useOptionalDesk()
37
41
  return useCallback((route?: string) => {
38
- if (desk) openSettings(desk, app, route)
42
+ if (desk) openAppSettings(desk, app, route)
39
43
  }, [desk, app])
40
44
  }
41
45
 
@@ -58,7 +62,7 @@ function AboutPanel({ app }: { readonly app: DeskApp }) {
58
62
  const copying = useCopy(text, textRef)
59
63
  const hasFacts = Boolean(app.facts)
60
64
  // Copy is for something to copy: not while facts are read, and not when there are none to report.
61
- const copyable = Boolean(sections?.some(section => section.facts.some(fact => fact.copy !== false)))
65
+ const copyable = Boolean(sections?.some(section => !section.pending && section.facts.some(fact => fact.copy !== false)))
62
66
  return (
63
67
  <Pane
64
68
  padded
@@ -77,17 +81,19 @@ function AboutPanel({ app }: { readonly app: DeskApp }) {
77
81
  <div className="desk-about-body">
78
82
  <div className="desk-about-brand">
79
83
  {app.icon != null && <div className="desk-about-icon" aria-hidden="true">{app.icon}</div>}
80
- <h2 className="desk-about-name">{app.name}</h2>
84
+ {app.wordmark != null && <div className="desk-about-wordmark" aria-hidden="true">{app.wordmark}</div>}
85
+ <h2 className={app.wordmark != null ? 'desk-about-name desk-about-name-hidden' : 'desk-about-name'}>{app.name}</h2>
81
86
  {app.version && <p className="desk-about-version desk-text-muted">Version {app.version}</p>}
82
87
  {app.description != null && <p className="desk-about-description">{app.description}</p>}
83
88
  </div>
84
- {app.links?.length ? (
85
- <nav className="desk-about-links" aria-label="Links">
86
- {app.links.map(link => (
89
+ {byGroup(app.links ?? [], link => link.group).map(({ group, items }) => (
90
+ <nav key={group} className="desk-about-links" aria-label={group || 'Links'}>
91
+ {group && <h3 className="desk-about-links-heading desk-text-small desk-text-muted">{group}</h3>}
92
+ {items.map(link => (
87
93
  <a key={link.url} href={link.url} target="_blank" rel="noopener noreferrer">{link.label}</a>
88
94
  ))}
89
95
  </nav>
90
- ) : null}
96
+ ))}
91
97
  {app.note != null && <p className="desk-about-note desk-text-small desk-text-muted">{app.note}</p>}
92
98
  {hasFacts && !sections && <Loading label="Reading what is running" />}
93
99
  {sections?.length ? (
@@ -104,8 +110,26 @@ function AboutPanel({ app }: { readonly app: DeskApp }) {
104
110
  )
105
111
  }
106
112
 
113
+ /** Items by their group, in the order each group first appears; those with none (or '') first, as group ''. */
114
+ function byGroup<T>(items: readonly T[], groupOf: (item: T) => string | undefined): { group: string; items: T[] }[] {
115
+ const groups: { group: string; items: T[] }[] = []
116
+ for (const item of items) {
117
+ const group = groupOf(item) ?? ''
118
+ let found = groups.find(g => g.group === group)
119
+ if (!found) {
120
+ found = { group, items: [] }
121
+ if (group) groups.push(found)
122
+ else groups.unshift(found)
123
+ }
124
+ found.items.push(item)
125
+ }
126
+ return groups
127
+ }
128
+
107
129
  function AboutFacts({ section }: { readonly section: AboutSection }) {
108
- const list = (
130
+ const list = section.pending ? (
131
+ <Loading label="Checking" size="small" />
132
+ ) : (
109
133
  <dl className="desk-about-facts">
110
134
  {section.facts.map(({ name, value, note, link }) => (
111
135
  <div key={name}>
@@ -129,20 +153,9 @@ function AboutFacts({ section }: { readonly section: AboutSection }) {
129
153
 
130
154
  /** The sidebar's groups, in the order each first appears; sections with no group come first, unheaded. */
131
155
  export function settingsGroups(sections: readonly SettingsSection[]): SidebarSection[] {
132
- const groups: { id: string; label?: string; items: SettingsSection[] }[] = []
133
- for (const section of sections) {
134
- const id = section.group ?? ''
135
- let group = groups.find(g => g.id === id)
136
- if (!group) {
137
- group = { id, ...(section.group ? { label: section.group } : {}), items: [] }
138
- if (id) groups.push(group)
139
- else groups.unshift(group)
140
- }
141
- group.items.push(section)
142
- }
143
- return groups.map(({ id, label, items }) => ({
144
- id: id || 'settings',
145
- ...(label ? { label } : {}),
156
+ return byGroup(sections, section => section.group).map(({ group, items }) => ({
157
+ id: group || 'settings',
158
+ ...(group ? { label: group } : {}),
146
159
  items: items.map(s => ({ id: s.id, label: s.title, ...(s.icon != null ? { icon: s.icon } : {}), ...(s.badge != null ? { badge: s.badge } : {}) })),
147
160
  }))
148
161
  }
@@ -1,6 +1,7 @@
1
- import { useSyncExternalStore } from 'react'
1
+ import { useLayoutEffect, useSyncExternalStore } from 'react'
2
2
  import type { ReactNode } from 'react'
3
3
  import type { LookChoice } from '../core/look.js'
4
+ import { getPreference, preferenceChanged, setPreference, subscribePreferences } from '../core/preferences.js'
4
5
  import { COLOR_SCHEME_KEY, LOOK_KEY } from '../core/scheme.js'
5
6
  import type { ColorScheme } from '../core/scheme.js'
6
7
  import type { Fx } from '../fx/fx.js'
@@ -27,6 +28,8 @@ import { SoundsToggle } from './soundsToggle.js'
27
28
  export interface AppearanceRows {
28
29
  /** Light, dark or the system's. Leave out for an app whose named themes decide it. */
29
30
  readonly scheme?: boolean
31
+ /** The scheme while the person has chosen none: `system` unless the app says. Give the head script the same. */
32
+ readonly defaultScheme?: ColorScheme
30
33
  /** The window chrome: the device's, Mac, GNOME or Windows. */
31
34
  readonly look?: boolean
32
35
  /** The app's sounds: one switch, `SoundsToggle`. */
@@ -51,57 +54,45 @@ export function shownRows(rows: AppearanceRows, fromHost: boolean): AppearanceRo
51
54
  /** Whether any row is left to show: with none, Appearance is no section at all. */
52
55
  export const hasRows = (rows: AppearanceRows): boolean => Boolean(rows.scheme || rows.look || rows.sounds || rows.theme != null)
53
56
 
54
- /* A choice kept in this browser, read by every part that shows it, here and in another tab. */
57
+ /* A choice kept in the preference store, read by every part that shows it, here and in another tab. */
55
58
  function storedChoice<T extends string>(key: string, values: readonly T[]) {
56
- const listeners = new Set<() => void>()
57
- const read = (): T | null => {
58
- try {
59
- const kept = globalThis.localStorage?.getItem(key)
60
- return values.find(v => v === kept) ?? null
61
- } catch {
62
- return null
63
- }
64
- }
65
- const write = (value: T | null) => {
66
- try {
67
- if (value === null) globalThis.localStorage?.removeItem(key)
68
- else globalThis.localStorage?.setItem(key, value)
69
- memory = null
70
- } catch {
71
- // Private windows and full storage: it applies until the page is closed. What storage still holds is
72
- // the old choice, read before this one, so it goes.
73
- memory = value
74
- try {
75
- globalThis.localStorage?.removeItem(key)
76
- } catch {
77
- // Unreadable too: then it is never read.
78
- }
79
- }
80
- listeners.forEach(listener => listener())
81
- }
82
- let memory: T | null = null
83
- const now = () => read() ?? memory
84
- const fromAnotherTab = (event: StorageEvent) => {
85
- if (event.key === null || event.key === key) listeners.forEach(listener => listener())
86
- }
87
- const subscribe = (listener: () => void) => {
88
- if (listeners.size === 0 && typeof window !== 'undefined') window.addEventListener('storage', fromAnotherTab)
89
- listeners.add(listener)
90
- return () => {
91
- listeners.delete(listener)
92
- if (listeners.size === 0 && typeof window !== 'undefined') window.removeEventListener('storage', fromAnotherTab)
93
- }
94
- }
95
- return { now, write, subscribe }
59
+ const now = (): T | null => values.find(v => v === getPreference(key)) ?? null
60
+ const write = (value: T | null) => setPreference(key, value)
61
+ const notify = () => preferenceChanged(key)
62
+ return { now, write, subscribe: subscribePreferences, notify }
96
63
  }
97
64
 
98
65
  const scheme = storedChoice<ColorScheme>(COLOR_SCHEME_KEY, ['system', 'light', 'dark'])
99
66
  const look = storedChoice<LookChoice>(LOOK_KEY, ['auto', 'mac', 'gnome', 'windows'])
100
67
 
101
- /** The colour scheme chosen in Appearance: `system` until one is. */
102
- export const colorScheme = (): ColorScheme => scheme.now() ?? 'system'
68
+ /*
69
+ * The app's default, while the person has chosen none: what a desk with the scheme row says, `system` when
70
+ * none does. Choosing the default itself forgets the choice, so a later default still applies.
71
+ */
72
+ let fallback: ColorScheme = 'system'
73
+
74
+ /** The desk with the scheme row says what applies while nothing is chosen, until it unmounts. Returns that default. */
75
+ export function useDefaultColorScheme(active: boolean, given: ColorScheme | undefined): ColorScheme {
76
+ const next = given ?? 'system'
77
+ useLayoutEffect(() => {
78
+ if (!active) return
79
+ fallback = next
80
+ scheme.notify()
81
+ return () => {
82
+ fallback = 'system'
83
+ scheme.notify()
84
+ }
85
+ }, [active, next])
86
+ return next
87
+ }
88
+
89
+ /** The scheme the person chose, or null while the app's default stands. */
90
+ export const useChosenColorScheme = (): ColorScheme | null => useSyncExternalStore(scheme.subscribe, scheme.now, () => null)
91
+
92
+ /** The colour scheme chosen in Appearance, or the app's default (`system` unless it gives one) until one is. */
93
+ export const colorScheme = (): ColorScheme => scheme.now() ?? fallback
103
94
  /** Chooses a colour scheme, remembers it, and tells everything that shows it. */
104
- export const setColorScheme = (next: ColorScheme): void => scheme.write(next === 'system' ? null : next)
95
+ export const setColorScheme = (next: ColorScheme): void => scheme.write(next === fallback ? null : next)
105
96
  /** The colour scheme, kept current: for an app that tells a native shell, or themes something of its own. */
106
97
  export const useColorScheme = (): ColorScheme => useSyncExternalStore(scheme.subscribe, colorScheme, () => 'system')
107
98