@liberation-data/desk 0.42.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 (200) hide show
  1. package/README.md +312 -5
  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 +10 -0
  7. package/dist/core/index.d.ts.map +1 -1
  8. package/dist/core/index.js +5 -0
  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 +18 -0
  19. package/dist/core/scheme.d.ts.map +1 -0
  20. package/dist/core/scheme.js +24 -0
  21. package/dist/core/scheme.js.map +1 -0
  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/core/types.d.ts +6 -0
  31. package/dist/core/types.d.ts.map +1 -1
  32. package/dist/css/apps.css +183 -0
  33. package/dist/css/overlays.css +6 -0
  34. package/dist/css/shell.css +100 -0
  35. package/dist/css/tokens.css +10 -0
  36. package/dist/css/windows.css +21 -0
  37. package/dist/desk.css +220 -0
  38. package/dist/fx/fx.d.ts.map +1 -1
  39. package/dist/fx/fx.js +7 -31
  40. package/dist/fx/fx.js.map +1 -1
  41. package/dist/host/checkChrome.d.ts +20 -0
  42. package/dist/host/checkChrome.d.ts.map +1 -0
  43. package/dist/host/checkChrome.js +219 -0
  44. package/dist/host/checkChrome.js.map +1 -0
  45. package/dist/host/checkTheme.d.ts +35 -0
  46. package/dist/host/checkTheme.d.ts.map +1 -0
  47. package/dist/host/checkTheme.js +128 -0
  48. package/dist/host/checkTheme.js.map +1 -0
  49. package/dist/host/index.d.ts +7 -0
  50. package/dist/host/index.d.ts.map +1 -0
  51. package/dist/host/index.js +8 -0
  52. package/dist/host/index.js.map +1 -0
  53. package/dist/host/router.d.ts +99 -0
  54. package/dist/host/router.d.ts.map +1 -0
  55. package/dist/host/router.js +279 -0
  56. package/dist/host/router.js.map +1 -0
  57. package/dist/react/Desktop.d.ts.map +1 -1
  58. package/dist/react/Desktop.js +13 -2
  59. package/dist/react/Desktop.js.map +1 -1
  60. package/dist/react/Dock.d.ts +26 -2
  61. package/dist/react/Dock.d.ts.map +1 -1
  62. package/dist/react/Dock.js +23 -27
  63. package/dist/react/Dock.js.map +1 -1
  64. package/dist/react/MenuBar.d.ts +1 -1
  65. package/dist/react/MenuBar.d.ts.map +1 -1
  66. package/dist/react/MenuBar.js +36 -6
  67. package/dist/react/MenuBar.js.map +1 -1
  68. package/dist/react/app.d.ts +215 -0
  69. package/dist/react/app.d.ts.map +1 -0
  70. package/dist/react/app.js +192 -0
  71. package/dist/react/app.js.map +1 -0
  72. package/dist/react/appFrame.d.ts +11 -7
  73. package/dist/react/appFrame.d.ts.map +1 -1
  74. package/dist/react/appFrame.js +24 -6
  75. package/dist/react/appFrame.js.map +1 -1
  76. package/dist/react/appPanels.d.ts +25 -0
  77. package/dist/react/appPanels.d.ts.map +1 -0
  78. package/dist/react/appPanels.js +123 -0
  79. package/dist/react/appPanels.js.map +1 -0
  80. package/dist/react/appearance.d.ts +61 -0
  81. package/dist/react/appearance.d.ts.map +1 -0
  82. package/dist/react/appearance.js +119 -0
  83. package/dist/react/appearance.js.map +1 -0
  84. package/dist/react/codeBlock.d.ts.map +1 -1
  85. package/dist/react/codeBlock.js +6 -60
  86. package/dist/react/codeBlock.js.map +1 -1
  87. package/dist/react/context.d.ts +23 -1
  88. package/dist/react/context.d.ts.map +1 -1
  89. package/dist/react/context.js +108 -5
  90. package/dist/react/context.js.map +1 -1
  91. package/dist/react/copy.d.ts +22 -0
  92. package/dist/react/copy.d.ts.map +1 -0
  93. package/dist/react/copy.js +68 -0
  94. package/dist/react/copy.js.map +1 -0
  95. package/dist/react/dockStatus.d.ts +19 -0
  96. package/dist/react/dockStatus.d.ts.map +1 -0
  97. package/dist/react/dockStatus.js +25 -0
  98. package/dist/react/dockStatus.js.map +1 -0
  99. package/dist/react/documentTitle.d.ts.map +1 -1
  100. package/dist/react/documentTitle.js +4 -1
  101. package/dist/react/documentTitle.js.map +1 -1
  102. package/dist/react/frameMenus.d.ts +28 -0
  103. package/dist/react/frameMenus.d.ts.map +1 -0
  104. package/dist/react/frameMenus.js +85 -0
  105. package/dist/react/frameMenus.js.map +1 -0
  106. package/dist/react/hostApp.d.ts +15 -0
  107. package/dist/react/hostApp.d.ts.map +1 -0
  108. package/dist/react/hostApp.js +77 -0
  109. package/dist/react/hostApp.js.map +1 -0
  110. package/dist/react/hostDock.d.ts +25 -0
  111. package/dist/react/hostDock.d.ts.map +1 -0
  112. package/dist/react/hostDock.js +98 -0
  113. package/dist/react/hostDock.js.map +1 -0
  114. package/dist/react/hostMenu.d.ts +2 -36
  115. package/dist/react/hostMenu.d.ts.map +1 -1
  116. package/dist/react/hostMenu.js.map +1 -1
  117. package/dist/react/hosted.d.ts +28 -0
  118. package/dist/react/hosted.d.ts.map +1 -0
  119. package/dist/react/hosted.js +53 -0
  120. package/dist/react/hosted.js.map +1 -0
  121. package/dist/react/index.d.ts +12 -0
  122. package/dist/react/index.d.ts.map +1 -1
  123. package/dist/react/index.js +8 -0
  124. package/dist/react/index.js.map +1 -1
  125. package/dist/react/layers.d.ts.map +1 -1
  126. package/dist/react/layers.js +7 -2
  127. package/dist/react/layers.js.map +1 -1
  128. package/dist/react/overlays.d.ts.map +1 -1
  129. package/dist/react/overlays.js +29 -12
  130. package/dist/react/overlays.js.map +1 -1
  131. package/dist/react/shell.d.ts +16 -1
  132. package/dist/react/shell.d.ts.map +1 -1
  133. package/dist/react/shell.js +19 -3
  134. package/dist/react/shell.js.map +1 -1
  135. package/dist/react/windowMenus.d.ts +19 -0
  136. package/dist/react/windowMenus.d.ts.map +1 -0
  137. package/dist/react/windowMenus.js +88 -0
  138. package/dist/react/windowMenus.js.map +1 -0
  139. package/dist/shell/HostShell.d.ts +53 -0
  140. package/dist/shell/HostShell.d.ts.map +1 -0
  141. package/dist/shell/HostShell.js +116 -0
  142. package/dist/shell/HostShell.js.map +1 -0
  143. package/dist/shell/index.d.ts +7 -0
  144. package/dist/shell/index.d.ts.map +1 -0
  145. package/dist/shell/index.js +9 -0
  146. package/dist/shell/index.js.map +1 -0
  147. package/dist/shell/keys.d.ts +47 -0
  148. package/dist/shell/keys.d.ts.map +1 -0
  149. package/dist/shell/keys.js +68 -0
  150. package/dist/shell/keys.js.map +1 -0
  151. package/dist/shell/motion.d.ts +72 -0
  152. package/dist/shell/motion.d.ts.map +1 -0
  153. package/dist/shell/motion.js +118 -0
  154. package/dist/shell/motion.js.map +1 -0
  155. package/llms.txt +84 -9
  156. package/package.json +10 -1
  157. package/src/core/hosted.ts +351 -0
  158. package/src/core/index.ts +29 -0
  159. package/src/core/location.ts +23 -3
  160. package/src/core/preferences.ts +132 -0
  161. package/src/core/scheme.ts +32 -0
  162. package/src/core/theme.ts +91 -0
  163. package/src/core/themeChange.ts +59 -0
  164. package/src/core/types.ts +6 -0
  165. package/src/css/apps.css +183 -0
  166. package/src/css/overlays.css +6 -0
  167. package/src/css/shell.css +99 -0
  168. package/src/css/tokens.css +10 -0
  169. package/src/css/windows.css +21 -0
  170. package/src/fx/fx.ts +7 -25
  171. package/src/host/checkChrome.ts +257 -0
  172. package/src/host/checkTheme.ts +134 -0
  173. package/src/host/index.ts +10 -0
  174. package/src/host/router.ts +374 -0
  175. package/src/react/Desktop.tsx +21 -3
  176. package/src/react/Dock.tsx +40 -24
  177. package/src/react/MenuBar.tsx +39 -6
  178. package/src/react/app.tsx +354 -0
  179. package/src/react/appFrame.tsx +39 -14
  180. package/src/react/appPanels.tsx +210 -0
  181. package/src/react/appearance.tsx +195 -0
  182. package/src/react/codeBlock.tsx +7 -65
  183. package/src/react/context.tsx +131 -4
  184. package/src/react/copy.ts +81 -0
  185. package/src/react/dockStatus.ts +42 -0
  186. package/src/react/documentTitle.ts +4 -1
  187. package/src/react/frameMenus.ts +100 -0
  188. package/src/react/hostApp.tsx +89 -0
  189. package/src/react/hostDock.tsx +118 -0
  190. package/src/react/hostMenu.ts +3 -38
  191. package/src/react/hosted.tsx +65 -0
  192. package/src/react/index.ts +34 -0
  193. package/src/react/layers.ts +7 -2
  194. package/src/react/overlays.tsx +30 -12
  195. package/src/react/shell.tsx +33 -3
  196. package/src/react/windowMenus.ts +107 -0
  197. package/src/shell/HostShell.tsx +220 -0
  198. package/src/shell/index.ts +11 -0
  199. package/src/shell/keys.ts +101 -0
  200. package/src/shell/motion.ts +163 -0
@@ -0,0 +1,354 @@
1
+ import { createContext, useContext } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import { isOpen } from '../core/desk.js'
4
+ import type { Desk } from '../core/desk.js'
5
+ import { bindShortcuts } from '../core/shortcuts.js'
6
+ import type { Keymap } from '../core/shortcuts.js'
7
+ import type { WindowId, WindowLimits } from '../core/types.js'
8
+ import type { AppearanceRows } from './appearance.js'
9
+ import type { DockEntry, DockItem } from './Dock.js'
10
+ import type { Menu, MenuItem } from './MenuBar.js'
11
+
12
+ /*
13
+ * WHAT AN APP SAYS ABOUT ITSELF, ONCE. Its name, icon, version and links give it an About; its settings,
14
+ * if it has any, give it Settings. Desk then puts both where every Desk app has them: the dock's system
15
+ * group ends with Settings and About, the app menu starts with "About <App>" and "Settings…", ⌘, opens
16
+ * Settings, and each opens as a panel of one size.
17
+ *
18
+ * An app with no settings shows none of it: no gear and no menu item, as a Mac app without preferences
19
+ * leaves the item out. About is always there, because every app has a name and a version.
20
+ *
21
+ * The declaration is read on every render, so what it holds can change: a group of settings that grows as
22
+ * plugins are installed, a badge on the gear. This file imports only types from the dock and the menu bar,
23
+ * which both read it.
24
+ */
25
+
26
+ /** A link on the About panel: the website, the source, where to report an issue. */
27
+ export interface AppLink {
28
+ readonly label: string
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
32
+ }
33
+
34
+ /** One line of the About panel's facts: what something is, and its value. */
35
+ export interface AboutFact {
36
+ readonly name: string
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
40
+ /** What the person might want to know of it. Shown under the value, never copied. */
41
+ readonly note?: ReactNode
42
+ /** Where to read more, shown after the note as a link: the guide to a setup that could be faster. Never copied. */
43
+ readonly link?: AppLink
44
+ /**
45
+ * `false` keeps this fact out of what Copy copies: a path with a person's name in it, an account. It is
46
+ * still shown. Facts are for a bug report, so never put a key or a password in one, copied or not.
47
+ */
48
+ readonly copy?: boolean
49
+ }
50
+
51
+ /** A titled group of facts; the first is often untitled. */
52
+ export interface AboutSection {
53
+ readonly title?: string
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
60
+ }
61
+
62
+ /** One section of Settings: a heading and the controls under it. */
63
+ export interface SettingsSection {
64
+ /** Names it in a link: `desk.open('settings', { route: id })` opens Settings on it. */
65
+ readonly id: string
66
+ readonly title: string
67
+ /** In the sidebar layout, the heading it is listed under. Sections of one group are listed together. */
68
+ readonly group?: string
69
+ /** In the sidebar layout, beside its name. */
70
+ readonly icon?: ReactNode
71
+ readonly badge?: ReactNode
72
+ readonly content: ReactNode
73
+ }
74
+
75
+ /** What an app with settings declares: Desk's Appearance, its own sections, and how Settings is laid out and opened. */
76
+ export interface AppSettings {
77
+ /**
78
+ * Desk's own Appearance section, first: `true` for light, dark or the system's, or the rows the app wants
79
+ * (`scheme`, `look`, `sounds`, and `theme` for a picker of its own). It hides what comes from a host when
80
+ * the app receives its theme from one; with nothing left it goes, and with no other section so does Settings.
81
+ */
82
+ readonly appearance?: boolean | AppearanceRows
83
+ /** The app's own sections, in order. They may change at runtime: a group that grows with plugins. */
84
+ readonly sections?: readonly SettingsSection[]
85
+ /**
86
+ * `panel` (the default): every section on one page, in a panel of one size, for a handful of settings.
87
+ * `sidebar`: a list of sections beside the one chosen, in a bigger window, for many.
88
+ */
89
+ readonly layout?: 'panel' | 'sidebar'
90
+ /** A count on the gear in the dock, as any dock item's badge: updates waiting, a plugin to finish setting up. */
91
+ readonly badge?: ReactNode
92
+ /**
93
+ * The app's own way of opening Settings, for an app whose Settings window is its own code. Desk then
94
+ * draws no window: the gear, the menu item and ⌘, call this, with the section a link asked for.
95
+ * `<SettingsContent>` draws Desk's sections inside a window of the app's, if it wants them.
96
+ */
97
+ readonly open?: (route?: string) => void
98
+ }
99
+
100
+ /** What an app declares about itself, given once to `DeskShell` or `DeskProvider` as `app`. */
101
+ export interface DeskApp {
102
+ /** As people read it: "About Garage", and the heading of the About panel. */
103
+ readonly name: string
104
+ /** The app's mark, drawn large on the About panel. */
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
111
+ readonly version?: string
112
+ /** A line under the name: what the app is for. */
113
+ readonly description?: ReactNode
114
+ readonly links?: readonly AppLink[]
115
+ /** The small print at the foot of About: the licence, the copyright. */
116
+ readonly legal?: ReactNode
117
+ /**
118
+ * A short line under the links, never copied: what this build can and cannot say, such as that the desktop
119
+ * app reports what is running and this is a browser. For something that is not a fact about what runs.
120
+ */
121
+ readonly note?: ReactNode
122
+ /**
123
+ * What is running, for a bug report: a hook, called inside the About panel, so it may read state and
124
+ * change. Return `null` while it is still being read, and the panel says so; `[]` when there is nothing to
125
+ * report, and there is no Copy. With facts, About has a Copy button that copies the name, the version and
126
+ * every fact not marked `copy: false`, as text.
127
+ */
128
+ readonly facts?: () => readonly AboutSection[] | null
129
+ /** Leave out for an app with no settings: then there is no gear, no menu item and no ⌘,. */
130
+ readonly settings?: AppSettings
131
+ /**
132
+ * This app receives its theme from a host, which chose it: Appearance offers no scheme, look or theme of
133
+ * its own. Left out, Desk finds it from the bridge an `AppFrame` puts in the page, once a theme arrives.
134
+ * The app that is the theme's source keeps them.
135
+ */
136
+ readonly themeFromHost?: boolean
137
+ }
138
+
139
+ /** The window Desk opens for Settings. An app that declares settings without an `open` of its own gives this id to Desk. */
140
+ export const SETTINGS_WINDOW = 'settings'
141
+ /** The window Desk opens for About. An app that declares itself gives this id to Desk. */
142
+ export const ABOUT_WINDOW = 'about'
143
+
144
+ /** The command "Settings…" and ⌘, perform. It answers at the top of the chain while the app has settings. */
145
+ export const SettingsCommand = 'desk.settings'
146
+
147
+ /*
148
+ * ⌘, is bound on the command itself, so it opens Settings in an app with no MenuBar. A menu item for one of
149
+ * these shows the shortcut and leaves the binding to this, so one press is one command. A host's native menu
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.
151
+ */
152
+ export const DESK_SHORTCUTS: Keymap = { 'mod+comma': SettingsCommand }
153
+ const hostOwns = new WeakMap<Desk, (shortcut: string) => boolean>()
154
+
155
+ /** Whether a menu item's shortcut is one Desk binds on the command already, so the menu only shows it. */
156
+ export const boundByDesk = (shortcut: string, command: string): boolean => DESK_SHORTCUTS[shortcut] === command
157
+
158
+ /** Binds Desk's own shortcuts on this desk, less those a host's menu bar owns. Returns a function that unbinds them. */
159
+ export const bindDeskShortcuts = (desk: Desk): (() => void) =>
160
+ bindShortcuts(desk, () => Object.fromEntries(Object.entries(DESK_SHORTCUTS).filter(([shortcut]) => !hostOwns.get(desk)?.(shortcut))))
161
+
162
+ /** While a host's menu bar is up, it gives these shortcuts their keys, so Desk leaves them alone. Returns a function that hands them back. */
163
+ export function hostOwnsShortcuts(desk: Desk, owns: (shortcut: string) => boolean): () => void {
164
+ hostOwns.set(desk, owns)
165
+ return () => {
166
+ if (hostOwns.get(desk) === owns) hostOwns.delete(desk)
167
+ }
168
+ }
169
+
170
+ /** Panels have one size: the desk centres them, filled or not. About is one, and Settings on one page. */
171
+ export const PANEL_LIMITS: WindowLimits = { minWidth: 360, minHeight: 200, maxWidth: 560, maxHeight: 560, fitContent: true }
172
+ /** The sidebar layout of Settings is bigger, for many sections and the list of them. */
173
+ export const SIDEBAR_LIMITS: WindowLimits = { minWidth: 600, minHeight: 400, maxWidth: 880, maxHeight: 640 }
174
+
175
+ const AppContext = createContext<DeskApp | null>(null)
176
+ /** Gives the declaration, with Appearance in place, to everything inside `DeskProvider`. */
177
+ export const AppProvider = AppContext.Provider
178
+
179
+ /** What the app declared about itself, or null when it declared nothing. */
180
+ export const useDeskApp = (): DeskApp | null => useContext(AppContext)
181
+
182
+ /** Whether the app has any Settings to show: its own window, or at least one section. */
183
+ export const hasSettings = (app: DeskApp | null): boolean =>
184
+ Boolean(app?.settings && (app.settings.open || (app.settings.sections?.length ?? 0) > 0))
185
+
186
+ /**
187
+ * Which of Desk's own windows this is, for an app that declared itself: About always, and Settings when the
188
+ * app declared settings and does not open a window of its own for them. Null for every other window, which
189
+ * stays the app's, as `settings` does for an app that declared only its About.
190
+ */
191
+ export function desksWindow(app: DeskApp | null, id: WindowId): 'about' | 'settings' | null {
192
+ if (!app) return null
193
+ if (id === ABOUT_WINDOW) return 'about'
194
+ if (id === SETTINGS_WINDOW && app.settings && !app.settings.open) return 'settings'
195
+ return null
196
+ }
197
+
198
+ /**
199
+ * The title Desk gives its own windows, or null for every other window. `Desktop` and `useDocumentTitle` use
200
+ * it already; a Window menu's title function can too: `id => appWindowTitle(APP, id) ?? TITLES[id]`.
201
+ */
202
+ export function appWindowTitle(app: DeskApp | null, id: WindowId): string | null {
203
+ const which = desksWindow(app, id)
204
+ return which === 'about' ? 'About' : which === 'settings' ? 'Settings' : null
205
+ }
206
+
207
+ /** How big Desk's own window may be: a panel, or the bigger sidebar layout of Settings. */
208
+ export const appWindowLimits = (app: DeskApp | null, which: 'about' | 'settings'): WindowLimits =>
209
+ which === 'settings' && app?.settings?.layout === 'sidebar' ? SIDEBAR_LIMITS : PANEL_LIMITS
210
+
211
+ /**
212
+ * Opens one of Desk's panels: filled, so it sits centred at its one size. A panel already open is brought
213
+ * forward where it is, and moved to `route` if one is given.
214
+ */
215
+ export function openPanel(desk: Desk, id: WindowId, route?: string): void {
216
+ const at = route === undefined ? {} : { route }
217
+ if (isOpen(desk.getState(), id)) desk.open(id, at)
218
+ else desk.open(id, { mode: 'filled', ...at })
219
+ }
220
+
221
+ /**
222
+ * Opens About as the dock and the menu do: a centred panel, or brought forward where it is. For a link or a
223
+ * query string that names it, so the app need not know how a panel is opened.
224
+ */
225
+ export const openAbout = (desk: Desk): void => openPanel(desk, ABOUT_WINDOW)
226
+
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 {
229
+ if (!hasSettings(app)) return
230
+ if (app?.settings?.open) app.settings.open(route)
231
+ else openPanel(desk, SETTINGS_WINDOW, route)
232
+ }
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
+
252
+ const glyph = (path: string) => (
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">
254
+ <path d={path} />
255
+ </svg>
256
+ )
257
+
258
+ const GEAR = glyph(
259
+ 'M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z',
260
+ )
261
+ const INFO = glyph('M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18zM12 11v5M12 8h.01')
262
+
263
+ /**
264
+ * Where the gear goes, when the app wants it somewhere other than just before About: put this among its own
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.
272
+ */
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
280
+
281
+ /** The separator the system group starts after. An app that has none gets one before Settings and About. */
282
+ export const SYSTEM_GROUP = 'system'
283
+
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
+ }
292
+
293
+ /**
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.
298
+ */
299
+ export function placeAppDockItems(entries: readonly DockEntry[], app: DeskApp | null, open: { settings: () => void; about: () => void }): Placed[] {
300
+ const settings: DockItem | null = hasSettings(app)
301
+ ? { id: SETTINGS_WINDOW, label: 'Settings', icon: GEAR, onSelect: open.settings, ...(app?.settings?.badge != null ? { badge: app.settings.badge } : {}) }
302
+ : null
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
326
+ const start = placed.findIndex(e => e.type === 'separator' && e.id === SYSTEM_GROUP)
327
+ if (start < 0) return [...placed, ...(placed.length ? [{ type: 'separator' as const, id: SYSTEM_GROUP }] : []), ...ending]
328
+ const next = placed.findIndex((e, i) => i > start && e.type === 'separator')
329
+ const end = next < 0 ? placed.length : next
330
+ return [...placed.slice(0, end), ...ending, ...placed.slice(end)]
331
+ }
332
+
333
+ const SEPARATOR: MenuItem = { type: 'separator' }
334
+
335
+ /**
336
+ * The app's menus with About and Settings at the top of the app menu, as a Mac app has them: "About <App>",
337
+ * a separator, "Settings…" (⌘,), a separator, then the app's own items. The app menu is the first one with
338
+ * `emphasis`; an app with none gets one, named for the app, in front. Items given as a function stay one, so
339
+ * a menu that is re-read on opening still is.
340
+ */
341
+ export function placeAppMenuItems(menus: readonly Menu[], app: DeskApp | null, openAbout: () => void): Menu[] {
342
+ if (!app) return [...menus]
343
+ const head: MenuItem[] = [{ type: 'action', label: `About ${app.name}`, onSelect: openAbout }]
344
+ if (hasSettings(app)) head.push(SEPARATOR, { type: 'command', label: 'Settings…', command: SettingsCommand, shortcut: 'mod+comma' })
345
+ const withHead = (items: readonly MenuItem[]): MenuItem[] =>
346
+ items.length === 0 ? head : [...head, ...(items[0]?.type === 'separator' ? [] : [SEPARATOR]), ...items]
347
+ const at = menus.findIndex(m => m.emphasis)
348
+ if (at < 0) return [{ id: 'app', label: app.name, emphasis: true, items: head }, ...menus]
349
+ return menus.map((menu, i) => {
350
+ if (i !== at) return menu
351
+ const { items } = menu
352
+ return { ...menu, items: typeof items === 'function' ? () => withHead(items()) : withHead(items) }
353
+ })
354
+ }
@@ -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' });