blit-ui 0.1.0

an experimental immediate-mode ui library
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
use blit::KeyboardEvent;

use super::*;

#[test]
fn keyboard_input_bubbles_until_claimed() {
    #[derive(Clone, Copy)]
    struct Keyboard(&'static str, Option<&'static str>);

    impl Interact<TestContext> for Keyboard {
        type State = ();
        type Response = Option<&'static str>;

        fn update(&self, _: &mut (), cx: &mut InteractCx<'_, f32>) -> Self::Response {
            let input = cx.keyboard()?;
            if self.1.is_some_and(|text| input != KeyboardEvent::Text(text)) {
                return None;
            }
            cx.claim_keyboard();
            Some(self.0)
        }
    }

    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let focused = Cell::new(None);
    let build = |mut ui: Ui<'_>| {
        if let Some(key) = focused.get() {
            let target = ui.resolve(Scope::ROOT, key);
            ui.node(target).focus();
        } else {
            ui.clear_focus();
        }
        let mut root = ui.layout(Overlay);
        let mut branch = |parent_key, parent, child_key, child| {
            let mut branch = root.child_in(Scope::ROOT, parent_key);
            let parent = branch.interact(Keyboard(parent, None));
            let child = branch
                .layout(Overlay)
                .child_in(Scope::ROOT, child_key)
                .interact(Keyboard(child, Some(child)));
            [parent, child]
        };
        let [a, b] = branch(1, "a", 2, "b");
        let [c, d] = branch(3, "c", 4, "d");
        let mut handled = [a, b, c, d].into_iter().flatten();
        let winner = handled.next();
        assert_eq!(handled.next(), None, "input was handled more than once");
        winner
    };
    assert_eq!(render(&mut tree, &mut context, build), None);
    let mut next = |input| render_inputs(&mut tree, &mut context, Duration::ZERO, [input], build);
    assert_eq!(next(Input::Text("b".into())), Some("b"));
    assert_eq!(next(Input::Text("d".into())), Some("a"));
    focused.set(Some(4));
    next(Input::None);
    assert_eq!(next(Input::Text("d".into())), Some("d"));
    assert_eq!(next(Input::Text("x".into())), Some("c"));
    assert_eq!(next(Input::MouseLeave), None);
}

/// whether the pointer is over this widget
#[derive(Clone, Copy)]
struct Hover;

impl<C: Context> Interact<C> for Hover {
    type State = ();
    type Response = bool;

    fn update(&self, _: &mut (), cx: &mut InteractCx<'_, C::Scalar>) -> bool {
        cx.hovered()
    }
}

/// claims a contact as it ends, reporting whether the claim held
#[derive(Clone, Copy)]
struct ClaimEnded;

impl<C: Context> Interact<C> for ClaimEnded {
    type State = ();
    type Response = bool;

    fn update(&self, _: &mut (), cx: &mut InteractCx<'_, C::Scalar>) -> bool {
        let ended = cx.contacts().find(|contact| contact.phase == Ended);
        ended.is_some_and(|mut contact| contact.claim())
    }
}

/// scrolling over this widget, holding its sequence
#[derive(Clone, Copy)]
struct Wheel;

impl<C: Context> Interact<C> for Wheel {
    type State = ();
    type Response = Option<Point<f32>>;

    fn update(&self, _: &mut (), cx: &mut InteractCx<'_, C::Scalar>) -> Option<Point<f32>> {
        let event = cx.scroll()?;
        cx.capture_scroll();
        Some(event.delta)
    }
}

/// claims two contacts and reports their distance
#[derive(Clone, Copy)]
struct Pinch;

impl<C: Context<Scalar = f32>> Interact<C> for Pinch {
    type State = ();
    type Response = Option<f32>;

    fn update(&self, _: &mut (), cx: &mut InteractCx<'_, f32>) -> Option<f32> {
        let mut free = cx.contacts().filter(|contact| contact.free);
        let [mut a, mut b] = [free.next()?, free.next()?];
        if a.phase == Cancelled || b.phase == Cancelled {
            return None;
        }
        a.capture();
        b.capture();
        Some((b.position.x - a.position.x).hypot(b.position.y - a.position.y))
    }
}

#[test]
fn new_kind_gets_geometry_after_layout() {
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let added = Cell::new(false);
    let build = |mut ui: Ui<'_>| {
        ui.interact(WATCH);
        added.get() && ui.interact(Hover)
    };
    render_inputs(
        &mut tree,
        &mut context,
        Duration::ZERO,
        [pointer(None, Move, [10.0, 10.0])],
        &build,
    );
    // the app changes state and asks for a frame
    added.set(true);
    tree.request_frame();
    assert!(!render(&mut tree, &mut context, &build));
    assert!(render(&mut tree, &mut context, &build));
    assert!(!tree.has_pending_redraw());
}

#[test]
fn gesture_sees_independent_contacts() {
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let build = |mut ui: Ui<'_>| ui.interact(WATCH).map(|seen| seen.map(|(contact, _)| contact));
    render(&mut tree, &mut context, &build);
    let mut step = |input| render_inputs(&mut tree, &mut context, Duration::ZERO, [input], &build);
    step(pointer(Some(1), Down, [10.0, 10.0]));
    step(pointer(Some(2), Down, [20.0, 10.0]));
    let [first, second] = step(pointer(Some(1), Move, [15.0, 10.0])).map(Option::unwrap);
    assert_eq!(first.source, ContactSource::Touch(1));
    assert_eq!(second.source, ContactSource::Touch(2));
    assert_eq!(first.delta, Point::new(5.0, 0.0));
    assert_eq!(second.phase, Held);
    let [ended, held] = step(pointer(Some(1), Up, [17.0, 10.0])).map(Option::unwrap);
    assert_eq!((ended.phase, ended.delta), (Ended, Point::new(2.0, 0.0)));
    assert_eq!(held.id, second.id);
    assert_eq!(step(Input::None), [Some(held), None]);
}

#[test]
fn deepest_gesture_captures_first() {
    let child_key = 1000;
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let build = |mut ui: Ui<'_>| {
        // requested out of preorder and before the child node exists
        let child_id = ui.resolve(Scope::ROOT, child_key);
        let child = ui.node(child_id).interact(CAPTURE)[0];
        let parent = ui.interact(CAPTURE)[0];
        let mut root = ui.layout(Overlay);
        root.child_in(Scope::ROOT, child_key)
            .insert(Fill('C', Size::uniform(200.0)));
        (parent, child)
    };
    render(&mut tree, &mut context, &build);
    let down = pointer(Some(1), Down, [10.0, 10.0]);
    let (parent, child) = render_inputs(&mut tree, &mut context, Duration::ZERO, [down], &build);
    assert_eq!(parent, None);
    assert!(child.unwrap().1);
}

#[test]
fn gestures_on_one_widget_share_captures() {
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let build = |mut ui: Ui<'_>| {
        ui.interact(CAPTURE);
        ui.interact(Pinch)
    };
    render(&mut tree, &mut context, &build);
    let mut step = |input| render_inputs(&mut tree, &mut context, Duration::ZERO, [input], &build);
    assert_eq!(step(pointer(Some(1), Down, [10.0, 10.0])), None);
    // the pinch still sees the fingers its widget captured
    assert_eq!(step(pointer(Some(2), Down, [20.0, 10.0])), Some(10.0));
    assert_eq!(step(pointer(Some(1), Move, [0.0, 10.0])), Some(20.0));
}

#[test]
fn unmounted_owner_retires_contact() {
    let child = 1000;
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let mounted = Cell::new(true);
    let build = |mut ui: Ui<'_>| {
        let root = ui.interact(WATCH)[0];
        let mut layout = ui.layout(Overlay);
        let owned = mounted.get().then(|| {
            let mut child = layout.child_in(Scope::ROOT, child);
            child.insert(Fill('B', Size::uniform(200.0)));
            child.interact(CAPTURE)[0]
        });
        (root, owned.flatten())
    };
    render(&mut tree, &mut context, &build);
    let mut step = |input| render_inputs(&mut tree, &mut context, Duration::ZERO, [input], &build);
    let (root, owned) = step(pointer(Some(1), Down, [10.0, 10.0]));
    assert!(root.is_none() && owned.unwrap().1);
    mounted.set(false);
    step(Input::None);
    assert_eq!(step(pointer(Some(1), Move, [30.0, 10.0])), (None, None));
}

#[test]
fn gesture_state_lives_while_requested() {
    // larger than any inline limit
    #[derive(Clone, Copy)]
    struct Count;

    impl<C: Context<Scalar = f32>> Interact<C> for Count {
        type State = [u64; 32];
        type Response = u64;

        fn update(&self, count: &mut [u64; 32], cx: &mut InteractCx<'_, f32>) -> u64 {
            count[31] += 1;
            cx.wake_at(Duration::from_secs(1));
            count[31]
        }
    }

    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let rows = Cell::new((0, 3));
    let build = |ui: Ui<'_>| {
        let mut root = ui.layout(Overlay);
        let mut updates = [None; 4];
        let (start, end) = rows.get();
        for (row, updates) in updates.iter_mut().enumerate().take(end).skip(start) {
            let mut child = root.child_in(Scope::ROOT, row);
            child.interact(Hover);
            *updates = Some(child.interact(Count));
        }
        updates
    };
    let mut step = || render(&mut tree, &mut context, &build);
    assert_eq!(step(), [Some(1), Some(1), Some(1), None]);
    assert_eq!(step(), [Some(2), Some(2), Some(2), None]);
    rows.set((1, 4));
    assert_eq!(step(), [None, Some(3), Some(3), Some(1)]);
    // row 0 returns in a recycled cell with fresh state
    rows.set((0, 3));
    assert_eq!(step(), [Some(1), Some(4), Some(4), None]);
    assert_eq!(tree.next_timer_deadline(), Some(Duration::from_secs(1)));
    rows.set((0, 0));
    render(&mut tree, &mut context, &build);
    render(&mut tree, &mut context, &build);
    assert_eq!(tree.next_timer_deadline(), None);
}

#[test]
fn named_kinds_follow_reordered_nodes() {
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let order = Cell::new([Some(0), Some(1), Some(2)]);
    let build = |mut ui: Ui<'_>| {
        let responses = [0, 1, 2].map(|key| {
            let id = ui.resolve(Scope::ROOT, key);
            let pressed = ui.node(id).interact(WATCH)[0].is_some_and(|(contact, _)| contact.phase == Started);
            (ui.node(id).interact(Hover), pressed)
        });
        let mut root = ui.layout(Rects);
        for (index, id) in order.get().into_iter().enumerate() {
            if let Some(id) = id {
                root.child_in(Scope::ROOT, id)
                    .item(Rect::new(index as f32 * 50.0, 0.0, 50.0, 100.0));
            }
        }
        responses
    };
    for nodes in [
        [Some(0), Some(1), Some(2)],
        [Some(2), Some(0), None],
        [Some(1), Some(2), Some(0)],
    ] {
        order.set(nodes);
        render(&mut tree, &mut context, &build);
        let responses = render_inputs(
            &mut tree,
            &mut context,
            Duration::ZERO,
            [pointer(None, Down, [5.0, 5.0])],
            &build,
        );
        let hovered = nodes[0].unwrap();
        assert_eq!(responses, [0, 1, 2].map(|id| (id == hovered, id == hovered)));
    }
}

#[test]
fn kind_order_follows_requests() {
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let claim_first = Cell::new(true);
    let build = |mut ui: Ui<'_>| {
        let capture = Watch(|contact| contact.phase == Ended);
        if claim_first.get() {
            let claimed = ui.interact(ClaimEnded);
            ui.interact(capture);
            claimed
        } else {
            ui.interact(capture);
            ui.interact(ClaimEnded)
        }
    };
    for first in [true, false] {
        claim_first.set(first);
        render(&mut tree, &mut context, &build);
        let claimed = render_inputs(
            &mut tree,
            &mut context,
            Duration::ZERO,
            [pointer(Some(1), Down, [10.0, 10.0]), pointer(Some(1), Up, [10.0, 10.0])],
            &build,
        );
        assert_eq!(claimed, first);
    }
}

#[test]
fn kind_requested_twice_keeps_one_slot() {
    static UPDATES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);

    #[derive(Clone, Copy)]
    struct Count;

    impl<C: Context<Scalar = f32>> Interact<C> for Count {
        type State = ();
        type Response = ();

        fn update(&self, _: &mut (), _: &mut InteractCx<'_, f32>) {
            UPDATES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        }
    }

    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let build = |ui: Ui<'_>| {
        let mut root = ui.layout(Overlay);
        for id in 0..64 {
            let mut child = root.child_in(Scope::ROOT, id);
            child.interact(Count);
            child.interact(Count);
        }
        for key in 0..64 {
            let id = root.resolve(Scope::ROOT, key);
            root.node(id).interact(Count);
        }
    };
    for _ in 0..3 {
        UPDATES.store(0, std::sync::atomic::Ordering::Relaxed);
        render(&mut tree, &mut context, &build);
        assert_eq!(UPDATES.load(std::sync::atomic::Ordering::Relaxed), 64);
    }
}

#[test]
fn tapping_unregistered_background_clears_focus() {
    let field = 1001;
    for id in [None, Some(1)] {
        let (mut tree, mut context) = tree(Size::uniform(200.0));
        let focus = Cell::new(true);
        let build = |mut ui: Ui<'_>| {
            if focus.replace(false) {
                let target = ui.resolve(Scope::ROOT, field);
                ui.node(target).focus();
            }
            let mut root = ui.layout(Rects);
            let mut field = root
                .child_in(Scope::ROOT, field)
                .item(Rect::new(0.0, 0.0, 100.0, 200.0));
            field.interact(WATCH);
            field.focused()
        };
        // programmatic focus shows from the next build
        assert!(!render(&mut tree, &mut context, &build));
        assert!(render(&mut tree, &mut context, &build));
        let mut step = |input| render_inputs(&mut tree, &mut context, Duration::ZERO, [input], &build);
        // mouse clears focus on press and touch on release
        assert_eq!(step(pointer(id, Down, [150.0, 10.0])), id.is_some());
        assert!(!step(pointer(id, Up, [150.0, 10.0])));
    }
}

#[test]
fn clipping() {
    let (mut tree, mut context) = tree(Size::new(5.0, 1.0));
    let build = |ui: Ui<'_>| {
        ui.layout(Overlay)
            .child()
            .layout(Fixed(Size::new(3.0, 1.0)))
            .clip(0.0)
            .child()
            .build(|mut ui: Ui<'_>| {
                let seen = ui.interact(WATCH)[0].is_some();
                let wheel = ui.interact(Wheel);
                ui.insert(Fill('C', Size::new(5.0, 1.0)));
                (seen, wheel)
            })
    };
    render(&mut tree, &mut context, &build);
    for (phase, x, inside) in [(Down, 4.5, false), (Up, 4.5, false), (Down, 2.5, true)] {
        let (seen, _) = render_inputs(
            &mut tree,
            &mut context,
            Duration::ZERO,
            [pointer(None, phase, [x, 0.5])],
            &build,
        );
        assert_eq!(seen, inside);
    }
    let (seen, wheel) = render_inputs(
        &mut tree,
        &mut context,
        Duration::ZERO,
        [Input::Scroll {
            position: Point::new(2.5, 0.5),
            delta: Point::new(0.0, 1.0),
            continuous: false,
            phase: ScrollPhase::Moved,
            modifiers: Modifiers::NONE,
        }],
        &build,
    );
    assert!(seen && wheel.is_some_and(|delta| delta != Point::ZERO));
}

#[test]
fn rounded_clip() {
    let (mut tree, mut context) = tree(Size::uniform(4.0));
    let rect = Rect::new(0.0, 0.0, 4.0, 4.0);
    let child = Rect::new(3.0, 3.0, 1.0, 1.0);
    // full radii round every corner, a single radius leaves the bottom right square
    for (radius, square) in [(Corners::all(2.0), false), (Corners::new().top_left(2.0), true)] {
        let build = |ui: Ui<'_>| {
            let mut root = ui.clip(radius).layout(Rects);
            let mut child = root.child().item(child).clip(0.0);
            child.insert(Fill('C', Size::uniform(1.0)));
            child.interact(WATCH)[0].is_some()
        };
        render(&mut tree, &mut context, &build);
        assert_eq!(
            context.commands,
            [PushClip(rect), PushClip(child), Paint('C', child), PopClip, PopClip]
        );
        for (position, inside) in [([3.1, 3.1], true), ([3.9, 3.9], square)] {
            let seen = render_inputs(
                &mut tree,
                &mut context,
                Duration::ZERO,
                [pointer(None, Down, position), pointer(None, Up, position)],
                &build,
            );
            assert_eq!(seen, inside);
        }
    }
}

#[test]
fn input_settles_against_its_layout() {
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let offset = Cell::new(0.0);
    let build = |ui: Ui<'_>| {
        // scrolling moves the row under a still pointer
        if matches!(ui.input(), Input::Scroll { .. }) {
            offset.set(100.0);
        }
        let mut root = ui.layout(Rects);
        let mut row = root.child().item(Rect::new(offset.get(), 0.0, 100.0, 200.0));
        row.interact(Hover)
    };
    render(&mut tree, &mut context, &build);
    let scroll = Input::Scroll {
        position: Point::new(150.0, 10.0),
        delta: Point::new(0.0, 1.0),
        modifiers: Modifiers::NONE,
        continuous: false,
        phase: ScrollPhase::Moved,
    };
    // the scrolled build still sees the layout from before it
    assert!(!render_inputs(
        &mut tree,
        &mut context,
        Duration::ZERO,
        [scroll],
        &build
    ));
    assert!(tree.has_pending_redraw());
    assert!(render(&mut tree, &mut context, &build));
    assert!(!tree.has_pending_redraw());
}

#[test]
fn removed_scroll_owner_keeps_its_sequence() {
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let owner = Cell::new(1100);
    let build = |ui: Ui<'_>| {
        let mut root = ui.layout(Overlay);
        let mut list = root.child_in(Scope::ROOT, owner.get());
        list.insert(Fill('L', Size::uniform(200.0)));
        list.interact(Wheel)
    };
    render(&mut tree, &mut context, &build);
    let mut scroll = |phase| {
        let input = Input::Scroll {
            position: Point::new(10.0, 10.0),
            delta: Point::new(0.0, 1.0),
            continuous: true,
            phase,
            modifiers: Modifiers::NONE,
        };
        render_inputs(&mut tree, &mut context, Duration::ZERO, [input], &build)
    };
    assert!(scroll(ScrollPhase::Started).is_some());
    // another widget takes the owner's node while the gesture continues
    owner.set(1101);
    assert_eq!(scroll(ScrollPhase::Moved), None);
    assert_eq!(scroll(ScrollPhase::Moved), None);
    assert!(scroll(ScrollPhase::Started).is_some());
}

#[test]
fn focus_within_reads_the_previous_layout() {
    let (mut tree, mut context) = tree(Size::uniform(100.0));
    let focus = Cell::new(false);
    let child = Cell::new(None);
    let seen = Cell::new(None);
    let build = |ui: Ui<'_>| {
        let mut list = ui.layout(Rects);
        // before any child is built
        seen.set(list.focus_within());
        let mut field = list.child_in(Scope::ROOT, 0).item(Rect::new(0.0, 0.0, 10.0, 10.0));
        field.interact(WATCH);
        child.set(Some(field.id()));
        if focus.get() {
            field.focus();
        }
    };
    render(&mut tree, &mut context, &build);
    focus.set(true);
    render(&mut tree, &mut context, &build);
    focus.set(false);
    render(&mut tree, &mut context, &build);
    assert!(seen.get().is_some() && seen.get() == child.get());
}

#[test]
fn focused_descendant_scrolls_into_view() {
    let (mut tree, mut context) = tree(Size::uniform(100.0));
    let offset = Cell::new(0.0);
    let request = Cell::new(None);
    let last = Cell::new(None);
    // a list scrolls its focused field into view once whenever focus moves to it
    let build = |ui: Ui<'_>| {
        let mut list = ui.layout(Rects);
        for index in 0..10 {
            let y = index as f32 * 30.0 - offset.get();
            let mut field = list.child_in(Scope::ROOT, index).item(Rect::new(0.0, y, 100.0, 30.0));
            field.interact(WATCH);
            if request.get() == Some(index) {
                field.focus();
            }
        }
        let focus = list.focus_within();
        if focus != last.replace(focus)
            && let Some(area) = focus.and_then(|id| list.node(id).area())
        {
            let below = area.y + area.height - 100.0;
            offset.set(offset.get() + below.max(0.0) + area.y.min(0.0));
        }
    };
    // the user scrolls first, which a focus that stays put must not undo
    for (scrolled, index, expected) in [(0.0, 8, 170.0), (0.0, 1, 0.0), (200.0, 2, 60.0)] {
        offset.set(scrolled);
        render(&mut tree, &mut context, &build);
        assert_eq!(offset.get(), scrolled);
        request.set(Some(index));
        render(&mut tree, &mut context, &build);
        request.set(None);
        render(&mut tree, &mut context, &build);
        assert_eq!(offset.get(), expected);
        render(&mut tree, &mut context, &build);
        let y = index as f32 * 30.0 - expected;
        assert_eq!(
            tree.find(Scope::ROOT, index).and_then(|id| tree.area(id)),
            Some(Rect::new(0.0, y, 100.0, 30.0))
        );
    }
}

#[test]
fn unmounted_target_cancels_pending_contact() {
    let (mut tree, mut context) = tree(Size::uniform(200.0));
    let mounted = Cell::new(true);
    let build = |mut ui: Ui<'_>| {
        let root = ui.interact(WATCH)[0];
        let mut layout = ui.layout(Overlay);
        if mounted.get() {
            let mut child = layout.child();
            child.insert(Fill('B', Size::uniform(200.0)));
            child.interact(WATCH);
        }
        root
    };
    render(&mut tree, &mut context, &build);
    let mut step = |input| render_inputs(&mut tree, &mut context, Duration::ZERO, [input], &build);
    assert!(step(pointer(Some(1), Down, [10.0, 10.0])).is_some());
    mounted.set(false);
    step(Input::None);
    // the ancestor does not inherit the contact
    assert_eq!(step(pointer(Some(1), Move, [30.0, 10.0])), None);
}