@c9up/orbit 0.0.0-stage → 0.1.1

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 (105) hide show
  1. package/Cargo.lock +139 -0
  2. package/Cargo.toml +20 -0
  3. package/LICENSE +21 -0
  4. package/README.md +320 -2
  5. package/android/orbit.cmake +31 -0
  6. package/android/src/main/cpp/NativeOrbit.cpp +309 -0
  7. package/android/src/main/cpp/NativeOrbit.h +35 -0
  8. package/android/src/main/cpp/orbit_host.h +34 -0
  9. package/android/src/main/java/com/c9up/orbit/OrbitEffects.kt +286 -0
  10. package/android/src/main/java/com/c9up/orbit/OrbitHost.kt +258 -0
  11. package/android/src/main/java/com/c9up/orbit/OrbitImage.kt +346 -0
  12. package/android/src/main/java/com/c9up/orbit/OrbitInput.kt +161 -0
  13. package/android/src/main/java/com/c9up/orbit/OrbitPress.kt +110 -0
  14. package/android/src/main/java/com/c9up/orbit/OrbitVector.kt +115 -0
  15. package/android/src/main/java/com/c9up/orbit/OrbitViews.kt +362 -0
  16. package/crates/orbit-host/Cargo.toml +14 -0
  17. package/crates/orbit-host/src/gesture.rs +561 -0
  18. package/crates/orbit-host/src/lib.rs +2042 -0
  19. package/crates/orbit-host/src/motion.rs +328 -0
  20. package/crates/orbit-host/src/vector.rs +1173 -0
  21. package/dist/color.d.ts +10 -0
  22. package/dist/color.d.ts.map +1 -0
  23. package/dist/color.js +62 -0
  24. package/dist/color.js.map +1 -0
  25. package/dist/commit.d.ts +77 -0
  26. package/dist/commit.d.ts.map +1 -0
  27. package/dist/commit.js +302 -0
  28. package/dist/commit.js.map +1 -0
  29. package/dist/errors.d.ts +76 -0
  30. package/dist/errors.d.ts.map +1 -0
  31. package/dist/errors.js +89 -0
  32. package/dist/errors.js.map +1 -0
  33. package/dist/host.d.ts +49 -0
  34. package/dist/host.d.ts.map +1 -0
  35. package/dist/host.js +20 -0
  36. package/dist/host.js.map +1 -0
  37. package/dist/index.d.ts +31 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +31 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/motion.d.ts +18 -0
  42. package/dist/motion.d.ts.map +1 -0
  43. package/dist/motion.js +67 -0
  44. package/dist/motion.js.map +1 -0
  45. package/dist/node.d.ts +46 -0
  46. package/dist/node.d.ts.map +1 -0
  47. package/dist/node.js +110 -0
  48. package/dist/node.js.map +1 -0
  49. package/dist/pages.d.ts +56 -0
  50. package/dist/pages.d.ts.map +1 -0
  51. package/dist/pages.js +157 -0
  52. package/dist/pages.js.map +1 -0
  53. package/dist/react-native/index.d.ts +33 -0
  54. package/dist/react-native/index.d.ts.map +1 -0
  55. package/dist/react-native/index.js +71 -0
  56. package/dist/react-native/index.js.map +1 -0
  57. package/dist/report.d.ts +13 -0
  58. package/dist/report.d.ts.map +1 -0
  59. package/dist/report.js +22 -0
  60. package/dist/report.js.map +1 -0
  61. package/dist/server.d.ts +109 -0
  62. package/dist/server.d.ts.map +1 -0
  63. package/dist/server.js +290 -0
  64. package/dist/server.js.map +1 -0
  65. package/dist/stack.d.ts +117 -0
  66. package/dist/stack.d.ts.map +1 -0
  67. package/dist/stack.js +299 -0
  68. package/dist/stack.js.map +1 -0
  69. package/dist/styles.d.ts +138 -0
  70. package/dist/styles.d.ts.map +1 -0
  71. package/dist/styles.js +920 -0
  72. package/dist/styles.js.map +1 -0
  73. package/dist/template.d.ts +27 -0
  74. package/dist/template.d.ts.map +1 -0
  75. package/dist/template.js +1436 -0
  76. package/dist/template.js.map +1 -0
  77. package/dist/textDecoder.d.ts +29 -0
  78. package/dist/textDecoder.d.ts.map +1 -0
  79. package/dist/textDecoder.js +149 -0
  80. package/dist/textDecoder.js.map +1 -0
  81. package/dist/url.d.ts +45 -0
  82. package/dist/url.d.ts.map +1 -0
  83. package/dist/url.js +303 -0
  84. package/dist/url.js.map +1 -0
  85. package/package.json +96 -3
  86. package/scripts/build-android.sh +27 -0
  87. package/scripts/test-styles.mjs +31 -0
  88. package/specs/NativeOrbit.ts +34 -0
  89. package/src/color.ts +68 -0
  90. package/src/commit.ts +373 -0
  91. package/src/errors.ts +178 -0
  92. package/src/host.ts +91 -0
  93. package/src/index.ts +64 -0
  94. package/src/motion.ts +88 -0
  95. package/src/node.ts +133 -0
  96. package/src/pages.ts +207 -0
  97. package/src/react-native/index.ts +115 -0
  98. package/src/react-native/react-native.d.ts +39 -0
  99. package/src/report.ts +25 -0
  100. package/src/server.ts +395 -0
  101. package/src/stack.ts +428 -0
  102. package/src/styles.ts +1021 -0
  103. package/src/template.ts +1681 -0
  104. package/src/textDecoder.ts +162 -0
  105. package/src/url.ts +333 -0
@@ -0,0 +1,561 @@
1
+ //! Press, long press, pan and swipe, recognized on the UI thread.
2
+ //!
3
+ //! The platform hands every touch to `touch` as it arrives, and calls `tick`
4
+ //! on every vsync; nothing waits for JS. What comes back is a list of
5
+ //! commands: the press feedback to show now (a ripple, on the UI thread), the
6
+ //! gesture a view claimed (the platform then stops feeding the scroll view
7
+ //! under it), and the events to deliver to JS when it can take them.
8
+ //!
9
+ //! The commit (JS thread) keeps the tree this needs: each mounted node's
10
+ //! parent, the gestures its handlers asked for, and whether it scrolls.
11
+
12
+ use std::collections::{HashMap, VecDeque};
13
+ use std::sync::Mutex;
14
+
15
+ /// Gesture flags a node's handlers ask for (its `gestures` prop).
16
+ pub const PRESS: u8 = 1;
17
+ pub const LONG_PRESS: u8 = 2;
18
+ pub const PAN: u8 = 4;
19
+ pub const SWIPE: u8 = 8;
20
+
21
+ /// Touch actions from the platform.
22
+ pub const DOWN: u8 = 0;
23
+ pub const MOVE: u8 = 1;
24
+ pub const UP: u8 = 2;
25
+ pub const CANCEL: u8 = 3;
26
+
27
+ /// Show or hide the press feedback: i32 tag, u8 on, f32 x, f32 y (the touch, dp, root).
28
+ pub const CMD_PRESSED: u8 = 1;
29
+ /// A view claimed the gesture: i32 tag. The scroll view under it stops.
30
+ pub const CMD_CLAIM: u8 = 2;
31
+ /// An event for JS: i32 tag, then the type and its JSON data, each i32 length + UTF-8.
32
+ pub const CMD_EVENT: u8 = 3;
33
+
34
+ /// How far a finger moves (dp) before a press becomes a drag.
35
+ const SLOP: f32 = 10.0;
36
+ /// How long a press is held before it is a long press.
37
+ const LONG_PRESS_NS: i64 = 500_000_000;
38
+ /// Inside a scroll view the feedback waits this long, so a scroll that starts
39
+ /// on a row does not flash it (Android's tap timeout).
40
+ const FEEDBACK_DELAY_NS: i64 = 100_000_000;
41
+ /// The speed (dp/s) a pan must end with to be a swipe.
42
+ const SWIPE_SPEED: f32 = 500.0;
43
+ /// The span of recent moves the release velocity is taken over.
44
+ const VELOCITY_WINDOW_NS: i64 = 100_000_000;
45
+
46
+ struct NodeInfo {
47
+ parent: i32,
48
+ flags: u8,
49
+ scroll: bool,
50
+ }
51
+
52
+ #[derive(PartialEq, Clone, Copy, Debug)]
53
+ enum State {
54
+ /// Down, not moved past the slop: a press or a long press to be.
55
+ Pending,
56
+ /// Held long enough: the long press fired, the release does nothing more.
57
+ LongPressed,
58
+ /// Moved past the slop and claimed by a pan or a swipe.
59
+ Panning,
60
+ }
61
+
62
+ struct Active {
63
+ owner: i32,
64
+ flags: u8,
65
+ in_scroll: bool,
66
+ start: (f32, f32, i64),
67
+ samples: VecDeque<(i64, f32, f32)>,
68
+ state: State,
69
+ feedback_shown: bool,
70
+ }
71
+
72
+ #[derive(Default)]
73
+ pub struct Gestures {
74
+ nodes: HashMap<i32, NodeInfo>,
75
+ active: Option<Active>,
76
+ }
77
+
78
+ pub static GESTURES: Mutex<Option<Gestures>> = Mutex::new(None);
79
+
80
+ pub fn with<R>(f: impl FnOnce(&mut Gestures) -> R) -> R {
81
+ let mut guard = GESTURES.lock().unwrap_or_else(|p| p.into_inner());
82
+ f(guard.get_or_insert_with(Gestures::default))
83
+ }
84
+
85
+ fn put_i32(out: &mut Vec<u8>, v: i32) {
86
+ out.extend_from_slice(&v.to_le_bytes());
87
+ }
88
+ fn put_f32(out: &mut Vec<u8>, v: f32) {
89
+ out.extend_from_slice(&v.to_le_bytes());
90
+ }
91
+ fn put_str(out: &mut Vec<u8>, s: &str) {
92
+ put_i32(out, s.len() as i32);
93
+ out.extend_from_slice(s.as_bytes());
94
+ }
95
+
96
+ fn feedback(out: &mut Vec<u8>, tag: i32, on: bool, x: f32, y: f32) {
97
+ out.push(CMD_PRESSED);
98
+ put_i32(out, tag);
99
+ out.push(u8::from(on));
100
+ put_f32(out, x);
101
+ put_f32(out, y);
102
+ }
103
+
104
+ fn event(out: &mut Vec<u8>, tag: i32, kind: &str, data: &serde_json::Value) {
105
+ out.push(CMD_EVENT);
106
+ put_i32(out, tag);
107
+ put_str(out, kind);
108
+ put_str(out, &data.to_string());
109
+ }
110
+
111
+ impl Gestures {
112
+ // ---- the tree, kept by the commit
113
+
114
+ pub fn set_node(&mut self, tag: i32, flags: u8, scroll: bool) {
115
+ let entry = self.nodes.entry(tag).or_insert(NodeInfo {
116
+ parent: 0,
117
+ flags: 0,
118
+ scroll: false,
119
+ });
120
+ entry.flags = flags;
121
+ entry.scroll = scroll;
122
+ }
123
+
124
+ pub fn set_parent(&mut self, tag: i32, parent: i32) {
125
+ if let Some(node) = self.nodes.get_mut(&tag) {
126
+ node.parent = parent;
127
+ }
128
+ }
129
+
130
+ pub fn remove(&mut self, tag: i32) {
131
+ self.nodes.remove(&tag);
132
+ if self.active.as_ref().map(|a| a.owner) == Some(tag) {
133
+ self.active = None;
134
+ }
135
+ }
136
+
137
+ /// The nearest node at or above `hit` that asked for a gesture, its flags,
138
+ /// and whether a scroll view holds it.
139
+ fn owner_of(&self, hit: i32) -> Option<(i32, u8, bool)> {
140
+ let mut owner: Option<(i32, u8)> = None;
141
+ let mut in_scroll = false;
142
+ let mut tag = hit;
143
+ // The depth bound stops a cycle a bad tree would make.
144
+ for _ in 0..10_000 {
145
+ let Some(info) = self.nodes.get(&tag) else {
146
+ break;
147
+ };
148
+ if owner.is_none() && info.flags != 0 {
149
+ owner = Some((tag, info.flags));
150
+ } else if owner.is_some() && info.scroll {
151
+ in_scroll = true;
152
+ break;
153
+ }
154
+ if info.parent == 0 {
155
+ break;
156
+ }
157
+ tag = info.parent;
158
+ }
159
+ owner.map(|(tag, flags)| (tag, flags, in_scroll))
160
+ }
161
+
162
+ // ---- recognition
163
+
164
+ /// A touch (dp, the root's coordinates). `hit` is the deepest view under a
165
+ /// DOWN; ignored otherwise.
166
+ pub fn touch(&mut self, action: u8, x: f32, y: f32, now: i64, hit: i32, out: &mut Vec<u8>) {
167
+ match action {
168
+ DOWN => {
169
+ self.end(out, x, y);
170
+ let Some((owner, flags, in_scroll)) = self.owner_of(hit) else {
171
+ return;
172
+ };
173
+ let mut active = Active {
174
+ owner,
175
+ flags,
176
+ in_scroll,
177
+ start: (x, y, now),
178
+ samples: VecDeque::from([(now, x, y)]),
179
+ state: State::Pending,
180
+ feedback_shown: false,
181
+ };
182
+ if flags & (PRESS | LONG_PRESS) != 0 && !in_scroll {
183
+ feedback(out, owner, true, x, y);
184
+ active.feedback_shown = true;
185
+ }
186
+ self.active = Some(active);
187
+ }
188
+ MOVE => self.moved(x, y, now, out),
189
+ UP => self.released(x, y, now, out),
190
+ _ => self.end(out, x, y),
191
+ }
192
+ }
193
+
194
+ /// Every vsync: a long press held long enough, a delayed feedback due.
195
+ pub fn tick(&mut self, now: i64, out: &mut Vec<u8>) {
196
+ let Some(active) = self.active.as_mut() else {
197
+ return;
198
+ };
199
+ if active.state != State::Pending {
200
+ return;
201
+ }
202
+ let (x, y, start) = active.start;
203
+ let feedback_wanted = active.flags & (PRESS | LONG_PRESS) != 0;
204
+ if feedback_wanted && !active.feedback_shown && now - start >= FEEDBACK_DELAY_NS {
205
+ feedback(out, active.owner, true, x, y);
206
+ active.feedback_shown = true;
207
+ }
208
+ if active.flags & LONG_PRESS != 0 && now - start >= LONG_PRESS_NS {
209
+ active.state = State::LongPressed;
210
+ event(
211
+ out,
212
+ active.owner,
213
+ "topLongPress",
214
+ &serde_json::json!({ "x": x, "y": y }),
215
+ );
216
+ }
217
+ }
218
+
219
+ fn moved(&mut self, x: f32, y: f32, now: i64, out: &mut Vec<u8>) {
220
+ let Some(active) = self.active.as_mut() else {
221
+ return;
222
+ };
223
+ active.samples.push_back((now, x, y));
224
+ while active.samples.len() > 2
225
+ && active
226
+ .samples
227
+ .front()
228
+ .is_some_and(|(t, _, _)| now - t > VELOCITY_WINDOW_NS)
229
+ {
230
+ active.samples.pop_front();
231
+ }
232
+ let (sx, sy, _) = active.start;
233
+ let (dx, dy) = (x - sx, y - sy);
234
+ let owner = active.owner;
235
+ let pans = active.flags & PAN != 0;
236
+ match active.state {
237
+ State::Pending if dx.hypot(dy) > SLOP => {
238
+ // Along a scroll view's axis, the scroll view wins; across
239
+ // it, or outside one, a pan or a swipe claims the gesture.
240
+ let wants_pan = active.flags & (PAN | SWIPE) != 0;
241
+ if !(wants_pan && (!active.in_scroll || dx.abs() > dy.abs())) {
242
+ self.end(out, x, y);
243
+ return;
244
+ }
245
+ if active.feedback_shown {
246
+ feedback(out, owner, false, x, y);
247
+ active.feedback_shown = false;
248
+ }
249
+ active.state = State::Panning;
250
+ out.push(CMD_CLAIM);
251
+ put_i32(out, owner);
252
+ if pans {
253
+ event(out, owner, "topPan", &pan_data("start", dx, dy, 0.0, 0.0));
254
+ }
255
+ }
256
+ State::Panning if pans => {
257
+ let (vx, vy) = velocity(&active.samples);
258
+ event(out, owner, "topPan", &pan_data("move", dx, dy, vx, vy));
259
+ }
260
+ _ => {}
261
+ }
262
+ }
263
+
264
+ fn released(&mut self, x: f32, y: f32, now: i64, out: &mut Vec<u8>) {
265
+ let Some(mut active) = self.active.take() else {
266
+ return;
267
+ };
268
+ active.samples.push_back((now, x, y));
269
+ let (sx, sy, _) = active.start;
270
+ let (dx, dy) = (x - sx, y - sy);
271
+ match active.state {
272
+ State::Pending => {
273
+ // A tap shorter than the feedback delay still flashes it.
274
+ if !active.feedback_shown && active.flags & (PRESS | LONG_PRESS) != 0 {
275
+ feedback(out, active.owner, true, sx, sy);
276
+ }
277
+ feedback(out, active.owner, false, x, y);
278
+ if active.flags & PRESS != 0 {
279
+ event(
280
+ out,
281
+ active.owner,
282
+ "topPress",
283
+ &serde_json::json!({ "x": x, "y": y }),
284
+ );
285
+ }
286
+ }
287
+ State::LongPressed => feedback(out, active.owner, false, x, y),
288
+ State::Panning => {
289
+ let (vx, vy) = velocity(&active.samples);
290
+ if active.flags & PAN != 0 {
291
+ event(
292
+ out,
293
+ active.owner,
294
+ "topPan",
295
+ &pan_data("end", dx, dy, vx, vy),
296
+ );
297
+ }
298
+ if active.flags & SWIPE != 0 {
299
+ let horizontal = vx.abs() >= vy.abs();
300
+ let speed = if horizontal { vx.abs() } else { vy.abs() };
301
+ if speed >= SWIPE_SPEED {
302
+ let direction = match (horizontal, vx > 0.0, vy > 0.0) {
303
+ (true, true, _) => "right",
304
+ (true, false, _) => "left",
305
+ (false, _, true) => "down",
306
+ (false, _, false) => "up",
307
+ };
308
+ event(
309
+ out,
310
+ active.owner,
311
+ "topSwipe",
312
+ &serde_json::json!({ "direction": direction, "vx": vx, "vy": vy }),
313
+ );
314
+ }
315
+ }
316
+ }
317
+ }
318
+ }
319
+
320
+ /// Drop the gesture in progress: its feedback goes, a pan is cancelled.
321
+ fn end(&mut self, out: &mut Vec<u8>, x: f32, y: f32) {
322
+ let Some(active) = self.active.take() else {
323
+ return;
324
+ };
325
+ if active.feedback_shown {
326
+ feedback(out, active.owner, false, x, y);
327
+ }
328
+ if active.state == State::Panning && active.flags & PAN != 0 {
329
+ let (sx, sy, _) = active.start;
330
+ event(
331
+ out,
332
+ active.owner,
333
+ "topPan",
334
+ &pan_data("cancel", x - sx, y - sy, 0.0, 0.0),
335
+ );
336
+ }
337
+ }
338
+ }
339
+
340
+ fn pan_data(phase: &str, dx: f32, dy: f32, vx: f32, vy: f32) -> serde_json::Value {
341
+ serde_json::json!({ "phase": phase, "dx": dx, "dy": dy, "vx": vx, "vy": vy })
342
+ }
343
+
344
+ /// dp per second over the recent moves.
345
+ fn velocity(samples: &VecDeque<(i64, f32, f32)>) -> (f32, f32) {
346
+ let (Some(first), Some(last)) = (samples.front(), samples.back()) else {
347
+ return (0.0, 0.0);
348
+ };
349
+ let dt = (last.0 - first.0) as f32 / 1e9;
350
+ if dt <= 0.0 {
351
+ return (0.0, 0.0);
352
+ }
353
+ ((last.1 - first.1) / dt, (last.2 - first.2) / dt)
354
+ }
355
+
356
+ #[cfg(test)]
357
+ mod tests {
358
+ use super::*;
359
+
360
+ const MS: i64 = 1_000_000;
361
+
362
+ /// Commands decoded: ("pressed", tag, on) / ("claim", tag) / (type, tag, data).
363
+ fn decode(buf: &[u8]) -> Vec<(String, i32, String)> {
364
+ let i32_at = |i: usize| i32::from_le_bytes([buf[i], buf[i + 1], buf[i + 2], buf[i + 3]]);
365
+ let mut out = Vec::new();
366
+ let mut i = 0;
367
+ while i < buf.len() {
368
+ let tag = i32_at(i + 1);
369
+ match buf[i] {
370
+ CMD_PRESSED => {
371
+ out.push(("pressed".into(), tag, (buf[i + 5] == 1).to_string()));
372
+ i += 1 + 4 + 1 + 8;
373
+ }
374
+ CMD_CLAIM => {
375
+ out.push(("claim".into(), tag, String::new()));
376
+ i += 5;
377
+ }
378
+ CMD_EVENT => {
379
+ let mut at = i + 5;
380
+ let len = i32_at(at) as usize;
381
+ let kind = String::from_utf8(buf[at + 4..at + 4 + len].to_vec()).unwrap();
382
+ at += 4 + len;
383
+ let len = i32_at(at) as usize;
384
+ let data = String::from_utf8(buf[at + 4..at + 4 + len].to_vec()).unwrap();
385
+ out.push((kind, tag, data));
386
+ i = at + 4 + len;
387
+ }
388
+ c => panic!("unknown command {c}"),
389
+ }
390
+ }
391
+ out
392
+ }
393
+
394
+ /// A row (5) with `flags` holding a text (7), in a column (3), maybe in a scroll view (1).
395
+ fn tree(flags: u8, scroll: bool) -> Gestures {
396
+ let mut g = Gestures::default();
397
+ g.set_node(1, 0, scroll);
398
+ g.set_node(3, 0, false);
399
+ g.set_node(5, flags, false);
400
+ g.set_node(7, 0, false);
401
+ g.set_parent(3, 1);
402
+ g.set_parent(5, 3);
403
+ g.set_parent(7, 5);
404
+ g
405
+ }
406
+
407
+ fn run(g: &mut Gestures, steps: &[(u8, f32, f32, i64)]) -> Vec<(String, i32, String)> {
408
+ let mut out = Vec::new();
409
+ for &(action, x, y, t) in steps {
410
+ if action == 9 {
411
+ g.tick(t * MS, &mut out);
412
+ } else {
413
+ g.touch(action, x, y, t * MS, 7, &mut out);
414
+ }
415
+ }
416
+ decode(&out)
417
+ }
418
+
419
+ const TICK: u8 = 9;
420
+
421
+ fn kinds(cmds: &[(String, i32, String)]) -> Vec<&str> {
422
+ cmds.iter().map(|(k, _, _)| k.as_str()).collect()
423
+ }
424
+
425
+ #[test]
426
+ fn a_tap_on_a_text_presses_its_pressable_row_with_feedback_at_once() {
427
+ let mut g = tree(PRESS, false);
428
+ let cmds = run(&mut g, &[(DOWN, 10.0, 10.0, 0), (UP, 12.0, 11.0, 80)]);
429
+ assert_eq!(kinds(&cmds), vec!["pressed", "pressed", "topPress"]);
430
+ assert!(cmds.iter().all(|(_, tag, _)| *tag == 5));
431
+ assert_eq!(cmds[0].2, "true");
432
+ assert_eq!(cmds[1].2, "false");
433
+ }
434
+
435
+ #[test]
436
+ fn moving_past_the_slop_cancels_a_press() {
437
+ let mut g = tree(PRESS, false);
438
+ let cmds = run(
439
+ &mut g,
440
+ &[
441
+ (DOWN, 10.0, 10.0, 0),
442
+ (MOVE, 10.0, 30.0, 16),
443
+ (UP, 10.0, 30.0, 32),
444
+ ],
445
+ );
446
+ assert_eq!(kinds(&cmds), vec!["pressed", "pressed"]);
447
+ }
448
+
449
+ #[test]
450
+ fn in_a_scroll_view_the_feedback_waits_and_a_scroll_never_flashes_it() {
451
+ let mut g = tree(PRESS, true);
452
+ // A scroll starting on the row: no feedback at all, no press.
453
+ let scrolled = run(
454
+ &mut g,
455
+ &[
456
+ (DOWN, 10.0, 10.0, 0),
457
+ (TICK, 0.0, 0.0, 16),
458
+ (MOVE, 10.0, 40.0, 32),
459
+ (UP, 10.0, 90.0, 60),
460
+ ],
461
+ );
462
+ assert!(scrolled.is_empty(), "{scrolled:?}");
463
+ // Held still: the feedback comes after the delay, the press on release.
464
+ let held = run(
465
+ &mut g,
466
+ &[
467
+ (DOWN, 10.0, 10.0, 0),
468
+ (TICK, 0.0, 0.0, 50),
469
+ (TICK, 0.0, 0.0, 110),
470
+ (UP, 10.0, 10.0, 200),
471
+ ],
472
+ );
473
+ assert_eq!(kinds(&held), vec!["pressed", "pressed", "topPress"]);
474
+ // A quick tap still flashes it.
475
+ let quick = run(&mut g, &[(DOWN, 10.0, 10.0, 0), (UP, 10.0, 10.0, 40)]);
476
+ assert_eq!(kinds(&quick), vec!["pressed", "pressed", "topPress"]);
477
+ }
478
+
479
+ #[test]
480
+ fn a_long_press_fires_from_the_tick_and_replaces_the_press() {
481
+ let mut g = tree(PRESS | LONG_PRESS, false);
482
+ let cmds = run(
483
+ &mut g,
484
+ &[
485
+ (DOWN, 10.0, 10.0, 0),
486
+ (TICK, 0.0, 0.0, 300),
487
+ (TICK, 0.0, 0.0, 510),
488
+ (UP, 10.0, 10.0, 700),
489
+ ],
490
+ );
491
+ assert_eq!(kinds(&cmds), vec!["pressed", "topLongPress", "pressed"]);
492
+ }
493
+
494
+ #[test]
495
+ fn a_horizontal_pan_in_a_scroll_view_claims_and_ends_as_a_swipe() {
496
+ let mut g = tree(PRESS | PAN | SWIPE, true);
497
+ let cmds = run(
498
+ &mut g,
499
+ &[
500
+ (DOWN, 100.0, 10.0, 0),
501
+ (MOVE, 80.0, 12.0, 16),
502
+ (MOVE, 40.0, 14.0, 32),
503
+ (UP, 0.0, 15.0, 48),
504
+ ],
505
+ );
506
+ assert_eq!(
507
+ kinds(&cmds),
508
+ vec!["claim", "topPan", "topPan", "topPan", "topSwipe"]
509
+ );
510
+ assert!(cmds[1].2.contains(r#""phase":"start""#));
511
+ assert!(cmds[3].2.contains(r#""phase":"end""#));
512
+ assert!(cmds[4].2.contains(r#""direction":"left""#), "{}", cmds[4].2);
513
+ assert!(!kinds(&cmds).contains(&"topPress"));
514
+ }
515
+
516
+ #[test]
517
+ fn a_vertical_drag_on_a_pannable_row_is_left_to_its_scroll_view() {
518
+ let mut g = tree(PRESS | SWIPE, true);
519
+ let cmds = run(
520
+ &mut g,
521
+ &[
522
+ (DOWN, 10.0, 10.0, 0),
523
+ (MOVE, 12.0, 40.0, 16),
524
+ (UP, 12.0, 120.0, 40),
525
+ ],
526
+ );
527
+ assert!(cmds.is_empty(), "{cmds:?}");
528
+ }
529
+
530
+ #[test]
531
+ fn a_slow_pan_is_no_swipe_and_a_cancel_ends_the_pan() {
532
+ let mut g = tree(PAN | SWIPE, false);
533
+ let slow = run(
534
+ &mut g,
535
+ &[
536
+ (DOWN, 0.0, 0.0, 0),
537
+ (MOVE, 20.0, 0.0, 200),
538
+ (MOVE, 40.0, 0.0, 400),
539
+ (UP, 41.0, 0.0, 600),
540
+ ],
541
+ );
542
+ assert_eq!(kinds(&slow), vec!["claim", "topPan", "topPan", "topPan"]);
543
+ let cancelled = run(
544
+ &mut g,
545
+ &[
546
+ (DOWN, 0.0, 0.0, 0),
547
+ (MOVE, 30.0, 0.0, 16),
548
+ (CANCEL, 30.0, 0.0, 32),
549
+ ],
550
+ );
551
+ assert_eq!(kinds(&cancelled), vec!["claim", "topPan", "topPan"]);
552
+ assert!(cancelled[2].2.contains(r#""phase":"cancel""#));
553
+ }
554
+
555
+ #[test]
556
+ fn a_touch_outside_any_pressable_does_nothing() {
557
+ let mut g = tree(0, false);
558
+ let cmds = run(&mut g, &[(DOWN, 1.0, 1.0, 0), (UP, 1.0, 1.0, 10)]);
559
+ assert!(cmds.is_empty());
560
+ }
561
+ }