sequoia-view 0.1.1

Sequoia GUI framework — view tree and runtime
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
#![allow(clippy::unwrap_used, clippy::expect_used)]

use sequoia_core::prelude::*;
use sequoia_text::TextSystem;
use sequoia_view::prelude::*;
use sequoia_view::{FillRule, Path, StrokeStyle};

fn rgb(color: Color) -> (f32, f32, f32) {
    (color.r, color.g, color.b)
}

struct DrawDemo {
    rule: FillRule,
}

impl View for DrawDemo {
    fn body(&self) -> impl View + 'static {
        Unit
    }

    fn is_leaf(&self) -> bool {
        true
    }

    fn paint_self(&self, ctx: &mut PaintCtx) {
        let _ = ctx.rect;
        let mut star = Path::new();
        star.polygon(&[
            Point::new(20.0, 0.0),
            Point::new(26.0, 14.0),
            Point::new(40.0, 14.0),
            Point::new(28.0, 24.0),
            Point::new(33.0, 40.0),
            Point::new(20.0, 30.0),
            Point::new(7.0, 40.0),
            Point::new(12.0, 24.0),
            Point::new(0.0, 14.0),
            Point::new(14.0, 14.0),
        ]);
        ctx.fill_rule(&star, self.rule, Color::WHITE);

        ctx.line(
            Point::new(0.0, 50.0),
            Point::new(40.0, 50.0),
            2.0,
            Color::RED,
        );

        let mut circle = Path::new();
        circle.circle((60.0, 20.0), 10.0);
        ctx.stroke(&circle, &StrokeStyle::new(2.0), Color::GREEN);
    }
}

#[test]
fn paint_ctx_draws_paths_and_line() {
    let rt = Runtime::new();
    rt.mount(DrawDemo {
        rule: FillRule::NonZero,
    });
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 200.0, 200.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let quads: Vec<_> = list.quads().collect();
    assert!(quads.len() >= 3, "quads: {}", quads.len());

    let white = quads
        .iter()
        .filter(|q| rgb(q.color) == rgb(Color::WHITE))
        .count();
    assert!(white > 1, "star should give several spans, got {white}");
    for q in quads.iter().filter(|q| rgb(q.color) == rgb(Color::WHITE)) {
        assert!(
            q.rect.right() <= 41.0 && q.rect.bottom() <= 41.0,
            "{:?}",
            q.rect
        );
    }

    let red: Vec<_> = quads
        .iter()
        .filter(|q| rgb(q.color) == rgb(Color::RED))
        .collect();
    assert_eq!(red.len(), 1, "{red:?}");
    assert!((red[0].rect.size.height - 2.0).abs() < 0.51);
}

#[test]
fn draw_offset_moves_geometry_without_reuse_loss() {
    struct OffsetView;
    impl View for OffsetView {
        fn body(&self) -> impl View + 'static {
            Unit
        }

        fn is_leaf(&self) -> bool {
            true
        }

        fn paint_self(&self, ctx: &mut PaintCtx) {
            let mut circle = Path::new();
            circle.circle((0.0, 0.0), 5.0);
            ctx.translate(30.0, 40.0);
            ctx.fill(&circle, Color::BLUE);
        }
    }

    let rt = Runtime::new();
    rt.mount(OffsetView);
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 100.0, 100.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let quad = list.quads().next().expect("circle quad");
    assert_eq!(quad.rect.size, Size::new(10.0, 10.0));
    assert!((quad.rect.left() - 25.0).abs() < 1e-3);
    assert!((quad.rect.top() - 35.0).abs() < 1e-3);
    assert_eq!(quad.radius, 5.0);

    let mut list2 = RenderList::new();
    rt.paint(&mut list2, &mut text);
    let stats = rt.shapes().stats();
    assert_eq!(stats.misses, 1, "{stats:?}");
    assert!(stats.hits >= 1, "{stats:?}");
}

#[test]
fn fill_rule_changes_geometry_from_cache() {
    let rt = Runtime::new();
    rt.mount(DrawDemo {
        rule: FillRule::EvenOdd,
    });
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 200.0, 200.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let stats = rt.shapes().stats();
    assert_eq!(stats.misses, 3, "{stats:?}");
    assert!(!rt.shapes().is_empty());
}

#[test]
fn horizontal_gradient_makes_abutting_bands() {
    struct Card;
    impl View for Card {
        fn body(&self) -> impl View + 'static {
            Unit
        }
        fn is_leaf(&self) -> bool {
            true
        }
        fn paint_self(&self, ctx: &mut PaintCtx) {
            let gradient = Gradient::linear(
                (0.0, 0.0),
                (100.0, 0.0),
                vec![
                    GradientStop::new(0.0, Color::RED),
                    GradientStop::new(1.0, Color::BLUE),
                ],
            );
            ctx.rounded_rect_gradient(Rect::from_xywh(0.0, 0.0, 100.0, 40.0), 8.0, &gradient);
        }
    }

    let rt = Runtime::new();
    rt.mount(Card);
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 100.0, 40.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let all: Vec<_> = list.quads().collect();
    assert!(all.len() > 20, "quads: {}", all.len());

    let mut rows: std::collections::HashMap<i32, Vec<_>> = std::collections::HashMap::new();
    for q in &all {
        rows.entry((q.rect.top() * 64.0).round() as i32)
            .or_default()
            .push(q);
    }
    let widest = rows.values_mut().max_by_key(|row| row.len()).expect("row");
    widest.sort_by(|a, b| a.rect.left().partial_cmp(&b.rect.left()).unwrap());
    assert!(widest.len() > 20, "bands in a row: {}", widest.len());

    let left = widest.first().expect("pasmo");
    let right = widest.last().expect("pasmo");
    assert!(left.color.r > 0.9 && left.color.b < 0.2, "{:?}", left.color);
    assert!(
        right.color.b > 0.9 && right.color.r < 0.2,
        "{:?}",
        right.color
    );

    for pair in widest.windows(2) {
        let (a, b) = (&pair[0], &pair[1]);
        let seam = a.rect.right() - b.rect.left();
        assert!(
            seam.abs() <= 0.5 + 1e-3,
            "seam: {:?} vs {:?} (offset {seam}, max overlap 0.5)",
            a.rect,
            b.rect
        );
    }

    for pair in widest.windows(2) {
        assert!(pair[1].color.r <= pair[0].color.r + 1e-3);
        assert!(pair[1].color.b >= pair[0].color.b - 1e-3);
    }
}

#[test]
fn vertical_gradient_keeps_alpha_edges() {
    struct Column;
    impl View for Column {
        fn body(&self) -> impl View + 'static {
            Unit
        }
        fn is_leaf(&self) -> bool {
            true
        }
        fn paint_self(&self, ctx: &mut PaintCtx) {
            let gradient = Gradient::linear(
                (0.0, 0.0),
                (0.0, 40.0),
                vec![
                    GradientStop::new(0.0, Color::WHITE),
                    GradientStop::new(
                        1.0,
                        Color {
                            a: 0.0,
                            ..Color::WHITE
                        },
                    ),
                ],
            );
            let mut path = Path::new();
            path.rect(Rect::from_xywh(0.0, 0.0, 30.0, 40.0));
            ctx.fill_gradient(&path, &gradient);
        }
    }

    let rt = Runtime::new();
    rt.mount(Column);
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 30.0, 40.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let quads: Vec<_> = list.quads().collect();
    assert!(quads.len() > 10, "pasm: {}", quads.len());

    let top = quads
        .iter()
        .min_by(|a, b| a.rect.top().total_cmp(&b.rect.top()))
        .unwrap();
    let bottom = quads
        .iter()
        .max_by(|a, b| a.rect.top().total_cmp(&b.rect.top()))
        .unwrap();
    assert!(top.color.a > 0.9, "{:?}", top.color);
    assert!(bottom.color.a < 0.1, "{:?}", bottom.color);
    assert!(
        quads
            .iter()
            .all(|q| (q.rect.size.width - 30.0).abs() < 1e-3)
    );
}

#[test]
fn outer_shadow_is_one_primitive_with_offset_and_blur() {
    struct Card;
    impl View for Card {
        fn body(&self) -> impl View + 'static {
            Unit
        }
        fn is_leaf(&self) -> bool {
            true
        }
        fn paint_self(&self, ctx: &mut PaintCtx) {
            ctx.shadow(
                Rect::from_xywh(0.0, 0.0, 100.0, 60.0),
                8.0,
                12.0,
                Point::new(0.0, 4.0),
                Color {
                    a: 0.4,
                    ..Color::BLACK
                },
            );
        }
    }

    let rt = Runtime::new();
    rt.mount(Card);
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 160.0, 120.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    assert!(
        list.quads().next().is_none(),
        "shadow is one primitive, not a layer stack"
    );
    let shadows: Vec<_> = list.shadows().collect();
    assert_eq!(shadows.len(), 1, "single shadow primitive");
    let s = shadows[0];
    assert_eq!(
        s.rect,
        Rect::from_xywh(0.0, 0.0, 100.0, 60.0),
        "caster shape"
    );
    assert_eq!(s.radius, 8.0);
    assert_eq!(s.blur, 12.0);
    assert_eq!(s.spread, 0.0);
    assert_eq!(s.offset, Vec2::new(0.0, 4.0));
    assert!(!s.inset, "outer shadow");
    assert_eq!(s.color.a, 0.4);

    let b = s.bounds();
    assert!((b.left() - (-6.0)).abs() < 1e-3, "{b:?}");
    assert!((b.top() - (-2.0)).abs() < 1e-3, "{b:?}");
    assert!((b.size.width - 112.0).abs() < 1e-3, "{b:?}");
}

#[test]
fn inner_shadow_is_inset_and_stays_inside_the_shape() {
    struct Card;
    impl View for Card {
        fn body(&self) -> impl View + 'static {
            Unit
        }
        fn is_leaf(&self) -> bool {
            true
        }
        fn paint_self(&self, ctx: &mut PaintCtx) {
            ctx.inner_shadow(
                Rect::from_xywh(10.0, 10.0, 80.0, 40.0),
                4.0,
                10.0,
                Point::new(0.0, 2.0),
                Color {
                    a: 0.5,
                    ..Color::BLACK
                },
            );
        }
    }

    let rt = Runtime::new();
    rt.mount(Card);
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 160.0, 120.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let s = *list.shadows().next().expect("inner shadow");
    assert!(s.inset, "inset");
    assert_eq!(s.rect, Rect::from_xywh(10.0, 10.0, 80.0, 40.0));
    assert_eq!(s.blur, 10.0);
    assert_eq!(s.offset, Vec2::new(0.0, 2.0));
    assert_eq!(s.bounds(), s.rect, "inner shadow stays inside the shape");
}

#[test]
fn invisible_shadows_paint_nothing() {
    struct Card;
    impl View for Card {
        fn body(&self) -> impl View + 'static {
            Unit
        }
        fn is_leaf(&self) -> bool {
            true
        }
        fn paint_self(&self, ctx: &mut PaintCtx) {
            let r = Rect::from_xywh(0.0, 0.0, 40.0, 20.0);

            ctx.shadow(r, 4.0, 0.0, Point::ZERO, Color::BLACK);
            ctx.inner_shadow(r, 4.0, 8.0, Point::ZERO, Color::TRANSPARENT);
            ctx.shadow(Rect::ZERO, 4.0, 8.0, Point::ZERO, Color::BLACK);
        }
    }

    let rt = Runtime::new();
    rt.mount(Card);
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 80.0, 60.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);
    assert!(list.is_empty(), "paints nothing: {:?}", list.items());
}

#[test]
fn convex_shapes_are_single_quads() {
    struct Shapes;
    impl View for Shapes {
        fn body(&self) -> impl View + 'static {
            Unit
        }

        fn is_leaf(&self) -> bool {
            true
        }

        fn paint_self(&self, ctx: &mut PaintCtx) {
            ctx.disk(Point::new(10.0, 10.0), 8.0, Color::RED);
            ctx.rounded_rect(Rect::from_xywh(0.0, 30.0, 40.0, 20.0), 4.0, Color::GREEN);
            ctx.fill_rect(Rect::from_xywh(0.0, 60.0, 20.0, 10.0), Color::BLUE);
            let _ = ctx.draw_svg("M 0 80 L 10 80 L 10 90 Z", None, Color::WHITE);
            let _ = ctx.draw_svg(
                "M 0 80 L 10 80 L 10 90",
                Some(&StrokeStyle::new(1.0)),
                Color::RED,
            );
        }
    }

    let rt = Runtime::new();
    rt.mount(Shapes);
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 100.0, 100.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let quads: Vec<_> = list.quads().collect();

    assert!(
        quads
            .iter()
            .filter(|q| rgb(q.color) == rgb(Color::RED))
            .count()
            >= 2
    );
    let disk = quads
        .iter()
        .find(|q| q.radius > 0.0 && rgb(q.color) == rgb(Color::RED))
        .expect("circle");
    assert_eq!(disk.radius, 8.0);
    assert_eq!(disk.rect.size, Size::new(16.0, 16.0));

    let rounded = quads
        .iter()
        .find(|q| q.radius > 0.0 && rgb(q.color) == rgb(Color::GREEN))
        .expect("rounded rect");
    assert_eq!(rounded.radius, 4.0);

    let svg_quads: Vec<_> = quads
        .iter()
        .filter(|q| rgb(q.color) == rgb(Color::WHITE))
        .collect();
    assert!(!svg_quads.is_empty(), "SVG triangle did not paint");
    for q in &svg_quads {
        assert!(
            q.rect.left() >= 0.0
                && q.rect.right() <= 11.0
                && q.rect.top() >= 79.0
                && q.rect.bottom() <= 91.0,
            "{:?}",
            q.rect
        );
    }
}

struct Toolkit;

impl View for Toolkit {
    fn body(&self) -> impl View + 'static {
        Unit
    }

    fn is_leaf(&self) -> bool {
        true
    }

    fn paint_self(&self, ctx: &mut PaintCtx) {
        let mut ring = Path::new();
        ring.circle((40.0, 40.0), 20.0);
        let sweep = Gradient::linear(
            (20.0, 40.0),
            (60.0, 40.0),
            vec![
                GradientStop::new(0.0, Color::RED),
                GradientStop::new(1.0, Color::BLUE),
            ],
        );
        ctx.stroke_gradient(&ring, &StrokeStyle::new(4.0), &sweep);

        ctx.rounded_rect_corners(
            Rect::from_xywh(0.0, 70.0, 60.0, 30.0),
            [20.0, 0.0, 20.0, 0.0],
            Color::GREEN,
        );

        ctx.sector(Point::new(120.0, 40.0), 20.0, 0.0, 90.0, Color::WHITE);
        ctx.donut_sector(
            Point::new(120.0, 100.0),
            12.0,
            20.0,
            0.0,
            180.0,
            Color::WHITE,
        );

        ctx.dotted_line(
            Point::new(0.0, 130.0),
            Point::new(80.0, 130.0),
            4.0,
            10.0,
            Color::BLACK,
        );
    }
}

fn paint(view: impl View + 'static, size: (f32, f32)) -> Vec<sequoia_core::Quad> {
    let rt = Runtime::new();
    rt.mount(view);
    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, size.0, size.1), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);
    list.quads().copied().collect()
}

#[test]
fn stroke_gradient_paints_both_ends() {
    let quads = paint(Toolkit, (200.0, 200.0));
    let reds = quads.iter().filter(|q| rgb(q.color) == rgb(Color::RED));
    let blues = quads.iter().filter(|q| rgb(q.color) == rgb(Color::BLUE));
    assert!(reds.count() > 0, "missing left end of outline gradient");
    assert!(blues.count() > 0, "missing right end of outline gradient");
}

#[test]
fn per_corner_radii_are_tessellated_not_single_quad() {
    let quads = paint(Toolkit, (200.0, 200.0));
    let green: Vec<_> = quads
        .iter()
        .filter(|q| rgb(q.color) == rgb(Color::GREEN))
        .collect();
    assert!(!green.is_empty(), "missing rounded-corner rect");

    assert!(
        green.iter().all(|q| q.radius == 0.0),
        "uneven radius took the fast path"
    );
    assert!(green.len() > 3, "corner quads: {}", green.len());
    for q in &green {
        assert!(
            q.rect.top() >= 70.0 && q.rect.bottom() <= 101.0,
            "{:?}",
            q.rect
        );
    }
}

#[test]
fn sector_covers_its_quadrant_only() {
    let quads = paint(Toolkit, (200.0, 200.0));
    let wedge: Vec<_> = quads
        .iter()
        .filter(|q| {
            rgb(q.color) == rgb(Color::WHITE) && q.rect.top() < 70.0 && q.rect.left() > 100.0
        })
        .collect();
    assert!(!wedge.is_empty(), "missing slice");
    for q in &wedge {
        assert!(
            q.rect.left() >= 119.0 && q.rect.right() <= 141.0,
            "{:?}",
            q.rect
        );
        assert!(
            q.rect.top() >= 39.0 && q.rect.bottom() <= 61.0,
            "{:?}",
            q.rect
        );
    }
}

#[test]
fn donut_sector_leaves_the_hole_empty() {
    let quads = paint(Toolkit, (200.0, 200.0));
    let ring: Vec<_> = quads
        .iter()
        .filter(|q| rgb(q.color) == rgb(Color::WHITE) && q.rect.top() > 95.0)
        .collect();
    assert!(!ring.is_empty(), "missing ring");

    let hole = Point::new(120.0, 106.0);
    for q in &ring {
        let covers = hole.x >= q.rect.left()
            && hole.x <= q.rect.right()
            && hole.y >= q.rect.top()
            && hole.y <= q.rect.bottom();
        assert!(!covers, "ring hole painted: {:?}", q.rect);
    }
    for q in &ring {
        assert!(
            q.rect.right() <= 141.0 && q.rect.bottom() <= 121.0,
            "{:?}",
            q.rect
        );
    }
}

#[test]
fn dotted_line_makes_one_disk_per_step() {
    let quads = paint(Toolkit, (200.0, 200.0));
    let dots: Vec<_> = quads
        .iter()
        .filter(|q| rgb(q.color) == rgb(Color::BLACK))
        .collect();

    assert_eq!(dots.len(), 9, "kropek: {}", dots.len());
    for d in &dots {
        assert_eq!(d.radius, 2.0);
        assert_eq!(d.rect.size, Size::new(4.0, 4.0));
        assert!((d.rect.top() - 128.0).abs() < 1e-3, "{:?}", d.rect);
    }
}

#[test]
fn crisp_line_lands_on_a_pixel_boundary() {
    struct Hairline;
    impl View for Hairline {
        fn body(&self) -> impl View + 'static {
            Unit
        }
        fn is_leaf(&self) -> bool {
            true
        }
        fn paint_self(&self, ctx: &mut PaintCtx) {
            ctx.line_crisp(
                Point::new(0.0, 50.0),
                Point::new(80.0, 50.0),
                1.0,
                Color::RED,
            );
        }
    }

    let quads = paint(Hairline, (100.0, 100.0));
    let red: Vec<_> = quads
        .iter()
        .filter(|q| rgb(q.color) == rgb(Color::RED))
        .collect();
    assert_eq!(red.len(), 1, "{red:?}");
    assert!((red[0].rect.top() - 50.0).abs() < 1e-3, "{:?}", red[0].rect);
    assert!(
        (red[0].rect.bottom() - 51.0).abs() < 1e-3,
        "{:?}",
        red[0].rect
    );
    assert!(
        (red[0].color.a - 1.0).abs() < 1e-3,
        "krycie {:?}",
        red[0].color
    );
}

#[test]
fn crisp_line_leaves_diagonals_alone() {
    struct Diagonal;
    impl View for Diagonal {
        fn body(&self) -> impl View + 'static {
            Unit
        }
        fn is_leaf(&self) -> bool {
            true
        }
        fn paint_self(&self, ctx: &mut PaintCtx) {
            ctx.line_crisp(
                Point::new(0.0, 0.0),
                Point::new(40.0, 40.0),
                1.0,
                Color::RED,
            );
        }
    }

    let quads = paint(Diagonal, (60.0, 60.0));
    let red: Vec<_> = quads
        .iter()
        .filter(|q| rgb(q.color) == rgb(Color::RED))
        .collect();
    assert!(!red.is_empty());

    assert!(
        red.iter().any(|q| q.color.a > 0.05 && q.color.a < 0.95),
        "missing partial coverage on diagonal"
    );
    let left = red
        .iter()
        .map(|q| q.rect.left())
        .fold(f32::INFINITY, f32::min);
    let right = red
        .iter()
        .map(|q| q.rect.right())
        .fold(f32::NEG_INFINITY, f32::max);
    let top = red
        .iter()
        .map(|q| q.rect.top())
        .fold(f32::INFINITY, f32::min);
    let bottom = red
        .iter()
        .map(|q| q.rect.bottom())
        .fold(f32::NEG_INFINITY, f32::max);

    assert!(
        (right - left) > 38.0 && (bottom - top) > 38.0,
        "bbox {left}/{top} — {right}/{bottom}"
    );
}