frus-widgets 0.3.0

frus — declarative widget tree
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
//! [`Navigator`]: shows a full-window **screen**, with a slide transition
//! between the outgoing and the incoming screen on a push or a pop.
//!
//! The `Navigator` is **controlled**: the application holds the route stack and
//! the transition's progress, and (re)builds the screens on every frame.

use std::hash::Hash;

use frus_core::{Rect, Scene};
use frus_layout::{Dimension, Style};

use crate::interaction::Status;
use crate::keyed::Keyed;
use crate::media::MediaQuery;
use crate::theme::Theme;
use crate::widget::Widget;

/// A screen container with a slide transition.
///
/// # Every page has a key
///
/// What a page keeps from one frame to the next — how far it is scrolled, where its
/// caret is, the sheet it has raised — is kept under the identity of the widget it
/// belongs to, and an identity is a position in the tree unless a key says otherwise. A
/// navigator's pages have no position worth the name. The page on show is its first
/// child whichever page it is, so two pages built the same way had one scroll offset
/// between them; and during a transition the page arriving is its second child, then its
/// first once it has arrived, so it had one identity while it slid in and another when it
/// stopped. [`Navigator::new`] and [`Navigator::from`] therefore each take a **key**, and
/// everything inside a page takes its identity from that key rather than from where the
/// page sits. The reference gives every route its own subtree and its own storage for the
/// same reason.
///
/// **A key names an entry of the stack, not a route.** The same route can be on the stack
/// twice — a thread opened from a thread — and those are two pages, each scrolled on its
/// own; a key made of the route alone would give them one offset again. `(depth, route)`
/// is the shape to reach for: the entry's index in the stack and what it shows. The two
/// pages of a transition must never share a key, and a debug build says so.
///
/// **A page that is not in the tree keeps its scroll offsets.** A page below the top is
/// not built, and when it is shown again under the same key it comes back where it was
/// left. What a page was animating is not kept: a widget seen again adopts its target,
/// as one seen for the first time does. And since nothing forgets the offsets of an entry
/// that was popped, an entry pushed again under the same key opens where the last one
/// was left.
///
/// A key a page's own root declares is replaced by the page's key. To name something
/// inside a page for a request by key — a focus, a scroll, a sheet — key it inside the
/// page.
///
/// ```
/// use frus_widgets::{Navigator, Text};
///
/// #[derive(Clone, Copy, Hash)]
/// enum Route {
///     Inbox,
///     Thread(u64),
/// }
///
/// // The stack the application holds: the inbox at depth 0, a thread pushed on it.
/// let settled: Navigator<()> = Navigator::new((1, Route::Thread(7)), Text::new("Thread 7"));
///
/// // Part-way through that push: the thread arriving, the inbox it came from leaving.
/// let pushing: Navigator<()> = Navigator::new((1, Route::Thread(7)), Text::new("Thread 7"))
///     .from((0, Route::Inbox), Text::new("Inbox"), 0.4, true);
/// # let _ = (settled, pushing);
/// ```
pub struct Navigator<Msg = crate::callback::Callback> {
    width: f32,
    height: f32,
    /// Transition progress (`1.0` = no transition in flight).
    progress: f32,
    /// Whether the pages are cut off at the navigator's own edge.
    clips: bool,
    /// `true` = push (entering from the right), `false` = pop (entering from the left).
    forward: bool,
    /// Whether a finger is driving the transition (a back gesture): the motion then follows
    /// it linearly instead of easing (milestone 620).
    gesture: bool,
    /// `[retained.., screen]` or `[retained.., outgoing, incoming]`, each wrapped in the
    /// [`Keyed`] its key makes.
    children: Vec<Box<dyn Widget<Msg>>>,
    /// How many of the first children are retained: kept, and not shown.
    hidden: usize,
}

impl<Msg: 'static> Navigator<Msg> {
    /// Shows a full-window screen (no transition), on **the surface it is being built
    /// for**, under the key `key` — unique to its entry of the stack (see
    /// [`Navigator`]).
    ///
    /// The size comes from [`MediaQuery::of`] — a window is a window, and the
    /// application has no business measuring one to say how far a screen slides.
    /// [`Navigator::size`] overrides it for a stack that is not the whole window, and
    /// for a test that would rather state a size than install a description.
    pub fn new(key: impl Hash, screen: impl Widget<Msg> + 'static) -> Self {
        let surface = MediaQuery::of();
        Self {
            width: surface.size.width,
            height: surface.size.height,
            progress: 1.0,
            forward: true,
            gesture: false,
            clips: true,
            children: vec![Box::new(Keyed::new(key, screen))],
            hidden: 0,
        }
    }

    /// Keeps a page **under** the one on show: it is built, so what its components hold is
    /// still there when the page is shown again, but it is not laid out, drawn or given
    /// input while it is covered.
    ///
    /// It is an entry of the stack like the others, so `key` is its `(depth, route)` and must
    /// not be the key of any page shown. Because the key — not the page's place among the
    /// children — is what identifies it, a page that goes from kept to shown, or back,
    /// is the same page throughout.
    pub fn retain(mut self, key: impl Hash, page: impl Widget<Msg> + 'static) -> Self {
        self.children
            .insert(self.hidden, Box::new(Keyed::new(key, page)));
        self.hidden += 1;
        self
    }

    /// Whether the pages are **cut off at the navigator's own edge**. `true` by default,
    /// as the reference's `Clip.hardEdge` is.
    ///
    /// A screen sliding in comes from outside the box and one sliding out goes outside
    /// it, so without this the only thing stopping them is the window: a navigator that
    /// is not the whole window paints its pages over whatever is beside it. `false` is
    /// for the rare transition meant to spill — a card that grows past its own frame —
    /// and it is a decision, not a default.
    pub fn clip_behavior(mut self, clips: bool) -> Self {
        self.clips = clips;
        self
    }

    /// The window's size, in logical pixels — an **override** of what
    /// [`Navigator::new`] read from the ambient description.
    pub fn size(mut self, width: f32, height: f32) -> Self {
        self.width = width;
        self.height = height;
        self
    }

    /// Adds the **outgoing** screen, under its own key, and the progress of a transition
    /// in flight.
    ///
    /// `key` is the key the outgoing screen had while it was on show, so that it keeps
    /// its state as it leaves; it must differ from the incoming screen's.
    pub fn from(
        mut self,
        key: impl Hash,
        previous: impl Widget<Msg> + 'static,
        progress: f32,
        forward: bool,
    ) -> Self {
        let previous = Keyed::new(key, previous);
        debug_assert!(
            self.children
                .last()
                .is_none_or(|incoming| incoming.key() != Widget::<Msg>::key(&previous)),
            "a navigator's two pages share a key, and would share their state: key each \
             page by its entry of the stack, `(depth, route)`"
        );
        self.children.insert(self.hidden, Box::new(previous));
        self.progress = progress.clamp(0.0, 1.0);
        self.forward = forward;
        self
    }

    /// Says that a **finger** is driving the transition — a back gesture — so the pages
    /// follow it linearly, as the reference's do while a pop gesture is under way, rather
    /// than on the transition's curves.
    #[must_use]
    pub fn gesture(mut self, gesture: bool) -> Self {
        self.gesture = gesture;
        self
    }
}

impl<Msg> Widget<Msg> for Navigator<Msg> {
    fn style(&self) -> Style {
        Style {
            width: Dimension::Length(self.width),
            height: Dimension::Length(self.height),
            ..Default::default()
        }
    }

    fn children(&self) -> &[Box<dyn Widget<Msg>>] {
        &self.children
    }

    fn paint(&self, _bounds: Rect, _status: Status, _theme: &Theme, _scene: &mut Scene) {}

    fn on_click(&self) -> Option<Msg> {
        None
    }

    fn navigator(&self) -> Option<(f32, bool)> {
        Some((self.progress, self.forward))
    }

    fn navigator_gesture(&self) -> bool {
        self.gesture
    }

    fn navigator_clips(&self) -> bool {
        self.clips
    }

    fn navigator_retained(&self) -> usize {
        self.hidden
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The theme these tests lay out under: one that follows iOS, whose page transition
    /// is the slide they describe (milestone 620) — whatever machine runs them.
    fn slide_theme() -> crate::Theme {
        crate::Theme::default().with_platform(frus_core::TargetPlatform::Ios)
    }
    use crate::{build_ui, Container, Runtime, Size};
    use frus_core::{Color, Primitive};

    fn screen(color: Color) -> Container<()> {
        Container::<()>::new()
            .width(400.0)
            .height(300.0)
            .color(color)
    }

    /// **A navigator's pages stop at its own edge.**
    ///
    /// A screen sliding in comes from outside the box and one sliding out goes outside
    /// it. Until milestone 398 the only thing stopping them was the window, so a
    /// navigator that was **not** the whole window painted its pages straight over
    /// whatever sat beside it — and a full-window one spent every transition frame
    /// drawing a screen nobody could see. The reference clips by default
    /// (`Clip.hardEdge`).
    ///
    /// The check is a 200×200 navigator in a 400×400 viewport, mid-transition: every
    /// primitive it paints has to be confined to its own box.
    #[test]
    fn a_navigators_pages_stop_at_its_own_edge() {
        let nav = |clips: bool| {
            let red = Color::rgb(1.0, 0.0, 0.0);
            let blue = Color::rgb(0.0, 0.0, 1.0);
            let mut navigator = Navigator::new("blue", screen(blue)).size(200.0, 200.0);
            navigator = navigator.clip_behavior(clips);
            let navigator = navigator.from("red", screen(red), 0.5, true);
            let ui = build_ui(
                &navigator,
                Size::new(400.0, 400.0),
                &Runtime::default(),
                &slide_theme(),
            );
            // The furthest right anything is allowed to be painted.
            ui.scene()
                .primitives()
                .iter()
                .filter_map(|p| match p {
                    Primitive::Rect { clip, .. } => Some(clip.x + clip.width),
                    _ => None,
                })
                .fold(0.0_f32, f32::max)
        };
        assert!(
            nav(true) <= 200.5,
            "clipped, nothing may be painted past x = 200: got {}",
            nav(true)
        );
        assert!(
            nav(false) > 200.5,
            "and a navigator told not to clip really does not"
        );
    }

    /// **Under a theme that follows Linux, a push zooms** (milestone 620): halfway, the page
    /// arriving is drawn in a layer scaled just under full size about its centre, and the
    /// page it covers in one scaled just over — the reference's 85 % → 100 % and
    /// 100 % → 105 % on the emphasized curve.
    #[test]
    fn a_push_under_a_desktop_theme_zooms() {
        let red = Color::rgb(1.0, 0.0, 0.0);
        let blue = Color::rgb(0.0, 0.0, 1.0);
        let nav = Navigator::new("blue", screen(blue))
            .size(400.0, 300.0)
            .from("red", screen(red), 0.5, true);
        let ui = build_ui(
            &nav,
            Size::new(400.0, 300.0),
            &Runtime::default(),
            &crate::Theme::default().with_platform(frus_core::TargetPlatform::Linux),
        );
        // The width each page's layer gives it on screen.
        let width_of = |c: Color| {
            ui.scene()
                .primitives()
                .iter()
                .find_map(|p| match p {
                    Primitive::Layer {
                        primitives,
                        transform: Some(transform),
                        ..
                    } if primitives
                        .iter()
                        .any(|q| matches!(q, Primitive::Rect { color, .. } if *color == c)) =>
                    {
                        Some(
                            transform
                                .affine
                                .apply_rect(Rect::new(0.0, 0.0, 400.0, 300.0))
                                .width,
                        )
                    }
                    _ => None,
                })
                .unwrap_or_else(|| panic!("{c:?} is in a scaled layer"))
        };
        let arriving = width_of(blue) / 400.0;
        let covered = width_of(red) / 400.0;
        assert!(arriving > 0.98 && arriving < 1.0, "arriving at {arriving}");
        assert!(covered > 1.04 && covered < 1.05, "covered at {covered}");
    }

    #[test]
    fn transition_renders_both_screens() {
        let red = Color::rgb(1.0, 0.0, 0.0);
        let blue = Color::rgb(0.0, 0.0, 1.0);
        let nav = Navigator::new("blue", screen(blue))
            .size(400.0, 300.0)
            .from("red", screen(red), 0.5, true);
        let ui = build_ui(
            &nav,
            Size::new(400.0, 300.0),
            &Runtime::default(),
            &slide_theme(),
        );
        let has = |c: Color| {
            ui.scene()
                .primitives()
                .iter()
                .any(|p| matches!(p, Primitive::Rect { color, .. } if *color == c))
        };
        assert!(has(red), "the outgoing screen is rendered");
        assert!(has(blue), "the incoming screen is rendered");
    }

    /// **An overlay belongs to its screen (milestone 326).** Found on a device: an app
    /// bar's overflow menu, left open while choosing an item that navigates, stayed drawn
    /// over the screen that replaced it.
    ///
    /// `process_overlays` runs after both screens and paints above the whole window, so a
    /// deferred overlay outranks everything — including the screen that covers its owner.
    /// The parallax is why nothing corrected it on its own: the outgoing screen travels
    /// only 30 % of the width, so the anchor the menu hangs from never leaves the window.
    ///
    /// `from` inserts the screen being left at index 0, so `children[1]` is always the
    /// destination — on a push, on a pop, and under a back gesture alike.
    #[test]
    fn a_departing_screens_overlay_is_not_drawn_over_the_incoming_one() {
        let mark = Color::rgb(0.0, 1.0, 0.0);
        let menu = || {
            crate::OverlayPortal::new(screen(Color::rgb(1.0, 0.0, 0.0))).overlay(
                Container::<()>::new().width(80.0).height(30.0).color(mark),
                crate::Placement::Below,
            )
        };
        let drawn = |nav: &Navigator<()>| {
            let ui = build_ui(
                nav,
                Size::new(400.0, 300.0),
                &Runtime::default(),
                &slide_theme(),
            );
            ui.scene()
                .primitives()
                .iter()
                .any(|p| matches!(p, Primitive::Rect { color, .. } if *color == mark))
        };

        // The screen being left holds the menu: it goes with the screen.
        let blue = Color::rgb(0.0, 0.0, 1.0);
        assert!(
            !drawn(
                &Navigator::new("blue", screen(blue))
                    .size(400.0, 300.0)
                    .from("menu", menu(), 0.5, true)
            ),
            "a push: the menu belongs to the screen being left"
        );
        assert!(
            !drawn(
                &Navigator::new("blue", screen(blue))
                    .size(400.0, 300.0)
                    .from("menu", menu(), 0.5, false)
            ),
            "a pop: the same, and the screen being left is the *front* one here"
        );

        // The destination's own overlay is untouched — this must not suppress overlays
        // wholesale, only the ones belonging to a screen on its way out.
        assert!(
            drawn(&Navigator::new("menu", menu()).size(400.0, 300.0).from(
                "blue",
                screen(blue),
                0.5,
                true
            )),
            "the destination's own menu is still drawn"
        );
        // And with no transition in flight, nothing changes at all.
        assert!(
            drawn(&Navigator::new("menu", menu()).size(400.0, 300.0)),
            "no transition: the menu is simply drawn"
        );
    }

    /// A page whose content scrolls — the shape of nearly every page an application has,
    /// which is what made two different ones indistinguishable by position. Its content
    /// is painted in `mark`, so where the content is drawn says how far it is scrolled.
    fn scrolling(mark: Color) -> Container<()> {
        Container::<()>::new().width(400.0).height(300.0).child(
            crate::SingleChildScrollView::new()
                .width(400.0)
                .height(300.0)
                .child(Container::new().width(400.0).height(1200.0).color(mark)),
        )
    }

    /// A navigator showing one page, settled.
    fn settled(key: impl Hash, page: Container<()>) -> Navigator<()> {
        Navigator::new(key, page).size(400.0, 300.0)
    }

    /// A navigator half-way through a transition from `(from, previous)` to `(key, page)`.
    fn moving(
        key: &str,
        page: Container<()>,
        from: &str,
        previous: Container<()>,
        forward: bool,
    ) -> Navigator<()> {
        Navigator::new(key, page)
            .size(400.0, 300.0)
            .from(from, previous, 0.5, forward)
    }

    /// How far the content painted in `mark` is scrolled, as drawn: its top edge sits at
    /// minus the offset. `None` when nothing in that colour is painted.
    fn drawn_offset(nav: &Navigator<()>, runtime: &Runtime, mark: Color) -> Option<f32> {
        let ui = build_ui(nav, Size::new(400.0, 300.0), runtime, &slide_theme());
        ui.scene().primitives().iter().find_map(|p| match p {
            Primitive::Rect { color, rect, .. } if *color == mark => Some(-rect.y),
            _ => None,
        })
    }

    /// The one scroll region a settled navigator registers.
    fn region(nav: &Navigator<()>) -> crate::WidgetId {
        let ui = build_ui(
            nav,
            Size::new(400.0, 300.0),
            &Runtime::default(),
            &slide_theme(),
        );
        let regions = ui.scroll_regions();
        assert_eq!(regions.len(), 1, "one page, one region");
        regions[0].id
    }

    const INBOX: Color = Color {
        r: 0.0,
        g: 1.0,
        b: 0.0,
        a: 1.0,
    };
    const ARCHIVE: Color = Color {
        r: 1.0,
        g: 0.0,
        b: 1.0,
        a: 1.0,
    };

    /// **Scrolling one page does not scroll another (milestone 528).** Found on a phone:
    /// "when I scroll another page, the one I just left scrolls too."
    ///
    /// Retained state is kept by identity, and identity was the position in the tree. A
    /// navigator's settled page is always its first child, so two pages built the same
    /// way — a scroll view in a box — had the same identity for their scroll regions and
    /// read one offset between them.
    #[test]
    fn two_pages_of_the_same_shape_keep_two_offsets() {
        let mut runtime = Runtime::default();
        let inbox = settled("inbox", scrolling(INBOX));
        runtime.scroll.insert(region(&inbox), (0.0, 400.0));
        assert_eq!(drawn_offset(&inbox, &runtime, INBOX), Some(400.0));

        let archive = settled("archive", scrolling(ARCHIVE));
        assert_eq!(
            drawn_offset(&archive, &runtime, ARCHIVE),
            Some(0.0),
            "the archive was never scrolled"
        );
    }

    /// **A page keeps its state through a transition (milestone 528).** Two children in
    /// flight and one at rest put the arriving page at index 1 during the slide and at
    /// index 0 once it had arrived — a different identity on each side of the settle, so
    /// an inbox scrolled down came back at the top all the way through the pop, and
    /// jumped to where it had been only when the pop was over.
    #[test]
    fn a_transition_does_not_move_state_between_its_pages() {
        let mut runtime = Runtime::default();
        runtime
            .scroll
            .insert(region(&settled("inbox", scrolling(INBOX))), (0.0, 400.0));
        runtime.scroll.insert(
            region(&settled("archive", scrolling(ARCHIVE))),
            (0.0, 700.0),
        );

        for forward in [true, false] {
            // Inbox to archive, and archive to inbox, each way round.
            let there = moving(
                "archive",
                scrolling(ARCHIVE),
                "inbox",
                scrolling(INBOX),
                forward,
            );
            let back = moving(
                "inbox",
                scrolling(INBOX),
                "archive",
                scrolling(ARCHIVE),
                forward,
            );
            for nav in [&there, &back] {
                assert_eq!(
                    drawn_offset(nav, &runtime, INBOX),
                    Some(400.0),
                    "the inbox, forward = {forward}"
                );
                assert_eq!(
                    drawn_offset(nav, &runtime, ARCHIVE),
                    Some(700.0),
                    "the archive, forward = {forward}"
                );
            }
        }
    }

    /// **The same route twice is two pages (milestone 528).** A stack can hold one route
    /// more than once — a thread opened from a thread — and each entry scrolls on its own.
    /// That is why the key names the entry and not the route: `(depth, route)` tells the
    /// two apart where the route alone would not.
    #[test]
    fn the_same_route_pushed_twice_keeps_two_offsets() {
        let mut runtime = Runtime::default();
        let lower = settled((1, "thread"), scrolling(INBOX));
        runtime.scroll.insert(region(&lower), (0.0, 400.0));

        let upper = settled((2, "thread"), scrolling(INBOX));
        assert_eq!(
            drawn_offset(&upper, &runtime, INBOX),
            Some(0.0),
            "the thread opened on top starts at its own top"
        );
        assert_eq!(
            drawn_offset(&lower, &runtime, INBOX),
            Some(400.0),
            "and the one below it is still where it was left"
        );
    }

    /// Two pages under one key would be one identity drawn twice in a frame, and a debug
    /// build refuses it rather than let them share their state.
    #[test]
    #[should_panic(expected = "share a key")]
    fn a_transitions_two_pages_may_not_share_a_key() {
        let _ = Navigator::<()>::new((1, "thread"), screen(Color::rgb(0.0, 0.0, 1.0))).from(
            (1, "thread"),
            screen(Color::rgb(1.0, 0.0, 0.0)),
            0.5,
            true,
        );
    }

    #[test]
    fn pop_parallaxes_and_orders_back_screen() {
        let red = Color::rgb(1.0, 0.0, 0.0);
        let blue = Color::rgb(0.0, 0.0, 1.0);
        // A pop half-way through: `red` = outgoing screen (front), `blue` = revealed back.
        let nav = Navigator::new("blue", screen(blue))
            .size(400.0, 300.0)
            .from("red", screen(red), 0.5, false);
        let ui = build_ui(
            &nav,
            Size::new(400.0, 300.0),
            &Runtime::default(),
            &slide_theme(),
        );
        let x_of = |c: Color| {
            ui.scene()
                .primitives()
                .iter()
                .find_map(|p| match p {
                    Primitive::Rect { color, rect, .. } if *color == c => Some(rect.x),
                    _ => None,
                })
                .expect("screen present")
        };
        let front = x_of(red); // +0.5·400 = 200
        let back = x_of(blue); // parallaxe : -0.5·400·0.3 = -60
        assert!(
            front > back,
            "the front ({front}) is to the right of the back ({back})"
        );
        // Without parallax the back would sit at -200; it is compressed toward 0.
        assert!(back > -200.0 && back < 0.0, "back parallaxed: {back}");
    }
}

#[cfg(test)]
mod retained_tests {
    use super::*;

    /// The theme these tests lay out under: one that follows iOS, whose page transition
    /// is the slide they describe (milestone 620) — whatever machine runs them.
    fn slide_theme() -> crate::Theme {
        crate::Theme::default().with_platform(frus_core::TargetPlatform::Ios)
    }
    use crate::{build_ui, Container, Runtime, Size};
    use frus_core::{Color, Primitive};

    const RED: Color = Color::rgb(1.0, 0.0, 0.0);
    const GREEN: Color = Color::rgb(0.0, 1.0, 0.0);
    const BLUE: Color = Color::rgb(0.0, 0.0, 1.0);

    fn screen(color: Color) -> Container<()> {
        Container::<()>::new()
            .width(400.0)
            .height(300.0)
            .color(color)
    }

    fn painted(nav: &Navigator<()>) -> impl Fn(Color) -> bool {
        let ui = build_ui(
            nav,
            Size::new(400.0, 300.0),
            &Runtime::default(),
            &slide_theme(),
        );
        let colours: Vec<Color> = ui
            .scene()
            .primitives()
            .iter()
            .filter_map(|p| match p {
                Primitive::Rect { color, .. } => Some(*color),
                _ => None,
            })
            .collect();
        move |c| colours.contains(&c)
    }

    /// **The pages under the one on show are kept, and stay out of the picture.** They are
    /// children of the navigator, so a walk that builds the tree builds them — which is what
    /// keeps what their components hold — and the paint walk does not reach them.
    #[test]
    fn retained_pages_are_in_the_tree_and_out_of_the_picture() {
        let nav = Navigator::new((2, 0), screen(BLUE))
            .size(400.0, 300.0)
            .retain((0, 0), screen(RED))
            .retain((1, 0), screen(GREEN));
        assert_eq!(Widget::<()>::children(&nav).len(), 3);
        assert_eq!(Widget::<()>::navigator_retained(&nav), 2);
        let has = painted(&nav);
        assert!(has(BLUE), "the page on show is drawn");
        assert!(!has(RED) && !has(GREEN), "the pages under it are not");
    }

    /// A transition still draws its two pages, whatever is kept under them — and the outgoing
    /// one goes *after* the retained ones, so the pair is where the walk expects it.
    #[test]
    fn a_transition_draws_its_two_pages_over_the_retained_ones() {
        let nav = Navigator::new("c", screen(BLUE))
            .size(400.0, 300.0)
            .retain("a", screen(RED))
            .from("b", screen(GREEN), 0.5, true);
        assert_eq!(Widget::<()>::navigator_retained(&nav), 1);
        let has = painted(&nav);
        assert!(has(GREEN) && has(BLUE), "both pages of the transition");
        assert!(!has(RED), "and not the one kept under them");
    }

    /// What identifies a page is its key, not where it sits among the children: the same page
    /// kept and then shown is the same page.
    #[test]
    fn a_page_keeps_its_identity_from_kept_to_shown() {
        let kept = Navigator::new("top", screen(BLUE)).retain("page", screen(RED));
        let shown = Navigator::new("page", screen(RED));
        let key = |nav: &Navigator<()>, at: usize| Widget::<()>::children(nav)[at].key();
        assert_eq!(key(&kept, 0), key(&shown, 0));
    }
}