gpui-fast 0.1.9

gpui: GPUI with gpui-fast rendering and layout optimizations
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
// Modified by Longbridge for gpui-fast.
//! Frames drawn with everything a window retains between frames must match the
//! frames it would draw with none of it.
//!
//! Each run drives two windows holding the same view through the same random
//! history of changes. One draws every frame as an application would, reusing
//! retained layout nodes. The other forgets them before every frame, so it draws each one as though it
//! were its first. Every frame, the two must paint the same primitives in the
//! same places and leave the same hitboxes; any difference is something a
//! retained shortcut got wrong.
//!
//! A shortcut taken within a single frame is taken by both windows alike, so
//! it is invisible here; its own tests have to cover it.

use std::{borrow::Cow, sync::Arc};

use rand::{Rng as _, SeedableRng as _, rngs::StdRng};

use crate::{
    AnyElement, App, Bounds, Context, DevicePixels, Entity, Font, FontId, FontMetrics, FontRun,
    Global, GlyphId, Hsla, InputEvent as _, IntoElement, LineLayout, ListAlignment, ListOffset,
    ListState, MouseMoveEvent, NoopTextSystem, Pixels, PlatformTextSystem, Render,
    RenderGlyphParams, Result, SharedString, Size, StyleRefinement, TestAppContext,
    TextRenderingMode, UniformListScrollHandle, Window, WindowHandle, anchored, deferred, div,
    hsla, list, point, prelude::*, px, size, uniform_list,
};

const WORDS: [&str; 10] = [
    "a",
    "grid",
    "cell",
    "ticking",
    "value",
    "with a longer label",
    "42",
    "lorem ipsum dolor",
    "x",
    "a label long enough to be cut short",
];

const PALETTE: [Hsla; 5] = [
    hsla(0.0, 0.0, 0.1, 1.0),
    hsla(0.6, 0.7, 0.5, 1.0),
    hsla(0.3, 0.6, 0.4, 1.0),
    hsla(0.0, 0.8, 0.6, 1.0),
    hsla(0.1, 0.9, 0.5, 0.5),
];

const GRID_CELLS: usize = 24;
const ROW_HEIGHT: f32 = 20.;
const INITIAL_ROWS: u64 = 30;

#[derive(Clone, Copy, Debug)]
enum CellFlag {
    Background,
    Underline,
    Truncate,
    Hover,
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct CellState {
    word: usize,
    color: usize,
    width: f32,
    background: bool,
    underline: bool,
    truncate: bool,
    hover: bool,
}

/// How list rows identify themselves.
#[derive(Clone, Copy, Debug)]
enum RowIdentity {
    Position,
    Id,
    /// Wrapped in an element with the id, so switching to or from `Id`
    /// hands a row's node to its wrapper or its first child.
    Wrapped,
}

#[derive(Clone, Debug)]
enum Change {
    Word {
        cell: usize,
        word: usize,
    },
    Color {
        cell: usize,
        color: usize,
    },
    Width {
        cell: usize,
        width: f32,
    },
    Toggle {
        cell: usize,
        flag: CellFlag,
    },
    Paragraph {
        words: usize,
        width: f32,
    },
    InsertRow {
        at: usize,
    },
    RemoveRow {
        at: usize,
    },
    Scroll {
        top: f32,
    },
    InsertChip {
        at: usize,
    },
    RemoveChip {
        at: usize,
    },
    RotateChips {
        by: usize,
    },
    RowIdentity(RowIdentity),
    Direction,
    Badge,
    /// Changes one panel, notifying only it.
    Panel {
        panel: usize,
        value: usize,
    },
    /// Changes the leaf view nested in a panel, notifying only it.
    Leaf {
        panel: usize,
    },
    /// Changes only the colors of a panel, notifying only it: its layout
    /// stays as it was, so the view around it is drawn from last frame
    /// around it.
    TintPanel {
        panel: usize,
    },
    /// Changes only the colors of the leaf in a panel, notifying only it: the
    /// views around it are drawn from last frame around it.
    TintLeaf {
        panel: usize,
    },
    /// Changes the model some panels read without observing it, notifying
    /// only the model.
    Shared {
        value: usize,
    },
    /// Changes a global some panels read.
    Global {
        value: usize,
    },
    MoveMouse {
        x: f32,
        y: f32,
    },
    Resize {
        width: f32,
        height: f32,
    },
    /// Focuses a panel, or nothing past the last.
    Focus {
        panel: usize,
    },
    Redraw,
}

impl Change {
    fn random(rng: &mut StdRng) -> Self {
        let cell = rng.random_range(0..GRID_CELLS);
        match rng.random_range(0..124) {
            0..20 => Change::Word {
                cell,
                word: rng.random_range(0..WORDS.len()),
            },
            20..27 => Change::Color {
                cell,
                color: rng.random_range(0..PALETTE.len()),
            },
            27..33 => Change::Width {
                cell,
                width: rng.random_range(20.0..220.0),
            },
            33..42 => Change::Toggle {
                cell,
                flag: [
                    CellFlag::Background,
                    CellFlag::Underline,
                    CellFlag::Truncate,
                    CellFlag::Hover,
                ][rng.random_range(0..4)],
            },
            42..48 => Change::Paragraph {
                words: rng.random_range(0..40),
                width: rng.random_range(60.0..400.0),
            },
            48..55 => Change::InsertRow {
                at: rng.random_range(0..64),
            },
            55..61 => Change::RemoveRow {
                at: rng.random_range(0..64),
            },
            61..67 => Change::Scroll {
                top: rng.random_range(0.0..400.0),
            },
            67..69 => Change::InsertChip {
                at: rng.random_range(0..16),
            },
            69..71 => Change::RemoveChip {
                at: rng.random_range(0..16),
            },
            71..72 => Change::RotateChips {
                by: rng.random_range(1..4),
            },
            72..75 => Change::RowIdentity(
                [RowIdentity::Position, RowIdentity::Id, RowIdentity::Wrapped]
                    [rng.random_range(0..3)],
            ),
            75..77 => Change::Direction,
            77..83 => Change::Badge,
            83..92 => Change::MoveMouse {
                x: rng.random_range(0.0..900.0),
                y: rng.random_range(0.0..700.0),
            },
            100..105 => Change::Panel {
                panel: rng.random_range(0..PANELS),
                value: rng.random_range(0..WORDS.len()),
            },
            105..109 => Change::Leaf {
                panel: rng.random_range(0..PANELS),
            },
            109..113 => Change::Shared {
                value: rng.random_range(0..WORDS.len()),
            },
            113..116 => Change::Global {
                value: rng.random_range(0..PALETTE.len()),
            },
            116..120 => Change::TintPanel {
                panel: rng.random_range(0..PANELS),
            },
            120..124 => Change::TintLeaf {
                panel: rng.random_range(0..PANELS),
            },
            95..98 => Change::Focus {
                panel: rng.random_range(0..=PANELS),
            },
            92..95 => Change::Resize {
                width: rng.random_range(300.0..1000.0),
                height: rng.random_range(240.0..800.0),
            },
            _ => Change::Redraw,
        }
    }
}

const PANELS: usize = 6;

/// A small application: a grid of cached cell views, wrapping paragraphs, a
/// row of chips, a cached child view, child views that are not cached, one of
/// them deferred, and the same rows in a uniform list and a list.
struct OracleView {
    cells: Vec<CellState>,
    /// A view per cell, showing the cell's state.
    cell_views: Vec<Entity<GridCell>>,
    paragraph_words: usize,
    paragraph_width: Pixels,
    rows: Vec<u64>,
    /// Chips share the row numbering, so no chip and row are the same thing.
    chips: Vec<u64>,
    next_row: u64,
    row_identity: RowIdentity,
    scroll_top: Pixels,
    column: bool,
    uniform_scroll: UniformListScrollHandle,
    list_state: ListState,
    badge: Entity<Badge>,
    panels: Vec<Entity<Panel>>,
    shared: Entity<Shared>,
}

impl OracleView {
    fn new(cx: &mut Context<Self>) -> Self {
        let shared = cx.new(|_| Shared { value: 0 });
        let cells: Vec<CellState> = (0..GRID_CELLS)
            .map(|ix| CellState {
                word: ix % WORDS.len(),
                color: ix % PALETTE.len(),
                width: 40. + (ix % 5) as f32 * 30.,
                background: ix.is_multiple_of(3),
                truncate: ix.is_multiple_of(4),
                ..CellState::default()
            })
            .collect();
        Self {
            cell_views: cells
                .iter()
                .map(|&cell| cx.new(|_| GridCell(cell)))
                .collect(),
            cells,
            paragraph_words: 12,
            paragraph_width: px(180.),
            rows: (0..INITIAL_ROWS).collect(),
            chips: (INITIAL_ROWS..INITIAL_ROWS + 6).collect(),
            next_row: INITIAL_ROWS + 6,
            row_identity: RowIdentity::Position,
            scroll_top: px(0.),
            column: false,
            uniform_scroll: UniformListScrollHandle::new(),
            list_state: ListState::new(INITIAL_ROWS as usize, ListAlignment::Top, px(40.)),
            badge: cx.new(|_| Badge { count: 0 }),
            panels: {
                (0..PANELS)
                    .map(|ix| {
                        let shared = shared.clone();
                        cx.new(|cx| Panel {
                            ix,
                            value: ix,
                            tint: 0,
                            shared,
                            leaf: cx.new(|_| Leaf { count: ix, tint: 0 }),
                            focus: cx.focus_handle(),
                        })
                    })
                    .collect()
            },
            shared,
        }
    }

    fn apply(&mut self, change: &Change, cx: &mut Context<Self>) {
        match *change {
            Change::Word { cell, word } => self.update_cell(cell, |cell| cell.word = word, cx),
            Change::Color { cell, color } => self.update_cell(cell, |cell| cell.color = color, cx),
            Change::Width { cell, width } => self.update_cell(cell, |cell| cell.width = width, cx),
            Change::Toggle { cell, flag } => self.update_cell(
                cell,
                |cell| {
                    let value = match flag {
                        CellFlag::Background => &mut cell.background,
                        CellFlag::Underline => &mut cell.underline,
                        CellFlag::Truncate => &mut cell.truncate,
                        CellFlag::Hover => &mut cell.hover,
                    };
                    *value = !*value;
                },
                cx,
            ),
            Change::Paragraph { words, width } => {
                self.paragraph_words = words;
                self.paragraph_width = px(width);
            }
            Change::InsertRow { at } => {
                let at = at % (self.rows.len() + 1);
                self.rows.insert(at, self.next_row);
                self.next_row += 1;
                self.list_state.splice(at..at, 1);
            }
            Change::RemoveRow { at } => {
                if self.rows.is_empty() {
                    return;
                }
                let at = at % self.rows.len();
                self.rows.remove(at);
                self.list_state.splice(at..at + 1, 0);
            }
            Change::Scroll { top } => self.scroll_top = px(top),
            Change::InsertChip { at } => {
                let at = at % (self.chips.len() + 1);
                self.chips.insert(at, self.next_row);
                self.next_row += 1;
            }
            Change::RemoveChip { at } => {
                if !self.chips.is_empty() {
                    let at = at % self.chips.len();
                    self.chips.remove(at);
                }
            }
            Change::RotateChips { by } => {
                if !self.chips.is_empty() {
                    let by = by % self.chips.len();
                    self.chips.rotate_left(by);
                }
            }
            Change::RowIdentity(identity) => self.row_identity = identity,
            Change::Direction => self.column = !self.column,
            Change::Badge
            | Change::Panel { .. }
            | Change::Leaf { .. }
            | Change::TintPanel { .. }
            | Change::TintLeaf { .. }
            | Change::Shared { .. } => {
                self.children().apply_to_children(change, cx);
                return;
            }
            Change::Global { .. }
            | Change::Focus { .. }
            | Change::MoveMouse { .. }
            | Change::Resize { .. }
            | Change::Redraw => return,
        }
        cx.notify();
    }

    /// The views nested in this one and the model, for changes applied to
    /// them alone.
    fn children(&self) -> Children {
        Children {
            badge: self.badge.clone(),
            panels: self.panels.clone(),
            shared: self.shared.clone(),
        }
    }

    /// Changes a cell and notifies its view; the parent is notified too,
    /// because it sizes the cell.
    fn update_cell(
        &mut self,
        ix: usize,
        change: impl FnOnce(&mut CellState),
        cx: &mut Context<Self>,
    ) {
        change(&mut self.cells[ix]);
        let cell = self.cells[ix];
        self.cell_views[ix].update(cx, |view, cx| {
            view.0 = cell;
            cx.notify();
        });
    }
}

/// Handles on the views nested in an [`OracleView`], and on its model.
struct Children {
    badge: Entity<Badge>,
    panels: Vec<Entity<Panel>>,
    shared: Entity<Shared>,
}

impl Children {
    /// Changes a nested view, or the model, notifying only it.
    /// The harness applies these without updating this view, so that it is
    /// dirty only because of what is nested in it, as it is when an
    /// application notifies a nested view on its own.
    fn apply_to_children(&self, change: &Change, cx: &mut App) {
        match *change {
            Change::Badge => {
                // Only the child is notified, so the parent's frame reuses
                // whatever it can of the last one around it.
                self.badge.update(cx, |badge, cx| {
                    badge.count += 1;
                    cx.notify();
                });
            }
            Change::Panel { panel, value } => {
                self.panels[panel].update(cx, |panel, cx| {
                    panel.value = value;
                    cx.notify();
                });
            }
            Change::Leaf { panel } => {
                let leaf = self.panels[panel].read(cx).leaf.clone();
                leaf.update(cx, |leaf, cx| {
                    leaf.count += 1;
                    cx.notify();
                });
            }
            Change::TintPanel { panel } => {
                self.panels[panel].update(cx, |panel, cx| {
                    panel.tint += 1;
                    cx.notify();
                });
            }
            Change::TintLeaf { panel } => {
                let leaf = self.panels[panel].read(cx).leaf.clone();
                leaf.update(cx, |leaf, cx| {
                    leaf.tint += 1;
                    cx.notify();
                });
            }
            Change::Shared { value } => {
                self.shared.update(cx, |shared, cx| {
                    shared.value = value;
                    cx.notify();
                });
            }
            _ => unreachable!("not a change to a nested view"),
        }
    }
}

/// A cell of the grid, drawn as a cached view.
struct GridCell(CellState);

impl Render for GridCell {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        render_cell(self.0)
    }
}

fn render_cell(cell: CellState) -> AnyElement {
    div()
        .flex()
        .flex_row()
        .w(px(cell.width))
        .h(px(18.))
        .text_color(PALETTE[(cell.color + 1) % PALETTE.len()])
        .when(cell.background, |this| this.bg(PALETTE[cell.color]))
        .when(cell.underline, |this| this.underline())
        .when(cell.truncate, |this| {
            this.overflow_hidden().whitespace_nowrap().text_ellipsis()
        })
        .when(cell.hover, |this| {
            this.hover(|style| style.bg(PALETTE[4]).text_color(PALETTE[0]))
        })
        .child(WORDS[cell.word])
        .id("cell")
        .role(accesskit::Role::Cell)
        .aria_label(WORDS[cell.word])
        .into_any_element()
}

fn render_row(row: u64, identity: RowIdentity) -> AnyElement {
    let word = WORDS[(row as usize * 7) % WORDS.len()];
    let row_element = div()
        .flex()
        .flex_row()
        .gap_2()
        .h(px(ROW_HEIGHT))
        .child(SharedString::from(format!("row {row}")))
        .child(
            div()
                .w(px(8. + (row % 4) as f32 * 6.))
                .h(px(8.))
                .bg(PALETTE[row as usize % PALETTE.len()]),
        )
        .child(
            div()
                .border_1()
                .border_color(PALETTE[1])
                .px_1()
                .when(row.is_multiple_of(3), |this| {
                    this.hover(|style| style.bg(PALETTE[2]))
                })
                .child(word),
        );
    match identity {
        RowIdentity::Position => row_element.into_any_element(),
        RowIdentity::Id => row_element
            .id(("row", row))
            .role(accesskit::Role::Row)
            .into_any_element(),
        RowIdentity::Wrapped => div()
            .id(("row", row))
            .role(accesskit::Role::Row)
            .aria_label(word)
            .child(row_element)
            .into_any_element(),
    }
}

/// A chip in a row whose children come and go and change places, which a
/// retained parent has to follow.
fn render_chip(chip: u64, identity: RowIdentity) -> AnyElement {
    let chip_element = div()
        .flex()
        .flex_row()
        .px_1()
        .h(px(16.))
        .min_w(px(10. + (chip % 5) as f32 * 8.))
        .bg(PALETTE[chip as usize % PALETTE.len()])
        .when(chip.is_multiple_of(2), |this| {
            this.border_1().border_color(PALETTE[0])
        })
        .child(SharedString::from(chip.to_string()));
    match identity {
        RowIdentity::Position => chip_element.into_any_element(),
        RowIdentity::Id => chip_element.id(("chip", chip)).into_any_element(),
        RowIdentity::Wrapped => div()
            .id(("chip", chip))
            .child(chip_element)
            .into_any_element(),
    }
}

impl Render for OracleView {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        let paragraph: String = (0..self.paragraph_words)
            .map(|ix| WORDS[ix % WORDS.len()])
            .collect::<Vec<_>>()
            .join(" ");

        let rows = self.rows.clone();
        let identity = self.row_identity;
        self.uniform_scroll
            .0
            .borrow()
            .base_handle
            .set_offset(point(px(0.), -self.scroll_top));
        let uniform_rows = uniform_list("uniform rows", rows.len(), {
            let rows = rows.clone();
            move |range, _, _| range.map(|ix| render_row(rows[ix], identity)).collect()
        })
        .track_scroll(&self.uniform_scroll)
        .w(px(260.))
        .h(px(120.));

        if !rows.is_empty() {
            let item_ix = ((self.scroll_top / px(ROW_HEIGHT)).floor() as usize).min(rows.len() - 1);
            self.list_state.scroll_to(ListOffset {
                item_ix,
                offset_in_item: px(self.scroll_top.as_f32() % ROW_HEIGHT),
            });
        }
        let list_rows = list(self.list_state.clone(), move |ix, _, _| {
            render_row(rows[ix], identity)
        })
        .w(px(260.))
        .h(px(120.));

        div()
            .id("oracle")
            .role(accesskit::Role::Pane)
            .size_full()
            .flex()
            .flex_wrap()
            .gap_2()
            .when(self.column, |this| this.flex_col())
            .child(
                div()
                    .flex()
                    .flex_row()
                    .flex_wrap()
                    .w(px(420.))
                    .gap_1()
                    // Every cell is a cached view notified when it changes, so
                    // the window drawing incrementally reuses the ones that did
                    // not change while the one drawing from scratch builds them
                    // all.
                    .children(self.cells.iter().zip(&self.cell_views).map(|(cell, view)| {
                        view.clone()
                            .cached(StyleRefinement::default().w(px(cell.width)).h(px(18.)))
                    })),
            )
            .child(
                div()
                    .w(self.paragraph_width)
                    .text_color(PALETTE[0])
                    .child(paragraph.clone()),
            )
            .child(
                // A flex item is measured for its content size before it is
                // shrunk to fit, so this text is shaped unconstrained and then
                // at whatever width it ends up with.
                div()
                    .flex()
                    .flex_row()
                    .w(self.paragraph_width * 0.8)
                    .child(div().text_color(PALETTE[1]).child(paragraph))
                    .child(div().w(px(24.)).h(px(12.)).bg(PALETTE[2])),
            )
            .child(
                self.badge
                    .clone()
                    .cached(StyleRefinement::default().w(px(120.)).h(px(24.))),
            )
            .child(
                div()
                    .flex()
                    .flex_row()
                    .gap_1()
                    .children(self.chips.iter().map(|&chip| render_chip(chip, identity))),
            )
            .child(
                div()
                    .flex()
                    .flex_row()
                    .flex_wrap()
                    .gap_1()
                    .children(self.panels[..PANELS - 1].iter().cloned()),
            )
            .child(
                div()
                    .size(px(10.))
                    .child(deferred(anchored().child(self.panels[PANELS - 1].clone()))),
            )
            .child(uniform_rows)
            .child(list_rows)
    }
}

/// A model panels read without observing it.
struct Shared {
    value: usize,
}

/// A global panels read.
struct Accent(usize);

impl Global for Accent {}

/// A child view that is not cached, so the window keeps it
/// from one frame to the next by itself. Some panels read the shared model
/// and the global.
struct Panel {
    ix: usize,
    value: usize,
    /// Shifts its colors, leaving its layout as it was.
    tint: usize,
    shared: Entity<Shared>,
    leaf: Entity<Leaf>,
    focus: crate::FocusHandle,
}

impl Render for Panel {
    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let shared = if self.ix.is_multiple_of(2) {
            self.shared.read(cx).value
        } else {
            0
        };
        let accent = if self.ix.is_multiple_of(3) {
            cx.try_global::<Accent>().map_or(0, |accent| accent.0)
        } else {
            0
        };
        div()
            .id("panel")
            .role(accesskit::Role::Group)
            .aria_label(WORDS[(self.value + shared) % WORDS.len()])
            .track_focus(&self.focus)
            .when(self.ix.is_multiple_of(2), |this| {
                this.on_a11y_action(accesskit::Action::Increment, |_, _, _| {})
            })
            .flex()
            .flex_col()
            .p_1()
            .bg(PALETTE[(self.value + accent + self.tint) % PALETTE.len()])
            .when(self.ix == 1, |this| {
                this.hover(|style| style.bg(PALETTE[4]))
            })
            .child(WORDS[(self.value + shared) % WORDS.len()])
            // Some leaves are cached, so a panel is drawn around a cached
            // view built again in it, as around one that is not.
            .when(self.ix >= PANELS / 2, |this| {
                this.child(
                    self.leaf
                        .clone()
                        .cached(StyleRefinement::default().w(px(30.)).h(px(8.))),
                )
            })
            .when(self.ix < PANELS / 2, |this| this.child(self.leaf.clone()))
    }
}

/// A view nested in a panel, notified on its own.
struct Leaf {
    count: usize,
    tint: usize,
}

impl Render for Leaf {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        let count = self.count;
        div()
            .id("leaf")
            // Now and then without a role, so without a node: the panel's
            // node, drawn from last frame around it, has other children.
            .when(count % 4 != 3, |this| this.role(accesskit::Role::List))
            .aria_active_descendant()
            .aria_value(SharedString::from(self.tint.to_string()))
            .a11y_synthetic_children(move |builder| {
                for ix in 0..count % 3 + 1 {
                    let id = builder.synthetic_node_id(ix);
                    let mut node = accesskit::Node::new(accesskit::Role::Image);
                    node.set_label(format!("box {ix}"));
                    builder.push_child(id, node);
                }
            })
            .flex()
            .flex_row()
            .children((0..self.count % 3 + 1).map(|ix| {
                div()
                    .w(px(4. + ix as f32 * 5.))
                    .h(px(6.))
                    .bg(PALETTE[(self.count + ix + self.tint) % PALETTE.len()])
            }))
    }
}

/// A child view that is sometimes notified on its own.
struct Badge {
    count: usize,
}

impl Render for Badge {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .id("badge")
            .role(accesskit::Role::Status)
            .flex()
            .flex_row()
            .gap_1()
            .size_full()
            .children((0..self.count % 4 + 1).map(|ix| {
                div()
                    .w(px(6. + ix as f32 * 3.))
                    .h(px(10.))
                    .bg(PALETTE[ix % PALETTE.len()])
            }))
            .child(SharedString::from(self.count.to_string()))
    }
}

/// The no-op text system, except that every glyph rasterizes to a small box,
/// so text paints a sprite per glyph and where each glyph went is compared.
pub(super) struct GlyphBoxTextSystem(pub(super) NoopTextSystem);

impl PlatformTextSystem for GlyphBoxTextSystem {
    fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
        self.0.add_fonts(fonts)
    }

    fn all_font_names(&self) -> Vec<String> {
        self.0.all_font_names()
    }

    fn font_id(&self, descriptor: &Font) -> Result<FontId> {
        self.0.font_id(descriptor)
    }

    fn font_metrics(&self, font_id: FontId) -> FontMetrics {
        self.0.font_metrics(font_id)
    }

    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>> {
        self.0.typographic_bounds(font_id, glyph_id)
    }

    fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
        self.0.advance(font_id, glyph_id)
    }

    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
        self.0.glyph_for_char(font_id, ch)
    }

    fn glyph_raster_bounds(&self, _params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
        Ok(Bounds {
            origin: point(DevicePixels(0), DevicePixels(-8)),
            size: size(DevicePixels(5), DevicePixels(9)),
        })
    }

    fn rasterize_glyph(
        &self,
        _params: &RenderGlyphParams,
        raster_bounds: Bounds<DevicePixels>,
    ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
        let area = raster_bounds.size.width.0 * raster_bounds.size.height.0;
        Ok((raster_bounds.size, vec![u8::MAX; area as usize]))
    }

    fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout {
        self.0.layout_line(text, font_size, runs)
    }

    fn recommended_rendering_mode(&self, font_id: FontId, font_size: Pixels) -> TextRenderingMode {
        self.0.recommended_rendering_mode(font_id, font_size)
    }
}

fn apply(cx: &mut TestAppContext, window: WindowHandle<OracleView>, change: &Change) {
    match *change {
        Change::MoveMouse { x, y } => {
            cx.update_window(window.into(), |_, window, cx| {
                window.dispatch_event(
                    MouseMoveEvent {
                        position: point(px(x), px(y)),
                        pressed_button: None,
                        modifiers: Default::default(),
                    }
                    .to_platform_input(),
                    cx,
                );
            })
            .unwrap();
        }
        Change::Resize { width, height } => {
            cx.simulate_window_resize(window.into(), size(px(width), px(height)));
        }
        Change::Global { value } => cx.update(|cx| cx.set_global(Accent(value))),
        Change::Focus { panel } => {
            cx.update_window(window.into(), |view, window, cx| {
                let view = view.downcast::<OracleView>().unwrap();
                match view.read(cx).panels.get(panel) {
                    Some(panel) => {
                        let focus = panel.read(cx).focus.clone();
                        window.focus(&focus, cx);
                    }
                    None => window.blur(cx),
                }
            })
            .unwrap();
        }
        Change::Badge
        | Change::Panel { .. }
        | Change::Leaf { .. }
        | Change::TintPanel { .. }
        | Change::TintLeaf { .. }
        | Change::Shared { .. } => {
            let view = window.read_with(cx, |view, _| view.children()).unwrap();
            cx.update(|cx| view.apply_to_children(change, cx));
        }
        _ => window
            .update(cx, |view, _, cx| view.apply(change, cx))
            .unwrap(),
    }
}

/// Draws a frame, after forgetting what the window retains if asked, and
/// returns what it drew with the layout nodes it reused.
fn draw(
    cx: &mut TestAppContext,
    window: WindowHandle<OracleView>,
    from_scratch: bool,
) -> (Vec<String>, u64, bool, Option<A11ySnapshot>) {
    cx.update_window(window.into(), |_, window, cx| {
        if from_scratch {
            window.forget_retained_state();
        }
        window.reset_layout_stats();
        window.draw(cx).clear(cx);
        (
            window.describe_rendered_frame(),
            window.layout_stats().nodes_reused,
            window.rendered_frame.retained.reused_any(),
            window
                .is_a11y_active()
                .then(|| super::a11y::a11y_snapshot(window)),
        )
    })
    .unwrap()
}

type A11ySnapshot = (String, Vec<super::a11y::A11yEntry>);

/// Drives both windows through one random history and returns how many
/// layout nodes the incremental window reused along the way.
fn run(seed: u64, steps: usize, a11y: bool) -> (u64, usize) {
    let mut cx = TestAppContext::with_text_system(Arc::new(GlyphBoxTextSystem(NoopTextSystem)));
    let incremental = cx.add_window(|_, cx| OracleView::new(cx));
    let from_scratch = cx.add_window(|_, cx| OracleView::new(cx));
    // What scroll layers composite is checked against drawing from scratch
    // by the layer oracle; here the incremental window draws its lists.
    cx.update_window(incremental.into(), |_, window, _| {
        window.set_scroll_layers(false)
    })
    .unwrap();
    if a11y {
        for window in [incremental, from_scratch] {
            cx.update_window(window.into(), |_, window, _| {
                window.set_a11y_active_for_tests(true)
            })
            .unwrap();
        }
    }
    let mut rng = StdRng::seed_from_u64(seed);
    let mut history: Vec<Vec<Change>> = Vec::new();
    let mut reused = 0;
    let mut frames_reusing_subtrees = 0;

    for step in 0..steps {
        let changes: Vec<Change> = if step == 0 {
            Vec::new()
        } else {
            (0..rng.random_range(1..=3))
                .map(|_| Change::random(&mut rng))
                .collect()
        };
        for change in &changes {
            apply(&mut cx, incremental, change);
            apply(&mut cx, from_scratch, change);
        }
        history.push(changes);

        let (expected, reused_from_scratch, subtrees_from_scratch, expected_a11y) =
            draw(&mut cx, from_scratch, true);
        let (actual, reused_incrementally, reused_subtrees, actual_a11y) =
            draw(&mut cx, incremental, false);
        if let (Some(actual), Some(expected)) = (&actual_a11y, &expected_a11y)
            && let Some(difference) = super::a11y::a11y_difference(actual, expected, 0.)
        {
            panic!(
                "seed {seed}, step {step}: the incremental accessibility tree differs from the \
                 tree built from scratch:\n{difference}\nchanges so far:\n{history:#?}"
            );
        }
        assert_eq!(actual_a11y.is_some(), a11y);
        assert_eq!(
            reused_from_scratch, 0,
            "a window that forgot its layout nodes cannot have reused any"
        );
        assert!(
            !subtrees_from_scratch,
            "a refreshed window cannot draw anything from its last frame"
        );
        reused += reused_incrementally;
        frames_reusing_subtrees += reused_subtrees as usize;

        if actual != expected {
            let first = actual
                .iter()
                .zip(&expected)
                .position(|(actual, expected)| actual != expected)
                .unwrap_or(actual.len().min(expected.len()));
            let excerpt = |lines: &[String]| {
                lines
                    .iter()
                    .enumerate()
                    .skip(first.saturating_sub(2))
                    .take(5)
                    .map(|(ix, line)| format!("  {ix}: {line}"))
                    .collect::<Vec<_>>()
                    .join("\n")
            };
            let history = history
                .iter()
                .enumerate()
                .map(|(step, changes)| format!("  {step}: {changes:?}"))
                .collect::<Vec<_>>()
                .join("\n");
            panic!(
                "seed {seed}, step {step}: the incremental frame differs from the frame drawn \
                 from scratch at line {first} ({} lines against {})\n\
                 incremental:\n{}\nfrom scratch:\n{}\nchanges so far:\n{history}",
                actual.len(),
                expected.len(),
                excerpt(&actual),
                excerpt(&expected),
            );
        }
    }
    (reused, frames_reusing_subtrees)
}

#[test]
fn incremental_frames_match_frames_drawn_from_scratch() {
    let (reused, frames_reusing_subtrees) = (0..24)
        .map(|seed| run(seed, 60, false))
        .fold((0, 0), |(a, b), (c, d)| (a + c, b + d));
    assert!(
        reused > 0,
        "the incremental window never reused a layout node, so nothing was compared"
    );
    assert!(
        frames_reusing_subtrees > 0,
        "the incremental window never drew a view again from its last frame"
    );
}

#[test]
fn incremental_accessibility_trees_match_trees_built_from_scratch() {
    let (_, frames_reusing_subtrees) = (100..116)
        .map(|seed| run(seed, 60, true))
        .fold((0, 0), |(a, b), (c, d)| (a + c, b + d));
    assert!(
        frames_reusing_subtrees > 0,
        "the incremental window never drew a view again from its last frame"
    );
}