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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//! Where a moving finger scrolls, and whether a scroll it sent moved anything.

use super::{HeadlessSurface, Tree};
use crate::{
  app::{App, events::ScrollPhase},
  components::{Column, ScrollVertical, Slider, Spacer},
  core::Signal,
};

/// At scale 2 in a 400 by 400 window: a slider over the top 20 logical pixels of a list 180 high with 1000 of
/// content, and beside the list nothing.
fn list_under_a_slider() -> (Tree, App) {
  let mut app = App::new();
  let mut tree = Tree::new();
  tree.set_scale_factor(2.0);
  tree.resize(400, 400);
  tree.set_root(
    Column::new()
      .child(Slider::new(Signal::new(0)).range(0, 10).width(100.0).height(20.0))
      .child(
        ScrollVertical::new(Spacer::new().width(100.0).height(1000.0))
          .width(100.0)
          .height(180.0),
      ),
  );
  tree.pass(&mut app, &HeadlessSurface);
  (tree, app)
}

#[test]
fn a_finger_scrolls_over_a_list_with_more_content_and_nowhere_else() {
  let (mut tree, _app) = list_under_a_slider();

  assert!(tree.touch_scrolls_at(100.0, 200.0), "over the list");
  assert!(!tree.touch_scrolls_at(100.0, 20.0), "over the slider");
  assert!(!tree.touch_scrolls_at(300.0, 200.0), "beside the list");
}

#[test]
fn a_list_that_shows_all_its_content_does_not_scroll() {
  let mut app = App::new();
  let mut tree = Tree::new();
  tree.resize(400, 400);
  tree.set_root(ScrollVertical::new(Spacer::new().width(100.0).height(50.0)).size(100.0, 200.0));
  tree.pass(&mut app, &HeadlessSurface);

  assert!(!tree.touch_scrolls_at(50.0, 100.0));
}

#[test]
fn the_content_moves_as_far_as_the_finger() {
  let mut app = App::new();
  let mut tree = Tree::new();
  tree.set_scale_factor(3.0);
  tree.resize(600, 600);
  let state = crate::layout::layout_kind::ScrollState::new();
  tree.set_root(
    ScrollVertical::new(Spacer::new().width(100.0).height(1000.0))
      .with_scroll_state(state.clone())
      .size(100.0, 100.0),
  );
  tree.pass(&mut app, &HeadlessSurface);

  // The finger travels 90 physical pixels up: 30 logical ones.
  tree.touch_scroll_begin(150.0, 150.0, 0.0, -90.0);
  tree.touch_scroll(150.0, 150.0, 0.0, -90.0, ScrollPhase::Scroll, false);

  assert_eq!(state.scroll_y(), 30.0);
}

#[test]
fn momentum_tells_whether_the_list_moved() {
  let (mut tree, _app) = list_under_a_slider();

  // Going up brings later content: the list moves until its end.
  tree.touch_scroll_begin(100.0, 200.0, 0.0, -100.0);
  assert!(tree.touch_scroll(100.0, 200.0, 0.0, -100.0, ScrollPhase::Scroll, true));
  assert!(tree.touch_scroll(100.0, 200.0, 0.0, -100_000.0, ScrollPhase::Scroll, true));
  assert!(
    !tree.touch_scroll(100.0, 200.0, 0.0, -100.0, ScrollPhase::Scroll, true),
    "at its end"
  );
  tree.touch_scroll_begin(300.0, 200.0, 0.0, -100.0);
  assert!(
    !tree.touch_scroll(300.0, 200.0, 0.0, -100.0, ScrollPhase::Scroll, true),
    "beside it"
  );
}

/// The page holds rows and, further down, a list of its own. Scrolling the page brings that list under the
/// finger, which goes on scrolling the page.
#[test]
fn a_gesture_keeps_scrolling_what_it_started_on() {
  use crate::layout::layout_kind::ScrollState;

  let mut app = App::new();
  let mut tree = Tree::new();
  tree.resize(200, 200);
  let (page, inner) = (ScrollState::new(), ScrollState::new());
  tree.set_root(
    ScrollVertical::new(
      Column::new()
        .child(Spacer::new().width(200.0).height(300.0))
        .child(
          ScrollVertical::new(Spacer::new().width(200.0).height(2000.0))
            .with_scroll_state(inner.clone())
            .size(200.0, 300.0),
        )
        .child(Spacer::new().width(200.0).height(2000.0)),
    )
    .with_scroll_state(page.clone())
    .size(200.0, 200.0),
  );
  tree.pass(&mut app, &HeadlessSurface);

  // The finger lands on the first rows; after 250 the inner list is under that point.
  tree.touch_scroll_begin(100.0, 100.0, 0.0, -250.0);
  tree.touch_scroll(100.0, 100.0, 0.0, -250.0, ScrollPhase::Scroll, false);
  tree.pass(&mut app, &HeadlessSurface);
  tree.touch_scroll(100.0, 100.0, 0.0, -100.0, ScrollPhase::Scroll, false);

  assert_eq!((page.scroll_y(), inner.scroll_y()), (350.0, 0.0));

  // A gesture that starts on the inner list scrolls it.
  tree.pass(&mut app, &HeadlessSurface);
  tree.touch_scroll_begin(100.0, 100.0, 0.0, -40.0);
  tree.touch_scroll(100.0, 100.0, 0.0, -40.0, ScrollPhase::Scroll, false);

  assert_eq!((page.scroll_y(), inner.scroll_y()), (350.0, 40.0));

  // Past the inner list's end the gesture moves nothing more: the page does not take the rest of it.
  tree.touch_scroll(100.0, 100.0, 0.0, -5000.0, ScrollPhase::Scroll, false);

  assert_eq!((page.scroll_y(), inner.scroll_y()), (350.0, 1700.0));

  // The next gesture the same way, started on the inner list at its end, scrolls the page.
  tree.pass(&mut app, &HeadlessSurface);
  tree.touch_scroll_begin(100.0, 100.0, 0.0, -30.0);
  tree.touch_scroll(100.0, 100.0, 0.0, -30.0, ScrollPhase::Scroll, false);

  assert_eq!((page.scroll_y(), inner.scroll_y()), (380.0, 1700.0));

  // And one the other way scrolls the inner list back.
  tree.pass(&mut app, &HeadlessSurface);
  tree.touch_scroll_begin(100.0, 100.0, 0.0, 30.0);
  tree.touch_scroll(100.0, 100.0, 0.0, 30.0, ScrollPhase::Scroll, false);

  assert_eq!((page.scroll_y(), inner.scroll_y()), (380.0, 1670.0));
}

/// A strip that scrolls sideways inside a page that scrolls down.
#[test]
fn the_travels_main_axis_picks_between_a_strip_and_the_page() {
  use crate::{components::ScrollHorizontal, layout::layout_kind::ScrollState};

  let mut app = App::new();
  let mut tree = Tree::new();
  tree.resize(200, 200);
  let (page, strip) = (ScrollState::new(), ScrollState::new());
  tree.set_root(
    ScrollVertical::new(
      Column::new()
        .child(
          ScrollHorizontal::new(Spacer::new().width(2000.0).height(100.0))
            .with_scroll_state(strip.clone())
            .size(200.0, 100.0),
        )
        .child(Spacer::new().width(200.0).height(2000.0)),
    )
    .with_scroll_state(page.clone())
    .size(200.0, 200.0),
  );
  tree.pass(&mut app, &HeadlessSurface);

  // Mostly sideways: the strip takes it, and the page gets none of its slant.
  tree.touch_scroll_begin(100.0, 50.0, -40.0, -10.0);
  tree.touch_scroll(100.0, 50.0, -40.0, -10.0, ScrollPhase::Scroll, false);
  assert_eq!((strip.scroll_x(), page.scroll_y()), (40.0, 0.0));

  // Mostly down the page, from the strip: the page takes it.
  tree.pass(&mut app, &HeadlessSurface);
  tree.touch_scroll_begin(100.0, 50.0, -10.0, -40.0);
  tree.touch_scroll(100.0, 50.0, -10.0, -40.0, ScrollPhase::Scroll, false);
  assert_eq!((strip.scroll_x(), page.scroll_y()), (40.0, 40.0));
}

/// Two taps of a finger land a few points apart, never on the same pixel.
#[test]
fn two_taps_a_few_points_apart_are_a_double_click() {
  use std::sync::{Arc, Mutex};

  use crate::{app::events::MouseButton, components::Rect};

  let mut app = App::new();
  let mut tree = Tree::new();
  tree.set_scale_factor(3.0);
  tree.resize(600, 600);
  tree.set_double_click_distance(40.0 * 3.0);
  let log = Arc::new(Mutex::new(Vec::new()));
  let (downs, ups, clicks, doubles) = (log.clone(), log.clone(), log.clone(), log.clone());
  tree.set_root(
    Rect::new(200.0, 200.0)
      .on_mouse_down(move |_| downs.lock().unwrap().push("down"))
      .on_mouse_up(move |_| ups.lock().unwrap().push("up"))
      .on_click(move |_| clicks.lock().unwrap().push("click"))
      .on_dblclick(move |_| doubles.lock().unwrap().push("double")),
  );
  tree.pass(&mut app, &HeadlessSurface);

  // 10 logical pixels apart: 30 physical ones.
  for (x, y) in [(300.0, 300.0), (324.0, 318.0)] {
    tree.mouse_down(x, y, MouseButton::Left);
    tree.mouse_up(x, y, MouseButton::Left);
  }

  // The second tap is a press and a release as well, and clicks nothing: the two are the double click.
  assert_eq!(*log.lock().unwrap(), ["down", "up", "down", "double", "up"]);
}

/// A list 100 high with 1000 of content, at scale 1, in a window it fills: the middle of the window, where a
/// tap on the status bar looks for the page, is in the list.
fn list() -> (Tree, App, crate::layout::layout_kind::ScrollState) {
  let mut app = App::new();
  let mut tree = Tree::new();
  tree.resize(100, 100);
  let state = crate::layout::layout_kind::ScrollState::new();
  tree.set_root(
    ScrollVertical::new(Spacer::new().width(100.0).height(1000.0))
      .with_scroll_state(state.clone())
      .size(100.0, 100.0),
  );
  tree.pass(&mut app, &HeadlessSurface);
  (tree, app, state)
}

#[test]
fn a_finger_pulls_a_list_past_its_start_less_than_it_travels_and_lets_it_spring_back() {
  use std::time::{Duration, Instant};

  let (mut tree, _app, state) = list();

  // Down from the top: there is nothing above to scroll to.
  tree.touch_scroll_begin(50.0, 50.0, 0.0, 40.0);
  assert!(tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false));
  let pulled = state.overscroll().1;
  assert!(
    (-40.0..-10.0).contains(&pulled),
    "drawn before the start, by less than 40: {pulled}"
  );
  assert_eq!(state.scroll_y(), 0.0);

  // Further, it gives less and less.
  tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false);
  let further = state.overscroll().1;
  assert!(
    further < pulled && pulled - further < -pulled,
    "{pulled} then {further}"
  );

  // Back up by half the travel: the pull lets go first, and the list does not scroll.
  tree.touch_scroll(50.0, 50.0, 0.0, -40.0, ScrollPhase::Scroll, false);
  assert_eq!((state.overscroll().1, state.scroll_y()), (pulled, 0.0));

  // The finger lifts: the list springs back and comes to rest at its start.
  tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
  assert!(tree.touch_animating());
  // The momentum's first event follows the lift at once and leaves the list where the spring holds it.
  tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::Start, false);
  assert_eq!(state.overscroll().1, pulled);
  let lifted = Instant::now();
  assert!(tree.touch_tick(lifted + Duration::from_millis(50)));
  let halfway = state.overscroll().1;
  assert!(pulled < halfway && halfway < 0.0, "{pulled} then {halfway}");
  assert!(!tree.touch_tick(lifted + Duration::from_secs(2)));
  assert_eq!(state.overscroll(), (0.0, 0.0));
  assert!(!tree.touch_animating());
}

#[test]
fn travel_past_the_end_and_back_scrolls_only_once_the_pull_is_let_go() {
  let (mut tree, _app, state) = list();

  tree.touch_scroll_begin(50.0, 50.0, 0.0, 30.0);
  tree.touch_scroll(50.0, 50.0, 0.0, 30.0, ScrollPhase::Scroll, false);
  // 30 back lets the pull go, the other 20 scroll.
  tree.touch_scroll(50.0, 50.0, 0.0, -50.0, ScrollPhase::Scroll, false);

  assert_eq!((state.overscroll().1, state.scroll_y()), (0.0, 20.0));
}

#[test]
fn momentum_that_reaches_the_end_carries_the_list_past_it_and_back() {
  use std::time::{Duration, Instant};

  let (mut tree, _app, state) = list();
  tree.touch_scroll_begin(50.0, 50.0, 0.0, -40.0);
  tree.touch_scroll(50.0, 50.0, 0.0, -100_000.0, ScrollPhase::Scroll, true);
  assert_eq!(
    (state.scroll_y(), state.overscroll().1),
    (900.0, 0.0),
    "momentum itself pulls nothing"
  );
  assert!(!tree.touch_scroll(50.0, 50.0, 0.0, -10.0, ScrollPhase::Scroll, true));

  // The finger was going up at 1500 px/s.
  let reached = Instant::now();
  tree.touch_scroll_bounce(0.0, -1500.0, reached);
  assert!(tree.touch_tick(reached + Duration::from_millis(60)));
  let out = state.overscroll().1;
  assert!(out > 10.0, "carried beyond the end: {out}");
  assert!(!tree.touch_tick(reached + Duration::from_secs(2)));
  assert_eq!((state.scroll_y(), state.overscroll().1), (900.0, 0.0));
}

#[test]
fn momentum_that_stops_short_of_an_end_does_not_bounce() {
  let (mut tree, _app, state) = list();
  tree.touch_scroll_begin(50.0, 50.0, 0.0, -40.0);
  tree.touch_scroll(50.0, 50.0, 0.0, -40.0, ScrollPhase::Scroll, true);

  tree.touch_scroll_bounce(0.0, -1500.0, std::time::Instant::now());

  assert!(!tree.touch_animating());
  assert_eq!(state.overscroll(), (0.0, 0.0));
}

#[test]
fn a_list_that_shows_all_its_content_is_not_pulled() {
  let mut app = App::new();
  let mut tree = Tree::new();
  tree.resize(200, 200);
  let state = crate::layout::layout_kind::ScrollState::new();
  tree.set_root(
    ScrollVertical::new(Spacer::new().width(100.0).height(50.0))
      .with_scroll_state(state.clone())
      .size(100.0, 100.0),
  );
  tree.pass(&mut app, &HeadlessSurface);

  tree.touch_scroll_begin(50.0, 50.0, 0.0, 40.0);
  tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false);

  assert_eq!(state.overscroll(), (0.0, 0.0));
}

#[test]
fn overscrolled_content_is_laid_out_past_its_end() {
  let (mut tree, mut app, state) = list();
  tree.touch_scroll_begin(50.0, 50.0, 0.0, 40.0);
  tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false);
  assert!(tree.needs_redraw());
  tree.pass(&mut app, &HeadlessSurface);

  // A point just under the list's top is now above its content: the gap the pull opened.
  let gap = -state.overscroll().1;
  assert!(gap > 10.0);
  let layout = tree.last_layout.as_ref().expect("a layout");
  let content = layout.children.first().expect("the list's content");
  // The content is drawn on a whole device pixel (here a logical one), as all that scrolls is.
  assert_eq!(content.offset.y, gap.round());
}

#[test]
fn a_finger_catches_a_list_that_is_springing_back_and_lets_it_go_again() {
  use std::time::{Duration, Instant};

  let (mut tree, _app, state) = list();
  tree.touch_scroll_begin(50.0, 50.0, 0.0, 60.0);
  tree.touch_scroll(50.0, 50.0, 0.0, 60.0, ScrollPhase::Scroll, false);
  tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
  tree.touch_tick(Instant::now() + Duration::from_millis(40));
  let caught_at = state.overscroll().1;
  assert!(caught_at < 0.0);

  // The finger lands: the list stays where it is, however long the finger rests.
  assert!(tree.touch_scroll_catch());
  assert!(!tree.touch_animating());
  assert!(!tree.touch_tick(Instant::now() + Duration::from_secs(1)));
  assert_eq!(state.overscroll().1, caught_at);
  assert!(!tree.touch_scroll_catch(), "nothing is springing any more");

  // It lifts without scrolling: the list springs back from there.
  tree.touch_scroll_settle();
  assert!(tree.touch_animating());
  assert!(!tree.touch_tick(Instant::now() + Duration::from_secs(2)));
  assert_eq!(state.overscroll(), (0.0, 0.0));
}

#[test]
fn a_tap_on_the_status_bar_scrolls_the_page_to_its_top() {
  use std::time::{Duration, Instant};

  let (mut tree, _app, state) = list();
  let now = Instant::now();
  assert!(!tree.touch_scroll_to_top(now), "already at its top");

  state.set_scroll(0.0, 400.0);
  assert!(tree.touch_scroll_to_top(now));
  assert!(tree.touch_animating());
  assert!(tree.touch_tick(now + Duration::from_millis(100)));
  let on_its_way = state.scroll_y();
  assert!(
    0.0 < on_its_way && on_its_way < 200.0,
    "most of the way after a third of the time: {on_its_way}"
  );
  assert!(!tree.touch_tick(now + Duration::from_millis(400)));
  assert_eq!(state.scroll_y(), 0.0);
  assert!(!tree.touch_animating());
}

#[test]
fn a_finger_stops_a_page_on_its_way_to_its_top() {
  use std::time::{Duration, Instant};

  let (mut tree, _app, state) = list();
  let now = Instant::now();
  state.set_scroll(0.0, 400.0);
  tree.touch_scroll_to_top(now);
  tree.touch_tick(now + Duration::from_millis(100));
  let stopped_at = state.scroll_y();

  assert!(tree.touch_scroll_catch());
  assert!(!tree.touch_tick(now + Duration::from_millis(400)));
  assert_eq!(state.scroll_y(), stopped_at);
}

#[test]
fn the_scroll_indicator_shortens_against_the_end_the_list_is_pulled_past() {
  use crate::layout::{layout_kind::ScrollAxis, scrollbar::ScrollBarStyle};

  let (mut tree, mut app, state) = list();
  let style = ScrollBarStyle::default();
  let thumb = |state: &crate::layout::layout_kind::ScrollState| {
    let bar = state
      .scrollbar_geometry_for_axis(ScrollAxis::Vertical, &style)
      .expect("a bar");
    (bar.thumb_y, bar.thumb_height)
  };
  let at_rest = thumb(&state);

  // Pulled past its start: the thumb keeps its top and gives up as much length as the content moved.
  tree.touch_scroll_begin(50.0, 50.0, 0.0, 30.0);
  tree.touch_scroll(50.0, 50.0, 0.0, 30.0, ScrollPhase::Scroll, false);
  tree.pass(&mut app, &HeadlessSurface);
  let pulled = thumb(&state);
  assert_eq!(pulled.0, at_rest.0);
  assert!(
    (pulled.1 - (at_rest.1 + state.overscroll().1)).abs() < 0.01,
    "{at_rest:?} then {pulled:?}"
  );

  // Pulled past its end: it keeps its bottom.
  tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
  tree.touch_tick(std::time::Instant::now() + std::time::Duration::from_secs(2));
  state.set_scroll(0.0, 900.0);
  tree.pass(&mut app, &HeadlessSurface);
  let at_end = thumb(&state);
  tree.touch_scroll_begin(50.0, 50.0, 0.0, -30.0);
  tree.touch_scroll(50.0, 50.0, 0.0, -30.0, ScrollPhase::Scroll, false);
  tree.pass(&mut app, &HeadlessSurface);
  let pulled = thumb(&state);
  assert!(pulled.1 < at_end.1);
  assert!(
    (pulled.0 + pulled.1 - (at_end.0 + at_end.1)).abs() < 0.01,
    "{at_end:?} then {pulled:?}"
  );
}

#[test]
fn a_list_let_go_while_the_finger_still_pulls_goes_on_outward_before_it_springs_back() {
  use std::time::{Duration, Instant};

  let (mut tree, _app, state) = list();
  tree.touch_scroll_begin(50.0, 50.0, 0.0, 40.0);
  tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false);
  tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
  let let_go_at = state.overscroll().1;

  // The finger was going down at 1200 px/s when it lifted; the shell's momentum finds the list at its end.
  let now = Instant::now();
  tree.touch_scroll_bounce(0.0, 1200.0, now);
  tree.touch_tick(now + Duration::from_millis(30));
  let further = state.overscroll().1;
  assert!(
    further < let_go_at,
    "further out than where it was let go: {let_go_at} then {further}"
  );

  assert!(!tree.touch_tick(now + Duration::from_secs(3)));
  assert_eq!(state.overscroll(), (0.0, 0.0));
}

#[test]
fn a_list_that_does_not_scroll_to_top_leaves_the_status_bars_tap_alone() {
  let (mut tree, _app, state) = list();
  state.set_scroll(0.0, 400.0);
  state.set_scrolls_to_top(false);

  assert!(!tree.touch_scroll_to_top(std::time::Instant::now()));
  assert_eq!(state.scroll_y(), 400.0);
}

#[test]
fn a_finger_scrolls_from_a_scroll_indicator_and_drags_a_bar_that_is_always_there() {
  use crate::layout::scrollbar::{ScrollBarStyle, ScrollBarVisibility};

  let list_with = |style: ScrollBarStyle| {
    let mut app = App::new();
    let mut tree = Tree::new();
    tree.resize(200, 200);
    tree.set_root(
      ScrollVertical::new(Spacer::new().width(100.0).height(1000.0))
        .scrollbar(style)
        .size(100.0, 100.0),
    );
    tree.pass(&mut app, &HeadlessSurface);
    // Scrolling shows an indicator; then a frame paints it, which is when a list takes its bar's style: a
    // pass with no window stops after layout.
    tree.touch_scroll_begin(50.0, 50.0, 0.0, -10.0);
    tree.touch_scroll(50.0, 50.0, 0.0, -10.0, ScrollPhase::Scroll, false);
    tree.pass(&mut app, &HeadlessSurface);
    tree.painted_quads();
    tree
  };
  // On the bar, at the list's right edge. A phone's default bar is an indicator, so this one says it stays.
  let mut bar = list_with(ScrollBarStyle {
    width: 12.0,
    padding: 0.0,
    visible: ScrollBarVisibility::Auto,
    ..ScrollBarStyle::thin()
  });
  assert!(!bar.touch_scrolls_at(96.0, 10.0), "a bar is dragged");

  let mut indicator = list_with(ScrollBarStyle {
    width: 12.0,
    padding: 0.0,
    end_inset: None,
    ..ScrollBarStyle::indicator()
  });
  assert!(
    indicator.touch_scrolls_at(96.0, 10.0),
    "an indicator is content to a moving finger"
  );
}

/// A text input over a list of 1000 of content, in a 200 by 400 window; the list's state.
fn input_over_a_list() -> (Tree, App, crate::layout::layout_kind::ScrollState) {
  use crate::components::TextInput;

  let state = crate::layout::layout_kind::ScrollState::new();
  let mut app = App::new();
  let mut tree = Tree::new();
  tree.resize(200, 400);
  tree.set_root(
    Column::new()
      .child(TextInput::new(Signal::new(String::new())).width(200.0).height(40.0))
      .child(
        ScrollVertical::new(Spacer::new().width(200.0).height(1000.0))
          .with_scroll_state(state.clone())
          .width(200.0)
          .height(300.0),
      ),
  );
  tree.pass(&mut app, &HeadlessSurface);
  (tree, app, state)
}

#[test]
fn a_tap_outside_the_text_input_being_typed_into_lets_it_go() {
  let (mut tree, mut app, _state) = input_over_a_list();
  tree.touch_press(100.0, 20.0);
  tree.touch_release(100.0, 20.0);
  tree.pass(&mut app, &HeadlessSurface);
  assert!(tree.focused_text_input(), "a tap on the input gives it the focus");

  tree.touch_scroll_begin(100.0, 200.0, 0.0, -10.0);
  assert!(
    tree.focused_text_input(),
    "a finger that scrolls leaves the keyboard up"
  );

  tree.touch_press(100.0, 200.0);
  tree.touch_release(100.0, 200.0);
  assert!(!tree.focused_text_input(), "a tap outside takes the keyboard down");
}

#[test]
fn scrolling_a_list_that_dismisses_the_keyboard_takes_the_focus_from_the_text_input() {
  let (mut tree, mut app, state) = input_over_a_list();
  tree.touch_press(100.0, 20.0);
  tree.touch_release(100.0, 20.0);
  tree.pass(&mut app, &HeadlessSurface);

  tree.touch_scroll_begin(100.0, 200.0, 0.0, -10.0);
  assert!(tree.focused_text_input(), "a list not set to keeps the keyboard up");

  state.set_dismisses_keyboard(true);
  tree.touch_scroll_begin(100.0, 200.0, 0.0, -10.0);
  assert!(!tree.focused_text_input());
}

#[test]
fn a_list_the_keyboard_follows_a_finger_over_keeps_the_focus_until_the_platform_takes_the_keyboard() {
  use crate::layout::layout_kind::KeyboardDismissal;
  let (mut tree, mut app, state) = input_over_a_list();
  assert!(!tree.touch_keyboard_follows_at(100.0, 200.0), "nothing is typed into");
  tree.touch_press(100.0, 20.0);
  tree.touch_release(100.0, 20.0);
  tree.pass(&mut app, &HeadlessSurface);
  assert!(!tree.touch_keyboard_follows_at(100.0, 200.0), "the list does not ask");

  state.set_keyboard_dismissal(KeyboardDismissal::Interactive);
  assert!(tree.touch_keyboard_follows_at(100.0, 200.0));
  tree.touch_scroll_begin(100.0, 200.0, 0.0, -10.0);
  assert!(tree.focused_text_input(), "scrolling alone leaves the keyboard up");

  tree.keyboard_was_put_away();
  assert!(!tree.focused_text_input(), "the finger took the keyboard down");
}

/// Five pages 100 wide, side by side, in a list that scrolls by pages.
fn pages() -> (Tree, App, crate::layout::layout_kind::ScrollState) {
  use crate::components::ScrollHorizontal;
  let mut app = App::new();
  let mut tree = Tree::new();
  tree.resize(200, 200);
  let state = crate::layout::layout_kind::ScrollState::new();
  state.set_paging(true);
  tree.set_root(
    ScrollHorizontal::new(Spacer::new().width(500.0).height(100.0))
      .with_scroll_state(state.clone())
      .size(100.0, 100.0),
  );
  tree.pass(&mut app, &HeadlessSurface);
  (tree, app, state)
}

/// A finger drags the pages by `travel` and lifts at `velocity`; then the list is left to come to rest.
fn drag_pages(tree: &mut Tree, travel: f32, velocity: f32) {
  use std::time::{Duration, Instant};
  tree.touch_scroll_begin(50.0, 50.0, travel.signum(), 0.0);
  tree.touch_scroll(50.0, 50.0, travel, 0.0, ScrollPhase::Scroll, false);
  tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
  assert!(tree.touch_scroll_pages());
  let lifted = Instant::now();
  tree.touch_scroll_page(velocity, 0.0, lifted);
  tree.touch_tick(lifted + Duration::from_secs(3));
  assert!(!tree.touch_animating());
}

#[test]
fn a_list_that_scrolls_by_pages_goes_to_the_nearest_when_let_go() {
  let (mut tree, _app, state) = pages();

  drag_pages(&mut tree, -40.0, 0.0);
  assert_eq!(state.scroll_x(), 0.0, "less than half a page: back");

  drag_pages(&mut tree, -60.0, 0.0);
  assert_eq!(state.scroll_x(), 100.0, "more than half: on");
  assert_eq!(state.page(), (1, 0));
}

#[test]
fn a_list_that_scrolls_by_pages_goes_to_the_next_when_thrown_and_no_further() {
  let (mut tree, _app, state) = pages();

  // A finger going left brings the next page, however little it travelled.
  drag_pages(&mut tree, -10.0, -900.0);
  assert_eq!(state.scroll_x(), 100.0);

  // Thrown as hard as can be, it is still the page beside.
  drag_pages(&mut tree, -30.0, -20_000.0);
  assert_eq!(state.scroll_x(), 200.0);

  // Thrown back.
  drag_pages(&mut tree, 10.0, 900.0);
  assert_eq!(state.scroll_x(), 100.0);

  // Dragged on, then thrown back: the page it started on.
  drag_pages(&mut tree, -30.0, 900.0);
  assert_eq!(state.scroll_x(), 100.0);
}

#[test]
fn a_list_on_its_way_to_a_page_moves_there_over_time_and_is_caught_by_a_finger() {
  use std::time::{Duration, Instant};
  let (mut tree, _app, state) = pages();

  tree.touch_scroll_begin(50.0, 50.0, -1.0, 0.0);
  tree.touch_scroll(50.0, 50.0, -60.0, 0.0, ScrollPhase::Scroll, false);
  tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
  let lifted = Instant::now();
  tree.touch_scroll_page(0.0, 0.0, lifted);
  assert!(tree.touch_tick(lifted + Duration::from_millis(50)), "still on its way");
  let between = state.scroll_x();
  assert!(between > 60.0 && between < 100.0, "between the two: {between}");

  // A finger lands: the list stops where it is, and let go without a move it goes on to the nearest page.
  assert!(tree.touch_scroll_catch());
  assert!(!tree.touch_animating());
  assert_eq!(state.scroll_x(), between);
  tree.touch_scroll_settle();
  tree.touch_tick(Instant::now() + Duration::from_secs(3));
  assert_eq!(state.scroll_x(), 100.0);
}

#[test]
fn a_list_that_does_not_scroll_by_pages_is_left_where_the_finger_left_it() {
  let (mut tree, _app, state) = pages();
  state.set_paging(false);

  tree.touch_scroll_begin(50.0, 50.0, -1.0, 0.0);
  tree.touch_scroll(50.0, 50.0, -40.0, 0.0, ScrollPhase::Scroll, false);
  tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
  assert!(!tree.touch_scroll_pages());
  tree.touch_scroll_page(-900.0, 0.0, std::time::Instant::now());

  assert!(!tree.touch_animating());
  assert_eq!(state.scroll_x(), 40.0);
}

#[test]
fn pages_inside_a_page_that_scrolls_go_to_the_next_as_a_shell_sends_a_throw() {
  use std::time::{Duration, Instant};

  use crate::components::ScrollHorizontal;
  let mut app = App::new();
  let mut tree = Tree::new();
  tree.set_scale_factor(3.0);
  tree.resize(600, 900);
  let state = crate::layout::layout_kind::ScrollState::new();
  state.set_paging(true);
  // A strip of five pages 200 wide in a page 200 wide that scrolls down, as an app has it.
  tree.set_root(
    ScrollVertical::new(
      Column::new()
        .child(Spacer::new().width(200.0).height(40.0))
        .child(
          ScrollHorizontal::new(Spacer::new().width(1000.0).height(100.0))
            .with_scroll_state(state.clone())
            .width(200.0),
        )
        .child(Spacer::new().width(200.0).height(2000.0)),
    )
    .size(200.0, 300.0),
  );
  tree.pass(&mut app, &HeadlessSurface);

  // What a shell sends for a finger that goes 40 logical pixels left on the strip and lifts while moving:
  // the gesture, its last event, then the start and the end of the momentum it does not leave.
  let (x, y) = (300.0, 270.0);
  tree.touch_scroll_begin(x, y, -12.0, 1.0);
  tree.touch_scroll(x, y, 0.0, 0.0, ScrollPhase::Start, false);
  for _ in 0..10 {
    tree.touch_scroll(x, y, -12.0, 0.0, ScrollPhase::Scroll, false);
    tree.pass(&mut app, &HeadlessSurface);
  }
  assert_eq!(state.scroll_x(), 40.0, "the strip follows the finger");
  tree.touch_scroll(x, y, 0.0, 0.0, ScrollPhase::End, false);
  tree.touch_scroll(x, y, 0.0, 0.0, ScrollPhase::Start, false);
  assert!(tree.touch_scroll_pages());
  tree.touch_scroll(x, y, 0.0, 0.0, ScrollPhase::End, false);
  let lifted = Instant::now();
  tree.touch_scroll_page(-2400.0, 30.0, lifted);

  // Frames go by, each laid out after a step.
  let mut frames = 0;
  while tree.touch_animating() {
    frames += 1;
    assert!(frames < 400, "the strip never came to rest: at {}", state.scroll_x());
    tree.touch_tick(lifted + Duration::from_millis(8 * frames));
    tree.pass(&mut app, &HeadlessSurface);
  }
  assert_eq!(state.scroll_x(), 200.0, "the next page");
  assert!(frames > 5, "over some frames, not at once: {frames}");
}