lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! What one finger comes to: a pointer where nothing scrolls, and over content that scrolls a tap, a scroll with
//! its momentum or a long press.

use std::time::{Duration, Instant};

use super::finger::{LONG_PRESS, PRESS_DELAY, TouchAction, TouchPointer};
use crate::app::{
  events::{ScrollPhase, TouchPhase},
  runtime::TouchMomentum,
};

const SCALE: f64 = 3.0;
const FINGER: u64 = 7;
const START: (f64, f64) = (100.0, 400.0);
const PRESS: TouchAction = TouchAction::Press { x: 100.0, y: 400.0 };
const RELEASE: TouchAction = TouchAction::Release {
  x: 100.0,
  y: 400.0,
  tap: true,
};
const TAP: TouchAction = TouchAction::Tap { x: 100.0, y: 400.0 };
const LONG: TouchAction = TouchAction::LongPress { x: 100.0, y: 400.0 };
/// The gesture a finger going up from [`START`] starts.
const TARGET_UP: TouchAction = TouchAction::ScrollTarget {
  x: 100.0,
  y: 400.0,
  dx: 0.0,
  dy: -40.0,
};

fn ms(millis: u64) -> Duration {
  Duration::from_millis(millis)
}

/// One event of the finger over content that scrolls and is at rest.
fn touch(pointer: &mut TouchPointer, phase: TouchPhase, position: (f64, f64), now: Instant) -> Vec<TouchAction> {
  pointer.touch(FINGER, phase, position, now, SCALE, false, |_| true)
}

fn scroll(dx: f64, dy: f64, phase: ScrollPhase) -> TouchAction {
  TouchAction::Scroll {
    x: START.0,
    y: START.1,
    dx,
    dy,
    phase,
  }
}

/// A finger that lands at [`START`] and moves up by `step` every 10 ms, `steps` times. Gives the time of the
/// last move and where it ended.
fn drag_up(pointer: &mut TouchPointer, start: Instant, step: f64, steps: u32) -> (Instant, (f64, f64)) {
  touch(pointer, TouchPhase::Start, START, start);
  let mut now = start;
  let mut position = START;
  for _ in 0..steps {
    now += ms(10);
    position.1 -= step;
    touch(pointer, TouchPhase::Move, position, now);
  }
  (now, position)
}

fn total_scroll(actions: &[TouchAction]) -> f64 {
  actions
    .iter()
    .map(|action| match action {
      TouchAction::Scroll { dy, .. } => *dy,
      _ => 0.0,
    })
    .sum()
}

#[test]
fn where_nothing_scrolls_a_finger_is_the_pointer_with_its_button_down() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();
  let mut event = |phase, position, at| pointer.touch(FINGER, phase, position, at, SCALE, false, |_| false);

  let down = event(TouchPhase::Start, START, start);
  let moved = event(TouchPhase::Move, (100.0, 200.0), start + ms(20));
  let up = event(TouchPhase::End, (100.0, 200.0), start + ms(900));

  assert_eq!(down, [PRESS]);
  assert_eq!(moved, [TouchAction::Move { x: 100.0, y: 200.0 }]);
  assert_eq!(
    up,
    [TouchAction::Release {
      x: 100.0,
      y: 200.0,
      tap: false
    }],
    "it travelled: no tap"
  );
  assert_eq!(pointer.tick(start + LONG_PRESS), []);
  assert_eq!(pointer.next_deadline(), None);
}

#[test]
fn a_finger_that_rests_where_nothing_scrolls_is_a_long_press_unless_it_travelled() {
  let start = Instant::now();
  let event = |pointer: &mut TouchPointer, phase, position, at| {
    pointer.touch(FINGER, phase, position, at, SCALE, false, |_| false)
  };

  let mut resting = TouchPointer::default();
  event(&mut resting, TouchPhase::Start, START, start);
  assert_eq!(resting.next_deadline(), Some(start + LONG_PRESS));
  // Within the slop: still resting.
  event(&mut resting, TouchPhase::Move, (110.0, 410.0), start + ms(100));
  assert_eq!(resting.tick(start + LONG_PRESS - ms(1)), []);
  assert_eq!(resting.tick(start + LONG_PRESS), [LONG]);
  assert_eq!(resting.tick(start + LONG_PRESS + ms(8)), []);
  assert_eq!(resting.next_deadline(), None);

  let mut travelling = TouchPointer::default();
  event(&mut travelling, TouchPhase::Start, START, start);
  event(&mut travelling, TouchPhase::Move, (100.0, 300.0), start + ms(100));
  assert_eq!(travelling.next_deadline(), None);
  assert_eq!(travelling.tick(start + LONG_PRESS), []);
}

#[test]
fn a_long_press_a_handler_took_clicks_nothing() {
  let start = Instant::now();
  let event = |pointer: &mut TouchPointer, phase, position, at| {
    pointer.touch(FINGER, phase, position, at, SCALE, false, |_| false)
  };

  let mut taken = TouchPointer::default();
  event(&mut taken, TouchPhase::Start, START, start);
  taken.tick(start + LONG_PRESS);
  taken.long_press_taken();
  let up = event(&mut taken, TouchPhase::End, START, start + ms(700));
  assert_eq!(up, [TouchAction::CancelPress { x: 100.0, y: 400.0 }]);

  let mut left = TouchPointer::default();
  event(&mut left, TouchPhase::Start, START, start);
  left.tick(start + LONG_PRESS);
  let up = event(&mut left, TouchPhase::End, START, start + ms(700));
  assert_eq!(up, [RELEASE]);
}

#[test]
fn a_finger_over_a_list_hovers_nothing_and_presses_nothing_at_first() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();

  let down = touch(&mut pointer, TouchPhase::Start, START, start);
  // Within the slop: 10 logical pixels are 30 physical ones at this scale.
  let moved = touch(&mut pointer, TouchPhase::Move, (110.0, 420.0), start + ms(30));

  assert_eq!(down, []);
  assert_eq!(moved, []);
}

#[test]
fn a_quick_tap_is_a_tap_at_once_where_it_landed() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();
  touch(&mut pointer, TouchPhase::Start, START, start);

  let up = touch(&mut pointer, TouchPhase::End, (110.0, 420.0), start + ms(60));

  assert_eq!(up, [TAP]);
  assert_eq!(pointer.next_deadline(), None);
}

#[test]
fn two_quick_taps_are_two_taps() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();
  touch(&mut pointer, TouchPhase::Start, START, start);
  let first = touch(&mut pointer, TouchPhase::End, START, start + ms(40));

  let down = touch(&mut pointer, TouchPhase::Start, START, start + ms(70));
  let second = touch(&mut pointer, TouchPhase::End, START, start + ms(110));

  assert_eq!((first, down, second), (vec![TAP], vec![], vec![TAP]));
}

#[test]
fn a_finger_that_rests_presses_before_it_lifts() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();
  touch(&mut pointer, TouchPhase::Start, START, start);

  assert_eq!(pointer.next_deadline(), Some(start + PRESS_DELAY));
  assert_eq!(pointer.tick(start + PRESS_DELAY - ms(1)), []);
  let pressed = pointer.tick(start + PRESS_DELAY);
  let again = pointer.tick(start + PRESS_DELAY + ms(8));
  let up = touch(&mut pointer, TouchPhase::End, START, start + ms(300));

  assert_eq!(pressed, [PRESS]);
  assert_eq!(again, []);
  assert_eq!(up, [RELEASE]);
  assert_eq!(pointer.next_deadline(), None);
}

#[test]
fn a_finger_that_moves_past_the_slop_scrolls_from_where_the_content_is() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();
  touch(&mut pointer, TouchPhase::Start, START, start);

  let first = touch(&mut pointer, TouchPhase::Move, (100.0, 360.0), start + ms(10));
  let second = touch(&mut pointer, TouchPhase::Move, (104.0, 340.0), start + ms(20));

  // The 40 that told the scroll from a tap pick the gesture's way and move nothing.
  assert_eq!(first, [TARGET_UP, scroll(0.0, 0.0, ScrollPhase::Start)]);
  assert_eq!(second, [scroll(4.0, -20.0, ScrollPhase::Scroll)]);
}

#[test]
fn a_scroll_that_starts_after_the_press_ends_the_press_without_a_click() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();
  touch(&mut pointer, TouchPhase::Start, START, start);
  pointer.tick(start + PRESS_DELAY);

  let moved = touch(&mut pointer, TouchPhase::Move, (100.0, 360.0), start + ms(200));

  assert_eq!(
    moved,
    [
      TouchAction::CancelPress { x: 100.0, y: 400.0 },
      TARGET_UP,
      scroll(0.0, 0.0, ScrollPhase::Start)
    ]
  );
}

#[test]
fn a_long_press_keeps_the_pointer_from_scrolling() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();
  touch(&mut pointer, TouchPhase::Start, START, start);
  pointer.tick(start + PRESS_DELAY);

  assert_eq!(pointer.next_deadline(), Some(start + LONG_PRESS));
  assert_eq!(pointer.tick(start + LONG_PRESS), [LONG]);
  assert_eq!(pointer.next_deadline(), None);
  let moved = touch(
    &mut pointer,
    TouchPhase::Move,
    (100.0, 300.0),
    start + LONG_PRESS + ms(50),
  );
  let up = touch(
    &mut pointer,
    TouchPhase::End,
    (100.0, 300.0),
    start + LONG_PRESS + ms(80),
  );

  assert_eq!(moved, [TouchAction::Move { x: 100.0, y: 300.0 }]);
  assert_eq!(
    up,
    [TouchAction::Release {
      x: 100.0,
      y: 300.0,
      tap: false
    }],
    "a long press that travelled is no tap"
  );
}

#[test]
fn a_long_press_no_turn_ran_during_still_presses() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();
  touch(&mut pointer, TouchPhase::Start, START, start);

  assert_eq!(pointer.tick(start + LONG_PRESS + ms(40)), [PRESS, LONG]);
  let moved = touch(
    &mut pointer,
    TouchPhase::Move,
    (100.0, 300.0),
    start + LONG_PRESS + ms(50),
  );
  assert_eq!(moved, [TouchAction::Move { x: 100.0, y: 300.0 }]);
}

#[test]
fn a_finger_that_stops_before_it_lifts_leaves_no_momentum() {
  let mut pointer = TouchPointer::default();
  let (last_move, position) = drag_up(&mut pointer, Instant::now(), 40.0, 5);

  let up = touch(&mut pointer, TouchPhase::End, position, last_move + ms(300));

  assert_eq!(up, [scroll(0.0, 0.0, ScrollPhase::End)]);
  assert_eq!(pointer.next_deadline(), None);
}

#[test]
fn a_flick_keeps_scrolling_the_way_it_went_and_comes_to_rest() {
  let mut pointer = TouchPointer::default();
  let (last_move, position) = drag_up(&mut pointer, Instant::now(), 40.0, 5);

  let up = touch(&mut pointer, TouchPhase::End, position, last_move + ms(5));
  assert_eq!(
    up,
    [scroll(0.0, 0.0, ScrollPhase::End), scroll(0.0, 0.0, ScrollPhase::Start)]
  );

  let mut now = last_move + ms(5);
  let mut momentum = Vec::new();
  let mut steps = Vec::new();
  while let Some(deadline) = pointer.next_deadline() {
    assert!(deadline > now, "momentum asks for a later turn");
    // A 120 Hz display's frame.
    now += ms(8);
    let actions = pointer.tick(now);
    steps.push(total_scroll(&actions));
    momentum.extend(actions);
    assert!(now < last_move + ms(10_000), "momentum ends");
  }

  assert!(steps.iter().all(|step| *step < 0.0), "every step goes up: {steps:?}");
  assert!(
    steps.windows(2).all(|pair| pair[1].abs() < pair[0].abs()),
    "every step is shorter than the one before"
  );
  assert_eq!(momentum.last(), Some(&scroll(0.0, 0.0, ScrollPhase::End)));
  // The finger moved 160 px in 45 ms: momentum carries the content well beyond one more such stretch.
  assert!(total_scroll(&momentum) < -500.0, "{}", total_scroll(&momentum));
}

#[test]
fn momentum_that_moves_nothing_is_ended_with_the_velocity_it_had() {
  let mut pointer = TouchPointer::default();
  let (last_move, position) = drag_up(&mut pointer, Instant::now(), 40.0, 5);
  touch(&mut pointer, TouchPhase::End, position, last_move + ms(5));
  assert!(pointer.next_deadline().is_some());

  let (end, velocity) = pointer.end_fling().expect("momentum to end");
  assert_eq!(end, scroll(0.0, 0.0, ScrollPhase::End));
  assert!(velocity.0 == 0.0 && velocity.1 < 0.0, "it was going up: {velocity:?}");
  assert_eq!(pointer.end_fling(), None);
  assert_eq!(pointer.next_deadline(), None);
  // The next touch is a tap again, not one that catches momentum.
  let now = last_move + ms(50);
  touch(&mut pointer, TouchPhase::Start, START, now);
  assert_eq!(touch(&mut pointer, TouchPhase::End, START, now + ms(30)), [TAP]);
}

#[test]
fn a_touch_during_momentum_stops_it_and_presses_nothing() {
  let mut pointer = TouchPointer::default();
  let (last_move, position) = drag_up(&mut pointer, Instant::now(), 40.0, 5);
  let lifted = last_move + ms(5);
  touch(&mut pointer, TouchPhase::End, position, lifted);
  pointer.tick(lifted + ms(8));

  let down = touch(&mut pointer, TouchPhase::Start, (200.0, 300.0), lifted + ms(16));
  let rested = pointer.tick(lifted + ms(16) + LONG_PRESS);
  let up = touch(
    &mut pointer,
    TouchPhase::End,
    (200.0, 300.0),
    lifted + ms(40) + LONG_PRESS,
  );

  assert_eq!(down, [scroll(0.0, 0.0, ScrollPhase::End)]);
  assert_eq!(rested, []);
  assert_eq!(up, [TouchAction::Settle]);
  assert_eq!(pointer.next_deadline(), None);
}

#[test]
fn a_finger_that_catches_moving_content_scrolls_on_with_its_first_move() {
  let mut pointer = TouchPointer::default();
  let (last_move, position) = drag_up(&mut pointer, Instant::now(), 40.0, 5);
  let lifted = last_move + ms(5);
  touch(&mut pointer, TouchPhase::End, position, lifted);
  touch(&mut pointer, TouchPhase::Start, START, lifted + ms(16));

  // Three physical pixels: far within the slop of a touch on content at rest.
  let moved = touch(&mut pointer, TouchPhase::Move, (100.0, 397.0), lifted + ms(24));

  assert_eq!(
    moved,
    [
      TouchAction::ScrollTarget {
        x: 100.0,
        y: 400.0,
        dx: 0.0,
        dy: -3.0
      },
      scroll(0.0, 0.0, ScrollPhase::Start)
    ]
  );
}

#[test]
fn a_finger_that_lands_on_content_springing_back_holds_it_and_lets_it_go() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();

  let down = pointer.touch(FINGER, TouchPhase::Start, START, start, SCALE, true, |_| true);
  let rested = pointer.tick(start + LONG_PRESS);
  let up = pointer.touch(FINGER, TouchPhase::End, START, start + ms(700), SCALE, false, |_| true);

  assert_eq!(down, []);
  assert_eq!(rested, [], "it presses nothing");
  assert_eq!(up, [TouchAction::Settle]);
}

#[test]
fn a_cancelled_touch_clicks_nothing() {
  let start = Instant::now();

  let mut resting = TouchPointer::default();
  touch(&mut resting, TouchPhase::Start, START, start);
  let cancelled = touch(&mut resting, TouchPhase::Cancel, START, start + ms(20));
  assert_eq!(cancelled, []);
  assert_eq!(resting.next_deadline(), None);

  let mut pressing = TouchPointer::default();
  touch(&mut pressing, TouchPhase::Start, START, start);
  pressing.tick(start + PRESS_DELAY);
  let cancelled = touch(&mut pressing, TouchPhase::Cancel, (120.0, 400.0), start + ms(200));
  assert_eq!(cancelled, [TouchAction::CancelPress { x: 120.0, y: 400.0 }]);

  let mut scrolling = TouchPointer::default();
  let (last_move, position) = drag_up(&mut scrolling, start, 40.0, 5);
  let cancelled = touch(&mut scrolling, TouchPhase::Cancel, position, last_move + ms(5));
  assert_eq!(cancelled, [scroll(0.0, 0.0, ScrollPhase::End)]);
  assert_eq!(scrolling.next_deadline(), None);
}

#[test]
fn a_second_finger_is_ignored_before_a_scroll_starts() {
  let mut pointer = TouchPointer::default();
  let start = Instant::now();
  touch(&mut pointer, TouchPhase::Start, START, start);

  let mut second = |phase, position, at| pointer.touch(8, phase, position, at, SCALE, false, |_| true);
  let down = second(TouchPhase::Start, (300.0, 300.0), start + ms(10));
  let moved = second(TouchPhase::Move, (300.0, 100.0), start + ms(20));
  let up = second(TouchPhase::End, (300.0, 100.0), start + ms(30));
  let first_up = touch(&mut pointer, TouchPhase::End, START, start + ms(40));

  assert_eq!((down, moved, up), (vec![], vec![], vec![]));
  assert_eq!(first_up, [TAP]);
}

#[test]
fn a_second_finger_joins_a_scroll_and_carries_it_on_when_the_first_lifts() {
  let mut pointer = TouchPointer::default();
  let (last_move, first_at) = drag_up(&mut pointer, Instant::now(), 40.0, 3);
  let mut second = |phase, position, at| pointer.touch(8, phase, position, at, SCALE, false, |_| true);

  // It lands elsewhere: the content stays where it is.
  let joined = second(TouchPhase::Start, (300.0, 600.0), last_move + ms(10));
  assert_eq!(joined, []);
  // It moves 60 up while the first stays: the middle of the two moves 30.
  let moved = second(TouchPhase::Move, (300.0, 540.0), last_move + ms(20));
  assert_eq!(moved, [scroll(0.0, -30.0, ScrollPhase::Scroll)]);

  // The first lifts: nothing ends, and the second scrolls alone, by all of its travel.
  let lifted = touch(&mut pointer, TouchPhase::End, first_at, last_move + ms(30));
  assert_eq!(lifted, []);
  let mut second = |phase, position, at| pointer.touch(8, phase, position, at, SCALE, false, |_| true);
  let alone = second(TouchPhase::Move, (300.0, 500.0), last_move + ms(40));
  assert_eq!(alone, [scroll(0.0, -40.0, ScrollPhase::Scroll)]);

  // The last finger lifts after a rest: the scroll ends.
  let ended = second(TouchPhase::End, (300.0, 500.0), last_move + ms(400));
  assert_eq!(ended, [scroll(0.0, 0.0, ScrollPhase::End)]);
  assert_eq!(pointer.next_deadline(), None);
}

/// How far momentum carries the content after the same flick.
fn momentum_distance(momentum: TouchMomentum) -> f64 {
  let mut pointer = TouchPointer::default();
  pointer.set_momentum(momentum);
  let (last_move, position) = drag_up(&mut pointer, Instant::now(), 40.0, 5);
  touch(&mut pointer, TouchPhase::End, position, last_move + ms(5));
  let mut now = last_move + ms(5);
  let mut distance = 0.0;
  while pointer.next_deadline().is_some() {
    now += ms(8);
    distance += total_scroll(&pointer.tick(now));
    assert!(now < last_move + ms(20_000), "momentum ends");
  }
  -distance
}

#[test]
fn the_apps_momentum_decides_how_far_a_flick_goes() {
  let system = momentum_distance(TouchMomentum::default());
  let fast = momentum_distance(TouchMomentum {
    deceleration_rate: 0.99,
    ..TouchMomentum::default()
  });
  let capped = momentum_distance(TouchMomentum {
    max_speed: 300.0,
    ..TouchMomentum::default()
  });
  let none = momentum_distance(TouchMomentum {
    min_speed: 100_000.0,
    ..TouchMomentum::default()
  });

  // The fast rate keeps a hundredth less of its speed each millisecond: about a fifth of the distance.
  assert!(fast > system * 0.15 && fast < system * 0.25, "{system} then {fast}");
  assert!(capped < system * 0.5, "{system} then {capped}");
  assert_eq!(none, 0.0);
}

#[test]
fn a_pointer_given_up_to_a_gesture_ends_what_it_was_doing_without_a_click() {
  let start = Instant::now();

  let mut resting = TouchPointer::default();
  touch(&mut resting, TouchPhase::Start, START, start);
  assert_eq!(resting.abandon(), []);
  assert_eq!(
    touch(&mut resting, TouchPhase::End, START, start + ms(40)),
    [],
    "no tap follows"
  );

  let mut pressing = TouchPointer::default();
  touch(&mut pressing, TouchPhase::Start, START, start);
  pressing.tick(start + PRESS_DELAY);
  assert_eq!(pressing.abandon(), [TouchAction::CancelPress { x: 100.0, y: 400.0 }]);

  let mut scrolling = TouchPointer::default();
  let (last_move, position) = drag_up(&mut scrolling, start, 40.0, 5);
  assert_eq!(scrolling.abandon(), [scroll(0.0, 0.0, ScrollPhase::End)]);
  assert_eq!(touch(&mut scrolling, TouchPhase::End, position, last_move + ms(5)), []);
  assert_eq!(scrolling.next_deadline(), None, "and no momentum");
}