@liberation-data/desk 0.41.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 (112) hide show
  1. package/README.md +152 -9
  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/controls.css +21 -2
  14. package/dist/css/dock.css +1 -1
  15. package/dist/css/looks.css +50 -4
  16. package/dist/css/overlays.css +6 -0
  17. package/dist/css/tokens.css +1 -0
  18. package/dist/css/windows.css +173 -5
  19. package/dist/desk.css +401 -12
  20. package/dist/react/Desktop.d.ts +12 -3
  21. package/dist/react/Desktop.d.ts.map +1 -1
  22. package/dist/react/Desktop.js +29 -9
  23. package/dist/react/Desktop.js.map +1 -1
  24. package/dist/react/Dock.d.ts +6 -1
  25. package/dist/react/Dock.d.ts.map +1 -1
  26. package/dist/react/Dock.js +9 -1
  27. package/dist/react/Dock.js.map +1 -1
  28. package/dist/react/MenuBar.d.ts +8 -1
  29. package/dist/react/MenuBar.d.ts.map +1 -1
  30. package/dist/react/MenuBar.js +34 -5
  31. package/dist/react/MenuBar.js.map +1 -1
  32. package/dist/react/app.d.ts +174 -0
  33. package/dist/react/app.d.ts.map +1 -0
  34. package/dist/react/app.js +146 -0
  35. package/dist/react/app.js.map +1 -0
  36. package/dist/react/appPanels.d.ts +22 -0
  37. package/dist/react/appPanels.d.ts.map +1 -0
  38. package/dist/react/appPanels.js +114 -0
  39. package/dist/react/appPanels.js.map +1 -0
  40. package/dist/react/appearance.d.ts +55 -0
  41. package/dist/react/appearance.d.ts.map +1 -0
  42. package/dist/react/appearance.js +140 -0
  43. package/dist/react/appearance.js.map +1 -0
  44. package/dist/react/arrange.d.ts +6 -0
  45. package/dist/react/arrange.d.ts.map +1 -1
  46. package/dist/react/arrange.js +21 -0
  47. package/dist/react/arrange.js.map +1 -1
  48. package/dist/react/codeBlock.d.ts.map +1 -1
  49. package/dist/react/codeBlock.js +6 -60
  50. package/dist/react/codeBlock.js.map +1 -1
  51. package/dist/react/context.d.ts +9 -1
  52. package/dist/react/context.d.ts.map +1 -1
  53. package/dist/react/context.js +47 -3
  54. package/dist/react/context.js.map +1 -1
  55. package/dist/react/copy.d.ts +22 -0
  56. package/dist/react/copy.d.ts.map +1 -0
  57. package/dist/react/copy.js +68 -0
  58. package/dist/react/copy.js.map +1 -0
  59. package/dist/react/documentTitle.d.ts.map +1 -1
  60. package/dist/react/documentTitle.js +4 -1
  61. package/dist/react/documentTitle.js.map +1 -1
  62. package/dist/react/index.d.ts +10 -1
  63. package/dist/react/index.d.ts.map +1 -1
  64. package/dist/react/index.js +7 -1
  65. package/dist/react/index.js.map +1 -1
  66. package/dist/react/layers.d.ts.map +1 -1
  67. package/dist/react/layers.js +7 -2
  68. package/dist/react/layers.js.map +1 -1
  69. package/dist/react/overlays.d.ts.map +1 -1
  70. package/dist/react/overlays.js +29 -12
  71. package/dist/react/overlays.js.map +1 -1
  72. package/dist/react/shell.d.ts +4 -1
  73. package/dist/react/shell.d.ts.map +1 -1
  74. package/dist/react/shell.js +2 -2
  75. package/dist/react/shell.js.map +1 -1
  76. package/dist/react/tabs.d.ts +7 -1
  77. package/dist/react/tabs.d.ts.map +1 -1
  78. package/dist/react/tabs.js +13 -8
  79. package/dist/react/tabs.js.map +1 -1
  80. package/dist/react/widgets.d.ts +33 -0
  81. package/dist/react/widgets.d.ts.map +1 -0
  82. package/dist/react/widgets.js +213 -0
  83. package/dist/react/widgets.js.map +1 -0
  84. package/llms.txt +75 -8
  85. package/package.json +1 -1
  86. package/src/core/index.ts +2 -0
  87. package/src/core/scheme.ts +21 -0
  88. package/src/core/types.ts +6 -0
  89. package/src/css/apps.css +149 -0
  90. package/src/css/controls.css +21 -2
  91. package/src/css/dock.css +1 -1
  92. package/src/css/looks.css +50 -4
  93. package/src/css/overlays.css +6 -0
  94. package/src/css/tokens.css +1 -0
  95. package/src/css/windows.css +173 -5
  96. package/src/react/Desktop.tsx +50 -12
  97. package/src/react/Dock.tsx +11 -1
  98. package/src/react/MenuBar.tsx +38 -5
  99. package/src/react/app.tsx +284 -0
  100. package/src/react/appPanels.tsx +197 -0
  101. package/src/react/appearance.tsx +204 -0
  102. package/src/react/arrange.ts +23 -0
  103. package/src/react/codeBlock.tsx +7 -65
  104. package/src/react/context.tsx +55 -3
  105. package/src/react/copy.ts +81 -0
  106. package/src/react/documentTitle.ts +4 -1
  107. package/src/react/index.ts +30 -1
  108. package/src/react/layers.ts +7 -2
  109. package/src/react/overlays.tsx +30 -12
  110. package/src/react/shell.tsx +5 -2
  111. package/src/react/tabs.tsx +23 -10
  112. package/src/react/widgets.tsx +265 -0
@@ -0,0 +1,204 @@
1
+ import { useSyncExternalStore } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import type { LookChoice } from '../core/look.js'
4
+ import { COLOR_SCHEME_KEY, LOOK_KEY } from '../core/scheme.js'
5
+ import type { ColorScheme } from '../core/scheme.js'
6
+ import type { Fx } from '../fx/fx.js'
7
+ import { RadioGroup, SegmentedControl } from './controls.js'
8
+ import { SoundsToggle } from './soundsToggle.js'
9
+
10
+ /*
11
+ * APPEARANCE: the rows of Settings that change how the whole app looks and sounds, made once here so every
12
+ * app has them the same way. The app chooses which rows: the colour scheme (light, dark or the system's),
13
+ * the look (the window chrome: the device's, Mac, GNOME or Windows), its sounds, and a slot for a theme
14
+ * picker of its own. An app whose named themes decide light or dark leaves the scheme out.
15
+ *
16
+ * The scheme and the look are remembered in this browser, under `desk.scheme` and `desk.look`, as a widget
17
+ * kept in front is. The scheme is `data-theme` on the root, which the stylesheet already reads; the look is
18
+ * the desk's `look`, chosen here in place of the one the app passes.
19
+ *
20
+ * AN APP THAT RECEIVES ITS THEME FROM A HOST does not choose it: the host does, and sends it (an app in an
21
+ * `AppFrame` with a `theme`, through the bridge). Then the scheme, the look and the app's theme slot are not
22
+ * offered and not applied, since the app follows the host's; sounds stay, being the app's own. The app that
23
+ * is the theme's source keeps them all.
24
+ */
25
+
26
+ /** Which rows Appearance has. `true` is the colour scheme alone. */
27
+ export interface AppearanceRows {
28
+ /** Light, dark or the system's. Leave out for an app whose named themes decide it. */
29
+ readonly scheme?: boolean
30
+ /** The window chrome: the device's, Mac, GNOME or Windows. */
31
+ readonly look?: boolean
32
+ /** The app's sounds: one switch, `SoundsToggle`. */
33
+ readonly sounds?: Fx<string>
34
+ /** What `sounds` silences, said under the switch. */
35
+ readonly soundsDescription?: ReactNode
36
+ /** The app's own theme picker, drawn after the scheme. */
37
+ readonly theme?: ReactNode
38
+ }
39
+
40
+ /** The rows an `appearance` declaration asks for: `true` is the scheme alone, left out is none. */
41
+ export const rowsOf = (appearance: boolean | AppearanceRows | undefined): AppearanceRows =>
42
+ appearance === true ? { scheme: true } : appearance || {}
43
+
44
+ /** The rows left to show: none of the theme's when it comes from a host. */
45
+ export function shownRows(rows: AppearanceRows, fromHost: boolean): AppearanceRows {
46
+ if (!fromHost) return rows
47
+ const { sounds, soundsDescription } = rows
48
+ return { ...(sounds ? { sounds } : {}), ...(soundsDescription != null ? { soundsDescription } : {}) }
49
+ }
50
+
51
+ /** Whether any row is left to show: with none, Appearance is no section at all. */
52
+ export const hasRows = (rows: AppearanceRows): boolean => Boolean(rows.scheme || rows.look || rows.sounds || rows.theme != null)
53
+
54
+ /* A choice kept in this browser, read by every part that shows it, here and in another tab. */
55
+ 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 }
96
+ }
97
+
98
+ const scheme = storedChoice<ColorScheme>(COLOR_SCHEME_KEY, ['system', 'light', 'dark'])
99
+ const look = storedChoice<LookChoice>(LOOK_KEY, ['auto', 'mac', 'gnome', 'windows'])
100
+
101
+ /** The colour scheme chosen in Appearance: `system` until one is. */
102
+ export const colorScheme = (): ColorScheme => scheme.now() ?? 'system'
103
+ /** 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)
105
+ /** The colour scheme, kept current: for an app that tells a native shell, or themes something of its own. */
106
+ export const useColorScheme = (): ColorScheme => useSyncExternalStore(scheme.subscribe, colorScheme, () => 'system')
107
+
108
+ /** The look chosen in Appearance, or null when the person has not chosen one and the app's own stands. */
109
+ export const lookChoice = (): LookChoice | null => look.now()
110
+ /** Chooses a look, remembers it, and puts it on the desk in place of the app's own. */
111
+ export const setLookChoice = (next: LookChoice): void => look.write(next)
112
+ /** The look chosen in Appearance, kept current, or null while the app's own stands. */
113
+ export const useLookChoice = (): LookChoice | null => useSyncExternalStore(look.subscribe, lookChoice, () => null)
114
+
115
+ /** Puts the scheme on the page: `data-theme`, or none to follow the system through the stylesheet's media query. */
116
+ export function showColorScheme(next: ColorScheme, root: HTMLElement = document.documentElement): void {
117
+ if (next === 'system') root.removeAttribute('data-theme')
118
+ else root.setAttribute('data-theme', next)
119
+ }
120
+
121
+ /*
122
+ * Whether a host sends this app its theme. The bridge an `AppFrame` puts in the page says so when the first
123
+ * theme arrives; an app that knows some other way says `themeFromHost`.
124
+ */
125
+ interface Bridge {
126
+ readonly theme?: unknown
127
+ onTheme?(handler: (theme: unknown) => void): void
128
+ }
129
+ const bridge = (): Bridge | undefined => (typeof window === 'undefined' ? undefined : (window as unknown as { desk?: Bridge }).desk)
130
+ const hostListeners = new Set<() => void>()
131
+ let watched: Bridge | undefined
132
+ let themed: Bridge | undefined
133
+
134
+ /**
135
+ * Whether a host has sent this page its theme, through the bridge: then `data-theme` on the root is the
136
+ * host's, and Appearance must not take it away as it hides. Read from the bridge itself, not only from its
137
+ * handler, so a theme that came before the desk mounted is known on the first render, and the remembered
138
+ * scheme is never put over it, not even for a frame.
139
+ */
140
+ export const hostThemed = (): boolean => {
141
+ const found = bridge()
142
+ return found !== undefined && (found.theme != null || themed === found)
143
+ }
144
+
145
+ function watchBridge() {
146
+ const found = bridge()
147
+ if (!found?.onTheme || watched === found) return
148
+ watched = found
149
+ found.onTheme(() => {
150
+ themed = found
151
+ hostListeners.forEach(listener => listener())
152
+ })
153
+ }
154
+
155
+ const subscribeHost = (listener: () => void) => {
156
+ watchBridge()
157
+ hostListeners.add(listener)
158
+ return () => void hostListeners.delete(listener)
159
+ }
160
+
161
+ /** Whether this app takes its theme from a host: as declared, or else as the bridge has found. */
162
+ export function useThemeFromHost(declared: boolean | undefined): boolean {
163
+ const found = useSyncExternalStore(subscribeHost, hostThemed, () => false)
164
+ return declared ?? found
165
+ }
166
+
167
+ const SCHEMES = [
168
+ { value: 'system', label: 'Follow the system' },
169
+ { value: 'light', label: 'Light' },
170
+ { value: 'dark', label: 'Dark' },
171
+ ] as const satisfies readonly { value: ColorScheme; label: string }[]
172
+
173
+ const LOOKS = [
174
+ { value: 'auto', label: 'Automatic' },
175
+ { value: 'mac', label: 'Mac' },
176
+ { value: 'gnome', label: 'GNOME' },
177
+ { value: 'windows', label: 'Windows' },
178
+ ] as const satisfies readonly { value: LookChoice; label: string }[]
179
+
180
+ /** What `AppearanceSettings` draws: the rows, and the look to show as chosen when the person has chosen none. */
181
+ export interface AppearanceSettingsProps {
182
+ readonly rows: AppearanceRows
183
+ /** The look the desk wears when the person has chosen none: the app's own. */
184
+ readonly look: LookChoice
185
+ }
186
+
187
+ /** The Appearance rows: what Settings shows, or what an app's own Settings window can draw. */
188
+ export function AppearanceSettings({ rows, look: appLook }: AppearanceSettingsProps) {
189
+ const chosenScheme = useColorScheme()
190
+ const chosenLook = useLookChoice() ?? appLook
191
+ return (
192
+ <div className="desk-fields">
193
+ {rows.scheme && <RadioGroup label="Colour scheme" value={chosenScheme} onChange={setColorScheme} options={SCHEMES} />}
194
+ {rows.theme}
195
+ {rows.look && (
196
+ <div className="desk-field">
197
+ <span className="desk-label" aria-hidden="true">Look</span>
198
+ <SegmentedControl label="Look" options={LOOKS} value={chosenLook} onChange={setLookChoice} />
199
+ </div>
200
+ )}
201
+ {rows.sounds && <SoundsToggle fx={rows.sounds} label="Sounds" description={rows.soundsDescription} />}
202
+ </div>
203
+ )
204
+ }
@@ -80,3 +80,26 @@ export function arrangement(
80
80
  ),
81
81
  }
82
82
  }
83
+
84
+ /**
85
+ * The area without a rectangle in one of its corners, such as the one widgets kept in front stand in, so
86
+ * windows laid out in it tile around them. Windows are rectangles, so the corner takes a whole column or a
87
+ * whole row with it: whichever leaves more room. A rectangle that misses the area leaves it as it is.
88
+ */
89
+ export function areaAround(area: Area, clear: Frame): Area {
90
+ const right = area.x + area.width
91
+ const bottom = area.y + area.height
92
+ const left = Math.max(area.x, clear.x)
93
+ const top = Math.max(area.y, clear.y)
94
+ const end = Math.min(right, clear.x + clear.width)
95
+ const foot = Math.min(bottom, clear.y + clear.height)
96
+ if (end <= left || foot <= top) return area
97
+ // A rectangle in a corner touches two sides; the room left beside it, and above or below it.
98
+ const column =
99
+ left > area.x ? { ...area, width: left - area.x }
100
+ : { ...area, x: end, width: right - end }
101
+ const row =
102
+ top > area.y ? { ...area, height: top - area.y }
103
+ : { ...area, y: foot, height: bottom - foot }
104
+ return column.width * column.height >= row.width * row.height ? column : row
105
+ }
@@ -1,6 +1,6 @@
1
- import { useEffect, useMemo, useRef, useState } from 'react'
1
+ import { useMemo, useRef, useState } from 'react'
2
2
  import type { ReactNode } from 'react'
3
- import { formatShortcut } from '../core/shortcuts.js'
3
+ import { useCopy } from './copy.js'
4
4
  import { Button, SegmentedControl } from './controls.js'
5
5
 
6
6
  /*
@@ -262,11 +262,6 @@ const NAMES: Record<CodeLanguage, string> = {
262
262
  shell: 'Shell', json: 'JSON', text: '',
263
263
  }
264
264
 
265
- /** How long "Copied" stays before the button says Copy again. */
266
- const COPIED_MS = 2000
267
-
268
- type Copying = 'idle' | 'copied' | 'select'
269
-
270
265
  /** A framed block of code to copy, highlighted for its language, with a Copy button in its corner. */
271
266
  export function CodeBlock(props: CodeBlockProps) {
272
267
  const { label, copy = true, highlight, className, variants, value, onValueChange } = props
@@ -279,72 +274,19 @@ export function CodeBlock(props: CodeBlockProps) {
279
274
  const code = (variants ? variant?.code : props.code) ?? ''
280
275
  const language: CodeLanguage = (variants ? variant?.language : props.language) ?? 'text'
281
276
  const body = useRef<HTMLElement>(null)
282
- const [copying, setCopying] = useState<Copying>('idle')
283
- const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
284
- const alive = useRef(true)
285
- const copied = useRef(code)
277
+ // The selection, where there is no clipboard, is the code as drawn: without the trailing newline,
278
+ // and with whatever a host's `highlight` adds to it.
279
+ const copying = useCopy(code, body)
286
280
  const shown = code.endsWith('\n') ? code.slice(0, -1) : code
287
281
  const content = useMemo(() => (highlight ?? highlighted)(shown, language), [shown, language, highlight])
288
282
 
289
- useEffect(() => {
290
- alive.current = true
291
- return () => {
292
- alive.current = false
293
- clearTimeout(timer.current)
294
- }
295
- }, [])
296
- // Copied or selected says something about this code; new code has been neither. Put back while
297
- // rendering, so not one frame says Copied over code nobody copied.
298
- if (copied.current !== code) {
299
- copied.current = code
300
- clearTimeout(timer.current)
301
- if (copying !== 'idle') setCopying('idle')
302
- }
303
-
304
- const settle = (next: Copying) => {
305
- // A copy that finishes after the block has gone has nothing left to say it in.
306
- if (!alive.current) return
307
- clearTimeout(timer.current)
308
- setCopying(next)
309
- if (next === 'copied') timer.current = setTimeout(() => setCopying('idle'), COPIED_MS)
310
- }
311
-
312
- /*
313
- * THE CLIPBOARD IS NOT ALWAYS THERE: a page over plain http has none, and a browser may refuse
314
- * it. Then the code is selected and the older copy command tried; where that fails too, the
315
- * selection stays and the button says which keys copy it, so the person is one keystroke away.
316
- * The selection is the code as drawn: without the trailing newline, and with whatever a host's
317
- * `highlight` adds to it.
318
- */
319
- const onCopy = async () => {
320
- try {
321
- await navigator.clipboard.writeText(code)
322
- settle('copied')
323
- } catch {
324
- const selection = window.getSelection()
325
- if (body.current && selection) selection.selectAllChildren(body.current)
326
- let copied = false
327
- try {
328
- copied = document.execCommand('copy')
329
- } catch {
330
- // Unsupported: the selection is the way.
331
- }
332
- settle(copied ? 'copied' : 'select')
333
- }
334
- }
335
-
336
- const keys = formatShortcut('mod+c')
337
283
  const name = variant
338
284
  ? label ? `${label}, ${variant.label}` : `${variant.label} code`
339
285
  : label ?? (NAMES[language] ? `${NAMES[language]} code` : 'Code')
340
286
  const actions = copy && (
341
287
  <div className="desk-code-actions">
342
- <Button intent="quiet" size="small" onClick={onCopy}>
343
- {copying === 'copied' ? 'Copied' : copying === 'select' ? `Press ${keys}` : 'Copy'}
344
- </Button>
345
- <span className="desk-code-status" role="status">
346
- {copying === 'copied' ? 'Copied' : copying === 'select' ? `Selected. Press ${keys} to copy.` : ''}
347
- </span>
288
+ <Button intent="quiet" size="small" onClick={() => void copying.copy()}>{copying.button}</Button>
289
+ <span className="desk-code-status" role="status">{copying.status}</span>
348
290
  </div>
349
291
  )
350
292
  return (
@@ -1,5 +1,6 @@
1
- import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useState, useSyncExternalStore } from 'react'
1
+ import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react'
2
2
  import type { ReactNode, RefObject } from 'react'
3
+ import { addCommandHandler } from '../core/commands.js'
3
4
  import { createBus } from '../core/events.js'
4
5
  import type { Bus } from '../core/events.js'
5
6
  import { createDesk, routeOf } from '../core/desk.js'
@@ -7,6 +8,10 @@ import { deviceLook, resolveLook } from '../core/look.js'
7
8
  import { holdAll } from '../core/services.js'
8
9
  import type { Holdable } from '../core/services.js'
9
10
  import type { Look, LookChoice } from '../core/look.js'
11
+ import { AppProvider, bindDeskShortcuts, hasSettings, openSettings, SettingsCommand } from './app.js'
12
+ import { AppearanceSettings, hasRows, hostThemed, rowsOf, showColorScheme, shownRows, useColorScheme, useLookChoice, useThemeFromHost } from './appearance.js'
13
+ import type { DeskApp, SettingsSection } from './app.js'
14
+ import type { AppearanceRows } from './appearance.js'
10
15
  import { DragProvider } from './dragContext.js'
11
16
  import { missingProvider } from './missingProvider.js'
12
17
  import type { Desk } from '../core/desk.js'
@@ -28,6 +33,7 @@ export interface DeskProviderProps {
28
33
  /**
29
34
  * The platform chrome: `auto` (the default) matches the device, or the app passes the one its user
30
35
  * chose. The app keeps that choice where it keeps its other preferences; the desk does not store it.
36
+ * With Appearance's `look` row, the person's choice there, remembered by Desk, stands in for this one.
31
37
  */
32
38
  readonly look?: LookChoice
33
39
  /**
@@ -35,15 +41,36 @@ export interface DeskProviderProps {
35
41
  * so the array need not be stable.
36
42
  */
37
43
  readonly services?: readonly Holdable[]
44
+ /**
45
+ * What the app says about itself: its name, icon, version and links, which give it an About, and its
46
+ * settings, if it has any, which give it Settings. The dock, the menu bar and the desktop put both in their
47
+ * standard places. Read on every render, so it may change.
48
+ */
49
+ readonly app?: DeskApp
38
50
  readonly children: ReactNode
39
51
  }
40
52
 
41
- export function DeskProvider({ desk, options, look = 'auto', services, children }: DeskProviderProps) {
53
+ export function DeskProvider({ desk, options, look: appLook = 'auto', services, app: declared, children }: DeskProviderProps) {
42
54
  const [value] = useState(() => desk ?? createDesk(options))
43
55
  const [bus] = useState(createBus)
44
56
  const [held] = useState(() => services ?? [])
45
57
  useEffect(() => holdAll(held), [held])
58
+ // Appearance: the rows the app asked for, less the theme's when a host chooses it.
59
+ const fromHost = useThemeFromHost(declared?.themeFromHost)
60
+ const rows = shownRows(rowsOf(declared?.settings?.appearance), fromHost)
61
+ const app = useMemo(() => withAppearance(declared, rows, appLook), [declared, rows.scheme, rows.look, rows.sounds, rows.theme, rows.soundsDescription, appLook])
62
+ const chosenLook = useLookChoice()
63
+ const scheme = useColorScheme()
64
+ const look = (rows.look && chosenLook) || appLook
46
65
  const resolved = resolveLook(look)
66
+ useLayoutEffect(() => {
67
+ if (!rows.scheme) return
68
+ showColorScheme(scheme)
69
+ // Hiding because a host's theme arrived: the attribute is the host's now, so it stays.
70
+ return () => {
71
+ if (!hostThemed()) showColorScheme('system')
72
+ }
73
+ }, [rows.scheme, scheme])
47
74
  // On the root, not the desk: menus and popovers are portalled out of it, and are chrome too.
48
75
  useLayoutEffect(() => {
49
76
  document.documentElement.dataset.look = resolved
@@ -51,17 +78,42 @@ export function DeskProvider({ desk, options, look = 'auto', services, children
51
78
  delete document.documentElement.dataset.look
52
79
  }
53
80
  }, [resolved])
81
+ // "Settings…" and ⌘, perform this; it answers at the top of the chain, so a window can still take it.
82
+ const latestApp = useRef(app ?? null)
83
+ latestApp.current = app ?? null
84
+ const hasApp = app !== undefined
85
+ useEffect(() => {
86
+ if (!hasApp) return
87
+ const remove = addCommandHandler(document, SettingsCommand, (route?: unknown) => openSettings(value, latestApp.current, typeof route === 'string' ? route : undefined), {
88
+ enabled: () => hasSettings(latestApp.current),
89
+ })
90
+ // ⌘, here, not only in the menu bar's keymap, so it works in an app that has none.
91
+ const unbind = bindDeskShortcuts(value)
92
+ return () => {
93
+ remove()
94
+ unbind()
95
+ }
96
+ }, [value, hasApp])
54
97
  return (
55
98
  <DeskContext.Provider value={value}>
56
99
  <BusContext.Provider value={bus}>
57
100
  <LookContext.Provider value={resolved}>
58
- <DragProvider>{children}</DragProvider>
101
+ <AppProvider value={app ?? null}>
102
+ <DragProvider>{children}</DragProvider>
103
+ </AppProvider>
59
104
  </LookContext.Provider>
60
105
  </BusContext.Provider>
61
106
  </DeskContext.Provider>
62
107
  )
63
108
  }
64
109
 
110
+ /** The app's declaration with Desk's Appearance as its first section, when it has rows to show. */
111
+ function withAppearance(app: DeskApp | undefined, rows: AppearanceRows, look: LookChoice): DeskApp | undefined {
112
+ if (!app?.settings || !hasRows(rows)) return app
113
+ const appearance: SettingsSection = { id: 'appearance', title: 'Appearance', content: <AppearanceSettings rows={rows} look={look} /> }
114
+ return { ...app, settings: { ...app.settings, sections: [appearance, ...(app.settings.sections ?? [])] } }
115
+ }
116
+
65
117
  /** The look the desk is wearing, for an app that draws chrome of its own. Outside a desk, the device's. */
66
118
  export const useLook = (): Look => useContext(LookContext) ?? deviceLook()
67
119
 
@@ -0,0 +1,81 @@
1
+ import { useEffect, useRef, useState } from 'react'
2
+ import type { RefObject } from 'react'
3
+ import { formatShortcut } from '../core/shortcuts.js'
4
+
5
+ /** How long "Copied" stays before the button says Copy again. */
6
+ const COPIED_MS = 2000
7
+
8
+ /** Where a copy has got to: nothing yet, done, or the text selected for the person to copy. */
9
+ export type Copying = 'idle' | 'copied' | 'select'
10
+
11
+ /** What `useCopy` gives a view: where the copy has got to, the action, and what to say about it. */
12
+ export interface CopyState {
13
+ readonly copying: Copying
14
+ readonly copy: () => Promise<void>
15
+ /** What the button says: Copy, Copied, or which keys copy the selection. */
16
+ readonly button: string
17
+ /** What a polite status region says, so the result is heard as well as seen. */
18
+ readonly status: string
19
+ }
20
+
21
+ /**
22
+ * Copying a block of text with a button, as `CodeBlock` and the About panel do.
23
+ *
24
+ * THE CLIPBOARD IS NOT ALWAYS THERE: a page over plain http has none, and a browser may refuse it.
25
+ * Then `select`'s element is selected and the older copy command tried; where that fails too, the
26
+ * selection stays and the button says which keys copy it, so the person is one keystroke away.
27
+ * New text has been neither copied nor selected, so the state goes back to idle when `text` changes.
28
+ */
29
+ export function useCopy(text: string, select: RefObject<HTMLElement | null>): CopyState {
30
+ const [copying, setCopying] = useState<Copying>('idle')
31
+ const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
32
+ const alive = useRef(true)
33
+ const copied = useRef(text)
34
+
35
+ useEffect(() => {
36
+ alive.current = true
37
+ return () => {
38
+ alive.current = false
39
+ clearTimeout(timer.current)
40
+ }
41
+ }, [])
42
+ // Put back while rendering, so not one frame says Copied over text nobody copied.
43
+ if (copied.current !== text) {
44
+ copied.current = text
45
+ clearTimeout(timer.current)
46
+ if (copying !== 'idle') setCopying('idle')
47
+ }
48
+
49
+ const settle = (next: Copying) => {
50
+ // A copy that finishes after the view has gone has nothing left to say it in.
51
+ if (!alive.current) return
52
+ clearTimeout(timer.current)
53
+ setCopying(next)
54
+ if (next === 'copied') timer.current = setTimeout(() => setCopying('idle'), COPIED_MS)
55
+ }
56
+
57
+ const copy = async () => {
58
+ try {
59
+ await navigator.clipboard.writeText(text)
60
+ settle('copied')
61
+ } catch {
62
+ const selection = window.getSelection()
63
+ if (select.current && selection) selection.selectAllChildren(select.current)
64
+ let done = false
65
+ try {
66
+ done = document.execCommand('copy')
67
+ } catch {
68
+ // Unsupported: the selection is the way.
69
+ }
70
+ settle(done ? 'copied' : 'select')
71
+ }
72
+ }
73
+
74
+ const keys = formatShortcut('mod+c')
75
+ return {
76
+ copying,
77
+ copy,
78
+ button: copying === 'copied' ? 'Copied' : copying === 'select' ? `Press ${keys}` : 'Copy',
79
+ status: copying === 'copied' ? 'Copied' : copying === 'select' ? `Selected. Press ${keys} to copy.` : '',
80
+ }
81
+ }
@@ -1,6 +1,7 @@
1
1
  import { useEffect, useState } from 'react'
2
2
  import { focusedId } from '../core/desk.js'
3
3
  import type { WindowId } from '../core/types.js'
4
+ import { appWindowTitle, useDeskApp } from './app.js'
4
5
  import { useDeskState } from './context.js'
5
6
 
6
7
  /*
@@ -32,13 +33,15 @@ export function useDocumentTitle(title: (id: WindowId) => string, options: Docum
32
33
  const { app, separator = DEFAULT_SEPARATOR } = options
33
34
  const state = useDeskState()
34
35
  const id = focusedId(state)
36
+ // Desk names its own About and Settings, so the app's title function need not.
37
+ const declared = useDeskApp()
35
38
 
36
39
  // What the page was called before the desk took it over: the app's name by default, and what
37
40
  // is put back on the way out. Read once, so a title this hook set is never mistaken for it.
38
41
  const [original] = useState(() => (typeof document === 'undefined' ? '' : document.title))
39
42
  const base = app ?? original
40
43
 
41
- const front = id === null ? '' : title(id)
44
+ const front = id === null ? '' : (appWindowTitle(declared, id) ?? title(id))
42
45
  const next = front && base ? `${front}${separator}${base}` : front || base
43
46
 
44
47
  useEffect(() => {
@@ -12,11 +12,40 @@ export type {
12
12
  DropTargetResult,
13
13
  } from './dnd.js'
14
14
  export { DeskShell } from './shell.js'
15
+ export {
16
+ ABOUT_WINDOW,
17
+ appWindowTitle,
18
+ dockSettings,
19
+ openAbout,
20
+ PANEL_LIMITS,
21
+ SETTINGS_WINDOW,
22
+ SettingsCommand,
23
+ SIDEBAR_LIMITS,
24
+ SYSTEM_GROUP,
25
+ useDeskApp,
26
+ } from './app.js'
27
+ export type { AboutFact, AboutSection, AppLink, AppSettings, DeskApp, SettingsSection } from './app.js'
28
+ export { AboutContent, aboutText, SettingsContent, useOpenSettings } from './appPanels.js'
29
+ export {
30
+ AppearanceSettings,
31
+ colorScheme,
32
+ lookChoice,
33
+ setColorScheme,
34
+ setLookChoice,
35
+ useColorScheme,
36
+ useLookChoice,
37
+ } from './appearance.js'
38
+ export type { AppearanceRows, AppearanceSettingsProps } from './appearance.js'
39
+ // Also in the core entry, with no React, for a server-rendered <head> and for moving a choice kept elsewhere.
40
+ export { COLOR_SCHEME_KEY, COLOR_SCHEME_SCRIPT, LOOK_KEY } from '../core/scheme.js'
41
+ export type { ColorScheme } from '../core/scheme.js'
15
42
  export type { DeskShellProps } from './shell.js'
16
43
  export { DeskProvider, useDesk, useDeskState, useLook, useWindowId, useWindowRoute } from './context.js'
17
44
  export type { DeskProviderProps } from './context.js'
18
45
  export { useCanPerform, useCommand, usePerform, useShortcuts } from './commands.js'
19
46
  export type { UseCommandOptions } from './commands.js'
47
+ export { isWidgetInFront, setWidgetInFront, useWidgetsInFront } from './widgets.js'
48
+ export type { Widget, WidgetCorner } from './widgets.js'
20
49
  export { Desktop } from './Desktop.js'
21
50
  export type { DeskLayout, DesktopProps } from './Desktop.js'
22
51
  export { useService, useServiceValue } from './services.js'
@@ -87,7 +116,7 @@ export { Composer, Thread } from './conversation.js'
87
116
  export type { ComposerProps, Message, ThreadProps } from './conversation.js'
88
117
  export { Alert, Popover, Sheet, ToastProvider, useToast } from './overlays.js'
89
118
  export type { AlertProps, PopoverProps, PopoverTriggerProps, SheetProps, Toast, ToastInput } from './overlays.js'
90
- export { MenuBar, menuAction, menuCommand, menuHeader, menuSeparator, windowMenuItems } from './MenuBar.js'
119
+ export { MenuBar, menuAction, menuCommand, menuHeader, menuSeparator, widgetMenuItems, windowMenuItems } from './MenuBar.js'
91
120
  export { useContextMenu } from './contextMenu.js'
92
121
  export type { ContextMenuOptions, ContextMenuResult, ContextMenuTargetProps } from './contextMenu.js'
93
122
  export type { Menu, MenuBarProps, MenuItem } from './MenuBar.js'
@@ -45,10 +45,15 @@ export function useFocusTrap(panel: RefObject<HTMLElement | null>, open: boolean
45
45
  const onKeyDown = (event: KeyboardEvent) => {
46
46
  if (event.key !== 'Tab' || !panel.current) return
47
47
  const stops = tabbable(panel.current)
48
- if (!stops.length) return
48
+ // Nothing to tab to: focus that is in it (a popover holding only text has it on itself) stays there.
49
+ if (!stops.length) {
50
+ if (panel.current.contains(document.activeElement)) event.preventDefault()
51
+ return
52
+ }
49
53
  const [first, last] = [stops[0] as HTMLElement, stops.at(-1) as HTMLElement]
50
54
  const active = document.activeElement
51
- if (event.shiftKey && (active === first || !panel.current.contains(active))) {
55
+ // The panel itself can hold focus (a popover is focusable so its keys scroll it): back from it is out of it.
56
+ if (event.shiftKey && (active === first || active === panel.current || !panel.current.contains(active))) {
52
57
  event.preventDefault()
53
58
  last.focus()
54
59
  } else if (!event.shiftKey && active === last) {