crcbl-ui 0.2.1

Crucible immediate-mode GUI
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
//! The virtualized outliner: the flattened model against a known tree, a
//! bounded build whatever the tree's size, expand and collapse by pointer and
//! by key, and selection by pointer and by key.

#[path = "outliner/disabled.rs"]
mod disabled;
#[path = "outliner/range.rs"]
mod range;
#[path = "outliner/retention.rs"]
mod retention;

use super::*;
use crate::tree::{
    LIST_OVERSCAN, OutlinerBuilder, OutlinerId, OutlinerOptions, OutlinerRow, OutlinerState,
    Response, SelectMode,
};

/// A row's height, in pixels.
const ROW: f32 = 20.0;
/// The outliner's content-box height: five rows.
const VIEW: f32 = 100.0;
/// The most rows one frame may build: the rows the view can show, one more for
/// a row cut by each edge, the overscan on both sides, and the focused row
/// outside it all.
const BOUND: usize = (VIEW / ROW) as usize + 1 + 2 * LIST_OVERSCAN + 1;

/// An item's id.
const fn id(item: u64) -> OutlinerId {
    OutlinerId(item)
}

/// The fixture tree, six items deep enough to have a grandchild:
///
/// ```text
/// 0 root
///   1 a          (leaf)
///   2 b          (branch)
///     3 c        (leaf)
///     4 d        (leaf)
///   5 e          (leaf)
/// ```
fn fixture(out: &mut OutlinerBuilder<'_>) {
    out.branch(id(0), |out| {
        out.leaf(id(1));
        out.branch(id(2), |out| {
            out.leaf(id(3));
            out.leaf(id(4));
        });
        out.leaf(id(5));
    });
}

/// A tree of one expanded root over `leaves` leaves, for the bound tests.
fn wide(leaves: u64) -> impl Fn(&mut OutlinerBuilder<'_>) {
    move |out| {
        out.branch(id(0), |out| {
            for leaf in 1..=leaves {
                out.leaf(id(leaf));
            }
        });
    }
}

/// What one frame of the outliner page built.
struct OutlinerPage {
    outliner: Response,
    /// Every row the row builder was called with, in order.
    built: Vec<OutlinerRow>,
    /// How many nodes the frame built, the page's own included.
    nodes: u32,
}

/// The outliner's stylesheet: a fixed height and nothing between its border box
/// and its content box, so row geometry is plain arithmetic.
fn sheet(ui: &mut Ui) {
    ui.add_stylesheet(
        "outliner.css",
        &format!("#tree {{ height: {VIEW}px; border-width: 0; padding: 0; }}"),
    );
}

fn options(select: SelectMode) -> OutlinerOptions {
    OutlinerOptions {
        row_height: ROW,
        select,
        ..OutlinerOptions::default()
    }
}

fn page(
    ui: &mut Ui,
    state: &mut OutlinerState,
    pointer: PointerInput,
    nav: NavInput,
    select: SelectMode,
    tree: impl FnOnce(&mut OutlinerBuilder<'_>),
) -> OutlinerPage {
    frame(ui, pointer, nav, |ui| {
        let mut built = Vec::new();
        let outliner = ui.outliner_with("#tree", state, &options(select), tree, |ui, row| {
            built.push(*row);
            ui.span(".label", "x", &[]);
        });
        let nodes = ui.style_stats().nodes;
        OutlinerPage {
            outliner,
            built,
            nodes,
        }
    })
}

/// The fixture page, selecting one row at a time.
fn fixture_page(
    ui: &mut Ui,
    state: &mut OutlinerState,
    pointer: PointerInput,
    nav: NavInput,
) -> OutlinerPage {
    page(ui, state, pointer, nav, SelectMode::Replace, fixture)
}

/// The key of the row for `item`, from the frame just built.
fn row_key(ui: &Ui, page: &OutlinerPage, item: OutlinerId) -> NodeKey {
    let index = page
        .built
        .iter()
        .position(|row| row.id == item)
        .unwrap_or_else(|| panic!("{item:?} was not built"));
    let content = children_of(ui, page.outliner.key)[0];
    // The rows are built in the order the builder saw them, bar a focused row
    // kept outside the window, which is built first.
    children_of(ui, content)[index]
}

/// The ids of `state`'s flattened rows, in order.
fn flat(state: &OutlinerState) -> Vec<u64> {
    state.rows().iter().map(|row| row.id.0).collect()
}

/// **The flattened model is exactly the visible rows of a known tree, at each
/// expansion**, and a collapsed branch's children are never walked — the
/// builder's own `branch` never runs their closure.
#[test]
fn the_flattened_model_is_the_visible_rows_of_a_known_tree() {
    let mut state = OutlinerState::new();
    let walked = std::cell::Cell::new(0_u32);
    let counted = |out: &mut OutlinerBuilder<'_>| {
        out.branch(id(0), |out| {
            walked.set(walked.get() + 1);
            out.leaf(id(1));
            out.branch(id(2), |out| {
                walked.set(walked.get() + 1);
                out.leaf(id(3));
                out.leaf(id(4));
            });
            out.leaf(id(5));
        });
    };

    state.flatten(counted);
    assert_eq!(flat(&state), [0], "a collapsed root is the whole model");
    assert_eq!(walked.get(), 0, "a collapsed root's children were walked");
    assert_eq!(state.rows()[0].depth, 0);
    assert!(state.rows()[0].branch && !state.rows()[0].open);

    state.set_expanded(id(0), true);
    state.flatten(counted);
    assert_eq!(flat(&state), [0, 1, 2, 5]);
    assert_eq!(walked.get(), 1, "the collapsed child was walked");
    let depths: Vec<u16> = state.rows().iter().map(|row| row.depth).collect();
    assert_eq!(depths, [0, 1, 1, 1], "depth is not the nesting");
    let parents: Vec<Option<u64>> = state
        .rows()
        .iter()
        .map(|row| row.parent.map(|parent| parent.0))
        .collect();
    assert_eq!(parents, [None, Some(0), Some(0), Some(0)]);

    state.set_expanded(id(2), true);
    state.flatten(counted);
    assert_eq!(flat(&state), [0, 1, 2, 3, 4, 5]);
    assert_eq!(walked.get(), 3, "both branches were not walked");
    let depths: Vec<u16> = state.rows().iter().map(|row| row.depth).collect();
    assert_eq!(depths, [0, 1, 1, 2, 2, 1]);

    state.set_expanded(id(0), false);
    state.flatten(counted);
    assert_eq!(
        flat(&state),
        [0],
        "collapsing the root did not hide its subtree"
    );
    assert_eq!(walked.get(), 3, "a collapsed root's children were walked");

    // The expansion under the closed root is kept, not forgotten.
    state.set_expanded(id(0), true);
    state.flatten(counted);
    assert_eq!(flat(&state), [0, 1, 2, 3, 4, 5], "the inner branch shut");
}

/// **`is_stale` is set by every expansion change and cleared by the flatten**,
/// so a still frame walks nothing at all.
#[test]
fn the_model_is_flattened_only_when_the_expansion_or_the_caller_says_so() {
    let mut state = OutlinerState::new();
    assert!(state.is_stale(), "a new state was not stale");
    state.flatten(fixture);
    assert!(!state.is_stale());

    assert!(
        state.set_expanded(id(0), true),
        "the change was not reported"
    );
    assert!(state.is_stale(), "expanding did not make the model stale");
    state.flatten(fixture);
    assert!(!state.set_expanded(id(0), true), "a no-op was reported");
    assert!(!state.is_stale(), "a no-op made the model stale");

    state.invalidate();
    assert!(state.is_stale());
}

/// **A 100 000-row outliner builds no more rows a frame than its view and
/// overscan hold, and exactly as many nodes as a 100-row one does**, while its
/// content is as tall as every row.
#[test]
fn a_hundred_thousand_row_outliner_builds_a_bounded_window() {
    let (mut long, mut short) = (Ui::new(), Ui::new());
    sheet(&mut long);
    sheet(&mut short);
    let mut many = OutlinerState::new();
    let mut few = OutlinerState::new();
    many.set_expanded(id(0), true);
    few.set_expanded(id(0), true);

    for _ in 0..3 {
        let big = page(
            &mut long,
            &mut many,
            idle(),
            NavInput::default(),
            SelectMode::Replace,
            wide(100_000),
        );
        let small = page(
            &mut short,
            &mut few,
            idle(),
            NavInput::default(),
            SelectMode::Replace,
            wide(100),
        );
        assert_eq!(many.rows().len(), 100_001, "the model is not every row");
        assert!(
            big.built.len() <= BOUND,
            "{} rows built: {:?}",
            big.built.len(),
            big.built
        );
        assert!(!big.built.is_empty(), "nothing was built");
        assert_eq!(big.nodes, small.nodes, "the node count grew with the tree");
    }

    let big = page(
        &mut long,
        &mut many,
        idle(),
        NavInput::default(),
        SelectMode::Replace,
        wide(100_000),
    );
    let content = children_of(&long, big.outliner.key)[0];
    let (min, max) = rect(&long, content);
    assert_eq!(
        max.y - min.y,
        100_001.0 * ROW,
        "the content is not every row"
    );
    assert_eq!(
        long.len(),
        short.len(),
        "the store holds more than the window"
    );
}

/// **A click on a row's toggle expands and collapses it, and the children show
/// in the same frame** — the row's own click selects instead, so the two
/// gestures never collide.
#[test]
fn a_click_on_the_toggle_expands_and_a_click_on_the_row_selects() {
    let mut ui = Ui::new();
    sheet(&mut ui);
    let mut state = OutlinerState::new();
    fixture_page(&mut ui, &mut state, idle(), NavInput::default());
    let first = fixture_page(&mut ui, &mut state, idle(), NavInput::default());
    assert_eq!(flat(&state), [0]);

    let root = row_key(&ui, &first, id(0));
    let toggle = children_of(&ui, root)[0];
    let grip = centre(&ui, toggle);
    fixture_page(&mut ui, &mut state, press(grip), NavInput::default());
    let opened = fixture_page(&mut ui, &mut state, release(grip), NavInput::default());
    assert!(
        state.is_expanded(id(0)),
        "the click did not expand the root"
    );
    assert_eq!(
        opened.built.iter().map(|row| row.id.0).collect::<Vec<_>>(),
        [0, 1, 2, 5],
        "the children did not show in the frame the click landed"
    );
    assert!(opened.outliner.changed, "the expansion was not reported");
    assert_eq!(state.selected_len(), 0, "the toggle click also selected");

    // And the same click closes it again.
    fixture_page(&mut ui, &mut state, press(grip), NavInput::default());
    let shut = fixture_page(&mut ui, &mut state, release(grip), NavInput::default());
    assert!(!state.is_expanded(id(0)));
    assert_eq!(shut.built.len(), 1, "the subtree stayed built");

    // A click on the row itself, past the toggle, selects it and nothing else.
    let (min, max) = rect(&ui, root);
    let body = Vec2::new(max.x - 2.0, (min.y + max.y) * 0.5);
    fixture_page(&mut ui, &mut state, press(body), NavInput::default());
    let picked = fixture_page(&mut ui, &mut state, release(body), NavInput::default());
    assert!(state.is_selected(id(0)), "the row click did not select it");
    assert!(!state.is_expanded(id(0)), "the row click also expanded it");
    assert!(picked.outliner.changed, "the selection was not reported");
}

/// **Right expands a focused row and left collapses it, as the WAI-ARIA tree
/// view pattern says**, and accept selects it: the whole outliner is drivable
/// with no pointer at all.
#[test]
fn keys_expand_collapse_and_select_a_focused_row() {
    let mut ui = Ui::new();
    sheet(&mut ui);
    let mut state = OutlinerState::new();
    fixture_page(&mut ui, &mut state, idle(), NavInput::default());
    fixture_page(&mut ui, &mut state, idle(), NavInput::NAVIGATION);
    let at_root = fixture_page(&mut ui, &mut state, idle(), NavInput::default());
    assert_eq!(
        ui.focused(),
        Some(row_key(&ui, &at_root, id(0))),
        "focus did not land on the root row"
    );

    let opened = fixture_page(&mut ui, &mut state, idle(), RIGHT);
    assert!(state.is_expanded(id(0)), "right did not expand the root");
    assert_eq!(
        opened.built.iter().map(|row| row.id.0).collect::<Vec<_>>(),
        [0, 1, 2, 5],
        "right's children did not show in the same frame"
    );

    // Right again steps into the first child, as the pattern says.
    let inside = fixture_page(&mut ui, &mut state, idle(), RIGHT);
    assert_eq!(
        ui.focused(),
        Some(row_key(&ui, &inside, id(1))),
        "right on an open row did not step to its first child"
    );

    // Left from a child goes to its parent, and left again closes it.
    let back = fixture_page(&mut ui, &mut state, idle(), LEFT);
    assert_eq!(ui.focused(), Some(row_key(&ui, &back, id(0))));
    let shut = fixture_page(&mut ui, &mut state, idle(), LEFT);
    assert!(!state.is_expanded(id(0)), "left did not collapse the root");
    assert_eq!(shut.built.len(), 1, "the subtree stayed built");

    // Accept selects the focused row.
    assert_eq!(state.selected_len(), 0);
    fixture_page(&mut ui, &mut state, idle(), NavInput::ACCEPT);
    assert!(state.is_selected(id(0)), "accept did not select the row");
}

/// **The caller's own expansion wins over the store**: collapsing a row from
/// outside, while that row holds focus, is not undone by the widget reading
/// last frame's row back — only a left or right the tree view rule answered
/// this frame moves the expansion.
#[test]
fn an_expansion_the_caller_changed_is_not_undone() {
    let mut ui = Ui::new();
    sheet(&mut ui);
    let mut state = OutlinerState::new();
    fixture_page(&mut ui, &mut state, idle(), NavInput::default());
    fixture_page(&mut ui, &mut state, idle(), NavInput::NAVIGATION);
    fixture_page(&mut ui, &mut state, idle(), RIGHT);
    assert!(state.is_expanded(id(0)), "right did not expand the root");
    let built = fixture_page(&mut ui, &mut state, idle(), NavInput::default());
    assert_eq!(
        ui.focused(),
        Some(row_key(&ui, &built, id(0))),
        "the row the caller is about to collapse does not hold focus"
    );

    state.set_expanded(id(0), false);
    let shut = fixture_page(&mut ui, &mut state, idle(), NavInput::default());
    assert!(!state.is_expanded(id(0)), "the widget re-expanded the row");
    assert_eq!(shut.built.len(), 1, "the subtree was built again");
}

/// **Selection follows its mode**: replace leaves one row selected, toggle adds
/// and removes, and a range takes every row between the anchor and the row it
/// lands on.
#[test]
fn the_selection_modes_replace_toggle_and_range() {
    let mut ui = Ui::new();
    sheet(&mut ui);
    let mut state = OutlinerState::new();
    state.set_expanded(id(0), true);
    state.set_expanded(id(2), true);
    state.flatten(fixture);
    assert_eq!(flat(&state), [0, 1, 2, 3, 4, 5]);

    assert!(state.select(id(1), SelectMode::Replace));
    assert_eq!(state.selected().map(|id| id.0).collect::<Vec<_>>(), [1]);
    assert_eq!(state.anchor(), Some(id(1)));

    assert!(state.select(id(4), SelectMode::Toggle));
    assert_eq!(state.selected().map(|id| id.0).collect::<Vec<_>>(), [1, 4]);
    assert!(state.select(id(4), SelectMode::Toggle), "toggle off");
    assert_eq!(state.selected().map(|id| id.0).collect::<Vec<_>>(), [1]);
    assert_eq!(
        state.anchor(),
        Some(id(4)),
        "toggle did not move the anchor"
    );

    // Replace takes the rows already selected away with it, not just adds.
    state.select(id(3), SelectMode::Toggle);
    assert_eq!(state.selected().map(|id| id.0).collect::<Vec<_>>(), [1, 3]);
    assert!(state.select(id(5), SelectMode::Replace));
    assert_eq!(
        state.selected().map(|id| id.0).collect::<Vec<_>>(),
        [5],
        "replace left the rows that were selected before it"
    );

    state.select(id(1), SelectMode::Replace);
    assert!(state.select(id(4), SelectMode::Range));
    assert_eq!(
        state.selected().map(|id| id.0).collect::<Vec<_>>(),
        [1, 2, 3, 4],
        "the range is not the rows between the anchor and the row"
    );
    assert_eq!(state.anchor(), Some(id(1)), "a range moved the anchor");
    assert!(
        !state.select(id(4), SelectMode::Range),
        "a no-op was reported"
    );

    // The range runs backwards too, and the frame's mode is what the widget
    // applies: a click with `Range` set takes the run, not just the row.
    let mut ui_state = OutlinerState::new();
    ui_state.set_expanded(id(0), true);
    page(
        &mut ui,
        &mut ui_state,
        idle(),
        NavInput::default(),
        SelectMode::Replace,
        fixture,
    );
    let built = page(
        &mut ui,
        &mut ui_state,
        idle(),
        NavInput::default(),
        SelectMode::Replace,
        fixture,
    );
    let click = |ui: &mut Ui, state: &mut OutlinerState, at: Vec2, mode: SelectMode| {
        page(ui, state, press(at), NavInput::default(), mode, fixture);
        page(ui, state, release(at), NavInput::default(), mode, fixture);
    };
    let body = |ui: &Ui, key: NodeKey| {
        let (min, max) = rect(ui, key);
        Vec2::new(max.x - 2.0, (min.y + max.y) * 0.5)
    };
    let fifth = body(&ui, row_key(&ui, &built, id(5)));
    let second = body(&ui, row_key(&ui, &built, id(1)));
    click(&mut ui, &mut ui_state, fifth, SelectMode::Replace);
    assert_eq!(ui_state.selected().map(|id| id.0).collect::<Vec<_>>(), [5]);
    click(&mut ui, &mut ui_state, second, SelectMode::Range);
    assert_eq!(
        ui_state.selected().map(|id| id.0).collect::<Vec<_>>(),
        [1, 2, 5],
        "a backwards range did not take the rows between"
    );
}

/// **A selected row is `:checked` and an expanded one `:open`, and the
/// stylesheet paints only those** — the selected row is the only row filled in
/// the selection colour, whatever the offset shows.
#[test]
fn only_the_selected_row_is_marked() {
    let selection = linear("#2f5a9e");
    let mut ui = Ui::new();
    sheet(&mut ui);
    let mut state = OutlinerState::new();
    state.set_expanded(id(0), true);
    state.select(id(2), SelectMode::Replace);
    fixture_page(&mut ui, &mut state, idle(), NavInput::default());
    let built = fixture_page(&mut ui, &mut state, idle(), NavInput::default());

    let marked: Vec<NodeKey> = built
        .built
        .iter()
        .map(|row| row_key(&ui, &built, row.id))
        .filter(|&key| style_of(&ui, key).background == selection)
        .collect();
    assert_eq!(
        marked,
        [row_key(&ui, &built, id(2))],
        "not exactly the selected row marked"
    );

    // Indentation is the depth, in `padding-left`, and nothing else.
    for row in &built.built {
        let key = row_key(&ui, &built, row.id);
        let (min, _) = rect(&ui, key);
        let (outer, _) = rect(&ui, built.outliner.key);
        let toggle = rect(&ui, children_of(&ui, key)[0]).0;
        assert_eq!(min.x, outer.x, "a row does not span the outliner");
        assert_eq!(
            toggle.x - min.x,
            f32::from(row.depth) * OutlinerOptions::default().indent,
            "{:?} is not indented by its depth",
            row.id
        );
    }
}

/// **Focus steps past the rows shown into rows that were not built**, down a
/// long outliner, and the focused row is kept when the offset moves away.
#[test]
fn focus_steps_past_the_view_and_keeps_its_row() {
    let mut ui = Ui::new();
    sheet(&mut ui);
    let mut state = OutlinerState::new();
    state.set_expanded(id(0), true);
    let long = wide(100_000);
    let step = |ui: &mut Ui, state: &mut OutlinerState, nav| {
        page(ui, state, idle(), nav, SelectMode::Replace, &long)
    };
    step(&mut ui, &mut state, NavInput::default());
    let first = step(&mut ui, &mut state, NavInput::NAVIGATION);
    assert_eq!(ui.focused(), Some(row_key(&ui, &first, id(0))));

    for index in 1..=40_u64 {
        let built = step(&mut ui, &mut state, DOWN);
        assert!(built.built.len() <= BOUND, "row {index}: {:?}", built.built);
        let row = row_key(&ui, &built, id(index));
        assert_eq!(
            ui.focused(),
            Some(row),
            "the step to row {index} went astray"
        );
        let (row_min, row_max) = rect(&ui, row);
        let (view_min, view_max) = rect(&ui, built.outliner.key);
        assert!(
            row_min.y >= view_min.y && row_max.y <= view_max.y,
            "row {index} at {row_min}..{row_max} is outside the view"
        );
    }

    // Scrolled away from the focused row, it is still built and still focused.
    let focused = ui.focused().expect("a row holds focus");
    let slot = ui.store.find(first.outliner.key).expect("stored");
    ui.store.get_mut(slot).scroll_offset = Vec2::new(0.0, 100_000.0);
    for _ in 0..3 {
        let built = step(&mut ui, &mut state, NavInput::default());
        assert_eq!(ui.focused(), Some(focused), "scrolling away took focus");
        assert!(built.built.len() <= BOUND, "{:?}", built.built);
        assert_eq!(built.built[0].id, id(40), "the kept row is not first");
    }
}

/// **A row far outside the window is brought into it by writing the
/// outliner's own scroll offset**, which is the claim
/// [`Ui::set_scroll_offset_of`] exists for: until the offset moves, that row
/// has no node and no rectangle, so nothing that works from a rectangle — the
/// scroll-into-view a focus move does — can reach it.
#[test]
fn setting_the_offset_brings_a_row_outside_the_window_into_it() {
    /// Which row is scrolled to: far enough past the window that the frame
    /// before builds nothing near it.
    const WANTED: u64 = 500;

    let mut ui = Ui::new();
    sheet(&mut ui);
    let mut state = OutlinerState::new();
    state.set_expanded(id(0), true);
    let long = wide(1_000);
    let step = |ui: &mut Ui, state: &mut OutlinerState| {
        page(
            ui,
            state,
            idle(),
            NavInput::default(),
            SelectMode::Replace,
            &long,
        )
    };

    let first = step(&mut ui, &mut state);
    assert!(
        !first.built.iter().any(|row| row.id == id(WANTED)),
        "row {WANTED} is already in the window, so this proves nothing",
    );
    assert_eq!(ui.scroll_offset_of(first.outliner.key), Vec2::ZERO);

    // The root is row 0 and leaf `n` is row `n`, so the wanted row's top is
    // its index times the row height.
    let top = WANTED as f32 * ROW;
    ui.set_scroll_offset_of(first.outliner.key, Vec2::new(0.0, top));
    let scrolled = step(&mut ui, &mut state);
    assert!(
        scrolled.built.iter().any(|row| row.id == id(WANTED)),
        "the row the offset names was not built: {:?}",
        scrolled.built,
    );
    let (row_min, row_max) = rect(&ui, row_key(&ui, &scrolled, id(WANTED)));
    let (view_min, view_max) = rect(&ui, scrolled.outliner.key);
    assert!(
        row_min.y >= view_min.y && row_max.y <= view_max.y,
        "row {WANTED} at {row_min}..{row_max} is outside the view \
         {view_min}..{view_max}",
    );
    assert_eq!(
        ui.scroll_offset_of(scrolled.outliner.key),
        Vec2::new(0.0, top),
        "the offset was not kept",
    );

    // Past the content's reach the next layout clamps it, as it clamps a
    // wheel's, so the frame after shows the last rows rather than nothing.
    ui.set_scroll_offset_of(first.outliner.key, Vec2::new(0.0, 1.0e9));
    step(&mut ui, &mut state);
    let reach = ui.scroll_offset_of(first.outliner.key).y;
    assert_eq!(
        reach,
        1_001.0 * ROW - VIEW,
        "the offset was not clamped to the content's reach",
    );
    let clamped = step(&mut ui, &mut state);
    assert_eq!(
        clamped.built.last().map(|row| row.id),
        Some(id(1_000)),
        "the last row is not in the window: {:?}",
        clamped.built,
    );
}

/// **Two clicks on one row close enough in time are a double-click, for the
/// frame the second lands** — and two clicks too far apart, or on two rows,
/// are not.
#[test]
fn a_second_click_on_a_row_soon_enough_is_a_double_click() {
    use crate::tree::{DOUBLE_CLICK_TIME, TextInput};

    let mut ui = Ui::new();
    sheet(&mut ui);
    let mut state = OutlinerState::new();
    state.set_expanded(id(0), true);
    // A frame `dt` long, so the clock the double-click is timed by moves.
    let step = |ui: &mut Ui, state: &mut OutlinerState, pointer, dt| {
        let text = TextInput {
            dt,
            ..TextInput::default()
        };
        frame_with_text(ui, pointer, NavInput::default(), text, |ui| {
            let mut built = Vec::new();
            let outliner = ui.outliner_with(
                "#tree",
                state,
                &options(SelectMode::Replace),
                fixture,
                |ui, row| {
                    built.push(*row);
                    ui.span(".label", "x", &[]);
                },
            );
            OutlinerPage {
                outliner,
                built,
                nodes: 0,
            }
        })
    };
    let short = DOUBLE_CLICK_TIME / 4;
    let first = step(&mut ui, &mut state, idle(), short);
    let at = |ui: &Ui, page: &OutlinerPage, item| {
        let (min, max) = rect(ui, row_key(ui, page, id(item)));
        Vec2::new(max.x - 2.0, (min.y + max.y) * 0.5)
    };
    let (one, five) = (at(&ui, &first, 1), at(&ui, &first, 5));
    let click = |ui: &mut Ui, state: &mut OutlinerState, on, dt| {
        step(ui, state, press(on), dt);
        step(ui, state, release(on), dt);
        state.double_clicked()
    };

    assert_eq!(click(&mut ui, &mut state, one, short), None, "one click");
    assert_eq!(
        click(&mut ui, &mut state, one, short),
        Some(id(1)),
        "a second click soon after was not a double-click"
    );
    step(&mut ui, &mut state, idle(), short);
    assert_eq!(state.double_clicked(), None, "it lasted past its frame");

    assert_eq!(
        click(&mut ui, &mut state, one, short),
        None,
        "a third click"
    );
    assert_eq!(
        click(&mut ui, &mut state, five, short),
        None,
        "clicks on two rows made a double-click"
    );
    assert_eq!(click(&mut ui, &mut state, five, DOUBLE_CLICK_TIME), None);
    assert_eq!(
        click(&mut ui, &mut state, five, DOUBLE_CLICK_TIME),
        None,
        "clicks too far apart made a double-click"
    );

    // Two clicks on one spot that are on two rows, because opening `b` put
    // its child `c` where `e` was.
    assert_eq!(click(&mut ui, &mut state, five, DOUBLE_CLICK_TIME), None);
    state.set_expanded(id(2), true);
    // Laid out once open, so the press lands on the row now under it.
    step(&mut ui, &mut state, idle(), short);
    assert_eq!(
        click(&mut ui, &mut state, five, short),
        None,
        "clicks on one spot over two rows made a double-click"
    );
    assert!(state.is_selected(id(3)), "the second click was not on `c`");
}