@liberation-data/desk 0.7.0 → 0.9.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 (78) hide show
  1. package/README.md +69 -2
  2. package/dist/core/commands.d.ts +2 -0
  3. package/dist/core/commands.d.ts.map +1 -1
  4. package/dist/core/commands.js +11 -4
  5. package/dist/core/commands.js.map +1 -1
  6. package/dist/core/desk.d.ts +14 -0
  7. package/dist/core/desk.d.ts.map +1 -1
  8. package/dist/core/desk.js +56 -7
  9. package/dist/core/desk.js.map +1 -1
  10. package/dist/core/index.d.ts +1 -1
  11. package/dist/core/index.d.ts.map +1 -1
  12. package/dist/core/index.js +1 -1
  13. package/dist/core/index.js.map +1 -1
  14. package/dist/core/location.d.ts.map +1 -1
  15. package/dist/core/location.js +15 -7
  16. package/dist/core/location.js.map +1 -1
  17. package/dist/core/types.d.ts +7 -1
  18. package/dist/core/types.d.ts.map +1 -1
  19. package/dist/css/dock.css +6 -0
  20. package/dist/css/tokens.css +1 -0
  21. package/dist/css/windows.css +33 -2
  22. package/dist/desk.css +40 -2
  23. package/dist/react/Desktop.d.ts +1 -1
  24. package/dist/react/Desktop.d.ts.map +1 -1
  25. package/dist/react/Desktop.js +24 -9
  26. package/dist/react/Desktop.js.map +1 -1
  27. package/dist/react/Dock.d.ts.map +1 -1
  28. package/dist/react/Dock.js +10 -7
  29. package/dist/react/Dock.js.map +1 -1
  30. package/dist/react/MenuBar.d.ts +5 -1
  31. package/dist/react/MenuBar.d.ts.map +1 -1
  32. package/dist/react/MenuBar.js +10 -3
  33. package/dist/react/MenuBar.js.map +1 -1
  34. package/dist/react/appFrame.d.ts +1 -1
  35. package/dist/react/appFrame.d.ts.map +1 -1
  36. package/dist/react/appFrame.js +1 -1
  37. package/dist/react/appFrame.js.map +1 -1
  38. package/dist/react/context.d.ts.map +1 -1
  39. package/dist/react/context.js +2 -1
  40. package/dist/react/context.js.map +1 -1
  41. package/dist/react/documentTitle.d.ts +10 -0
  42. package/dist/react/documentTitle.d.ts.map +1 -0
  43. package/dist/react/documentTitle.js +25 -0
  44. package/dist/react/documentTitle.js.map +1 -0
  45. package/dist/react/events.d.ts.map +1 -1
  46. package/dist/react/events.js +2 -1
  47. package/dist/react/events.js.map +1 -1
  48. package/dist/react/index.d.ts +2 -0
  49. package/dist/react/index.d.ts.map +1 -1
  50. package/dist/react/index.js +1 -0
  51. package/dist/react/index.js.map +1 -1
  52. package/dist/react/missingProvider.d.ts +3 -0
  53. package/dist/react/missingProvider.d.ts.map +1 -0
  54. package/dist/react/missingProvider.js +21 -0
  55. package/dist/react/missingProvider.js.map +1 -0
  56. package/dist/react/overlays.d.ts.map +1 -1
  57. package/dist/react/overlays.js +2 -1
  58. package/dist/react/overlays.js.map +1 -1
  59. package/llms.txt +33 -5
  60. package/package.json +16 -1
  61. package/src/core/commands.ts +9 -4
  62. package/src/core/desk.ts +61 -7
  63. package/src/core/index.ts +2 -0
  64. package/src/core/location.ts +15 -7
  65. package/src/core/types.ts +7 -1
  66. package/src/css/dock.css +6 -0
  67. package/src/css/tokens.css +1 -0
  68. package/src/css/windows.css +33 -2
  69. package/src/react/Desktop.tsx +45 -10
  70. package/src/react/Dock.tsx +10 -4
  71. package/src/react/MenuBar.tsx +12 -3
  72. package/src/react/appFrame.tsx +5 -1
  73. package/src/react/context.tsx +2 -1
  74. package/src/react/documentTitle.ts +52 -0
  75. package/src/react/events.tsx +2 -1
  76. package/src/react/index.ts +2 -0
  77. package/src/react/missingProvider.ts +23 -0
  78. package/src/react/overlays.tsx +2 -1
@@ -1,4 +1,4 @@
1
- import { cascadeFrame, focusedId, normalise } from './desk.js'
1
+ import { cascadeFrame, focusedId, isMinimized, normalise } from './desk.js'
2
2
  import type { Desk } from './desk.js'
3
3
  import type { DeskState, DeskWindow, Size, WindowId } from './types.js'
4
4
 
@@ -7,12 +7,15 @@ import type { DeskState, DeskWindow, Size, WindowId } from './types.js'
7
7
  * never frames. A shared link should open the same things, not reproduce
8
8
  * someone else's window positions on a different screen.
9
9
  *
10
- * #w=notes,clock~,inspector&f=clock
10
+ * #w=notes,clock~,inspector_&f=clock
11
11
  *
12
- * `~` marks a free window; the others fill the desk. `f` names the focused window.
12
+ * `~` marks a free window; the others fill the desk. `_` marks one that is minimized — open and
13
+ * off the desk — which is not a position but which windows are open and how, so it travels.
14
+ * `f` names the focused window.
13
15
  */
14
16
 
15
17
  const FLOAT = '~'
18
+ const MINIMIZED = '_'
16
19
 
17
20
  const warned = new Set<string>()
18
21
  /** Said once per message: a warning on every parse would be noise, not help. */
@@ -36,7 +39,9 @@ export function serialize(state: DeskState, options: LocationOptions = {}): URLS
36
39
  if (!state.windows.length) return params
37
40
  params.set(
38
41
  options.key ?? 'w',
39
- state.windows.map(w => `${encodeURIComponent(w.id)}${w.mode === 'floating' ? FLOAT : ''}`).join(','),
42
+ state.windows
43
+ .map(w => `${encodeURIComponent(w.id)}${w.mode === 'floating' ? FLOAT : ''}${isMinimized(state, w.id) ? MINIMIZED : ''}`)
44
+ .join(','),
40
45
  )
41
46
  const focused = focusedId(state)
42
47
  if (focused) params.set(options.focusKey ?? 'f', focused)
@@ -58,8 +63,10 @@ export function parse(params: URLSearchParams, stage: Size, options: LocationOpt
58
63
  .split(',')
59
64
  .filter(Boolean)
60
65
  .map(token => {
61
- const floating = token.endsWith(FLOAT)
62
- return { id: decodeURIComponent(floating ? token.slice(0, -1) : token), floating }
66
+ const minimized = token.endsWith(MINIMIZED)
67
+ const rest = minimized ? token.slice(0, -1) : token
68
+ const floating = rest.endsWith(FLOAT)
69
+ return { id: decodeURIComponent(floating ? rest.slice(0, -1) : rest), floating, minimized }
63
70
  })
64
71
  .filter(e => e.id && known(e.id))
65
72
 
@@ -72,7 +79,8 @@ export function parse(params: URLSearchParams, stage: Size, options: LocationOpt
72
79
  const focus = params.get(options.focusKey ?? 'f')
73
80
  const ids = windows.map(w => w.id)
74
81
  const stack = focus && ids.includes(focus) ? [...ids.filter(id => id !== focus), focus] : ids
75
- return normalise({ windows, stack })
82
+ const minimized = entries.filter(e => e.minimized).map(e => e.id)
83
+ return normalise(minimized.length ? { windows, stack, minimized } : { windows, stack })
76
84
  }
77
85
 
78
86
  export interface LocationEnv {
package/src/core/types.ts CHANGED
@@ -24,8 +24,14 @@ export type WindowMode = DeskWindow['mode']
24
24
  export interface DeskState {
25
25
  /** Open windows in the order they were opened. Arrange lays them out in this order, so focusing never reshuffles them. */
26
26
  readonly windows: readonly DeskWindow[]
27
- /** Window ids back to front. The last one has focus. */
27
+ /** Window ids back to front. The last one that is not minimized has focus. */
28
28
  readonly stack: readonly WindowId[]
29
+ /**
30
+ * Open, still mounted, and off the desk. Minimizing is not a way for a window to sit on the desk,
31
+ * so it is not a mode: the window keeps the mode and the frame it had, and comes back to them.
32
+ * Absent while nothing is minimized, which is most of the time.
33
+ */
34
+ readonly minimized?: readonly WindowId[]
29
35
  }
30
36
 
31
37
  export interface CascadeOptions {
package/src/css/dock.css CHANGED
@@ -130,6 +130,12 @@
130
130
  background: var(--desk-accent);
131
131
  }
132
132
 
133
+ /* Open, and all of it off the desk: the dot is hollow, so "open" still reads at a glance. */
134
+ .desk-dock-item[data-minimized]::after {
135
+ background: transparent;
136
+ box-shadow: inset 0 0 0 1px var(--desk-muted);
137
+ }
138
+
133
139
  .desk-dock-badge {
134
140
  position: absolute;
135
141
  top: -5px;
@@ -11,6 +11,7 @@
11
11
  --desk-rule: #dde1e8;
12
12
  --desk-accent: #5b57f5;
13
13
  --desk-close: #ef6b63;
14
+ --desk-minimize: #f0b429;
14
15
  --desk-mode: #34c47a;
15
16
  --desk-info: #34c47a;
16
17
  --desk-radius: 12px;
@@ -91,6 +91,9 @@
91
91
  }
92
92
 
93
93
  .desk-control {
94
+ display: flex;
95
+ align-items: center;
96
+ justify-content: center;
94
97
  width: 12px;
95
98
  height: 12px;
96
99
  padding: 0;
@@ -100,16 +103,44 @@
100
103
  background: var(--desk-rule);
101
104
  }
102
105
 
106
+ /* The cluster, not the dot: arriving anywhere over the three colours and marks all three. */
103
107
  .desk-window[data-focused] .desk-control[data-control="close"],
104
- .desk-control[data-control="close"]:hover {
108
+ .desk-controls:hover .desk-control[data-control="close"] {
105
109
  background: var(--desk-close);
106
110
  }
107
111
 
112
+ .desk-window[data-focused] .desk-control[data-control="minimize"],
113
+ .desk-controls:hover .desk-control[data-control="minimize"] {
114
+ background: var(--desk-minimize);
115
+ }
116
+
108
117
  .desk-window[data-focused] .desk-control[data-control="mode"],
109
- .desk-control[data-control="mode"]:hover {
118
+ .desk-controls:hover .desk-control[data-control="mode"] {
110
119
  background: var(--desk-mode);
111
120
  }
112
121
 
122
+ /*
123
+ * The mark inside a dot says what pressing it does. It is there only while the pointer is over
124
+ * the cluster, or while a control has the keyboard: a dot carrying its mark all the time is
125
+ * three icons in a title bar, and the colours stop being the quiet thing they are for.
126
+ */
127
+ .desk-control-glyph {
128
+ width: 12px;
129
+ height: 12px;
130
+ fill: none;
131
+ stroke: currentColor;
132
+ stroke-width: 1.4;
133
+ stroke-linecap: round;
134
+ color: rgb(0 0 0 / .58);
135
+ opacity: 0;
136
+ transition: opacity .12s var(--desk-ease);
137
+ }
138
+
139
+ .desk-controls:hover .desk-control-glyph,
140
+ .desk-control:focus-visible .desk-control-glyph {
141
+ opacity: 1;
142
+ }
143
+
113
144
  .desk-control:focus-visible {
114
145
  outline: 2px solid var(--desk-accent);
115
146
  outline-offset: 2px;
@@ -1,7 +1,7 @@
1
1
  import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react'
2
2
  import type { CSSProperties, PointerEvent as ReactPointerEvent, ReactNode } from 'react'
3
3
  import { addCommandHandler, addDeskCommands, DeskCommands, STAGE_ATTRIBUTE, WINDOW_ATTRIBUTE, windowElement } from '../core/commands.js'
4
- import { focusedId } from '../core/desk.js'
4
+ import { focusedId, isMinimized, onDesk } from '../core/desk.js'
5
5
  import type { Desk } from '../core/desk.js'
6
6
  import type { DeskWindow, Frame, WindowId, WindowLimits } from '../core/types.js'
7
7
  import { arrangement } from './arrange.js'
@@ -36,7 +36,7 @@ export interface DesktopProps {
36
36
  readonly info?: (id: WindowId) => ReactNode
37
37
  /** Window-wide controls on the trailing side of the title bar. One or two; more belongs in a toolbar. */
38
38
  readonly actions?: (id: WindowId) => ReactNode
39
- /** Shown when no window is open. */
39
+ /** Shown when no window is on the desk — none open, or every one of them minimized. */
40
40
  readonly empty?: ReactNode
41
41
  /** Shown inside a window while its code loads. Default: a quiet "Loading…". */
42
42
  readonly loading?: WindowLoading
@@ -159,7 +159,7 @@ export function Desktop({ renderWindow, title, note, info, actions, limits, empt
159
159
  () => arrangeNow(desk, element),
160
160
  {
161
161
  enabled: () => {
162
- const { windows } = desk.getState()
162
+ const windows = onDesk(desk.getState())
163
163
  return windows.length > 1 || windows.some(w => w.mode === 'floating')
164
164
  },
165
165
  },
@@ -186,7 +186,7 @@ export function Desktop({ renderWindow, title, note, info, actions, limits, empt
186
186
  data-layout={mode}
187
187
  {...{ [STAGE_ATTRIBUTE]: '' }}
188
188
  >
189
- {state.windows.length === 0 && empty}
189
+ {onDesk(state).length === 0 && empty}
190
190
  {state.windows.map(window => (
191
191
  <WindowView
192
192
  key={window.id}
@@ -194,8 +194,10 @@ export function Desktop({ renderWindow, title, note, info, actions, limits, empt
194
194
  layout={mode}
195
195
  depth={state.stack.indexOf(window.id)}
196
196
  focused={window.id === focused}
197
- // One window at a time: the rest stay mounted, keeping their state, and simply wait offstage.
198
- hidden={mode === 'fullscreen' && window.id !== focused}
197
+ // Offstage, not gone: it keeps its DOM, so nothing in it is lost. One window at a time on
198
+ // touch, and minimizing, are the same thing to everything below this line.
199
+ hidden={(mode === 'fullscreen' && window.id !== focused) || isMinimized(state, window.id)}
200
+ minimized={isMinimized(state, window.id)}
199
201
  title={title(window.id)}
200
202
  note={note?.(window.id)}
201
203
  info={info?.(window.id)}
@@ -227,10 +229,29 @@ const WindowContent = memo(function WindowContent({ id, render, loading, failed
227
229
  )
228
230
  })
229
231
 
232
+ /*
233
+ * What each dot does, shown on the way to pressing one: the glyphs come up together when the
234
+ * pointer arrives over the cluster, as a Mac's do, so a control is never chosen on colour alone.
235
+ * Drawn rather than lettered: a font puts its own cross where it likes, and these are 12px wide.
236
+ */
237
+ const GLYPHS = {
238
+ close: <path d="M4.1 4.1 7.9 7.9M7.9 4.1 4.1 7.9" />,
239
+ minimize: <path d="M3.6 6h4.8" />,
240
+ mode: <path d="M3.8 3.8h2.6L3.8 6.4zM8.2 8.2H5.6L8.2 5.6z" fill="currentColor" stroke="none" />,
241
+ }
242
+
243
+ const Glyph = ({ control }: { readonly control: keyof typeof GLYPHS }) => (
244
+ <svg className="desk-control-glyph" viewBox="0 0 12 12" aria-hidden="true">
245
+ {GLYPHS[control]}
246
+ </svg>
247
+ )
248
+
230
249
  interface WindowViewProps {
231
250
  readonly window: DeskWindow
232
251
  readonly layout: DeskLayout
233
252
  readonly hidden?: boolean
253
+ /** Hidden because it was minimized, rather than because another window has the screen. */
254
+ readonly minimized?: boolean
234
255
  readonly depth: number
235
256
  readonly focused: boolean
236
257
  readonly title: ReactNode
@@ -244,7 +265,10 @@ interface WindowViewProps {
244
265
  /** Moving by the title bar, or resizing from the corner or from the left, right or bottom edge. */
245
266
  /** Lays every open window out at once: what Window → Arrange does, and what a shrunken desk needs. */
246
267
  function arrangeNow(desk: Desk, element: HTMLElement) {
247
- const { windows, stack } = desk.getState()
268
+ const state = desk.getState()
269
+ // Minimized windows are off the desk: laying one out would put it back without being asked.
270
+ const windows = onDesk(state)
271
+ const stack = state.stack.filter(id => !isMinimized(state, id))
248
272
  const [focused, previous] = [stack.at(-1), stack.at(-2)]
249
273
  if (windows.length === 1) {
250
274
  if (focused) desk.fill(focused)
@@ -285,7 +309,7 @@ function reshape(gesture: Gesture, origin: Frame, dx: number, dy: number): Frame
285
309
  }
286
310
  }
287
311
 
288
- function WindowView({ window, layout, hidden, depth, focused, title, note, info, actions, limits, children }: WindowViewProps) {
312
+ function WindowView({ window, layout, hidden, minimized, depth, focused, title, note, info, actions, limits, children }: WindowViewProps) {
289
313
  const desk = useDesk()
290
314
  // While dragging, the frame lives here and commits once on release, so a drag
291
315
  // re-renders one window rather than notifying every subscriber per pixel.
@@ -393,6 +417,7 @@ function WindowView({ window, layout, hidden, depth, focused, title, note, info,
393
417
  className="desk-window"
394
418
  data-mode={layout === 'fullscreen' ? 'fullscreen' : live ? 'floating' : window.mode}
395
419
  data-hidden={hidden || undefined}
420
+ data-minimized={minimized || undefined}
396
421
  inert={hidden || undefined}
397
422
  data-focused={focused || undefined}
398
423
  data-dragging={live ? true : undefined}
@@ -411,9 +436,19 @@ function WindowView({ window, layout, hidden, depth, focused, title, note, info,
411
436
  data-draggable={layout === 'desktop' || undefined}
412
437
  >
413
438
  <div className="desk-controls">
414
- <button type="button" className="desk-control" data-control="close" aria-label="Close" onClick={() => desk.close(window.id)} />
439
+ <button type="button" className="desk-control" data-control="close" aria-label="Close" onClick={() => desk.close(window.id)}>
440
+ <Glyph control="close" />
441
+ </button>
415
442
  {layout === 'desktop' && (
416
- <button type="button" className="desk-control" data-control="mode" aria-label="Zoom" onClick={zoom} />
443
+ <>
444
+ {/* Off the desk, still open: nothing is unmounted, so nothing inside it is lost. */}
445
+ <button type="button" className="desk-control" data-control="minimize" aria-label="Minimize" onClick={() => desk.minimize(window.id)}>
446
+ <Glyph control="minimize" />
447
+ </button>
448
+ <button type="button" className="desk-control" data-control="mode" aria-label="Zoom" onClick={zoom}>
449
+ <Glyph control="mode" />
450
+ </button>
451
+ </>
417
452
  )}
418
453
  </div>
419
454
  <h2 id={titleId} className="desk-title">
@@ -1,6 +1,6 @@
1
1
  import { useEffect, useId, useRef, useState } from 'react'
2
2
  import type { KeyboardEvent, ReactNode } from 'react'
3
- import { focusedId, instancesOf, windowType } from '../core/desk.js'
3
+ import { focusedId, instancesOf, isMinimized, windowType } from '../core/desk.js'
4
4
  import type { DeskState, WindowId } from '../core/types.js'
5
5
  import { useDesk, useDeskState } from './context.js'
6
6
  import { useContextMenu } from './contextMenu.js'
@@ -73,10 +73,13 @@ const windowOf = (item: DockItem) => item.window ?? item.id
73
73
 
74
74
  const statusOf = (state: DeskState, items: readonly DockItem[]) => {
75
75
  const key = focusedId(state)
76
+ const windows = items.flatMap(i => instancesOf(state, windowOf(i)))
76
77
  return {
77
78
  // Any window of this kind counts: a second query window is still Query, running.
78
- running: items.some(i => instancesOf(state, windowOf(i)).length > 0),
79
+ running: windows.length > 0,
79
80
  focused: items.some(i => key !== null && windowType(key) === windowOf(i)),
81
+ // Open, and none of it on the desk. One window of a kind still showing means the item is not.
82
+ minimized: windows.length > 0 && windows.every(w => isMinimized(state, w.id)),
80
83
  }
81
84
  }
82
85
 
@@ -165,7 +168,7 @@ interface DockButtonProps {
165
168
 
166
169
  function DockButton({ item, tabIndex, pins }: DockButtonProps) {
167
170
  const desk = useDesk()
168
- const { running, focused } = statusOf(useDeskState(), [item])
171
+ const { running, focused, minimized } = statusOf(useDeskState(), [item])
169
172
  const context = useContextMenu({ label: item.label, items: item.contextMenu ?? (() => []), disabled: !item.contextMenu })
170
173
  // Only kept items take drops, so something carried lands among them and never splits the fixed ones.
171
174
  const droppable = Boolean(pins && item.movable)
@@ -182,6 +185,7 @@ function DockButton({ item, tabIndex, pins }: DockButtonProps) {
182
185
  aria-label={item.label}
183
186
  data-running={running || undefined}
184
187
  data-focused={focused || undefined}
188
+ data-minimized={minimized || undefined}
185
189
  // An item with a menu stays reachable while it cannot open, so it can still be removed.
186
190
  disabled={item.disabled && !item.contextMenu}
187
191
  aria-disabled={(item.disabled && item.contextMenu && true) || undefined}
@@ -219,7 +223,7 @@ interface StackButtonProps {
219
223
  function StackButton({ stack, tabIndex, open, setOpenStack }: StackButtonProps) {
220
224
  const desk = useDesk()
221
225
  const state = useDeskState()
222
- const { running, focused } = statusOf(state, stack.items)
226
+ const { running, focused, minimized } = statusOf(state, stack.items)
223
227
  const onOpenChange = (next: boolean) => setOpenStack(next ? stack.id : null)
224
228
  const button = useRef<HTMLButtonElement>(null)
225
229
  const panel = useRef<HTMLDivElement>(null)
@@ -264,6 +268,7 @@ function StackButton({ stack, tabIndex, open, setOpenStack }: StackButtonProps)
264
268
  aria-controls={open ? panelId : undefined}
265
269
  data-running={running || undefined}
266
270
  data-focused={focused || undefined}
271
+ data-minimized={minimized || undefined}
267
272
  onClick={() => onOpenChange(!open)}
268
273
  >
269
274
  <span className="desk-dock-icon" aria-hidden="true">
@@ -307,6 +312,7 @@ function StackButton({ stack, tabIndex, open, setOpenStack }: StackButtonProps)
307
312
  data-rove
308
313
  className="desk-stack-item"
309
314
  data-running={status.running || undefined}
315
+ data-minimized={status.minimized || undefined}
310
316
  disabled={item.disabled}
311
317
  onClick={() => select(item)}
312
318
  >
@@ -2,7 +2,7 @@ import { useEffect, useId, useMemo, useRef, useState } from 'react'
2
2
  import type { KeyboardEvent as ReactKeyboardEvent, ReactNode } from 'react'
3
3
  import { canPerform, perform } from '../core/commands.js'
4
4
  import type { CommandId } from '../core/commands.js'
5
- import { focusedId } from '../core/desk.js'
5
+ import { focusedId, isMinimized } from '../core/desk.js'
6
6
  import { bindShortcuts, formatShortcut, isApplePlatform } from '../core/shortcuts.js'
7
7
  import type { Desk } from '../core/desk.js'
8
8
  import type { DeskState, WindowId } from '../core/types.js'
@@ -59,10 +59,19 @@ export const menuAction = (label: string, onSelect: () => void, options: Omit<Op
59
59
  export const menuSeparator = (): MenuItem => ({ type: 'separator' })
60
60
  export const menuHeader = (label: string): MenuItem => ({ type: 'header', label })
61
61
 
62
- /** One item per open window, the key window checked — what a Window menu lists at its foot. */
62
+ /**
63
+ * One item per open window, the key window checked — what a Window menu lists at its foot. A
64
+ * minimized window says so and is still listed: the menu is where someone looks for a window
65
+ * they cannot see, and choosing it brings it back.
66
+ */
63
67
  export function windowMenuItems(state: DeskState, focus: (id: WindowId) => void, title: (id: WindowId) => string): MenuItem[] {
64
68
  const key = focusedId(state)
65
- return state.windows.map(w => menuAction(title(w.id), () => focus(w.id), { checked: w.id === key }))
69
+ return state.windows.map(w =>
70
+ menuAction(title(w.id), () => focus(w.id), {
71
+ checked: w.id === key,
72
+ ...(isMinimized(state, w.id) ? { detail: 'Minimized' } : {}),
73
+ }),
74
+ )
66
75
  }
67
76
 
68
77
  export interface MenuBarProps {
@@ -31,6 +31,10 @@ const PROTOCOL = 1
31
31
  * the page is, for an app that decides its own colours but wants to know; `tokens` are custom
32
32
  * properties the bridge sets on the app's own root, so an app that reads them gets the desk's
33
33
  * colours with nothing to write.
34
+ *
35
+ * The mode is stamped as `data-theme`, which is the attribute desk's own stylesheet reads and the
36
+ * one the HIG tells app authors to honour — so a page already written that way follows the desk
37
+ * with nothing added to it.
34
38
  */
35
39
  export interface AppTheme {
36
40
  readonly mode: 'light' | 'dark'
@@ -188,7 +192,7 @@ export const APP_BRIDGE_SCRIPT = `(() => {
188
192
  if (!theme) return;
189
193
  desk.theme = theme;
190
194
  const root = document.documentElement;
191
- root.dataset.deskTheme = theme.mode;
195
+ root.dataset.theme = theme.mode;
192
196
  root.style.colorScheme = theme.mode;
193
197
  for (const [name, value] of Object.entries(theme.tokens || {})) root.style.setProperty(name, value);
194
198
  themed.forEach(h => h(theme));
@@ -4,6 +4,7 @@ import { createBus } from '../core/events.js'
4
4
  import type { Bus } from '../core/events.js'
5
5
  import { createDesk } from '../core/desk.js'
6
6
  import { DragProvider } from './dragContext.js'
7
+ import { missingProvider } from './missingProvider.js'
7
8
  import type { Desk } from '../core/desk.js'
8
9
  import type { DeskOptions, DeskState, WindowId } from '../core/types.js'
9
10
 
@@ -37,7 +38,7 @@ export const useOptionalDesk = (): Desk | null => useContext(DeskContext)
37
38
 
38
39
  export function useDesk(): Desk {
39
40
  const desk = useContext(DeskContext)
40
- if (!desk) throw new Error('useDesk must be used inside a <DeskProvider>')
41
+ if (!desk) throw missingProvider('useDesk', 'a <DeskProvider>')
41
42
  return desk
42
43
  }
43
44
 
@@ -0,0 +1,52 @@
1
+ import { useEffect, useState } from 'react'
2
+ import { focusedId } from '../core/desk.js'
3
+ import type { WindowId } from '../core/types.js'
4
+ import { useDeskState } from './context.js'
5
+
6
+ /*
7
+ * The browser's title bar, and every tab strip and window switcher that reads it, says what
8
+ * is in front — as an application's title bar does. The frontmost window's name comes first,
9
+ * because that is the part a narrow tab still shows.
10
+ *
11
+ * The app's own name is not asked for twice: left out, it is whatever the document was
12
+ * already called, which is the <title> in the page's HTML. That stays the one place the app
13
+ * is named, and the title is put back as it was when the desk goes away — a desk mounted in
14
+ * a larger page must not leave the last window's name behind it.
15
+ *
16
+ * A window's name is wanted as a string here, not as the ReactNode <Desktop> renders: an
17
+ * icon beside a label is a title bar's business, and `document.title` is text. It is the same
18
+ * signature `windowMenuItems` asks for, so an app with a Window menu already has one.
19
+ */
20
+
21
+ export interface DocumentTitleOptions {
22
+ /** The app's name, kept after the window's: `Rides — Garage`. Defaults to the document's own title. */
23
+ readonly app?: string
24
+ /** What goes between the two names. */
25
+ readonly separator?: string
26
+ }
27
+
28
+ const DEFAULT_SEPARATOR = ' — '
29
+
30
+ /** Keeps `document.title` on the frontmost window: `Rides — Garage`, and `Garage` with nothing open. */
31
+ export function useDocumentTitle(title: (id: WindowId) => string, options: DocumentTitleOptions = {}): void {
32
+ const { app, separator = DEFAULT_SEPARATOR } = options
33
+ const state = useDeskState()
34
+ const id = focusedId(state)
35
+
36
+ // What the page was called before the desk took it over: the app's name by default, and what
37
+ // is put back on the way out. Read once, so a title this hook set is never mistaken for it.
38
+ const [original] = useState(() => (typeof document === 'undefined' ? '' : document.title))
39
+ const base = app ?? original
40
+
41
+ const front = id === null ? '' : title(id)
42
+ const next = front && base ? `${front}${separator}${base}` : front || base
43
+
44
+ useEffect(() => {
45
+ if (next) document.title = next
46
+ }, [next])
47
+
48
+ // Only on the way out: a restore that ran between renders would flicker the real title away.
49
+ useEffect(() => () => {
50
+ document.title = original
51
+ }, [original])
52
+ }
@@ -8,6 +8,7 @@ import type { WindowId } from '../core/types.js'
8
8
  import { useCommand, useShortcuts } from './commands.js'
9
9
  import { Composer } from './conversation.js'
10
10
  import { useFocusTrap, useLayer } from './layers.js'
11
+ import { missingProvider } from './missingProvider.js'
11
12
  import { BusContext, useDesk, useDeskState, useWindowId } from './context.js'
12
13
 
13
14
  /** Supplies a bus of your own, or shares one across several desks. A desk already has one. */
@@ -18,7 +19,7 @@ export function BusProvider({ bus, children }: { readonly bus?: Bus; readonly ch
18
19
 
19
20
  export function useBus(): Bus {
20
21
  const bus = useContext(BusContext)
21
- if (!bus) throw new Error('useBus must be used inside a <DeskProvider> or <BusProvider>')
22
+ if (!bus) throw missingProvider('useBus', 'a <DeskProvider> or <BusProvider>')
22
23
  return bus
23
24
  }
24
25
 
@@ -19,6 +19,8 @@ export { useCanPerform, useCommand, usePerform, useShortcuts } from './commands.
19
19
  export type { UseCommandOptions } from './commands.js'
20
20
  export { Desktop } from './Desktop.js'
21
21
  export type { DeskLayout, DesktopProps } from './Desktop.js'
22
+ export { useDocumentTitle } from './documentTitle.js'
23
+ export type { DocumentTitleOptions } from './documentTitle.js'
22
24
  export { lazyWindow } from './windowBoundary.js'
23
25
  export type { WindowFailed, WindowLoading } from './windowBoundary.js'
24
26
  export { Button, Checkbox, ChoiceGroup, RadioGroup, SegmentedControl, Slider, TextField, Toggle } from './controls.js'
@@ -0,0 +1,23 @@
1
+ /*
2
+ * A hook that needs a provider names a hook its author never called: `useDesk`, reached through
3
+ * `useDeskState`, reached through their own `useDocumentTitle`. An error boundary shows the
4
+ * message and not the stack, so the message carries the first frame outside the accessors that
5
+ * only re-expose a context — the line that has to change.
6
+ */
7
+ const ACCESSORS = new Set(['caller', 'missingProvider', 'useDesk', 'useOptionalDesk', 'useDeskState', 'useBus', 'useToast'])
8
+
9
+ /** `at Object.name (loc)` and `name@loc`: V8 or not, either engine can be the one that throws. */
10
+ const FRAME = /^\s*(?:at\s+(?:[\w$.]*?([\w$]+)\s+\()?|([\w$]+)@)(.+?)\)?$/
11
+
12
+ function caller(): string {
13
+ for (const line of (new Error().stack ?? '').split('\n')) {
14
+ const frame = FRAME.exec(line)
15
+ const name = frame?.[1] ?? frame?.[2]
16
+ if (frame && name && !ACCESSORS.has(name)) return ` — called from ${name} (${frame[3]})`
17
+ }
18
+ return ''
19
+ }
20
+
21
+ /** What a context hook throws when its provider is missing, pointed at the caller. */
22
+ export const missingProvider = (hook: string, provider: string): Error =>
23
+ new Error(`${hook} must be used inside ${provider}${caller()}`)
@@ -5,6 +5,7 @@ import { windowElement } from '../core/commands.js'
5
5
  import { Button } from './controls.js'
6
6
  import { useWindowId } from './context.js'
7
7
  import { useFocusTrap, useLayer } from './layers.js'
8
+ import { missingProvider } from './missingProvider.js'
8
9
 
9
10
  /*
10
11
  * Interruption, least to most (HIG.md §7): a popover belongs to one control, a
@@ -196,7 +197,7 @@ export function ToastProvider({ children }: { readonly children: ReactNode }) {
196
197
 
197
198
  export function useToast(): ToastApi {
198
199
  const api = useContext(ToastContext)
199
- if (!api) throw new Error('useToast must be used inside a <ToastProvider>')
200
+ if (!api) throw missingProvider('useToast', 'a <ToastProvider>')
200
201
  return api
201
202
  }
202
203