@liberation-data/desk 0.46.0 → 0.48.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.
@@ -0,0 +1,202 @@
1
+ import type { AppGesture } from '../core/hosted.js'
2
+ import { animate, FADE_MS, linear, SLIDE_MS, slideOffsets, spring, timerClock } from './motion.js'
3
+ import type { Animation, FrameClock, SlideOffsets } from './motion.js'
4
+
5
+ /*
6
+ * THE SWIPE: FINGERS MOVE THE DESKTOPS, ONE TO ONE.
7
+ *
8
+ * The app in front passes on a two-finger swipe that started on its background; the host's router
9
+ * passes it here. While the fingers move, the desktops follow them exactly. When they lift, the
10
+ * swipe goes on to the next workspace if it went far enough or fast enough that way, or springs back
11
+ * if not, on part 1's spring, from where it is and as fast as it was going. A new swipe, or a key,
12
+ * catches a slide still settling and carries on from there.
13
+ *
14
+ * No DOM: a host's main process runs this and moves its views' bounds from each frame.
15
+ */
16
+
17
+ /** Past half the width, a released swipe goes on; short of it, it goes back. */
18
+ export const COMMIT_DISTANCE = 0.5
19
+ /** Faster than this, in widths a second, a flick goes on (or back) whatever the distance. */
20
+ export const COMMIT_SPEED = 0.5
21
+
22
+ /** Progress past the last workspace, or before the first, as a rubber band gives it: a little, and less the further. */
23
+ const rubber = (over: number) => (over * 0.15) / (over + 0.15)
24
+
25
+ /**
26
+ * Where the desktops are, as a signed progress (positive toward the next workspace), after `delta` px
27
+ * of a swipe that started at `start`. One to one, up to a whole workspace either way; where there is
28
+ * no workspace to go to, it gives only a little.
29
+ */
30
+ export function swipeProgress(start: number, delta: number, width: number, canPrevious: boolean, canNext: boolean): number {
31
+ const p = Math.max(-1, Math.min(1, start + delta / Math.max(1, width)))
32
+ if (p > 0 && !canNext) return rubber(p)
33
+ if (p < 0 && !canPrevious) return -rubber(-p)
34
+ return p
35
+ }
36
+
37
+ /**
38
+ * Where a released swipe settles: 1 for the next workspace, -1 the previous, 0 back where it started.
39
+ * A flick decides by its direction (one back toward the start goes back); a slow release by distance.
40
+ */
41
+ export function swipeDecision(progress: number, velocity: number, canPrevious: boolean, canNext: boolean): -1 | 0 | 1 {
42
+ const way = Math.abs(velocity) >= COMMIT_SPEED ? (progress === 0 || Math.sign(velocity) === Math.sign(progress) ? Math.sign(velocity) : 0) : Math.abs(progress) >= COMMIT_DISTANCE ? Math.sign(progress) : 0
43
+ if ((way > 0 && !canNext) || (way < 0 && !canPrevious)) return 0
44
+ return way as -1 | 0 | 1
45
+ }
46
+
47
+ /** One frame of a slide, as the host draws it. */
48
+ export interface SlideFrame {
49
+ /** The workspace the slide started from. */
50
+ readonly from: number
51
+ /** The one coming in beside it; `from` while nothing is: at rest, or a rubber band past the first or last. */
52
+ readonly to: number
53
+ /** From 0 (`from` fills the desktop) toward 1 (`to` does). */
54
+ readonly progress: number
55
+ /** Where each desktop sits, in px from the desktop area's left edge. */
56
+ readonly offsets: SlideOffsets
57
+ /** Reduced motion: `to` fades in at its place over `from`, which stays put; this is its opacity. */
58
+ readonly fade?: number
59
+ }
60
+
61
+ export interface SwipeControllerOptions {
62
+ /** How many workspaces, and which is on screen, in the switcher's order. */
63
+ readonly count: () => number
64
+ readonly current: () => number
65
+ /** The desktop area's width, in px. */
66
+ readonly width: () => number
67
+ readonly reducedMotion?: () => boolean
68
+ /** Frames: a page's (`pageClock`), or a timer's in a main process (the default). */
69
+ readonly clock?: FrameClock
70
+ /** Draw this frame: move the views to its offsets. */
71
+ readonly frame: (frame: SlideFrame) => void
72
+ /** The slide has landed on this workspace: it is the current one now. */
73
+ readonly arrive: (index: number) => void
74
+ }
75
+
76
+ export interface SwipeController {
77
+ /** A gesture from the app in front, as the router passed it. */
78
+ gesture(gesture: AppGesture): void
79
+ /** Slides to a workspace, as the keys do: on the same spring, and as catchable. */
80
+ slideTo(index: number): void
81
+ /** Whether a swipe is under the fingers or a slide is settling. */
82
+ busy(): boolean
83
+ /**
84
+ * Stops a swipe or slide where it is, with no more frames and no `arrive`: for a host closing, or
85
+ * putting its views back itself. The rest of the swipe is ignored; the next starts from `current`.
86
+ */
87
+ stop(): void
88
+ }
89
+
90
+ /** Follows swipes and settles them; see the file's note. */
91
+ export function createSwipeController(options: SwipeControllerOptions): SwipeController {
92
+ const clock = options.clock ?? timerClock()
93
+ let anchor = -1
94
+ /** Signed progress from `anchor`, and the workspace on the other side while one is chosen. */
95
+ let p = 0
96
+ let other: number | null = null
97
+ let start = 0
98
+ let tracking = false
99
+ let running: Animation | null = null
100
+
101
+ const draw = (fade?: number) => {
102
+ // A rubber band past the first or last workspace has nothing beside it: `from` moves alone.
103
+ const next = anchor + Math.sign(p)
104
+ const to = other ?? (next >= 0 && next < options.count() ? next : anchor)
105
+ const way = to > anchor || (to === anchor && p >= 0) ? 1 : -1
106
+ const progress = Math.abs(p)
107
+ const offsets = slideOffsets(progress, way, options.width())
108
+ // Fading in, the arriving desktop is at its own place already.
109
+ options.frame({ from: anchor, to, progress, offsets: fade === undefined ? offsets : { ...offsets, arriving: 0 }, ...(fade === undefined ? {} : { fade }) })
110
+ }
111
+
112
+ /** Stops a settle where it is, so whatever comes next starts from there. */
113
+ const catchIt = () => {
114
+ if (running) running.stop(), (running = null)
115
+ if (anchor < 0) anchor = options.current()
116
+ }
117
+
118
+ const settle = (target: number, velocity: number, to: number | null) => {
119
+ const from = p
120
+ const goal = target === 0 ? 0 : Math.sign(target)
121
+ // Going on, the arriving desktop is chosen now: a flick from no distance has no side to draw it on yet.
122
+ other = to ?? (goal === 0 ? null : anchor + goal)
123
+ if (goal !== 0 && options.reducedMotion?.()) {
124
+ // Less motion: the arriving desktop fades in at its place; nothing slides on.
125
+ const run = animate({ durationMs: FADE_MS, curve: linear(FADE_MS), step: o => draw(o), clock })
126
+ running = run
127
+ return void run.done.then(how => how !== 'stopped' && running === run && land(goal))
128
+ }
129
+ const span = goal - from
130
+ if (Math.abs(span) < 1e-4) return land(goal)
131
+ // The spring runs from 0 to 1 along the span, starting as fast as the fingers were going.
132
+ const curve = spring(SLIDE_MS, 0, Math.max(-30, Math.min(30, velocity / span)))
133
+ const run = animate({ durationMs: SLIDE_MS, curve, step: u => ((p = from + span * u), draw()), clock })
134
+ running = run
135
+ // Stopped or caught as its last frame was drawn, it has ended, but it lands no more.
136
+ void run.done.then(how => how !== 'stopped' && running === run && land(goal))
137
+ }
138
+
139
+ const land = (goal: number) => {
140
+ running = null
141
+ const index = other ?? anchor + goal
142
+ // Home exactly, from however near it was let go.
143
+ if (goal === 0) (p = 0), draw()
144
+ // At rest before the host hears, so a slide it starts from `arrive` starts afresh.
145
+ rest()
146
+ if (goal !== 0) options.arrive(index)
147
+ }
148
+
149
+ const rest = () => {
150
+ anchor = -1
151
+ p = 0
152
+ other = null
153
+ }
154
+
155
+ return {
156
+ gesture(g) {
157
+ const count = options.count()
158
+ if (g.phase === 'begin') {
159
+ // A slide still settling stops where it is, and the fingers take it up from there, toward
160
+ // the workspace it was going to if they keep going that way.
161
+ catchIt()
162
+ start = p
163
+ tracking = true
164
+ return
165
+ }
166
+ if (!tracking) return
167
+ const width = options.width()
168
+ const towardOther = other !== null && Math.sign(other - anchor)
169
+ p = swipeProgress(start, g.delta, width, anchor > 0, anchor < count - 1)
170
+ // Back past where it started: whatever was beside it before is not the one beside it now.
171
+ if (towardOther !== false && Math.sign(p) !== towardOther) other = null
172
+ if (g.phase === 'move') return draw()
173
+ tracking = false
174
+ if (g.phase === 'cancel') return settle(0, 0, other)
175
+ const v = (g.velocity ?? 0) / Math.max(1, width)
176
+ const goal = swipeDecision(p, v, anchor > 0, anchor < count - 1)
177
+ settle(goal, v, goal === 0 ? other : other !== null && Math.sign(other - anchor) === goal ? other : null)
178
+ },
179
+
180
+ slideTo(index) {
181
+ if (tracking) return
182
+ const moving = running !== null
183
+ catchIt()
184
+ // Back to where it started, from wherever a caught slide had got to; with nothing moving, nothing
185
+ // to do, and the next swipe or key starts from whatever is current then.
186
+ if (index === anchor) return void (moving || p !== 0 ? settle(0, 0, other) : (anchor = -1))
187
+ // Toward the workspace asked for: from wherever a caught slide had got to, if it was going that way.
188
+ const way = Math.sign(index - anchor)
189
+ if (Math.sign(p) !== way) p = 0
190
+ settle(way, 0, index)
191
+ },
192
+
193
+ busy: () => tracking || running !== null,
194
+
195
+ stop() {
196
+ running?.stop()
197
+ running = null
198
+ tracking = false
199
+ rest()
200
+ },
201
+ }
202
+ }