@liberation-data/desk 0.6.0 → 0.8.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 (61) hide show
  1. package/README.md +99 -1
  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 +2 -2
  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 +14 -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 +5 -0
  22. package/dist/desk.css +12 -0
  23. package/dist/react/Desktop.d.ts +10 -3
  24. package/dist/react/Desktop.d.ts.map +1 -1
  25. package/dist/react/Desktop.js +30 -13
  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 +9 -2
  35. package/dist/react/appFrame.d.ts.map +1 -1
  36. package/dist/react/appFrame.js +26 -5
  37. package/dist/react/appFrame.js.map +1 -1
  38. package/dist/react/documentTitle.d.ts +10 -0
  39. package/dist/react/documentTitle.d.ts.map +1 -0
  40. package/dist/react/documentTitle.js +25 -0
  41. package/dist/react/documentTitle.js.map +1 -0
  42. package/dist/react/index.d.ts +2 -0
  43. package/dist/react/index.d.ts.map +1 -1
  44. package/dist/react/index.js +1 -0
  45. package/dist/react/index.js.map +1 -1
  46. package/llms.txt +38 -5
  47. package/package.json +16 -1
  48. package/src/core/commands.ts +9 -4
  49. package/src/core/desk.ts +61 -7
  50. package/src/core/index.ts +3 -0
  51. package/src/core/location.ts +15 -7
  52. package/src/core/types.ts +15 -1
  53. package/src/css/dock.css +6 -0
  54. package/src/css/tokens.css +1 -0
  55. package/src/css/windows.css +5 -0
  56. package/src/react/Desktop.tsx +48 -14
  57. package/src/react/Dock.tsx +10 -4
  58. package/src/react/MenuBar.tsx +12 -3
  59. package/src/react/appFrame.tsx +52 -5
  60. package/src/react/documentTitle.ts +52 -0
  61. package/src/react/index.ts +2 -0
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 {
@@ -51,3 +57,11 @@ export interface OpenOptions {
51
57
  readonly mode?: WindowMode
52
58
  readonly frame?: Frame
53
59
  }
60
+
61
+ /** How big a window may be. Everything is optional; a window with no limits takes what it is given. */
62
+ export interface WindowLimits {
63
+ readonly minWidth?: number
64
+ readonly minHeight?: number
65
+ readonly maxWidth?: number
66
+ readonly maxHeight?: number
67
+ }
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;
@@ -105,6 +105,11 @@
105
105
  background: var(--desk-close);
106
106
  }
107
107
 
108
+ .desk-window[data-focused] .desk-control[data-control="minimize"],
109
+ .desk-control[data-control="minimize"]:hover {
110
+ background: var(--desk-minimize);
111
+ }
112
+
108
113
  .desk-window[data-focused] .desk-control[data-control="mode"],
109
114
  .desk-control[data-control="mode"]:hover {
110
115
  background: var(--desk-mode);
@@ -1,9 +1,9 @@
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
- import type { DeskWindow, Frame, WindowId } from '../core/types.js'
6
+ import type { DeskWindow, Frame, WindowId, WindowLimits } from '../core/types.js'
7
7
  import { arrangement } from './arrange.js'
8
8
  import { InfoTip } from './infoTip.js'
9
9
  import { useDesk, useDeskState, WindowContext } from './context.js'
@@ -21,6 +21,13 @@ export interface DesktopProps {
21
21
  * gives way to the actions and disappears before the title does when the window is narrow.
22
22
  */
23
23
  readonly note?: (id: WindowId) => ReactNode
24
+ /**
25
+ * How big this window may be. Most windows want the whole desk when filled and whatever they are
26
+ * dragged to when free, which is what `null` says. A window whose content has a natural size —
27
+ * an About box, a small form — says so, and is never stretched past it, however it is filled,
28
+ * arranged or dragged.
29
+ */
30
+ readonly limits?: (id: WindowId) => WindowLimits | null
24
31
  /**
25
32
  * What this window is, behind an (i) at the end of its title bar — the same place in every window,
26
33
  * so a person who wonders what they are looking at always knows where to ask. Explanation only: a
@@ -29,7 +36,7 @@ export interface DesktopProps {
29
36
  readonly info?: (id: WindowId) => ReactNode
30
37
  /** Window-wide controls on the trailing side of the title bar. One or two; more belongs in a toolbar. */
31
38
  readonly actions?: (id: WindowId) => ReactNode
32
- /** Shown when no window is open. */
39
+ /** Shown when no window is on the desk — none open, or every one of them minimized. */
33
40
  readonly empty?: ReactNode
34
41
  /** Shown inside a window while its code loads. Default: a quiet "Loading…". */
35
42
  readonly loading?: WindowLoading
@@ -84,7 +91,7 @@ const halfFrame = (stage: HTMLElement, side: 'start' | 'end'): Frame => {
84
91
  return { x: side === 'start' ? area.x : area.x + width + area.gap, y: area.y, width, height: area.height }
85
92
  }
86
93
 
87
- export function Desktop({ renderWindow, title, note, info, actions, empty, loading, failed, layout = 'auto', className }: DesktopProps) {
94
+ export function Desktop({ renderWindow, title, note, info, actions, limits, empty, loading, failed, layout = 'auto', className }: DesktopProps) {
88
95
  const desk = useDesk()
89
96
  const state = useDeskState()
90
97
  const stage = useRef<HTMLDivElement>(null)
@@ -152,7 +159,7 @@ export function Desktop({ renderWindow, title, note, info, actions, empty, loadi
152
159
  () => arrangeNow(desk, element),
153
160
  {
154
161
  enabled: () => {
155
- const { windows } = desk.getState()
162
+ const windows = onDesk(desk.getState())
156
163
  return windows.length > 1 || windows.some(w => w.mode === 'floating')
157
164
  },
158
165
  },
@@ -179,7 +186,7 @@ export function Desktop({ renderWindow, title, note, info, actions, empty, loadi
179
186
  data-layout={mode}
180
187
  {...{ [STAGE_ATTRIBUTE]: '' }}
181
188
  >
182
- {state.windows.length === 0 && empty}
189
+ {onDesk(state).length === 0 && empty}
183
190
  {state.windows.map(window => (
184
191
  <WindowView
185
192
  key={window.id}
@@ -187,11 +194,14 @@ export function Desktop({ renderWindow, title, note, info, actions, empty, loadi
187
194
  layout={mode}
188
195
  depth={state.stack.indexOf(window.id)}
189
196
  focused={window.id === focused}
190
- // One window at a time: the rest stay mounted, keeping their state, and simply wait offstage.
191
- 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)}
192
201
  title={title(window.id)}
193
202
  note={note?.(window.id)}
194
203
  info={info?.(window.id)}
204
+ limits={limits?.(window.id) ?? null}
195
205
  actions={actions?.(window.id)}
196
206
  >
197
207
  {/* Memoised on the id and the render function: moving or focusing a window
@@ -223,11 +233,14 @@ interface WindowViewProps {
223
233
  readonly window: DeskWindow
224
234
  readonly layout: DeskLayout
225
235
  readonly hidden?: boolean
236
+ /** Hidden because it was minimized, rather than because another window has the screen. */
237
+ readonly minimized?: boolean
226
238
  readonly depth: number
227
239
  readonly focused: boolean
228
240
  readonly title: ReactNode
229
241
  readonly note?: ReactNode
230
242
  readonly info?: ReactNode
243
+ readonly limits?: WindowLimits | null
231
244
  readonly actions?: ReactNode
232
245
  readonly children: ReactNode
233
246
  }
@@ -235,7 +248,10 @@ interface WindowViewProps {
235
248
  /** Moving by the title bar, or resizing from the corner or from the left, right or bottom edge. */
236
249
  /** Lays every open window out at once: what Window → Arrange does, and what a shrunken desk needs. */
237
250
  function arrangeNow(desk: Desk, element: HTMLElement) {
238
- const { windows, stack } = desk.getState()
251
+ const state = desk.getState()
252
+ // Minimized windows are off the desk: laying one out would put it back without being asked.
253
+ const windows = onDesk(state)
254
+ const stack = state.stack.filter(id => !isMinimized(state, id))
239
255
  const [focused, previous] = [stack.at(-1), stack.at(-2)]
240
256
  if (windows.length === 1) {
241
257
  if (focused) desk.fill(focused)
@@ -276,7 +292,7 @@ function reshape(gesture: Gesture, origin: Frame, dx: number, dy: number): Frame
276
292
  }
277
293
  }
278
294
 
279
- function WindowView({ window, layout, hidden, depth, focused, title, note, info, actions, children }: WindowViewProps) {
295
+ function WindowView({ window, layout, hidden, minimized, depth, focused, title, note, info, actions, limits, children }: WindowViewProps) {
280
296
  const desk = useDesk()
281
297
  // While dragging, the frame lives here and commits once on release, so a drag
282
298
  // re-renders one window rather than notifying every subscriber per pixel.
@@ -355,12 +371,25 @@ function WindowView({ window, layout, hidden, depth, focused, title, note, info,
355
371
  }
356
372
 
357
373
  const frame = layout === 'desktop' ? (live ?? (window.mode === 'floating' ? window.frame : null)) : null
374
+ /*
375
+ * A window that says how big it may be is capped in the browser rather than in the state: filling
376
+ * the desk, being arranged into a cell and being dragged wider all end at the same place, and the
377
+ * window sits in the middle of whatever room it was given rather than pinned to a corner of it.
378
+ */
379
+ const capped: CSSProperties = {
380
+ ...(limits?.maxWidth ? { maxWidth: limits.maxWidth } : {}),
381
+ ...(limits?.maxHeight ? { maxHeight: limits.maxHeight } : {}),
382
+ ...(limits?.minWidth ? { minWidth: limits.minWidth } : {}),
383
+ ...(limits?.minHeight ? { minHeight: limits.minHeight } : {}),
384
+ // Filled and capped: it sits in the middle of the room, not in the corner of it.
385
+ ...(limits && (limits.maxWidth || limits.maxHeight) && !frame ? { margin: 'auto' } : {}),
386
+ }
358
387
  const style: CSSProperties | undefined =
359
388
  layout !== 'desktop'
360
- ? undefined
389
+ ? capped
361
390
  : frame
362
- ? { left: frame.x, top: frame.y, width: frame.width, height: frame.height, zIndex: 10 + depth }
363
- : { zIndex: 10 + depth }
391
+ ? { left: frame.x, top: frame.y, width: frame.width, height: frame.height, zIndex: 10 + depth, ...capped }
392
+ : { zIndex: 10 + depth, ...capped }
364
393
 
365
394
  return (
366
395
  <WindowContext.Provider value={context}>
@@ -371,6 +400,7 @@ function WindowView({ window, layout, hidden, depth, focused, title, note, info,
371
400
  className="desk-window"
372
401
  data-mode={layout === 'fullscreen' ? 'fullscreen' : live ? 'floating' : window.mode}
373
402
  data-hidden={hidden || undefined}
403
+ data-minimized={minimized || undefined}
374
404
  inert={hidden || undefined}
375
405
  data-focused={focused || undefined}
376
406
  data-dragging={live ? true : undefined}
@@ -391,7 +421,11 @@ function WindowView({ window, layout, hidden, depth, focused, title, note, info,
391
421
  <div className="desk-controls">
392
422
  <button type="button" className="desk-control" data-control="close" aria-label="Close" onClick={() => desk.close(window.id)} />
393
423
  {layout === 'desktop' && (
394
- <button type="button" className="desk-control" data-control="mode" aria-label="Zoom" onClick={zoom} />
424
+ <>
425
+ {/* Off the desk, still open: nothing is unmounted, so nothing inside it is lost. */}
426
+ <button type="button" className="desk-control" data-control="minimize" aria-label="Minimize" onClick={() => desk.minimize(window.id)} />
427
+ <button type="button" className="desk-control" data-control="mode" aria-label="Zoom" onClick={zoom} />
428
+ </>
395
429
  )}
396
430
  </div>
397
431
  <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 {
@@ -20,8 +20,31 @@ import { useBus } from './events.js'
20
20
  /** The protocol version. Every message carries it, so a mismatch is ignored rather than misread. */
21
21
  const PROTOCOL = 1
22
22
 
23
+ /*
24
+ * THE HOST'S LOOK, OFFERED TO THE APP.
25
+ *
26
+ * An app runs in a frame of its own, so by default it wears whatever its author hard-coded: a white
27
+ * page inside a dark desk, a dark one inside a light desk. Switching the desk's theme should carry
28
+ * the app too — the person switched the whole thing, not the chrome around it.
29
+ *
30
+ * So the host offers its palette, and the app takes it or ignores it. `mode` says which way round
31
+ * the page is, for an app that decides its own colours but wants to know; `tokens` are custom
32
+ * properties the bridge sets on the app's own root, so an app that reads them gets the desk's
33
+ * colours with nothing to write.
34
+ *
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.
38
+ */
39
+ export interface AppTheme {
40
+ readonly mode: 'light' | 'dark'
41
+ /** Custom properties, named as they will be set: `{ '--sb-bg-dark': '#0b0f1a' }`. */
42
+ readonly tokens: Readonly<Record<string, string>>
43
+ }
44
+
23
45
  type HostMessage =
24
- | { readonly desk: 1; readonly kind: 'hello'; readonly window: string | null; readonly listens: readonly string[]; readonly says: readonly string[] }
46
+ | { readonly desk: 1; readonly kind: 'hello'; readonly window: string | null; readonly listens: readonly string[]; readonly says: readonly string[]; readonly theme: AppTheme | null }
47
+ | { readonly desk: 1; readonly kind: 'theme'; readonly theme: AppTheme }
25
48
  | { readonly desk: 1; readonly kind: 'event'; readonly topic: string; readonly payload: unknown; readonly from: string | null }
26
49
  | { readonly desk: 1; readonly kind: 'drop'; readonly type: string; readonly payload: unknown; readonly from: string | null }
27
50
 
@@ -43,6 +66,8 @@ export interface AppFrameProps {
43
66
  readonly listens?: readonly string[]
44
67
  /** Topics the app may say. Anything else it publishes is dropped. Default: none. */
45
68
  readonly says?: readonly string[]
69
+ /** The desk's own look, offered to the app: it is applied to the app's root and re-sent on change. */
70
+ readonly theme?: AppTheme
46
71
  /** What may be dropped on it. Default: nothing. */
47
72
  readonly accepts?: Accepts
48
73
  /** Windows the app may ask to open. Default: none. */
@@ -61,6 +86,7 @@ export function AppFrame({
61
86
  srcDoc,
62
87
  listens = [],
63
88
  says = [],
89
+ theme,
64
90
  accepts,
65
91
  opens = [],
66
92
  sandbox = 'allow-scripts allow-forms',
@@ -79,8 +105,8 @@ export function AppFrame({
79
105
  frame.current?.contentWindow?.postMessage(message, '*')
80
106
  }
81
107
 
82
- const latest = useRef({ listens, says, opens, windowId })
83
- latest.current = { listens, says, opens, windowId }
108
+ const latest = useRef({ listens, says, opens, windowId, theme })
109
+ latest.current = { listens, says, opens, windowId, theme }
84
110
 
85
111
  // What the app says: only from this frame, only on topics it was granted.
86
112
  useEffect(() => {
@@ -89,7 +115,7 @@ export function AppFrame({
89
115
  const message = event.data
90
116
  const granted = latest.current
91
117
  if (message.kind === 'ready') {
92
- post({ desk: PROTOCOL, kind: 'hello', window: granted.windowId, listens: granted.listens, says: granted.says })
118
+ post({ desk: PROTOCOL, kind: 'hello', window: granted.windowId, listens: granted.listens, says: granted.says, theme: granted.theme ?? null })
93
119
  } else if (message.kind === 'publish') {
94
120
  if (typeof message.topic === 'string' && granted.says.some(pattern => topicMatches(pattern, message.topic))) {
95
121
  bus.publish(message.topic, message.payload, { from: granted.windowId })
@@ -102,6 +128,14 @@ export function AppFrame({
102
128
  return () => window.removeEventListener('message', onMessage)
103
129
  }, [bus, desk])
104
130
 
131
+ // The desk's look, again, whenever it changes: a person switching theme switches the app too.
132
+ const themeKey = theme ? `${theme.mode}|${Object.entries(theme.tokens).map(([k, v]) => `${k}:${v}`).join(';')}` : ''
133
+ useEffect(() => {
134
+ if (!latest.current.theme) return
135
+ post({ desk: PROTOCOL, kind: 'theme', theme: latest.current.theme })
136
+ // eslint-disable-next-line react-hooks/exhaustive-deps
137
+ }, [themeKey])
138
+
105
139
  // What the app hears: only the topics it was granted, and never its own words back.
106
140
  const listenKey = listens.join('|')
107
141
  useEffect(() => {
@@ -154,10 +188,22 @@ export const APP_BRIDGE_SCRIPT = `(() => {
154
188
  const handlers = new Map();
155
189
  const drops = [];
156
190
  const send = message => parent.postMessage(Object.assign({ desk: ${PROTOCOL} }, message), '*');
191
+ const applyTheme = theme => {
192
+ if (!theme) return;
193
+ desk.theme = theme;
194
+ const root = document.documentElement;
195
+ root.dataset.theme = theme.mode;
196
+ root.style.colorScheme = theme.mode;
197
+ for (const [name, value] of Object.entries(theme.tokens || {})) root.style.setProperty(name, value);
198
+ themed.forEach(h => h(theme));
199
+ };
200
+ const themed = [];
157
201
  const desk = {
158
202
  window: null,
159
203
  listens: [],
160
204
  says: [],
205
+ theme: null,
206
+ onTheme(handler) { themed.push(handler); if (desk.theme) handler(desk.theme); },
161
207
  on(topic, handler) { (handlers.get(topic) || handlers.set(topic, []).get(topic)).push(handler); },
162
208
  onDrop(handler) { drops.push(handler); },
163
209
  publish(topic, payload) { send({ kind: 'publish', topic, payload }); },
@@ -168,7 +214,8 @@ export const APP_BRIDGE_SCRIPT = `(() => {
168
214
  addEventListener('message', event => {
169
215
  if (event.source !== parent || !event.data || event.data.desk !== ${PROTOCOL}) return;
170
216
  const message = event.data;
171
- if (message.kind === 'hello') { desk.window = message.window; desk.listens = message.listens; desk.says = message.says; }
217
+ if (message.kind === 'hello') { desk.window = message.window; desk.listens = message.listens; desk.says = message.says; applyTheme(message.theme); }
218
+ if (message.kind === 'theme') applyTheme(message.theme);
172
219
  if (message.kind === 'event') for (const [pattern, list] of handlers) if (matches(pattern, message.topic)) list.forEach(h => h(message));
173
220
  if (message.kind === 'drop') drops.forEach(h => h(message));
174
221
  });
@@ -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
+ }
@@ -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'