@liberation-data/desk 0.44.0 → 0.46.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 (116) hide show
  1. package/README.md +72 -9
  2. package/dist/core/desk.d.ts +2 -0
  3. package/dist/core/desk.d.ts.map +1 -1
  4. package/dist/core/desk.js +11 -2
  5. package/dist/core/desk.js.map +1 -1
  6. package/dist/core/hosted.d.ts +15 -0
  7. package/dist/core/hosted.d.ts.map +1 -1
  8. package/dist/core/hosted.js +28 -0
  9. package/dist/core/hosted.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/theme.d.ts +15 -0
  15. package/dist/core/theme.d.ts.map +1 -1
  16. package/dist/core/theme.js +73 -1
  17. package/dist/core/theme.js.map +1 -1
  18. package/dist/core/types.d.ts +5 -0
  19. package/dist/core/types.d.ts.map +1 -1
  20. package/dist/css/apps.css +17 -0
  21. package/dist/css/shell.css +89 -0
  22. package/dist/css/windows.css +28 -0
  23. package/dist/desk.css +45 -0
  24. package/dist/host/checkTheme.d.ts +2 -7
  25. package/dist/host/checkTheme.d.ts.map +1 -1
  26. package/dist/host/checkTheme.js +3 -27
  27. package/dist/host/checkTheme.js.map +1 -1
  28. package/dist/host/router.d.ts +1 -0
  29. package/dist/host/router.d.ts.map +1 -1
  30. package/dist/host/router.js +11 -0
  31. package/dist/host/router.js.map +1 -1
  32. package/dist/react/Desktop.d.ts.map +1 -1
  33. package/dist/react/Desktop.js +11 -14
  34. package/dist/react/Desktop.js.map +1 -1
  35. package/dist/react/app.d.ts +15 -3
  36. package/dist/react/app.d.ts.map +1 -1
  37. package/dist/react/app.js +20 -8
  38. package/dist/react/app.js.map +1 -1
  39. package/dist/react/appPanels.d.ts.map +1 -1
  40. package/dist/react/appPanels.js +44 -5
  41. package/dist/react/appPanels.js.map +1 -1
  42. package/dist/react/context.d.ts +16 -1
  43. package/dist/react/context.d.ts.map +1 -1
  44. package/dist/react/context.js +5 -3
  45. package/dist/react/context.js.map +1 -1
  46. package/dist/react/hostApp.d.ts +3 -1
  47. package/dist/react/hostApp.d.ts.map +1 -1
  48. package/dist/react/hostApp.js +3 -2
  49. package/dist/react/hostApp.js.map +1 -1
  50. package/dist/react/hosted.d.ts +10 -3
  51. package/dist/react/hosted.d.ts.map +1 -1
  52. package/dist/react/hosted.js +68 -8
  53. package/dist/react/hosted.js.map +1 -1
  54. package/dist/react/index.d.ts +3 -3
  55. package/dist/react/index.d.ts.map +1 -1
  56. package/dist/react/index.js +2 -2
  57. package/dist/react/index.js.map +1 -1
  58. package/dist/react/mediaQuery.d.ts +3 -0
  59. package/dist/react/mediaQuery.d.ts.map +1 -0
  60. package/dist/react/mediaQuery.js +11 -0
  61. package/dist/react/mediaQuery.js.map +1 -0
  62. package/dist/react/shell.d.ts +3 -1
  63. package/dist/react/shell.d.ts.map +1 -1
  64. package/dist/react/shell.js +2 -2
  65. package/dist/react/shell.js.map +1 -1
  66. package/dist/react/sidebar.d.ts +6 -1
  67. package/dist/react/sidebar.d.ts.map +1 -1
  68. package/dist/react/sidebar.js +6 -3
  69. package/dist/react/sidebar.js.map +1 -1
  70. package/dist/react/windowDescription.d.ts +7 -0
  71. package/dist/react/windowDescription.d.ts.map +1 -1
  72. package/dist/react/windowDescription.js +9 -0
  73. package/dist/react/windowDescription.js.map +1 -1
  74. package/dist/shell/HostOverview.d.ts +41 -0
  75. package/dist/shell/HostOverview.d.ts.map +1 -0
  76. package/dist/shell/HostOverview.js +219 -0
  77. package/dist/shell/HostOverview.js.map +1 -0
  78. package/dist/shell/HostShell.d.ts +3 -1
  79. package/dist/shell/HostShell.d.ts.map +1 -1
  80. package/dist/shell/HostShell.js +6 -4
  81. package/dist/shell/HostShell.js.map +1 -1
  82. package/dist/shell/index.d.ts +4 -0
  83. package/dist/shell/index.d.ts.map +1 -1
  84. package/dist/shell/index.js +2 -0
  85. package/dist/shell/index.js.map +1 -1
  86. package/dist/shell/zoom.d.ts +62 -0
  87. package/dist/shell/zoom.d.ts.map +1 -0
  88. package/dist/shell/zoom.js +116 -0
  89. package/dist/shell/zoom.js.map +1 -0
  90. package/llms.txt +18 -10
  91. package/package.json +12 -7
  92. package/src/core/desk.ts +11 -2
  93. package/src/core/hosted.ts +33 -0
  94. package/src/core/index.ts +1 -1
  95. package/src/core/theme.ts +69 -1
  96. package/src/core/types.ts +5 -0
  97. package/src/css/apps.css +17 -0
  98. package/src/css/shell.css +89 -0
  99. package/src/css/windows.css +28 -0
  100. package/src/host/checkTheme.ts +3 -25
  101. package/src/host/router.ts +12 -0
  102. package/src/react/Desktop.tsx +16 -21
  103. package/src/react/app.tsx +32 -8
  104. package/src/react/appPanels.tsx +64 -6
  105. package/src/react/context.tsx +20 -2
  106. package/src/react/hostApp.tsx +3 -2
  107. package/src/react/hosted.tsx +68 -8
  108. package/src/react/index.ts +3 -2
  109. package/src/react/mediaQuery.ts +18 -0
  110. package/src/react/shell.tsx +4 -2
  111. package/src/react/sidebar.tsx +10 -3
  112. package/src/react/windowDescription.ts +12 -1
  113. package/src/shell/HostOverview.tsx +317 -0
  114. package/src/shell/HostShell.tsx +8 -5
  115. package/src/shell/index.ts +18 -0
  116. package/src/shell/zoom.ts +151 -0
@@ -0,0 +1,317 @@
1
+ import { useEffect, useLayoutEffect, useRef, useState } from 'react'
2
+ import type { KeyboardEvent } from 'react'
3
+ import { applyTheme } from '../core/theme.js'
4
+ import type { DeskTheme } from '../core/theme.js'
5
+ import type { ShellInsets, ShellWorkspace } from './HostShell.js'
6
+ import { animate, linear, pageClock } from './motion.js'
7
+ import type { Animation } from './motion.js'
8
+ import { CAPTURE_WAIT_MS, overviewTiles, planZoom, poseAt } from './zoom.js'
9
+ import type { OverviewPose } from './zoom.js'
10
+
11
+ /*
12
+ * THE OVERVIEW, AND SWITCHING THROUGH IT.
13
+ *
14
+ * Drawn on an overlay over the apps' live views: in an app host, its own transparent view on top
15
+ * (the window's page under the views gets no frames while they cover it); in a page, a layer over the
16
+ * desktop area. The host asks for a move with `request` and does what the overlay says:
17
+ *
18
+ * onWantCapture(id) capture this workspace's view now and pass the picture back in `snapshots`;
19
+ * return a promise of it too, and it counts even when it is the same picture
20
+ * as last time (a still workspace captures byte for byte the same)
21
+ * onCover() the overlay now covers the desktop: the live views under it may be hidden
22
+ * onArrive(id) a full-size picture of this workspace is showing: swap its live view in, then
23
+ * clear `request`
24
+ *
25
+ * Input to first motion is kept short: the fog starts fading in at once, the tiles already held are
26
+ * used, and only the current workspace is captured, waited for at most CAPTURE_WAIT_MS.
27
+ */
28
+
29
+ export type OverviewRequest = { readonly kind: 'open' } | { readonly kind: 'switch'; readonly to: string }
30
+
31
+ export interface HostOverviewProps {
32
+ /** In the switcher's order. */
33
+ readonly workspaces: readonly ShellWorkspace[]
34
+ readonly current: string
35
+ /** A picture of each workspace, by id: URLs or `data:` URIs. A live one without one shows a note. */
36
+ readonly snapshots?: Readonly<Record<string, string>>
37
+ /** What to do; a new object starts a new move, null clears the overlay. */
38
+ readonly request: OverviewRequest | null
39
+ readonly theme?: DeskTheme | null
40
+ /** Where the desktop area is, when the overlay shares the page with the chrome. */
41
+ readonly insets?: ShellInsets
42
+ /** False when the window can't be seen (covered, minimised, another desktop): moves jump. */
43
+ readonly visible?: boolean
44
+ /** Less motion: every move a short fade in place. Read from the system when left out. */
45
+ readonly reducedMotion?: boolean
46
+ /**
47
+ * A pose to hold instead of moving: what a gesture that drives the overview by hand draws each frame,
48
+ * or a still for a picture. `request` is ignored while it is set.
49
+ */
50
+ readonly pose?: OverviewPose
51
+ /**
52
+ * Capture this workspace now and pass the picture in `snapshots`, which the tiles draw. Return a promise
53
+ * of its URL as well to say when it is ready: a picture identical to the last one is still fresh, and a
54
+ * rejected one goes on with the picture held. Returning nothing, the overlay watches `snapshots` for a
55
+ * changed picture instead, which an unchanged workspace never gives, so it waits out CAPTURE_WAIT_MS.
56
+ */
57
+ readonly onWantCapture?: (id: string) => void | PromiseLike<string | void>
58
+ readonly onCover?: () => void
59
+ readonly onArrive: (id: string) => void
60
+ }
61
+
62
+ const CLOSED: OverviewPose = { zoom: 0, centre: 0, fog: 0, opacity: 1 }
63
+ const NONE: ShellInsets = { top: 0, right: 0, bottom: 0, left: 0 }
64
+
65
+ const prefersLessMotion = () => typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches
66
+ const pageHidden = () => typeof document !== 'undefined' && document.visibilityState === 'hidden'
67
+
68
+ /** Resolves once a picture is decoded and ready to draw without a blank frame, or after `ms` regardless. */
69
+ function decoded(url: string | undefined, ms: number): Promise<void> {
70
+ if (!url || typeof Image === 'undefined') return Promise.resolve()
71
+ const image = new Image()
72
+ image.src = url
73
+ const decode = typeof image.decode === 'function' ? image.decode().catch(() => {}) : Promise.resolve()
74
+ let timer: ReturnType<typeof setTimeout> | undefined
75
+ return Promise.race([decode, new Promise<void>(resolve => (timer = setTimeout(resolve, ms)))]).finally(() => clearTimeout(timer))
76
+ }
77
+
78
+ export function HostOverview(props: HostOverviewProps) {
79
+ const { workspaces, current, snapshots = {}, request, theme, insets = NONE } = props
80
+ useLayoutEffect(() => {
81
+ if (theme) applyTheme(theme)
82
+ }, [theme])
83
+
84
+ const indexOf = (id: string) => Math.max(0, workspaces.findIndex(w => w.id === id))
85
+ const latest = useRef(props)
86
+ latest.current = props
87
+
88
+ const layer = useRef<HTMLDivElement>(null)
89
+ const fog = useRef<HTMLDivElement>(null)
90
+ const tiles = useRef<(HTMLButtonElement | null)[]>([])
91
+ const pose = useRef<OverviewPose>({ ...CLOSED, centre: indexOf(current) })
92
+ const running = useRef<Animation | null>(null)
93
+ const [open, setOpen] = useState(false)
94
+ const [said, setSaid] = useState('')
95
+ const captured = useRef<(() => void) | null>(null)
96
+ // Whether onCover has been said since the overlay last cleared: a move asked for mid-flight carries on.
97
+ const covered = useRef(false)
98
+ // Where the focus was before the overlay, to give it back when it clears.
99
+ const returnTo = useRef<HTMLElement | null>(null)
100
+
101
+ /** Draws a pose: writes transforms and opacities straight to the tiles, which the compositor moves. */
102
+ const paint = (at: OverviewPose) => {
103
+ pose.current = at
104
+ const width = layer.current?.clientWidth || 1
105
+ if (fog.current) fog.current.style.opacity = String(at.fog)
106
+ overviewTiles(workspaces.length, at, width).forEach((place, i) => {
107
+ const tile = tiles.current[i]
108
+ if (!tile) return
109
+ tile.style.transform = `translate3d(${place.x}px, 0, ${place.z}px) scale(${place.scale})`
110
+ tile.style.opacity = String(place.opacity * at.opacity)
111
+ tile.style.zIndex = String(place.layer)
112
+ tile.style.setProperty('--desk-overview-depth', String(at.zoom))
113
+ })
114
+ }
115
+
116
+ /** Runs a move to a pose, from where things stand. */
117
+ const move = (to: OverviewPose, reduced: boolean): Promise<'finished' | 'jumped' | 'stopped'> => {
118
+ running.current?.stop()
119
+ const plan = planZoom(pose.current, to, reduced)
120
+ if (!plan.phases.length || pageHidden() || latest.current.visible === false) {
121
+ paint(to)
122
+ return Promise.resolve('jumped')
123
+ }
124
+ const run = animate({ durationMs: plan.durationMs, curve: linear(plan.durationMs), step: p => paint(poseAt(plan, p * plan.durationMs)), clock: pageClock() })
125
+ running.current = run
126
+ return run.done
127
+ }
128
+
129
+ useLayoutEffect(() => {
130
+ if (props.pose) paint(props.pose)
131
+ })
132
+
133
+ // A fresh capture of the current workspace has come in.
134
+ useEffect(() => {
135
+ captured.current?.()
136
+ }, [snapshots[current]])
137
+
138
+ useLayoutEffect(() => {
139
+ if (!request || props.pose) {
140
+ running.current?.stop()
141
+ covered.current = false
142
+ setOpen(false)
143
+ setSaid('')
144
+ pose.current = { ...CLOSED, centre: indexOf(current) }
145
+ if (document.activeElement === document.body) returnTo.current?.focus()
146
+ returnTo.current = null
147
+ return
148
+ }
149
+ returnTo.current ??= document.activeElement as HTMLElement | null
150
+ // A move asked for while the overview is open: the tiles take no clicks on the way.
151
+ setOpen(false)
152
+ let cancelled = false
153
+ const cover = () => {
154
+ if (covered.current) return
155
+ covered.current = true
156
+ latest.current.onCover?.()
157
+ }
158
+ const reduced = latest.current.reducedMotion ?? prefersLessMotion()
159
+ const jump = pageHidden() || latest.current.visible === false
160
+ const here = indexOf(current)
161
+ const target = request.kind === 'switch' ? indexOf(request.to) : here
162
+
163
+ // A switch with nothing to see, or a reduced-motion switch, needs no capture of where we are.
164
+ if (request.kind === 'switch' && (jump || reduced)) {
165
+ // A jump waits a microtask too, after any cleanup, so StrictMode's rerun arrives once.
166
+ if (!jump) paint({ zoom: 0, centre: target, fog: 0, opacity: 0 })
167
+ void (jump ? Promise.resolve() : move({ zoom: 0, centre: target, fog: 0, opacity: 1 }, true)).then(() => {
168
+ if (cancelled) return
169
+ cover()
170
+ latest.current.onArrive(request.to)
171
+ })
172
+ return () => void (cancelled = true)
173
+ }
174
+
175
+ // Asked mid-flight, the views are already covered: carry on from where the tiles are, no capture.
176
+ const already = covered.current
177
+ // First motion at once: the fog starts coming in while the current workspace is captured.
178
+ if (!already) paint({ zoom: 0, centre: here, fog: 0, opacity: 0 })
179
+ const haze = jump || reduced || already ? null : animate({ durationMs: CAPTURE_WAIT_MS, curve: linear(CAPTURE_WAIT_MS), step: p => paint({ ...pose.current, fog: p * 0.6 }) })
180
+ const before = latest.current.snapshots?.[current]
181
+ let waiting: ReturnType<typeof setTimeout> | undefined
182
+ let given: string | undefined
183
+ const fresh = already
184
+ ? Promise.resolve()
185
+ : new Promise<void>(resolve => {
186
+ captured.current = () => {
187
+ if (latest.current.snapshots?.[current] !== before) resolve()
188
+ }
189
+ waiting = setTimeout(resolve, CAPTURE_WAIT_MS)
190
+ const answer = latest.current.onWantCapture?.(current)
191
+ if (answer && typeof answer.then === 'function') {
192
+ answer.then(
193
+ url => {
194
+ if (typeof url === 'string') given = url
195
+ resolve()
196
+ },
197
+ () => resolve(),
198
+ )
199
+ }
200
+ })
201
+
202
+ void fresh
203
+ .then(() => (already ? undefined : decoded(given ?? latest.current.snapshots?.[current], CAPTURE_WAIT_MS)))
204
+ .then(async () => {
205
+ if (cancelled) return
206
+ haze?.stop()
207
+ captured.current = null
208
+ if (!reduced && !already) {
209
+ // The picture exactly over the live view: nothing changes on screen, and the views can go.
210
+ paint({ ...pose.current, opacity: 1 })
211
+ cover()
212
+ }
213
+ if (request.kind === 'switch') {
214
+ await move({ zoom: 0, centre: target, fog: 1, opacity: 1 }, false)
215
+ if (!cancelled) latest.current.onArrive(request.to)
216
+ return
217
+ }
218
+ await move({ zoom: 1, centre: here, fog: 1, opacity: 1 }, reduced)
219
+ if (cancelled) return
220
+ cover()
221
+ setOpen(true)
222
+ setSaid(`Overview, ${workspaces.length} workspaces`)
223
+ })
224
+ return () => {
225
+ cancelled = true
226
+ haze?.stop()
227
+ running.current?.stop()
228
+ clearTimeout(waiting)
229
+ captured.current = null
230
+ }
231
+ // A new request object is a new move; the rest is read when it starts.
232
+ }, [request, Boolean(props.pose)])
233
+
234
+ // Once the tiles can take it, the focus goes to the current one.
235
+ useEffect(() => {
236
+ if (open) tiles.current[indexOf(latest.current.current)]?.focus()
237
+ }, [open])
238
+
239
+ const reduced = () => latest.current.reducedMotion ?? prefersLessMotion()
240
+ const choose = (i: number) => {
241
+ if (!open) return
242
+ setOpen(false)
243
+ const id = workspaces[i]!.id
244
+ // Cleared or replaced on the way (the move stopped): the host has moved on, so no arrival.
245
+ void move({ zoom: 0, centre: i, fog: 1, opacity: 1 }, reduced()).then(how => how !== 'stopped' && latest.current.onArrive(id))
246
+ }
247
+
248
+ const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
249
+ if (!open) return
250
+ const index = tiles.current.indexOf(document.activeElement as HTMLButtonElement)
251
+ if (event.key === 'Escape') {
252
+ event.preventDefault()
253
+ return choose(indexOf(current))
254
+ }
255
+ const next =
256
+ event.key === 'ArrowRight' ? Math.min(index + 1, workspaces.length - 1)
257
+ : event.key === 'ArrowLeft' ? Math.max(index - 1, 0)
258
+ : event.key === 'Home' ? 0
259
+ : event.key === 'End' ? workspaces.length - 1
260
+ : null
261
+ if (next === null || index < 0) return
262
+ event.preventDefault()
263
+ // The one Tab stop goes with the focus, as in the switcher.
264
+ tiles.current.forEach((tile, i) => tile && (tile.tabIndex = i === next ? 0 : -1))
265
+ tiles.current[next]?.focus()
266
+ // The row follows the focus, so the chosen tile comes to the centre.
267
+ void move({ ...pose.current, zoom: 1, centre: next }, false)
268
+ }
269
+
270
+ if (!request && !props.pose) return null
271
+ return (
272
+ <div
273
+ ref={layer}
274
+ className="desk-overview"
275
+ style={{ top: insets.top, right: insets.right, bottom: insets.bottom, left: insets.left }}
276
+ role="dialog"
277
+ aria-label="Overview"
278
+ aria-modal={open || undefined}
279
+ onKeyDown={onKeyDown}
280
+ >
281
+ <div ref={fog} className="desk-overview-fog" />
282
+ {workspaces.map((w, i) => {
283
+ const picture = snapshots[w.id]
284
+ return (
285
+ <button
286
+ key={w.id}
287
+ ref={el => void (tiles.current[i] = el)}
288
+ type="button"
289
+ className="desk-overview-tile"
290
+ tabIndex={open && i === indexOf(current) ? 0 : -1}
291
+ aria-current={w.id === current ? 'true' : undefined}
292
+ aria-label={w.title}
293
+ disabled={!open}
294
+ data-state={w.state}
295
+ onClick={() => choose(i)}
296
+ >
297
+ <span className="desk-overview-shadow" aria-hidden="true" />
298
+ {picture ? (
299
+ <img src={picture} alt="" draggable={false} />
300
+ ) : (
301
+ <span className="desk-overview-note" aria-hidden="true">
302
+ <strong>{w.title}</strong>
303
+ {w.state === 'live' ? 'Live' : null}
304
+ </span>
305
+ )}
306
+ <span className="desk-overview-title" aria-hidden="true">
307
+ {w.title}
308
+ </span>
309
+ </button>
310
+ )
311
+ })}
312
+ <div className="desk-shell-announce" role="status" aria-live="polite">
313
+ {said}
314
+ </div>
315
+ </div>
316
+ )
317
+ }
@@ -66,8 +66,10 @@ export interface HostShellProps {
66
66
  readonly onInsets?: (insets: ShellInsets) => void
67
67
  /** The switching keys; the platform's defaults if left out, none at all with null. */
68
68
  readonly keys?: SwitchKeys | null
69
- /** How a click in the switcher switches. */
69
+ /** How a click in the switcher switches: through the overview by default; the keys always slide. */
70
70
  readonly clickSwitch?: SwitchHow
71
+ /** The overview key was pressed on this page; a host catches it first when an app has focus. */
72
+ readonly onOverview?: () => void
71
73
  readonly dockSide?: DockSide
72
74
  }
73
75
 
@@ -104,7 +106,7 @@ export function HostShell(props: HostShellProps) {
104
106
  )
105
107
  }
106
108
 
107
- function Shell({ workspaces, current, chrome, theme, onSwitch, onCommand, onInsets, keys, clickSwitch = 'slide', dockSide = 'left' }: HostShellProps) {
109
+ function Shell({ workspaces, current, chrome, theme, onSwitch, onCommand, onInsets, onOverview, keys, clickSwitch = 'zoom', dockSide = 'left' }: HostShellProps) {
108
110
  useLayoutEffect(() => {
109
111
  if (theme) applyTheme(theme)
110
112
  }, [theme])
@@ -129,14 +131,15 @@ function Shell({ workspaces, current, chrome, theme, onSwitch, onCommand, onInse
129
131
  if (!bound) return
130
132
  const onKey = (event: globalThis.KeyboardEvent) => {
131
133
  const action = switchKeyAction(event, bound)
132
- if (action !== 'previous' && action !== 'next') return
133
- const to = neighbour(workspaces, current, action)
134
+ if (!action) return
134
135
  event.preventDefault()
136
+ if (action === 'overview') return onOverview?.()
137
+ const to = neighbour(workspaces, current, action)
135
138
  if (to) onSwitch(to.id, 'slide')
136
139
  }
137
140
  window.addEventListener('keydown', onKey)
138
141
  return () => window.removeEventListener('keydown', onKey)
139
- }, [bound, workspaces, current, onSwitch])
142
+ }, [bound, workspaces, current, onSwitch, onOverview])
140
143
 
141
144
  const root = useRef<HTMLDivElement>(null)
142
145
  useInsets(root, dockSide, onInsets)
@@ -4,8 +4,26 @@
4
4
  * app does, and nothing in core or react imports it.
5
5
  */
6
6
  export { dockFromHost, HostShell } from './HostShell.js'
7
+ export { HostOverview } from './HostOverview.js'
8
+ export type { HostOverviewProps, OverviewRequest } from './HostOverview.js'
7
9
  export type { HostShellProps, ShellInsets, ShellWorkspace, SwitchHow } from './HostShell.js'
8
10
  export { neighbour, platformOf, switchKeyAction, switchKeys } from './keys.js'
9
11
  export type { KeyInput, ShellPlatform, SwitchKey, SwitchKeys } from './keys.js'
10
12
  export { animate, FADE_MS, linear, pageClock, SLIDE_MS, slideOffsets, slidePlan, spring, timerClock } from './motion.js'
11
13
  export type { Animation, AnimateOptions, FrameClock, SlideOffsets, SlidePlan } from './motion.js'
14
+ export {
15
+ CAPTURE_WAIT_MS,
16
+ cubicBezier,
17
+ easeIn,
18
+ easeInOut,
19
+ easeOut,
20
+ OVERVIEW_SCALE,
21
+ overviewTiles,
22
+ planZoom,
23
+ poseAt,
24
+ ZOOM_ACROSS_MS,
25
+ ZOOM_FADE_MS,
26
+ ZOOM_IN_MS,
27
+ ZOOM_OUT_MS,
28
+ } from './zoom.js'
29
+ export type { OverviewPose, TilePlace, ZoomPhase, ZoomPlan } from './zoom.js'
@@ -0,0 +1,151 @@
1
+ /*
2
+ * THE OVERVIEW AND THE ZOOM, AS NUMBERS.
3
+ *
4
+ * A live view can't be scaled (smaller bounds relay it out), so the zoom is drawn with pictures on an
5
+ * overlay: the current workspace's capture sits exactly over its live view, shrinks back into a fog
6
+ * where the workspaces float, the row moves across, and the target grows back to full size, where the
7
+ * host swaps its live view in. This file is where everything stands at each moment, with no DOM: the
8
+ * overlay draws from it, and a host can read the same plan to know how long a move takes.
9
+ *
10
+ * A pose is one moment: how far zoomed out (0 is a full-size desktop, 1 the overview), which workspace
11
+ * is centred (fractional while the row moves), how much fog shows, and how opaque the tiles are.
12
+ */
13
+
14
+ /** Zooming out, moving across, zooming in, and the capture wait, in ms. */
15
+ export const ZOOM_OUT_MS = 200
16
+ export const ZOOM_ACROSS_MS = 250
17
+ export const ZOOM_IN_MS = 200
18
+ /** How long to wait for a fresh capture of the current workspace before zooming from the picture already held. */
19
+ export const CAPTURE_WAIT_MS = 100
20
+ /** Reduced motion: every move is this cross-fade, in place. */
21
+ export const ZOOM_FADE_MS = 120
22
+
23
+ /** A CSS `cubic-bezier`, as a function of time from 0 to 1. */
24
+ export function cubicBezier(x1: number, y1: number, x2: number, y2: number): (t: number) => number {
25
+ const at = (a: number, b: number, s: number) => 3 * a * s * (1 - s) ** 2 + 3 * b * s * s * (1 - s) + s ** 3
26
+ return t => {
27
+ if (t <= 0) return 0
28
+ if (t >= 1) return 1
29
+ // Find s with x(s) = t by bisection: x is increasing, and 20 halvings are well under a pixel.
30
+ let lo = 0
31
+ let hi = 1
32
+ for (let i = 0; i < 20; i++) {
33
+ const mid = (lo + hi) / 2
34
+ if (at(x1, x2, mid) < t) lo = mid
35
+ else hi = mid
36
+ }
37
+ return at(y1, y2, (lo + hi) / 2)
38
+ }
39
+ }
40
+
41
+ export const easeOut = cubicBezier(0, 0, 0.58, 1)
42
+ export const easeIn = cubicBezier(0.42, 0, 1, 1)
43
+ export const easeInOut = cubicBezier(0.42, 0, 0.58, 1)
44
+ const straight = (t: number) => Math.min(1, Math.max(0, t))
45
+
46
+ export interface OverviewPose {
47
+ /** 0 is a full-size desktop, 1 the overview. */
48
+ readonly zoom: number
49
+ /** The index of the centred workspace; fractional while the row moves. */
50
+ readonly centre: number
51
+ readonly fog: number
52
+ readonly opacity: number
53
+ }
54
+
55
+ export interface ZoomPhase {
56
+ readonly name: 'out' | 'across' | 'in' | 'fade'
57
+ readonly durationMs: number
58
+ readonly from: OverviewPose
59
+ readonly to: OverviewPose
60
+ readonly ease: (t: number) => number
61
+ }
62
+
63
+ export interface ZoomPlan {
64
+ readonly phases: readonly ZoomPhase[]
65
+ readonly durationMs: number
66
+ /** The pose asked for: what poseAt gives for a plan with nothing to do (no phases). */
67
+ readonly to: OverviewPose
68
+ }
69
+
70
+ /**
71
+ * How to get from one pose to another. Out of a full-size desktop first if the row must move or the
72
+ * overview is the goal (200 ms, ease-out); across if another workspace is to be centred (250 ms,
73
+ * ease-in-out); then in, if the goal is a desktop (200 ms, ease-in). A click on another workspace is
74
+ * all three, about 650 ms. Under reduced motion it is one 120 ms fade in at the goal.
75
+ */
76
+ export function planZoom(from: OverviewPose, to: OverviewPose, reducedMotion = false): ZoomPlan {
77
+ if (reducedMotion) {
78
+ const phases = [{ name: 'fade' as const, durationMs: ZOOM_FADE_MS, from: { ...to, opacity: 0 }, to: { ...to, opacity: 1 }, ease: straight }]
79
+ return { phases, durationMs: ZOOM_FADE_MS, to }
80
+ }
81
+ const phases: ZoomPhase[] = []
82
+ let at = from
83
+ const go = (name: ZoomPhase['name'], durationMs: number, ease: (t: number) => number, next: OverviewPose) => {
84
+ phases.push({ name, durationMs, from: at, to: next, ease })
85
+ at = next
86
+ }
87
+ const moves = from.centre !== to.centre
88
+ if (at.zoom < 1 && (moves || to.zoom === 1)) go('out', ZOOM_OUT_MS, easeOut, { zoom: 1, centre: at.centre, fog: Math.max(at.fog, to.fog), opacity: 1 })
89
+ if (moves) go('across', ZOOM_ACROSS_MS, easeInOut, { ...at, centre: to.centre })
90
+ if (to.zoom < at.zoom) go('in', ZOOM_IN_MS, easeIn, { ...to, centre: to.centre })
91
+ return { phases, durationMs: phases.reduce((total, p) => total + p.durationMs, 0), to }
92
+ }
93
+
94
+ const lerp = (a: number, b: number, t: number) => a + (b - a) * t
95
+
96
+ /** Where a plan stands `elapsedMs` after it started: its goal from its duration on. */
97
+ export function poseAt(plan: ZoomPlan, elapsedMs: number): OverviewPose {
98
+ let left = elapsedMs
99
+ for (const phase of plan.phases) {
100
+ if (left < phase.durationMs) {
101
+ const t = phase.ease(left / phase.durationMs)
102
+ return {
103
+ zoom: lerp(phase.from.zoom, phase.to.zoom, t),
104
+ centre: lerp(phase.from.centre, phase.to.centre, t),
105
+ fog: lerp(phase.from.fog, phase.to.fog, t),
106
+ opacity: lerp(phase.from.opacity, phase.to.opacity, t),
107
+ }
108
+ }
109
+ left -= phase.durationMs
110
+ }
111
+ return plan.phases.at(-1)?.to ?? plan.to
112
+ }
113
+
114
+ /** One tile as drawn: offsets in px from the desktop area's centre, a scale, and an opacity. */
115
+ export interface TilePlace {
116
+ readonly x: number
117
+ /** Into the screen, under the layer's perspective: negative is further back. */
118
+ readonly z: number
119
+ readonly scale: number
120
+ readonly opacity: number
121
+ /** Paint order: the centred tile on top. */
122
+ readonly layer: number
123
+ }
124
+
125
+ /** The centred tile's scale in the overview, and how much space is between tiles. */
126
+ export const OVERVIEW_SCALE = 0.62
127
+ const GAP = 1.08
128
+ /** Each step away from the centre: this far back, this much smaller, this much fainter. */
129
+ const STEP_BACK = 80
130
+ const STEP_SCALE = 0.92
131
+ const STEP_OPACITY = 0.85
132
+
133
+ /**
134
+ * Every tile's place at a pose, for a desktop area `width` px wide. At zoom 0 the centred tile fills
135
+ * the area and its neighbours sit just beside it, as in the slide; at 1 the row floats in the fog,
136
+ * neighbours further back, smaller and fainter, and nothing rotates.
137
+ */
138
+ export function overviewTiles(count: number, pose: OverviewPose, width: number): TilePlace[] {
139
+ const scale = lerp(1, OVERVIEW_SCALE, pose.zoom)
140
+ const pitch = width * lerp(1, OVERVIEW_SCALE * GAP, pose.zoom)
141
+ return Array.from({ length: count }, (_, i) => {
142
+ const away = Math.min(Math.abs(i - pose.centre), 2)
143
+ return {
144
+ x: Math.round((i - pose.centre) * pitch),
145
+ z: -STEP_BACK * away * pose.zoom,
146
+ scale: scale * lerp(1, STEP_SCALE ** away, pose.zoom),
147
+ opacity: lerp(1, STEP_OPACITY ** away, pose.zoom),
148
+ layer: count - Math.round(Math.abs(i - pose.centre)),
149
+ }
150
+ })
151
+ }