@liberation-data/desk 0.42.0 → 0.43.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 (88) hide show
  1. package/README.md +95 -5
  2. package/dist/core/index.d.ts +2 -0
  3. package/dist/core/index.d.ts.map +1 -1
  4. package/dist/core/index.js +1 -0
  5. package/dist/core/index.js.map +1 -1
  6. package/dist/core/scheme.d.ts +12 -0
  7. package/dist/core/scheme.d.ts.map +1 -0
  8. package/dist/core/scheme.js +15 -0
  9. package/dist/core/scheme.js.map +1 -0
  10. package/dist/core/types.d.ts +6 -0
  11. package/dist/core/types.d.ts.map +1 -1
  12. package/dist/css/apps.css +149 -0
  13. package/dist/css/overlays.css +6 -0
  14. package/dist/css/windows.css +21 -0
  15. package/dist/desk.css +176 -0
  16. package/dist/react/Desktop.d.ts.map +1 -1
  17. package/dist/react/Desktop.js +13 -2
  18. package/dist/react/Desktop.js.map +1 -1
  19. package/dist/react/Dock.d.ts +6 -1
  20. package/dist/react/Dock.d.ts.map +1 -1
  21. package/dist/react/Dock.js +9 -1
  22. package/dist/react/Dock.js.map +1 -1
  23. package/dist/react/MenuBar.d.ts +1 -1
  24. package/dist/react/MenuBar.d.ts.map +1 -1
  25. package/dist/react/MenuBar.js +19 -5
  26. package/dist/react/MenuBar.js.map +1 -1
  27. package/dist/react/app.d.ts +174 -0
  28. package/dist/react/app.d.ts.map +1 -0
  29. package/dist/react/app.js +146 -0
  30. package/dist/react/app.js.map +1 -0
  31. package/dist/react/appPanels.d.ts +22 -0
  32. package/dist/react/appPanels.d.ts.map +1 -0
  33. package/dist/react/appPanels.js +114 -0
  34. package/dist/react/appPanels.js.map +1 -0
  35. package/dist/react/appearance.d.ts +55 -0
  36. package/dist/react/appearance.d.ts.map +1 -0
  37. package/dist/react/appearance.js +140 -0
  38. package/dist/react/appearance.js.map +1 -0
  39. package/dist/react/codeBlock.d.ts.map +1 -1
  40. package/dist/react/codeBlock.js +6 -60
  41. package/dist/react/codeBlock.js.map +1 -1
  42. package/dist/react/context.d.ts +9 -1
  43. package/dist/react/context.d.ts.map +1 -1
  44. package/dist/react/context.js +47 -3
  45. package/dist/react/context.js.map +1 -1
  46. package/dist/react/copy.d.ts +22 -0
  47. package/dist/react/copy.d.ts.map +1 -0
  48. package/dist/react/copy.js +68 -0
  49. package/dist/react/copy.js.map +1 -0
  50. package/dist/react/documentTitle.d.ts.map +1 -1
  51. package/dist/react/documentTitle.js +4 -1
  52. package/dist/react/documentTitle.js.map +1 -1
  53. package/dist/react/index.d.ts +7 -0
  54. package/dist/react/index.d.ts.map +1 -1
  55. package/dist/react/index.js +5 -0
  56. package/dist/react/index.js.map +1 -1
  57. package/dist/react/layers.d.ts.map +1 -1
  58. package/dist/react/layers.js +7 -2
  59. package/dist/react/layers.js.map +1 -1
  60. package/dist/react/overlays.d.ts.map +1 -1
  61. package/dist/react/overlays.js +29 -12
  62. package/dist/react/overlays.js.map +1 -1
  63. package/dist/react/shell.d.ts +4 -1
  64. package/dist/react/shell.d.ts.map +1 -1
  65. package/dist/react/shell.js +2 -2
  66. package/dist/react/shell.js.map +1 -1
  67. package/llms.txt +46 -5
  68. package/package.json +1 -1
  69. package/src/core/index.ts +2 -0
  70. package/src/core/scheme.ts +21 -0
  71. package/src/core/types.ts +6 -0
  72. package/src/css/apps.css +149 -0
  73. package/src/css/overlays.css +6 -0
  74. package/src/css/windows.css +21 -0
  75. package/src/react/Desktop.tsx +21 -3
  76. package/src/react/Dock.tsx +11 -1
  77. package/src/react/MenuBar.tsx +20 -5
  78. package/src/react/app.tsx +284 -0
  79. package/src/react/appPanels.tsx +197 -0
  80. package/src/react/appearance.tsx +204 -0
  81. package/src/react/codeBlock.tsx +7 -65
  82. package/src/react/context.tsx +55 -3
  83. package/src/react/copy.ts +81 -0
  84. package/src/react/documentTitle.ts +4 -1
  85. package/src/react/index.ts +27 -0
  86. package/src/react/layers.ts +7 -2
  87. package/src/react/overlays.tsx +30 -12
  88. package/src/react/shell.tsx +5 -2
@@ -7,6 +7,7 @@ import { bindShortcuts, formatShortcut, isApplePlatform } from '../core/shortcut
7
7
  import type { Keymap } from '../core/shortcuts.js'
8
8
  import type { Desk } from '../core/desk.js'
9
9
  import type { DeskState, WindowId } from '../core/types.js'
10
+ import { ABOUT_WINDOW, boundByDesk, hostOwnsShortcuts, openPanel, placeAppMenuItems, useDeskApp } from './app.js'
10
11
  import { useDesk, useDeskState } from './context.js'
11
12
  import { hasModKey, hostMenuModel, sameHostModel } from './hostMenu.js'
12
13
  import { isWidgetInFront, setWidgetInFront, useWidgetsInFront } from './widgets.js'
@@ -177,15 +178,27 @@ export const typeAhead = (items: readonly Resolved[], from: number, letter: stri
177
178
  return -1
178
179
  }
179
180
 
180
- /** The shortcut of every command item in these menus, as `bindShortcuts` takes them; `keep` leaves some out. */
181
+ /**
182
+ * The shortcut of every command item in these menus, as `bindShortcuts` takes them; `keep` leaves some out.
183
+ * So do those Desk binds on the command itself (⌘, for Settings): the item shows it, and binding it here too would be twice.
184
+ */
181
185
  const menuKeymap = (menus: readonly Menu[], keep: (shortcut: string) => boolean = () => true): Keymap =>
182
186
  Object.fromEntries(
183
187
  menus
184
188
  .flatMap(m => (typeof m.items === 'function' ? m.items() : m.items))
185
- .flatMap(i => (i.type === 'command' && i.shortcut && keep(i.shortcut) ? [[i.shortcut, i.command] as const] : [])),
189
+ .flatMap(i => (i.type === 'command' && i.shortcut && !boundByDesk(i.shortcut, i.command) && keep(i.shortcut) ? [[i.shortcut, i.command] as const] : [])),
186
190
  )
187
191
 
188
- export function MenuBar(props: MenuBarProps) {
192
+ /** Whether a host gives this shortcut a key of its own: those it says, or else every one with `mod` in it. */
193
+ const ownedBy = (host: MenuHost) => (shortcut: string) => (host.ownsShortcut ? host.ownsShortcut(shortcut) : hasModKey(shortcut))
194
+
195
+ export function MenuBar(given: MenuBarProps) {
196
+ const desk = useDesk()
197
+ const app = useDeskApp()
198
+ // 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
189
202
  return props.host ? <HostedMenuBar {...props} host={props.host} /> : <DrawnMenuBar {...props} />
190
203
  }
191
204
 
@@ -260,10 +273,12 @@ function HostedMenuBar({ menus, status = [], host, shortcuts = true }: MenuBarPr
260
273
  }
261
274
  }, [desk, host, publish, refresh])
262
275
 
263
- // The host gives its own shortcuts a key; the page binds the rest, read at each key press.
276
+ // The host gives its own shortcuts a key; the page binds the rest, read at each key press. Desk's own
277
+ // shortcuts (⌘,) step aside for the host's too, while it is up.
278
+ useEffect(() => hostOwnsShortcuts(desk, ownedBy(host)), [desk, host])
264
279
  useEffect(() => {
265
280
  if (!shortcuts) return undefined
266
- const owns = (shortcut: string) => (host.ownsShortcut ? host.ownsShortcut(shortcut) : hasModKey(shortcut))
281
+ const owns = ownedBy(host)
267
282
  return bindShortcuts(desk, () => menuKeymap([...latest.current.menus, ...latest.current.status], shortcut => !owns(shortcut)), { apple })
268
283
  }, [desk, host, shortcuts, apple])
269
284
 
@@ -0,0 +1,284 @@
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
+ }
31
+
32
+ /** One line of the About panel's facts: what something is, and its value. */
33
+ export interface AboutFact {
34
+ readonly name: string
35
+ readonly value: string
36
+ /** What the person might want to know of it. Shown under the value, never copied. */
37
+ readonly note?: ReactNode
38
+ /** Where to read more, shown after the note as a link: the guide to a setup that could be faster. Never copied. */
39
+ readonly link?: AppLink
40
+ /**
41
+ * `false` keeps this fact out of what Copy copies: a path with a person's name in it, an account. It is
42
+ * still shown. Facts are for a bug report, so never put a key or a password in one, copied or not.
43
+ */
44
+ readonly copy?: boolean
45
+ }
46
+
47
+ /** A titled group of facts; the first is often untitled. */
48
+ export interface AboutSection {
49
+ readonly title?: string
50
+ readonly facts: readonly AboutFact[]
51
+ }
52
+
53
+ /** One section of Settings: a heading and the controls under it. */
54
+ export interface SettingsSection {
55
+ /** Names it in a link: `desk.open('settings', { route: id })` opens Settings on it. */
56
+ readonly id: string
57
+ readonly title: string
58
+ /** In the sidebar layout, the heading it is listed under. Sections of one group are listed together. */
59
+ readonly group?: string
60
+ /** In the sidebar layout, beside its name. */
61
+ readonly icon?: ReactNode
62
+ readonly badge?: ReactNode
63
+ readonly content: ReactNode
64
+ }
65
+
66
+ /** What an app with settings declares: Desk's Appearance, its own sections, and how Settings is laid out and opened. */
67
+ export interface AppSettings {
68
+ /**
69
+ * Desk's own Appearance section, first: `true` for light, dark or the system's, or the rows the app wants
70
+ * (`scheme`, `look`, `sounds`, and `theme` for a picker of its own). It hides what comes from a host when
71
+ * the app receives its theme from one; with nothing left it goes, and with no other section so does Settings.
72
+ */
73
+ readonly appearance?: boolean | AppearanceRows
74
+ /** The app's own sections, in order. They may change at runtime: a group that grows with plugins. */
75
+ readonly sections?: readonly SettingsSection[]
76
+ /**
77
+ * `panel` (the default): every section on one page, in a panel of one size, for a handful of settings.
78
+ * `sidebar`: a list of sections beside the one chosen, in a bigger window, for many.
79
+ */
80
+ readonly layout?: 'panel' | 'sidebar'
81
+ /** A count on the gear in the dock, as any dock item's badge: updates waiting, a plugin to finish setting up. */
82
+ readonly badge?: ReactNode
83
+ /**
84
+ * The app's own way of opening Settings, for an app whose Settings window is its own code. Desk then
85
+ * draws no window: the gear, the menu item and ⌘, call this, with the section a link asked for.
86
+ * `<SettingsContent>` draws Desk's sections inside a window of the app's, if it wants them.
87
+ */
88
+ readonly open?: (route?: string) => void
89
+ }
90
+
91
+ /** What an app declares about itself, given once to `DeskShell` or `DeskProvider` as `app`. */
92
+ export interface DeskApp {
93
+ /** As people read it: "About Garage", and the heading of the About panel. */
94
+ readonly name: string
95
+ /** The app's mark, drawn large on the About panel. */
96
+ readonly icon?: ReactNode
97
+ readonly version?: string
98
+ /** A line under the name: what the app is for. */
99
+ readonly description?: ReactNode
100
+ readonly links?: readonly AppLink[]
101
+ /** The small print at the foot of About: the licence, the copyright. */
102
+ readonly legal?: ReactNode
103
+ /**
104
+ * A short line under the links, never copied: what this build can and cannot say, such as that the desktop
105
+ * app reports what is running and this is a browser. For something that is not a fact about what runs.
106
+ */
107
+ readonly note?: ReactNode
108
+ /**
109
+ * What is running, for a bug report: a hook, called inside the About panel, so it may read state and
110
+ * change. Return `null` while it is still being read, and the panel says so; `[]` when there is nothing to
111
+ * report, and there is no Copy. With facts, About has a Copy button that copies the name, the version and
112
+ * every fact not marked `copy: false`, as text.
113
+ */
114
+ readonly facts?: () => readonly AboutSection[] | null
115
+ /** Leave out for an app with no settings: then there is no gear, no menu item and no ⌘,. */
116
+ readonly settings?: AppSettings
117
+ /**
118
+ * This app receives its theme from a host, which chose it: Appearance offers no scheme, look or theme of
119
+ * its own. Left out, Desk finds it from the bridge an `AppFrame` puts in the page, once a theme arrives.
120
+ * The app that is the theme's source keeps them.
121
+ */
122
+ readonly themeFromHost?: boolean
123
+ }
124
+
125
+ /** The window Desk opens for Settings. An app that declares settings without an `open` of its own gives this id to Desk. */
126
+ export const SETTINGS_WINDOW = 'settings'
127
+ /** The window Desk opens for About. An app that declares itself gives this id to Desk. */
128
+ export const ABOUT_WINDOW = 'about'
129
+
130
+ /** The command "Settings…" and ⌘, perform. It answers at the top of the chain while the app has settings. */
131
+ export const SettingsCommand = 'desk.settings'
132
+
133
+ /*
134
+ * ⌘, is bound on the command itself, so it opens Settings in an app with no MenuBar. A menu item for one of
135
+ * these shows the shortcut and leaves the binding to this, so one press is one command. A host's native menu
136
+ * 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
+ */
138
+ const DESK_SHORTCUTS: Keymap = { 'mod+comma': SettingsCommand }
139
+ const hostOwns = new WeakMap<Desk, (shortcut: string) => boolean>()
140
+
141
+ /** Whether a menu item's shortcut is one Desk binds on the command already, so the menu only shows it. */
142
+ export const boundByDesk = (shortcut: string, command: string): boolean => DESK_SHORTCUTS[shortcut] === command
143
+
144
+ /** Binds Desk's own shortcuts on this desk, less those a host's menu bar owns. Returns a function that unbinds them. */
145
+ export const bindDeskShortcuts = (desk: Desk): (() => void) =>
146
+ bindShortcuts(desk, () => Object.fromEntries(Object.entries(DESK_SHORTCUTS).filter(([shortcut]) => !hostOwns.get(desk)?.(shortcut))))
147
+
148
+ /** 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. */
149
+ export function hostOwnsShortcuts(desk: Desk, owns: (shortcut: string) => boolean): () => void {
150
+ hostOwns.set(desk, owns)
151
+ return () => {
152
+ if (hostOwns.get(desk) === owns) hostOwns.delete(desk)
153
+ }
154
+ }
155
+
156
+ /** Panels have one size: the desk centres them, filled or not. About is one, and Settings on one page. */
157
+ export const PANEL_LIMITS: WindowLimits = { minWidth: 360, minHeight: 200, maxWidth: 560, maxHeight: 560, fitContent: true }
158
+ /** The sidebar layout of Settings is bigger, for many sections and the list of them. */
159
+ export const SIDEBAR_LIMITS: WindowLimits = { minWidth: 600, minHeight: 400, maxWidth: 880, maxHeight: 640 }
160
+
161
+ const AppContext = createContext<DeskApp | null>(null)
162
+ /** Gives the declaration, with Appearance in place, to everything inside `DeskProvider`. */
163
+ export const AppProvider = AppContext.Provider
164
+
165
+ /** What the app declared about itself, or null when it declared nothing. */
166
+ export const useDeskApp = (): DeskApp | null => useContext(AppContext)
167
+
168
+ /** Whether the app has any Settings to show: its own window, or at least one section. */
169
+ export const hasSettings = (app: DeskApp | null): boolean =>
170
+ Boolean(app?.settings && (app.settings.open || (app.settings.sections?.length ?? 0) > 0))
171
+
172
+ /**
173
+ * Which of Desk's own windows this is, for an app that declared itself: About always, and Settings when the
174
+ * app declared settings and does not open a window of its own for them. Null for every other window, which
175
+ * stays the app's, as `settings` does for an app that declared only its About.
176
+ */
177
+ export function desksWindow(app: DeskApp | null, id: WindowId): 'about' | 'settings' | null {
178
+ if (!app) return null
179
+ if (id === ABOUT_WINDOW) return 'about'
180
+ if (id === SETTINGS_WINDOW && app.settings && !app.settings.open) return 'settings'
181
+ return null
182
+ }
183
+
184
+ /**
185
+ * The title Desk gives its own windows, or null for every other window. `Desktop` and `useDocumentTitle` use
186
+ * it already; a Window menu's title function can too: `id => appWindowTitle(APP, id) ?? TITLES[id]`.
187
+ */
188
+ export function appWindowTitle(app: DeskApp | null, id: WindowId): string | null {
189
+ const which = desksWindow(app, id)
190
+ return which === 'about' ? 'About' : which === 'settings' ? 'Settings' : null
191
+ }
192
+
193
+ /** How big Desk's own window may be: a panel, or the bigger sidebar layout of Settings. */
194
+ export const appWindowLimits = (app: DeskApp | null, which: 'about' | 'settings'): WindowLimits =>
195
+ which === 'settings' && app?.settings?.layout === 'sidebar' ? SIDEBAR_LIMITS : PANEL_LIMITS
196
+
197
+ /**
198
+ * Opens one of Desk's panels: filled, so it sits centred at its one size. A panel already open is brought
199
+ * forward where it is, and moved to `route` if one is given.
200
+ */
201
+ export function openPanel(desk: Desk, id: WindowId, route?: string): void {
202
+ const at = route === undefined ? {} : { route }
203
+ if (isOpen(desk.getState(), id)) desk.open(id, at)
204
+ else desk.open(id, { mode: 'filled', ...at })
205
+ }
206
+
207
+ /**
208
+ * Opens About as the dock and the menu do: a centred panel, or brought forward where it is. For a link or a
209
+ * query string that names it, so the app need not know how a panel is opened.
210
+ */
211
+ export const openAbout = (desk: Desk): void => openPanel(desk, ABOUT_WINDOW)
212
+
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 {
215
+ if (!hasSettings(app)) return
216
+ if (app?.settings?.open) app.settings.open(route)
217
+ else openPanel(desk, SETTINGS_WINDOW, route)
218
+ }
219
+
220
+ const glyph = (path: string) => (
221
+ <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
+ <path d={path} />
223
+ </svg>
224
+ )
225
+
226
+ const GEAR = glyph(
227
+ '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',
228
+ )
229
+ const INFO = glyph('M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18zM12 11v5M12 8h.01')
230
+
231
+ /**
232
+ * 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.
234
+ */
235
+ export const dockSettings = (): DockEntry => ({ type: 'settings', id: SETTINGS_WINDOW })
236
+
237
+ /** The separator the system group starts after. An app that has none gets one before Settings and About. */
238
+ export const SYSTEM_GROUP = 'system'
239
+
240
+ type Placed = Exclude<DockEntry, { readonly type: 'settings' }>
241
+
242
+ /**
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.
246
+ */
247
+ export function placeAppDockItems(entries: readonly DockEntry[], app: DeskApp | null, open: { settings: () => void; about: () => void }): Placed[] {
248
+ const settings: DockItem | null = hasSettings(app)
249
+ ? { id: SETTINGS_WINDOW, label: 'Settings', icon: GEAR, onSelect: open.settings, ...(app?.settings?.badge != null ? { badge: app.settings.badge } : {}) }
250
+ : 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]
256
+ const start = placed.findIndex(e => e.type === 'separator' && e.id === SYSTEM_GROUP)
257
+ if (start < 0) return [...placed, ...(placed.length ? [{ type: 'separator' as const, id: SYSTEM_GROUP }] : []), ...ending]
258
+ const next = placed.findIndex((e, i) => i > start && e.type === 'separator')
259
+ const end = next < 0 ? placed.length : next
260
+ return [...placed.slice(0, end), ...ending, ...placed.slice(end)]
261
+ }
262
+
263
+ const SEPARATOR: MenuItem = { type: 'separator' }
264
+
265
+ /**
266
+ * The app's menus with About and Settings at the top of the app menu, as a Mac app has them: "About <App>",
267
+ * a separator, "Settings…" (⌘,), a separator, then the app's own items. The app menu is the first one with
268
+ * `emphasis`; an app with none gets one, named for the app, in front. Items given as a function stay one, so
269
+ * a menu that is re-read on opening still is.
270
+ */
271
+ export function placeAppMenuItems(menus: readonly Menu[], app: DeskApp | null, openAbout: () => void): Menu[] {
272
+ if (!app) return [...menus]
273
+ const head: MenuItem[] = [{ type: 'action', label: `About ${app.name}`, onSelect: openAbout }]
274
+ if (hasSettings(app)) head.push(SEPARATOR, { type: 'command', label: 'Settings…', command: SettingsCommand, shortcut: 'mod+comma' })
275
+ const withHead = (items: readonly MenuItem[]): MenuItem[] =>
276
+ items.length === 0 ? head : [...head, ...(items[0]?.type === 'separator' ? [] : [SEPARATOR]), ...items]
277
+ const at = menus.findIndex(m => m.emphasis)
278
+ if (at < 0) return [{ id: 'app', label: app.name, emphasis: true, items: head }, ...menus]
279
+ return menus.map((menu, i) => {
280
+ if (i !== at) return menu
281
+ const { items } = menu
282
+ return { ...menu, items: typeof items === 'function' ? () => withHead(items()) : withHead(items) }
283
+ })
284
+ }
@@ -0,0 +1,197 @@
1
+ import { useCallback, useEffect, useRef } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import { openSettings, useDeskApp } from './app.js'
4
+ import type { AboutSection, DeskApp, SettingsSection } from './app.js'
5
+ import { useOptionalDesk, useWindowRoute } from './context.js'
6
+ import { Button } from './controls.js'
7
+ import { useCopy } from './copy.js'
8
+ import { Pane, Section } from './pane.js'
9
+ import { Loading } from './progress.js'
10
+ import { Sidebar } from './sidebar.js'
11
+ import type { SidebarSection } from './sidebar.js'
12
+ import { Split } from './split.js'
13
+
14
+ /*
15
+ * The windows behind About and Settings, drawn from what the app declared. Both read the declaration from
16
+ * the desk, so a section added at runtime, or a fact that changes, shows without the window being rebuilt.
17
+ */
18
+
19
+ /** The app's name and version, then every fact that may be copied, by section: what About's Copy copies. */
20
+ export function aboutText(app: Pick<DeskApp, 'name' | 'version'>, sections: readonly AboutSection[]): string {
21
+ const blocks = [app.version ? `${app.name} ${app.version}` : app.name]
22
+ for (const section of sections) {
23
+ const lines = section.facts.filter(fact => fact.copy !== false).map(({ name, value }) => `${name}: ${value}`)
24
+ if (lines.length) blocks.push([...(section.title ? [section.title] : []), ...lines].join('\n'))
25
+ }
26
+ return blocks.join('\n\n')
27
+ }
28
+
29
+ /**
30
+ * Opens Settings as the gear, the menu item and ⌘, do, on the section `route` names: Desk's panel, or the
31
+ * app's own window when it has one. Nothing, when the app has no Settings to show. Read inside the desk,
32
+ * because whether there are settings depends on what the desk knows: Appearance hides when a host themes it.
33
+ */
34
+ export function useOpenSettings(): (route?: string) => void {
35
+ const app = useDeskApp()
36
+ const desk = useOptionalDesk()
37
+ return useCallback((route?: string) => {
38
+ if (desk) openSettings(desk, app, route)
39
+ }, [desk, app])
40
+ }
41
+
42
+ const NO_FACTS = () => undefined
43
+
44
+ /** About the app: its icon, name, version and links, what is running if it says, and Copy for a bug report. */
45
+ export function AboutContent() {
46
+ const app = useDeskApp()
47
+ // A declaration that gains or loses its facts calls one hook more or fewer: drawn afresh, not misordered.
48
+ return app ? <AboutPanel key={app.facts ? 'facts' : 'plain'} app={app} /> : null
49
+ }
50
+
51
+ function AboutPanel({ app }: { readonly app: DeskApp }) {
52
+ // The app's hook, called on every render of this panel and only here, so its own hooks keep their order.
53
+ const sections = (app.facts ?? NO_FACTS)()
54
+ // Where there is no clipboard, the text is selected: this text, held out of sight, not the panel, which
55
+ // shows facts marked not to be copied.
56
+ const text = sections ? aboutText(app, sections) : ''
57
+ const textRef = useRef<HTMLPreElement>(null)
58
+ const copying = useCopy(text, textRef)
59
+ const hasFacts = Boolean(app.facts)
60
+ // 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)))
62
+ return (
63
+ <Pane
64
+ padded
65
+ label={`About ${app.name}`}
66
+ className="desk-about"
67
+ footer={
68
+ copyable ? (
69
+ <div className="desk-about-foot">
70
+ <pre ref={textRef} className="desk-about-copied" aria-hidden="true">{text}</pre>
71
+ <Button onClick={() => void copying.copy()}>{copying.button}</Button>
72
+ <span className="desk-text-small desk-text-muted" role="status">{copying.copying === 'copied' ? 'Copied, for a bug report' : copying.status}</span>
73
+ </div>
74
+ ) : null
75
+ }
76
+ >
77
+ <div className="desk-about-body">
78
+ <div className="desk-about-brand">
79
+ {app.icon != null && <div className="desk-about-icon" aria-hidden="true">{app.icon}</div>}
80
+ <h2 className="desk-about-name">{app.name}</h2>
81
+ {app.version && <p className="desk-about-version desk-text-muted">Version {app.version}</p>}
82
+ {app.description != null && <p className="desk-about-description">{app.description}</p>}
83
+ </div>
84
+ {app.links?.length ? (
85
+ <nav className="desk-about-links" aria-label="Links">
86
+ {app.links.map(link => (
87
+ <a key={link.url} href={link.url} target="_blank" rel="noopener noreferrer">{link.label}</a>
88
+ ))}
89
+ </nav>
90
+ ) : null}
91
+ {app.note != null && <p className="desk-about-note desk-text-small desk-text-muted">{app.note}</p>}
92
+ {hasFacts && !sections && <Loading label="Reading what is running" />}
93
+ {sections?.length ? (
94
+ // One grid for every section, so the names make one column down the panel, as wide as the longest.
95
+ <div className="desk-about-sections">
96
+ {sections.map((section, n) => (
97
+ <AboutFacts key={section.title ?? `facts-${n}`} section={section} />
98
+ ))}
99
+ </div>
100
+ ) : null}
101
+ </div>
102
+ {app.legal != null && <p className="desk-about-legal desk-text-small desk-text-muted">{app.legal}</p>}
103
+ </Pane>
104
+ )
105
+ }
106
+
107
+ function AboutFacts({ section }: { readonly section: AboutSection }) {
108
+ const list = (
109
+ <dl className="desk-about-facts">
110
+ {section.facts.map(({ name, value, note, link }) => (
111
+ <div key={name}>
112
+ <dt>{name}</dt>
113
+ <dd>
114
+ {value}
115
+ {(note != null || link) && (
116
+ <span className="desk-about-fact-note desk-text-small desk-text-muted">
117
+ {note}
118
+ {note != null && link && ' '}
119
+ {link && <a href={link.url} target="_blank" rel="noopener noreferrer">{link.label}</a>}
120
+ </span>
121
+ )}
122
+ </dd>
123
+ </div>
124
+ ))}
125
+ </dl>
126
+ )
127
+ return section.title ? <Section title={section.title}>{list}</Section> : <section aria-label="Facts">{list}</section>
128
+ }
129
+
130
+ /** The sidebar's groups, in the order each first appears; sections with no group come first, unheaded. */
131
+ 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 } : {}),
146
+ items: items.map(s => ({ id: s.id, label: s.title, ...(s.icon != null ? { icon: s.icon } : {}), ...(s.badge != null ? { badge: s.badge } : {}) })),
147
+ }))
148
+ }
149
+
150
+ /**
151
+ * The app's settings sections, Appearance first when it has one, laid out as it declared: on one page, or a sidebar beside the chosen one.
152
+ * Desk's Settings window is this; an app whose Settings window is its own can draw it there. The window's
153
+ * route is the section: a link to `settings` with a route opens on it, and choosing one in the sidebar moves
154
+ * the route, so the address says where Settings is.
155
+ */
156
+ export function SettingsContent() {
157
+ const app = useDeskApp()
158
+ const sections = app?.settings?.sections ?? []
159
+ const [route, setRoute] = useWindowRoute()
160
+ const page = useRef<HTMLDivElement>(null)
161
+ const sidebar = app?.settings?.layout === 'sidebar'
162
+
163
+ // On one page, a link to a section scrolls it into view.
164
+ useEffect(() => {
165
+ if (sidebar || !route) return
166
+ page.current?.querySelector(`[data-settings-section="${CSS.escape(route)}"]`)?.scrollIntoView?.({ block: 'start' })
167
+ }, [route, sidebar])
168
+
169
+ if (!sections.length) return null
170
+ if (!sidebar) {
171
+ return (
172
+ <Pane padded label="Settings" className="desk-settings">
173
+ <div ref={page} className="desk-settings-page">
174
+ {sections.map(section => (
175
+ <div key={section.id} data-settings-section={section.id}>
176
+ <Section title={section.title}>{section.content}</Section>
177
+ </div>
178
+ ))}
179
+ </div>
180
+ </Pane>
181
+ )
182
+ }
183
+ // With no route, or one to a section that has gone, the first section as listed, not as declared.
184
+ const groups = settingsGroups(sections)
185
+ const chosen = sections.find(s => s.id === route) ?? sections.find(s => s.id === groups[0]?.items[0]?.id) ?? sections[0]!
186
+ return (
187
+ <Split side={<Sidebar label="Settings" sections={groups} value={chosen.id} onChange={setRoute} />} size={220} min={180} label="Resize the list of settings">
188
+ <Pane padded label={chosen.title} className="desk-settings">
189
+ <SettingsSectionView section={chosen} />
190
+ </Pane>
191
+ </Split>
192
+ )
193
+ }
194
+
195
+ function SettingsSectionView({ section }: { readonly section: SettingsSection }): ReactNode {
196
+ return <Section title={section.title}>{section.content}</Section>
197
+ }