@liberation-data/desk 0.43.0 → 0.45.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.
- package/README.md +238 -10
- package/dist/core/desk.d.ts +2 -0
- package/dist/core/desk.d.ts.map +1 -1
- package/dist/core/desk.js +11 -2
- package/dist/core/desk.js.map +1 -1
- package/dist/core/hosted.d.ts +222 -0
- package/dist/core/hosted.d.ts.map +1 -0
- package/dist/core/hosted.js +166 -0
- package/dist/core/hosted.js.map +1 -0
- package/dist/core/index.d.ts +9 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +5 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/location.d.ts +8 -1
- package/dist/core/location.d.ts.map +1 -1
- package/dist/core/location.js +18 -2
- package/dist/core/location.js.map +1 -1
- package/dist/core/preferences.d.ts +27 -0
- package/dist/core/preferences.d.ts.map +1 -0
- package/dist/core/preferences.js +121 -0
- package/dist/core/preferences.js.map +1 -0
- package/dist/core/scheme.d.ts +9 -3
- package/dist/core/scheme.d.ts.map +1 -1
- package/dist/core/scheme.js +13 -4
- package/dist/core/scheme.js.map +1 -1
- package/dist/core/theme.d.ts +41 -0
- package/dist/core/theme.d.ts.map +1 -0
- package/dist/core/theme.js +56 -0
- package/dist/core/theme.js.map +1 -0
- package/dist/core/themeChange.d.ts +25 -0
- package/dist/core/themeChange.d.ts.map +1 -0
- package/dist/core/themeChange.js +49 -0
- package/dist/core/themeChange.js.map +1 -0
- package/dist/core/types.d.ts +5 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/css/apps.css +52 -1
- package/dist/css/shell.css +100 -0
- package/dist/css/tokens.css +10 -0
- package/dist/css/windows.css +28 -0
- package/dist/desk.css +90 -1
- package/dist/fx/fx.d.ts.map +1 -1
- package/dist/fx/fx.js +7 -31
- package/dist/fx/fx.js.map +1 -1
- package/dist/host/checkChrome.d.ts +20 -0
- package/dist/host/checkChrome.d.ts.map +1 -0
- package/dist/host/checkChrome.js +219 -0
- package/dist/host/checkChrome.js.map +1 -0
- package/dist/host/checkTheme.d.ts +35 -0
- package/dist/host/checkTheme.d.ts.map +1 -0
- package/dist/host/checkTheme.js +128 -0
- package/dist/host/checkTheme.js.map +1 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.d.ts.map +1 -0
- package/dist/host/index.js +8 -0
- package/dist/host/index.js.map +1 -0
- package/dist/host/router.d.ts +99 -0
- package/dist/host/router.d.ts.map +1 -0
- package/dist/host/router.js +279 -0
- package/dist/host/router.js.map +1 -0
- package/dist/react/Desktop.d.ts.map +1 -1
- package/dist/react/Desktop.js +11 -14
- package/dist/react/Desktop.js.map +1 -1
- package/dist/react/Dock.d.ts +23 -4
- package/dist/react/Dock.d.ts.map +1 -1
- package/dist/react/Dock.js +17 -29
- package/dist/react/Dock.js.map +1 -1
- package/dist/react/MenuBar.d.ts +1 -1
- package/dist/react/MenuBar.d.ts.map +1 -1
- package/dist/react/MenuBar.js +20 -4
- package/dist/react/MenuBar.js.map +1 -1
- package/dist/react/app.d.ts +65 -12
- package/dist/react/app.d.ts.map +1 -1
- package/dist/react/app.js +79 -21
- package/dist/react/app.js.map +1 -1
- package/dist/react/appFrame.d.ts +11 -7
- package/dist/react/appFrame.d.ts.map +1 -1
- package/dist/react/appFrame.js +24 -6
- package/dist/react/appFrame.js.map +1 -1
- package/dist/react/appPanels.d.ts +4 -1
- package/dist/react/appPanels.d.ts.map +1 -1
- package/dist/react/appPanels.js +76 -28
- package/dist/react/appPanels.js.map +1 -1
- package/dist/react/appearance.d.ts +7 -1
- package/dist/react/appearance.d.ts.map +1 -1
- package/dist/react/appearance.js +32 -53
- package/dist/react/appearance.js.map +1 -1
- package/dist/react/context.d.ts +25 -1
- package/dist/react/context.d.ts.map +1 -1
- package/dist/react/context.js +73 -14
- package/dist/react/context.js.map +1 -1
- package/dist/react/dockStatus.d.ts +19 -0
- package/dist/react/dockStatus.d.ts.map +1 -0
- package/dist/react/dockStatus.js +25 -0
- package/dist/react/dockStatus.js.map +1 -0
- package/dist/react/frameMenus.d.ts +28 -0
- package/dist/react/frameMenus.d.ts.map +1 -0
- package/dist/react/frameMenus.js +85 -0
- package/dist/react/frameMenus.js.map +1 -0
- package/dist/react/hostApp.d.ts +15 -0
- package/dist/react/hostApp.d.ts.map +1 -0
- package/dist/react/hostApp.js +77 -0
- package/dist/react/hostApp.js.map +1 -0
- package/dist/react/hostDock.d.ts +25 -0
- package/dist/react/hostDock.d.ts.map +1 -0
- package/dist/react/hostDock.js +98 -0
- package/dist/react/hostDock.js.map +1 -0
- package/dist/react/hostMenu.d.ts +2 -36
- package/dist/react/hostMenu.d.ts.map +1 -1
- package/dist/react/hostMenu.js.map +1 -1
- package/dist/react/hosted.d.ts +28 -0
- package/dist/react/hosted.d.ts.map +1 -0
- package/dist/react/hosted.js +53 -0
- package/dist/react/hosted.js.map +1 -0
- package/dist/react/index.d.ts +10 -5
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +6 -3
- package/dist/react/index.js.map +1 -1
- package/dist/react/mediaQuery.d.ts +3 -0
- package/dist/react/mediaQuery.d.ts.map +1 -0
- package/dist/react/mediaQuery.js +11 -0
- package/dist/react/mediaQuery.js.map +1 -0
- package/dist/react/shell.d.ts +13 -1
- package/dist/react/shell.d.ts.map +1 -1
- package/dist/react/shell.js +19 -3
- package/dist/react/shell.js.map +1 -1
- package/dist/react/sidebar.d.ts +6 -1
- package/dist/react/sidebar.d.ts.map +1 -1
- package/dist/react/sidebar.js +6 -3
- package/dist/react/sidebar.js.map +1 -1
- package/dist/react/windowDescription.d.ts +7 -0
- package/dist/react/windowDescription.d.ts.map +1 -1
- package/dist/react/windowDescription.js +9 -0
- package/dist/react/windowDescription.js.map +1 -1
- package/dist/react/windowMenus.d.ts +19 -0
- package/dist/react/windowMenus.d.ts.map +1 -0
- package/dist/react/windowMenus.js +88 -0
- package/dist/react/windowMenus.js.map +1 -0
- package/dist/shell/HostShell.d.ts +53 -0
- package/dist/shell/HostShell.d.ts.map +1 -0
- package/dist/shell/HostShell.js +116 -0
- package/dist/shell/HostShell.js.map +1 -0
- package/dist/shell/index.d.ts +7 -0
- package/dist/shell/index.d.ts.map +1 -0
- package/dist/shell/index.js +9 -0
- package/dist/shell/index.js.map +1 -0
- package/dist/shell/keys.d.ts +47 -0
- package/dist/shell/keys.d.ts.map +1 -0
- package/dist/shell/keys.js +68 -0
- package/dist/shell/keys.js.map +1 -0
- package/dist/shell/motion.d.ts +72 -0
- package/dist/shell/motion.d.ts.map +1 -0
- package/dist/shell/motion.js +118 -0
- package/dist/shell/motion.js.map +1 -0
- package/llms.txt +57 -20
- package/package.json +10 -1
- package/src/core/desk.ts +11 -2
- package/src/core/hosted.ts +351 -0
- package/src/core/index.ts +28 -1
- package/src/core/location.ts +23 -3
- package/src/core/preferences.ts +132 -0
- package/src/core/scheme.ts +16 -5
- package/src/core/theme.ts +91 -0
- package/src/core/themeChange.ts +59 -0
- package/src/core/types.ts +5 -0
- package/src/css/apps.css +52 -1
- package/src/css/shell.css +99 -0
- package/src/css/tokens.css +10 -0
- package/src/css/windows.css +28 -0
- package/src/fx/fx.ts +7 -25
- package/src/host/checkChrome.ts +257 -0
- package/src/host/checkTheme.ts +134 -0
- package/src/host/index.ts +10 -0
- package/src/host/router.ts +374 -0
- package/src/react/Desktop.tsx +16 -21
- package/src/react/Dock.tsx +34 -28
- package/src/react/MenuBar.tsx +22 -4
- package/src/react/app.tsx +116 -22
- package/src/react/appFrame.tsx +39 -14
- package/src/react/appPanels.tsx +102 -31
- package/src/react/appearance.tsx +36 -45
- package/src/react/context.tsx +99 -13
- package/src/react/dockStatus.ts +42 -0
- package/src/react/frameMenus.ts +100 -0
- package/src/react/hostApp.tsx +89 -0
- package/src/react/hostDock.tsx +118 -0
- package/src/react/hostMenu.ts +3 -38
- package/src/react/hosted.tsx +65 -0
- package/src/react/index.ts +12 -4
- package/src/react/mediaQuery.ts +18 -0
- package/src/react/shell.tsx +30 -3
- package/src/react/sidebar.tsx +10 -3
- package/src/react/windowDescription.ts +12 -1
- package/src/react/windowMenus.ts +107 -0
- package/src/shell/HostShell.tsx +220 -0
- package/src/shell/index.ts +11 -0
- package/src/shell/keys.ts +101 -0
- package/src/shell/motion.ts +163 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* THE KEYS THAT SWITCH WORKSPACES, PER PLATFORM.
|
|
3
|
+
*
|
|
4
|
+
* Every OS has keys of its own for its own workspaces, and a host's must never fight them: macOS takes
|
|
5
|
+
* Ctrl+←/→ for Spaces and Ctrl+↑ for Mission Control; GNOME takes Ctrl+Alt+←/→ (and Super+PageUp/Down),
|
|
6
|
+
* KDE Meta+Ctrl+←/→; Windows takes Ctrl+Win+←/→ for its virtual desktops. So the defaults step around
|
|
7
|
+
* each, and a host can offer its own in Settings.
|
|
8
|
+
*
|
|
9
|
+
* A key reaches whichever view has focus, usually an app's. So a host catches these before the app
|
|
10
|
+
* does (in Electron, `before-input-event`) and asks `switchKeyAction` what the key means; the shell's
|
|
11
|
+
* own page uses the same function for the keys it sees itself.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** One key with its modifiers. `meta` is ⌘ on a Mac, Super (the Windows key) elsewhere. */
|
|
15
|
+
export interface SwitchKey {
|
|
16
|
+
readonly key: string
|
|
17
|
+
readonly ctrl?: boolean
|
|
18
|
+
readonly alt?: boolean
|
|
19
|
+
readonly meta?: boolean
|
|
20
|
+
readonly shift?: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** What each switching key does. */
|
|
24
|
+
export interface SwitchKeys {
|
|
25
|
+
readonly previous: SwitchKey
|
|
26
|
+
readonly next: SwitchKey
|
|
27
|
+
/** Opens the overview, once there is one to open. */
|
|
28
|
+
readonly overview: SwitchKey
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type ShellPlatform = 'mac' | 'linux' | 'windows'
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The platform, from a browser's `navigator.platform` or user agent, or a Node `process.platform`
|
|
35
|
+
* ('darwin', 'linux', 'win32').
|
|
36
|
+
*/
|
|
37
|
+
export function platformOf(name: string): ShellPlatform {
|
|
38
|
+
if (/mac|darwin|iphone|ipad/i.test(name)) return 'mac'
|
|
39
|
+
if (/win/i.test(name)) return 'windows'
|
|
40
|
+
return 'linux'
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const arrows = (mods: Omit<SwitchKey, 'key'>): SwitchKeys => ({
|
|
44
|
+
previous: { key: 'ArrowLeft', ...mods },
|
|
45
|
+
next: { key: 'ArrowRight', ...mods },
|
|
46
|
+
overview: { key: 'ArrowUp', ...mods },
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
/** The default keys on a platform: Ctrl+Option on a Mac, Super+Alt on Linux, Ctrl+Alt on Windows. */
|
|
50
|
+
export function switchKeys(platform: ShellPlatform): SwitchKeys {
|
|
51
|
+
switch (platform) {
|
|
52
|
+
case 'mac':
|
|
53
|
+
return arrows({ ctrl: true, alt: true })
|
|
54
|
+
case 'linux':
|
|
55
|
+
return arrows({ meta: true, alt: true })
|
|
56
|
+
case 'windows':
|
|
57
|
+
return arrows({ ctrl: true, alt: true })
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A key event from a page (`ctrlKey`…) or from Electron's `before-input-event` (`control`…). */
|
|
62
|
+
export interface KeyInput {
|
|
63
|
+
readonly key: string
|
|
64
|
+
/** `keydown`/`keyup` from a page, `keyDown`/`keyUp` from Electron: only a key going down switches. */
|
|
65
|
+
readonly type?: string
|
|
66
|
+
readonly ctrlKey?: boolean
|
|
67
|
+
readonly altKey?: boolean
|
|
68
|
+
readonly metaKey?: boolean
|
|
69
|
+
readonly shiftKey?: boolean
|
|
70
|
+
readonly control?: boolean
|
|
71
|
+
readonly alt?: boolean
|
|
72
|
+
readonly meta?: boolean
|
|
73
|
+
readonly shift?: boolean
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const matches = (input: KeyInput, key: SwitchKey): boolean =>
|
|
77
|
+
input.key === key.key &&
|
|
78
|
+
Boolean(input.ctrlKey ?? input.control) === Boolean(key.ctrl) &&
|
|
79
|
+
Boolean(input.altKey ?? input.alt) === Boolean(key.alt) &&
|
|
80
|
+
Boolean(input.metaKey ?? input.meta) === Boolean(key.meta) &&
|
|
81
|
+
Boolean(input.shiftKey ?? input.shift) === Boolean(key.shift)
|
|
82
|
+
|
|
83
|
+
/** What a key does, if it is one of the switching keys: modifiers must match exactly. */
|
|
84
|
+
export function switchKeyAction(input: KeyInput, keys: SwitchKeys): 'previous' | 'next' | 'overview' | null {
|
|
85
|
+
// Electron's before-input-event sends the key's release too: one press must switch once.
|
|
86
|
+
if (/up$/i.test(input.type ?? '')) return null
|
|
87
|
+
if (matches(input, keys.previous)) return 'previous'
|
|
88
|
+
if (matches(input, keys.next)) return 'next'
|
|
89
|
+
if (matches(input, keys.overview)) return 'overview'
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The workspace one step from `current`, in the order given (the switcher's order), or null at
|
|
95
|
+
* either end: like the OS's own workspaces, switching does not wrap round.
|
|
96
|
+
*/
|
|
97
|
+
export function neighbour<T extends { readonly id: string }>(workspaces: readonly T[], current: string, step: 'previous' | 'next'): T | null {
|
|
98
|
+
const index = workspaces.findIndex(w => w.id === current)
|
|
99
|
+
if (index < 0) return null
|
|
100
|
+
return workspaces[index + (step === 'next' ? 1 : -1)] ?? null
|
|
101
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* THE SLIDE, AS NUMBERS.
|
|
3
|
+
*
|
|
4
|
+
* When a host switches workspaces it moves the apps' live views itself, from its own process, a frame
|
|
5
|
+
* at a time; the shell draws the chrome around them. Both must move on one curve, so the curve is
|
|
6
|
+
* here as plain functions, with no DOM: a host's main process steps its views' bounds from them, and
|
|
7
|
+
* the shell animates with the same ones.
|
|
8
|
+
*
|
|
9
|
+
* Progress runs from 0 (the workspace being left fills the desktop) to 1 (the one arriving does). The
|
|
10
|
+
* spring is critically damped: it arrives without overshooting, and it can start from any progress
|
|
11
|
+
* and speed, so a slide caught mid-flight, or flung, carries on from where it is.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** How a switch is drawn: the desktops slide, or (reduced motion) the arriving one fades in over the other. */
|
|
15
|
+
export interface SlidePlan {
|
|
16
|
+
readonly kind: 'slide' | 'fade'
|
|
17
|
+
readonly durationMs: number
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The default slide, and the short fade that replaces it under reduced motion. */
|
|
21
|
+
export const SLIDE_MS = 350
|
|
22
|
+
export const FADE_MS = 120
|
|
23
|
+
|
|
24
|
+
/** What a switch should do, given whether the person asked for less motion. */
|
|
25
|
+
export const slidePlan = (reducedMotion: boolean, durationMs = SLIDE_MS): SlidePlan =>
|
|
26
|
+
reducedMotion ? { kind: 'fade', durationMs: FADE_MS } : { kind: 'slide', durationMs }
|
|
27
|
+
|
|
28
|
+
/** The distance left that counts as arrived: under a tenth of a pixel on a 4K-wide desktop. */
|
|
29
|
+
const SETTLED = 0.00002
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A critically damped spring towards 1, from `from` with a starting speed `velocity` (progress per
|
|
33
|
+
* second), stiff enough to settle within `durationMs`. Returns the progress at a time in milliseconds
|
|
34
|
+
* since it started: exactly 1 from `durationMs` on.
|
|
35
|
+
*/
|
|
36
|
+
export function spring(durationMs = SLIDE_MS, from = 0, velocity = 0): (elapsedMs: number) => number {
|
|
37
|
+
// (1 + ωt)·e^(−ωt) falls to SETTLED when ωt is about 13.5: pick ω so that happens at durationMs.
|
|
38
|
+
const omega = settleConstant() / (durationMs / 1000)
|
|
39
|
+
const d0 = from - 1
|
|
40
|
+
const c2 = velocity + omega * d0
|
|
41
|
+
return elapsedMs => {
|
|
42
|
+
if (elapsedMs >= durationMs) return 1
|
|
43
|
+
const t = Math.max(0, elapsedMs) / 1000
|
|
44
|
+
// A hard fling would carry a critically damped spring past its target once: hold it at 1 instead.
|
|
45
|
+
return Math.min(1, 1 + (d0 + c2 * t) * Math.exp(-omega * t))
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let settle: number | undefined
|
|
50
|
+
/** The ωt at which a critically damped spring from rest is within SETTLED of its target. */
|
|
51
|
+
function settleConstant(): number {
|
|
52
|
+
if (settle !== undefined) return settle
|
|
53
|
+
let x = 10
|
|
54
|
+
// Newton's method on f(x) = (1 + x)e^(−x) − SETTLED; a handful of steps is exact to the float.
|
|
55
|
+
for (let i = 0; i < 20; i++) x -= ((1 + x) * Math.exp(-x) - SETTLED) / (-x * Math.exp(-x))
|
|
56
|
+
return (settle = x)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A plain linear ramp, for the fade: opacity follows time evenly. */
|
|
60
|
+
export const linear = (durationMs: number) => (elapsedMs: number): number => Math.min(1, Math.max(0, elapsedMs / durationMs))
|
|
61
|
+
|
|
62
|
+
/** Where the two desktops sit at a given progress, in whole pixels from the desktop area's left edge. */
|
|
63
|
+
export interface SlideOffsets {
|
|
64
|
+
/** The workspace being left. */
|
|
65
|
+
readonly leaving: number
|
|
66
|
+
/** The workspace arriving. */
|
|
67
|
+
readonly arriving: number
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The desktops' offsets at `progress`. Moving to a workspace further along (`direction` 1) the one
|
|
72
|
+
* being left goes out to the left and the next comes in from the right; back (`-1`), the other way.
|
|
73
|
+
* Rounded to whole pixels, as a view's bounds are, and with no gap or overlap between the two.
|
|
74
|
+
*
|
|
75
|
+
* Mid-slide both views reach past the desktop area, so the host must clip them to it (the area
|
|
76
|
+
* `onInsets` gives): a view placed at these offsets with nothing clipping it covers the dock beside.
|
|
77
|
+
*/
|
|
78
|
+
export function slideOffsets(progress: number, direction: 1 | -1, width: number): SlideOffsets {
|
|
79
|
+
const whole = Math.round(width)
|
|
80
|
+
const leaving = -direction * Math.round(progress * whole)
|
|
81
|
+
return { leaving, arriving: leaving + direction * whole }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** What `animate` needs to schedule frames: rAF in a page, a timer in a main process. */
|
|
85
|
+
export interface FrameClock {
|
|
86
|
+
readonly now: () => number
|
|
87
|
+
/** Calls back on the next frame. */
|
|
88
|
+
readonly frame: (callback: () => void) => void
|
|
89
|
+
/** Calls back after a delay, whatever the frames do. */
|
|
90
|
+
readonly after: (ms: number, callback: () => void) => void
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A clock for a page: frames from requestAnimationFrame, which a hidden page never gives. */
|
|
94
|
+
export const pageClock = (): FrameClock => ({
|
|
95
|
+
now: () => performance.now(),
|
|
96
|
+
frame: callback => requestAnimationFrame(() => callback()),
|
|
97
|
+
after: (ms, callback) => setTimeout(callback, ms),
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
/** A clock for a process with no frames of its own, as an app host's main process: about 60 a second. */
|
|
101
|
+
export const timerClock = (frameMs = 1000 / 60): FrameClock => ({
|
|
102
|
+
now: () => performance.now(),
|
|
103
|
+
frame: callback => setTimeout(callback, frameMs),
|
|
104
|
+
after: (ms, callback) => setTimeout(callback, ms),
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
export interface AnimateOptions {
|
|
108
|
+
readonly durationMs: number
|
|
109
|
+
/** The curve: progress at a time; `spring(durationMs)` by default. */
|
|
110
|
+
readonly curve?: (elapsedMs: number) => number
|
|
111
|
+
/** Called with each frame's progress, and finally with 1. */
|
|
112
|
+
readonly step: (progress: number) => void
|
|
113
|
+
readonly clock?: FrameClock
|
|
114
|
+
/**
|
|
115
|
+
* How long past `durationMs` to wait for frames before jumping to the end. A window that is covered,
|
|
116
|
+
* minimised or on another desktop gets no frames, and an animation must never hang on them.
|
|
117
|
+
*/
|
|
118
|
+
readonly timeoutMs?: number
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** A running animation. `stop` leaves it where it is; `done` resolves at the end, or when stopped. */
|
|
122
|
+
export interface Animation {
|
|
123
|
+
readonly done: Promise<'finished' | 'jumped' | 'stopped'>
|
|
124
|
+
/** Where it is now, for a gesture that catches it mid-flight. */
|
|
125
|
+
progress(): number
|
|
126
|
+
stop(): void
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Steps `step` through a curve, frame by frame, from any clock. Ends at 1 when time is up, and jumps
|
|
131
|
+
* straight there if frames stop arriving for `timeoutMs`.
|
|
132
|
+
*/
|
|
133
|
+
export function animate({ durationMs, curve = spring(durationMs), step, clock = pageClock(), timeoutMs = 250 }: AnimateOptions): Animation {
|
|
134
|
+
const start = clock.now()
|
|
135
|
+
let current = 0
|
|
136
|
+
let ended = false
|
|
137
|
+
let finish!: (how: 'finished' | 'jumped' | 'stopped') => void
|
|
138
|
+
const done = new Promise<'finished' | 'jumped' | 'stopped'>(resolve => (finish = resolve))
|
|
139
|
+
const end = (how: 'finished' | 'jumped' | 'stopped') => {
|
|
140
|
+
if (ended) return
|
|
141
|
+
ended = true
|
|
142
|
+
if (how !== 'stopped') step((current = 1))
|
|
143
|
+
finish(how)
|
|
144
|
+
}
|
|
145
|
+
let lastFrame = start
|
|
146
|
+
const watchdog = () => {
|
|
147
|
+
if (ended) return
|
|
148
|
+
// Frames have stalled past the end: jump. Otherwise look again later.
|
|
149
|
+
if (clock.now() - lastFrame >= timeoutMs && clock.now() - start >= durationMs) end('jumped')
|
|
150
|
+
else clock.after(timeoutMs, watchdog)
|
|
151
|
+
}
|
|
152
|
+
const tick = () => {
|
|
153
|
+
if (ended) return
|
|
154
|
+
lastFrame = clock.now()
|
|
155
|
+
const elapsed = lastFrame - start
|
|
156
|
+
if (elapsed >= durationMs) return end('finished')
|
|
157
|
+
step((current = curve(elapsed)))
|
|
158
|
+
clock.frame(tick)
|
|
159
|
+
}
|
|
160
|
+
clock.frame(tick)
|
|
161
|
+
clock.after(durationMs + timeoutMs, watchdog)
|
|
162
|
+
return { done, progress: () => current, stop: () => end('stopped') }
|
|
163
|
+
}
|