frust-widgets 0.5.2

Baseline widget set for Frust: text, buttons, images, flex, stack and scroll containers, and the authoring helpers for custom widgets.
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
//! Integration tests for `container`/`colored_box`
//! (`crates/frust-widgets/src/container.rs`): child-sizing passthrough (the
//! wrapper hugs its child; a childless `.expand()`/`.size(...)` box;
//! `.size_centered(...)`'s fixed-size-with-centered-child case), paint
//! discipline (glow then fill then border, the border's half-width inset
//! generalized per corner, per-corner radius forwarding, dashed vs. solid
//! border style), the "no decoration = no paint commands" contract, and
//! semantics passthrough (purely decorative — the container itself
//! contributes no accessibility node). Also carries a lighter integration
//! pass for `divider` (`crates/frust-widgets/src/divider.rs`) alongside its
//! own in-module unit tests.

use std::any::Any;

use frust_core::accesskit::Role;
use frust_core::{
    BoxConstraints, BuildCtx, ChangeFlags, CornerRadii, DashPattern, LayoutCtx, PaintCtx,
    PaintScene, RenderRoot, View, Widget, any,
};
use frust_widgets::{BorderStyle, Column, colored_box, container, divider, text};
use kurbo::{BezPath, Point, RoundedRect, Shape, Size};
use peniko::{Brush, Color};

// ---------------------------------------------------------------------------
// Local fixture: a leaf of fixed intrinsic size (mirrors
// `frust_widgets::test_support::leaf`, unavailable here without the crate's
// non-default `test-support` feature — the same local-fixture approach
// `tests/visit_children.rs`/`tests/semantics_tree.rs` already use).
// ---------------------------------------------------------------------------

struct Leaf {
    intrinsic: Size,
}

fn leaf(width: f64, height: f64) -> Leaf {
    Leaf {
        intrinsic: Size::new(width, height),
    }
}

struct LeafWidget {
    intrinsic: Size,
}

impl View<()> for Leaf {
    type Element = LeafWidget;

    fn build(&self, _ctx: &mut BuildCtx<'_>) -> LeafWidget {
        LeafWidget {
            intrinsic: self.intrinsic,
        }
    }

    fn rebuild(
        &self,
        _prev: &Self,
        element: &mut LeafWidget,
        _ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        element.intrinsic = self.intrinsic;
        ChangeFlags::LAYOUT
    }
}

impl Widget for LeafWidget {
    fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        bc.constrain(self.intrinsic)
    }

    fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut dyn PaintScene) {
        scene.fill_rect(ctx.origin(), ctx.size(), Color::BLACK);
    }
}

fn build<S: 'static, V: View<S>>(view: &V) -> V::Element {
    let mut counter = 0u64;
    view.build(&mut BuildCtx::new(&mut counter))
}

// ---------------------------------------------------------------------------
// Paint recorder: glow/fill/border/plain-fill_rect calls in emission order
// (`calls`), plus the border's exact stroke path (`border_paths`) so a
// per-corner border geometry test can compare it against an independently
// reconstructed `kurbo::RoundedRect` path, and a plain `fill_rect` log
// (`rects`) `divider`/a child leaf paint into.
// ---------------------------------------------------------------------------

#[derive(Debug, PartialEq)]
enum PaintCall {
    /// A [`frust_core::PaintScene::fill_rounded_rect_radii`] call — the only
    /// fill path the widget uses now; a uniform `.radius(f64)` arrives here
    /// as `CornerRadii::uniform`.
    Fill { radii: CornerRadii, color: Color },
    /// A solid border stroke ([`BorderStyle::Solid`], the default).
    Border { width: f64, color: Color },
    /// A dashed border stroke ([`BorderStyle::Dashed`]).
    DashedBorder {
        width: f64,
        color: Color,
        dash: DashPattern,
    },
    /// A `.glow(...)` ambient shadow.
    Shadow {
        origin: Point,
        size: Size,
        radius: f64,
        std_dev: f64,
        color: Color,
    },
}

#[derive(Default)]
struct Recorder {
    calls: Vec<PaintCall>,
    border_paths: Vec<BezPath>,
    rects: Vec<(Point, Size)>,
}

impl PaintScene for Recorder {
    fn fill_rect(&mut self, origin: Point, size: Size, _color: Color) {
        self.rects.push((origin, size));
    }
    fn draw_text(&mut self, _origin: Point, _text: &str) {}

    fn fill_rounded_rect_radii(
        &mut self,
        _origin: Point,
        _size: Size,
        radii: CornerRadii,
        color: Color,
    ) {
        self.calls.push(PaintCall::Fill { radii, color });
    }

    fn stroke_path(&mut self, _origin: Point, path: &BezPath, width: f64, brush: &Brush) {
        if let Brush::Solid(color) = brush {
            self.calls.push(PaintCall::Border {
                width,
                color: *color,
            });
            self.border_paths.push(path.clone());
        }
    }

    fn stroke_path_dashed(
        &mut self,
        _origin: Point,
        path: &BezPath,
        width: f64,
        dash: DashPattern,
        brush: &Brush,
    ) {
        if let Brush::Solid(color) = brush {
            self.calls.push(PaintCall::DashedBorder {
                width,
                color: *color,
                dash,
            });
            self.border_paths.push(path.clone());
        }
    }

    fn draw_shadow(&mut self, origin: Point, size: Size, radius: f64, std_dev: f64, color: Color) {
        self.calls.push(PaintCall::Shadow {
            origin,
            size,
            radius,
            std_dev,
            color,
        });
    }
}

fn paint_at(w: &mut impl Widget, size: Size) -> Recorder {
    let mut rec = Recorder::default();
    let mut ctx = PaintCtx::new(Point::ZERO, size);
    w.paint(&mut ctx, &mut rec);
    rec
}

// ---------------------------------------------------------------------------
// Child sizing passthrough
// ---------------------------------------------------------------------------

#[test]
fn wrapper_hugs_the_child_exactly() {
    let view: frust_widgets::ContainerView<()> = container(leaf(40.0, 20.0)).fill(Color::WHITE);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    assert_eq!(
        size,
        Size::new(40.0, 20.0),
        "decoration (fill/border/radius) never grows the box beyond the child's own size"
    );
}

#[test]
fn childless_default_collapses_to_the_incoming_minimum() {
    // No `.expand()`/`.size(...)`: the `Sizing::Hug` default, mirroring
    // `SizedBox`'s childless-spacer precedent.
    let view: frust_widgets::ContainerView<()> = colored_box();
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let bc = BoxConstraints::new(Size::new(10.0, 15.0), Size::new(500.0, 500.0));
    let size = w.layout(&mut lctx, &bc);
    assert_eq!(size, Size::new(10.0, 15.0));
}

#[test]
fn childless_expand_fills_a_loose_constraints_max() {
    // The `AppBackground` full-bleed case: a `Stack` hands a loose (min-zero)
    // constraint, and `.expand()` must still claim the whole viewport.
    let view: frust_widgets::ContainerView<()> = colored_box().fill(Color::WHITE).expand();
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(320.0, 640.0)));
    assert_eq!(size, Size::new(320.0, 640.0));
}

#[test]
fn childless_expand_fills_a_tight_constraint_too() {
    let view: frust_widgets::ContainerView<()> = colored_box().expand();
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::tight(Size::new(200.0, 100.0)));
    assert_eq!(size, Size::new(200.0, 100.0));
}

// ---------------------------------------------------------------------------
// Infinite-max fill axes: `BoxConstraints::constrain`'s clamp is a no-op when
// `max == f64::INFINITY`, so `.expand()` must collapse to `bc.min()` on an
// unbounded axis rather than reporting the old 1e7 `EXPAND_INTRINSIC`
// sentinel — the real shape `Flex`'s inflexible-child intrinsic-probe pass
// (`flex.rs:341`), `ScrollView`'s content layout (`scroll.rs:621`), and
// `ListView`'s row layout (`list_view.rs:1720`) all hand a childless
// expanding box.
// ---------------------------------------------------------------------------

#[test]
fn childless_expand_collapses_to_min_under_an_unbounded_axis() {
    // Both axes unbounded (no enclosing bound at all): `.expand()` collapses
    // to the incoming minimum on both, not a large sentinel.
    let view: frust_widgets::ContainerView<()> = colored_box().fill(Color::WHITE).expand();
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let bc = BoxConstraints::new(Size::new(5.0, 8.0), Size::new(f64::INFINITY, f64::INFINITY));
    let size = w.layout(&mut lctx, &bc);
    assert_eq!(
        size,
        Size::new(5.0, 8.0),
        "both axes unbounded: `.expand()` collapses to `bc.min()` on both, \
         not a large sentinel"
    );
}

#[test]
fn childless_expand_fills_the_bounded_axis_and_collapses_the_unbounded_one() {
    // A mixed case: width bounded (fills to it, the normal `.expand()`
    // behavior), height unbounded (collapses to its minimum instead).
    let view: frust_widgets::ContainerView<()> = colored_box().fill(Color::WHITE).expand();
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let bc = BoxConstraints::new(Size::new(0.0, 3.0), Size::new(320.0, f64::INFINITY));
    let size = w.layout(&mut lctx, &bc);
    assert_eq!(size, Size::new(320.0, 3.0));
}

#[test]
fn expand_with_child_returns_the_childs_measured_size_under_an_unbounded_axis() {
    // `.expand()` has no effect once a child is attached (layout always
    // hugs the child — see the module docs' "Sizing + a child" section), so
    // under a fully unbounded incoming constraint the container reports the
    // child's own measured size on both axes, never a large sentinel.
    let view: frust_widgets::ContainerView<()> = container(leaf(40.0, 20.0)).expand();
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let bc = BoxConstraints::new(Size::ZERO, Size::new(f64::INFINITY, f64::INFINITY));
    let size = w.layout(&mut lctx, &bc);
    assert_eq!(size, Size::new(40.0, 20.0));
}

#[test]
fn colored_box_expand_inside_a_flex_intrinsic_pass_does_not_report_the_old_sentinel() {
    // `Column`'s inflexible-child pass (`flex.rs`) probes each inflexible
    // child under an unbounded main axis (`0..INFINITY`) to measure its
    // natural size — the same "intrinsic probe" shape `ScrollView`/
    // `ListView` hand a childless expanding box on their own unbounded
    // content axis. A childless `.expand()`ed `colored_box` sitting in that
    // slot must collapse to its measured minimum (zero here) on the probed
    // main axis, not leak the old 1e7 `EXPAND_INTRINSIC` sentinel into the
    // flex's main-axis allocation.
    let view: frust_widgets::FlexView<()> =
        Column(vec![any(colored_box().fill(Color::WHITE).expand())]);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(320.0, 640.0)));
    assert_eq!(
        size,
        Size::new(320.0, 0.0),
        "the Column's cross axis (width) still fills to its bound, but the \
         probed main axis (height) collapses to zero instead of leaking the \
         old EXPAND_INTRINSIC sentinel into the flex's main-axis allocation"
    );
}

#[test]
fn childless_size_forces_a_fixed_extent() {
    // The `FillBox`/swatch case: an explicit fixed size, clamped into
    // whatever constraint is loose enough to allow it.
    let view: frust_widgets::ContainerView<()> = colored_box().fill(Color::WHITE).size(48.0, 24.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    assert_eq!(size, Size::new(48.0, 24.0));
}

#[test]
fn childless_size_clamps_into_a_tighter_incoming_max() {
    let view: frust_widgets::ContainerView<()> = colored_box().size(48.0, 24.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(30.0, 30.0)));
    assert_eq!(
        size,
        Size::new(30.0, 24.0),
        "a fixed size larger than the incoming max still clamps, same as SizedBox"
    );
}

// ---------------------------------------------------------------------------
// Paint discipline
// ---------------------------------------------------------------------------

#[test]
fn no_decoration_paints_nothing_of_its_own() {
    let view: frust_widgets::ContainerView<()> = container(leaf(40.0, 20.0));
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(40.0, 20.0));
    assert!(
        rec.calls.is_empty(),
        "neither .fill nor .border were set — the container paints no commands of its own"
    );
}

#[test]
fn fill_only_paints_exactly_one_fill_command() {
    let view: frust_widgets::ContainerView<()> = colored_box()
        .fill(Color::from_rgb8(0x11, 0x22, 0x33))
        .size(10.0, 10.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(10.0, 10.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Fill {
            radii: CornerRadii::default(),
            color: Color::from_rgb8(0x11, 0x22, 0x33)
        }]
    );
}

#[test]
fn fill_then_border_paint_in_that_order_with_radius_forwarded() {
    let view: frust_widgets::ContainerView<()> = colored_box()
        .fill(Color::from_rgb8(0xAA, 0xBB, 0xCC))
        .radius(6.0)
        .border(Color::from_rgb8(0x00, 0x00, 0x00), 2.0)
        .size(50.0, 50.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(50.0, 50.0));
    assert_eq!(
        rec.calls,
        vec![
            PaintCall::Fill {
                radii: CornerRadii::uniform(6.0),
                color: Color::from_rgb8(0xAA, 0xBB, 0xCC)
            },
            PaintCall::Border {
                width: 2.0,
                color: Color::from_rgb8(0x00, 0x00, 0x00)
            },
        ],
        "fill paints before the border, and radius forwards to the fill call"
    );
}

#[test]
fn border_only_paints_without_a_fill_command() {
    let view: frust_widgets::ContainerView<()> = colored_box()
        .border(Color::from_rgb8(0x99, 0x00, 0x00), 1.0)
        .size(20.0, 20.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(20.0, 20.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Border {
            width: 1.0,
            color: Color::from_rgb8(0x99, 0x00, 0x00)
        }]
    );
}

/// Recreates the exact geometry `Widget::paint` builds for the border's
/// rounded-rect stroke path, so the half-width inset can be asserted against
/// the resulting `kurbo::Rect` bounds directly (mirrors `Button`'s/
/// `material::card`'s outlined-variant test shape).
#[test]
fn border_strokes_fully_inside_the_container_bounds_inset_by_half_width() {
    let width = 4.0;
    let radius = 10.0;
    let size = Size::new(60.0, 40.0);
    let half = width / 2.0;
    let rr = RoundedRect::new(
        half,
        half,
        size.width - half,
        size.height - half,
        (radius - half).max(0.0),
    );
    let bounds = rr.rect();
    assert_eq!(bounds.x0, half);
    assert_eq!(bounds.y0, half);
    assert_eq!(bounds.x1, size.width - half);
    assert_eq!(bounds.y1, size.height - half);

    // Same geometry, exercised through the real widget: a border wider than
    // twice the radius clamps the inset corner radius at zero rather than
    // going negative.
    let view: frust_widgets::ContainerView<()> = colored_box()
        .fill(Color::WHITE)
        .radius(2.0)
        .border(Color::BLACK, 10.0)
        .size(60.0, 40.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, size);
    assert_eq!(
        rec.calls[1],
        PaintCall::Border {
            width: 10.0,
            color: Color::BLACK
        }
    );
}

// ---------------------------------------------------------------------------
// Per-corner radius, dashed border, glow — container decoration extras.
// ---------------------------------------------------------------------------

#[test]
fn per_corner_radii_forward_to_the_fill_call_uncollapsed() {
    // Four distinct corners: `fill_rounded_rect_radii` must receive them
    // exactly, not collapsed to a single uniform value.
    let radii = CornerRadii::new(2.0, 4.0, 6.0, 8.0);
    let view: frust_widgets::ContainerView<()> = colored_box()
        .fill(Color::from_rgb8(0x10, 0x20, 0x30))
        .radius(radii)
        .size(50.0, 50.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(50.0, 50.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Fill {
            radii,
            color: Color::from_rgb8(0x10, 0x20, 0x30)
        }]
    );
}

#[test]
fn a_bare_f64_radius_still_builds_a_uniform_corner_radii() {
    // `.radius(impl Into<CornerRadii>)`'s uniform ergonomics: `.radius(6.0)`
    // must still forward `CornerRadii::uniform(6.0)`, matching the pre-existing
    // `.radius(f64)` call shape exactly.
    let view: frust_widgets::ContainerView<()> = colored_box()
        .fill(Color::WHITE)
        .radius(6.0)
        .size(10.0, 10.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(10.0, 10.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Fill {
            radii: CornerRadii::uniform(6.0),
            color: Color::WHITE
        }]
    );
}

/// Per-corner fills and the border stroke path must agree: the border's
/// rounded-rect path is built from the *same* per-corner radii the fill call
/// receives, each corner independently inset by half the stroke width —
/// reconstructed here exactly as `Widget::paint` builds it, mirroring
/// `border_strokes_fully_inside_the_container_bounds_inset_by_half_width`'s
/// independent-geometry approach for the uniform case.
#[test]
fn per_corner_border_path_agrees_with_the_per_corner_fill_inset_by_half_width() {
    let radii = CornerRadii::new(2.0, 6.0, 10.0, 14.0);
    let width = 4.0;
    let size = Size::new(60.0, 50.0);
    let half = width / 2.0;

    let view: frust_widgets::ContainerView<()> = colored_box()
        .radius(radii)
        .border(Color::BLACK, width)
        .size(size.width, size.height);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, size);

    let expected = RoundedRect::new(
        half,
        half,
        size.width - half,
        size.height - half,
        (
            (radii.top_left - half).max(0.0),
            (radii.top_right - half).max(0.0),
            (radii.bottom_right - half).max(0.0),
            (radii.bottom_left - half).max(0.0),
        ),
    )
    // Mirrors `BORDER_TOLERANCE` in `container.rs` — a private const, so
    // reconstructed here as the same literal.
    .to_path(0.1);
    assert_eq!(rec.border_paths, vec![expected]);
}

#[test]
fn border_style_defaults_to_solid() {
    let view: frust_widgets::ContainerView<()> =
        colored_box().border(Color::BLACK, 2.0).size(20.0, 20.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(20.0, 20.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Border {
            width: 2.0,
            color: Color::BLACK
        }]
    );
}

#[test]
fn dashed_border_style_produces_a_dashed_stroke_command() {
    let dash = DashPattern::new(4.0, 2.0);
    let view: frust_widgets::ContainerView<()> = colored_box()
        .border(Color::BLACK, 2.0)
        .border_style(BorderStyle::Dashed(dash))
        .size(20.0, 20.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(20.0, 20.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::DashedBorder {
            width: 2.0,
            color: Color::BLACK,
            dash
        }],
        "`.border_style(BorderStyle::Dashed(..))` must route through \
         `stroke_path_dashed`, not the solid `stroke_path`"
    );
}

#[test]
fn glow_paints_before_fill_and_border_shadow_beneath_the_shape() {
    let glow_color = Color::from_rgb8(0x00, 0x00, 0x00);
    let view: frust_widgets::ContainerView<()> = colored_box()
        .glow(glow_color, 4.0, 0.0)
        .fill(Color::WHITE)
        .border(Color::BLACK, 1.0)
        .size(30.0, 30.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(30.0, 30.0));
    assert_eq!(
        rec.calls,
        vec![
            PaintCall::Shadow {
                origin: Point::ZERO,
                size: Size::new(30.0, 30.0),
                radius: 0.0,
                std_dev: 4.0,
                color: glow_color,
            },
            PaintCall::Fill {
                radii: CornerRadii::default(),
                color: Color::WHITE,
            },
            PaintCall::Border {
                width: 1.0,
                color: Color::BLACK,
            },
        ],
        "glow paints beneath the fill/border, mirroring `material::card`'s \
         `draw_shadow`-then-`fill` elevation order"
    );
}

#[test]
fn glow_spread_inflates_the_shadow_rect_symmetrically() {
    let view: frust_widgets::ContainerView<()> =
        colored_box().glow(Color::BLACK, 3.0, 5.0).size(40.0, 20.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(40.0, 20.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Shadow {
            origin: Point::new(-5.0, -5.0),
            size: Size::new(50.0, 30.0),
            // clamped fill radius (no `.radius(...)` call) is 0; spread(5)
            // adds on top per the CSS box-shadow spread+border-radius rule
            // (`.glow`'s doc) — a square caster's shadow still gains rounded
            // corners equal to the spread distance.
            radius: 5.0,
            std_dev: 3.0,
            color: Color::BLACK,
        }]
    );
}

#[test]
fn glow_negative_spread_shrinks_the_shadow_rect_and_clamps_at_zero() {
    // A spread more negative than half the smaller dimension would drive the
    // shadow rect negative; it clamps to zero instead of underflowing.
    let view: frust_widgets::ContainerView<()> = colored_box()
        .glow(Color::BLACK, 1.0, -100.0)
        .size(40.0, 20.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(40.0, 20.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Shadow {
            origin: Point::new(100.0, 100.0),
            size: Size::ZERO,
            radius: 0.0,
            std_dev: 1.0,
            color: Color::BLACK,
        }]
    );
}

#[test]
fn glow_radius_lowers_through_the_largest_corner() {
    let radii = CornerRadii::new(2.0, 9.0, 3.0, 4.0);
    let view: frust_widgets::ContainerView<()> = colored_box()
        .radius(radii)
        .glow(Color::BLACK, 1.0, 0.0)
        .size(20.0, 20.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(20.0, 20.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Shadow {
            origin: Point::ZERO,
            size: Size::new(20.0, 20.0),
            radius: 9.0,
            std_dev: 1.0,
            color: Color::BLACK,
        }]
    );
}

#[test]
fn glow_clamps_a_pill_radius_to_the_fill_geometry_then_adds_spread() {
    // `.radius(999.0)` is the `RADIUS_FULL` pill/circle idiom (muxr's
    // sessions-pane pulsing dot). kurbo clamps a `RoundedRect`'s own corner
    // radius to half the shorter side, so the fill's true rendered corner on
    // a 10x10 box is 5px, not 999 — the unclamped radius fed straight into
    // `draw_shadow` was the device-confirmed defect (a thick dark ring
    // swallowing the pulse ring beneath it).
    let view: frust_widgets::ContainerView<()> = colored_box()
        .radius(999.0)
        .glow(Color::BLACK, 1.0, 2.0)
        .size(10.0, 10.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(10.0, 10.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Shadow {
            origin: Point::new(-2.0, -2.0),
            size: Size::new(14.0, 14.0),
            // clamp(999, 10/2 = 5) + spread(2) = 5 + 2 = 7.
            radius: 7.0,
            std_dev: 1.0,
            color: Color::BLACK,
        }],
        "a circular fill keeps a circular halo: the shadow radius is the \
         fill's clamped corner (size.min_side/2) plus spread, never the raw \
         999 pill radius"
    );
}

#[test]
fn glow_plain_small_radius_keeps_radius_plus_spread() {
    let view: frust_widgets::ContainerView<()> = colored_box()
        .radius(4.0)
        .glow(Color::BLACK, 1.0, 3.0)
        .size(100.0, 60.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(100.0, 60.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Shadow {
            origin: Point::new(-3.0, -3.0),
            size: Size::new(106.0, 66.0),
            // A radius well under the box's own half-min-side isn't clamped
            // at all: clamp(4, 60/2 = 30) + spread(3) = 4 + 3 = 7.
            radius: 7.0,
            std_dev: 1.0,
            color: Color::BLACK,
        }]
    );
}

#[test]
fn glow_zero_spread_keeps_the_clamped_fill_radius() {
    let view: frust_widgets::ContainerView<()> = colored_box()
        .radius(999.0)
        .glow(Color::BLACK, 1.0, 0.0)
        .size(10.0, 10.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, Size::new(10.0, 10.0));
    assert_eq!(
        rec.calls,
        vec![PaintCall::Shadow {
            origin: Point::ZERO,
            size: Size::new(10.0, 10.0),
            // clamp(999, 10/2 = 5) + spread(0) = 5.
            radius: 5.0,
            std_dev: 1.0,
            color: Color::BLACK,
        }]
    );
}

// ---------------------------------------------------------------------------
// `.size_centered(...)` — the `Panel::fixed` fixed-size-with-centered-child
// case.
// ---------------------------------------------------------------------------

#[test]
fn size_centered_forces_the_fixed_size_even_with_a_child_attached() {
    let view: frust_widgets::ContainerView<()> =
        container(leaf(20.0, 10.0)).size_centered(100.0, 60.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    assert_eq!(
        size,
        Size::new(100.0, 60.0),
        "`.size_centered` forces the fixed size even though a child is attached, \
         unlike plain `.size`"
    );
}

#[test]
fn size_centered_centers_the_child_in_the_free_space() {
    let view: frust_widgets::ContainerView<()> =
        container(leaf(20.0, 10.0)).size_centered(100.0, 60.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    let rec = paint_at(&mut w, size);
    // (100-20)/2 = 40, (60-10)/2 = 25 — the child's leaf paints a fill_rect at
    // its absolute origin, which threads through the container's own origin
    // (zero here) plus the child's centered local origin.
    assert!(
        rec.rects
            .contains(&(Point::new(40.0, 25.0), Size::new(20.0, 10.0))),
        "expected the 20x10 child centered at (40, 25) inside the 100x60 box; \
         recorded rects: {:?}",
        rec.rects
    );
}

#[test]
fn plain_size_still_hugs_the_child_tight_once_attached() {
    // The split between `.size` (childless only) and `.size_centered` (the
    // one exception): `.size` on a container with a child attached is
    // ignored, and layout keeps hugging the child exactly.
    let view: frust_widgets::ContainerView<()> = container(leaf(20.0, 10.0)).size(100.0, 60.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    assert_eq!(
        size,
        Size::new(20.0, 10.0),
        "`.size` (not `.size_centered`) stays ignored once a child is attached"
    );
}

#[test]
fn size_centered_childless_behaves_like_plain_size() {
    let view: frust_widgets::ContainerView<()> = colored_box().size_centered(48.0, 24.0);
    let mut w = build(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(500.0, 500.0)));
    assert_eq!(size, Size::new(48.0, 24.0));
}

// ---------------------------------------------------------------------------
// `divider` — a lighter integration pass alongside its own in-module unit
// tests (`crates/frust-widgets/src/divider.rs`): fills the cross-axis, takes
// a fixed thickness on the main axis, paints exactly one `fill_rect` in its
// color.
// ---------------------------------------------------------------------------

#[test]
fn divider_default_orientation_fills_width_with_thickness_on_height() {
    let view = divider(Color::from_rgb8(0xEE, 0xEE, 0xEE));
    let mut w: frust_widgets::DividerWidget = build::<(), _>(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(400.0, 400.0)));
    assert_eq!(size, Size::new(400.0, 1.0));
}

#[test]
fn divider_vertical_fills_height_with_thickness_on_width() {
    let view = divider(Color::BLACK).vertical().thickness(2.0);
    let mut w: frust_widgets::DividerWidget = build::<(), _>(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(400.0, 400.0)));
    assert_eq!(size, Size::new(2.0, 400.0));
}

#[test]
fn divider_paints_its_line_via_a_single_fill_rect() {
    let view = divider(Color::from_rgb8(0x10, 0x10, 0x10));
    let mut w: frust_widgets::DividerWidget = build::<(), _>(&view);
    let mut lctx = LayoutCtx::new();
    let size = w.layout(&mut lctx, &BoxConstraints::loose(Size::new(100.0, 100.0)));
    let rec = paint_at(&mut w, size);
    assert_eq!(rec.rects, vec![(Point::ZERO, size)]);
}

// ---------------------------------------------------------------------------
// Semantics passthrough — purely decorative
// ---------------------------------------------------------------------------

/// Every contributed role *except* the always-present `Role::Window` root
/// node `RenderRoot::semantics` publishes for any tree (see
/// `tests/semantics_tree.rs`'s identical precedent) — the roles this test
/// file actually cares about are the widget tree's own.
fn inspect_roles<V: View<()>>(mut build: impl FnMut(&mut ()) -> V) -> Vec<Role> {
    let mut root: RenderRoot<(), V> = RenderRoot::new();
    let mut state = ();
    root.rebuild(&mut build, &mut state);
    let mut text_ctx = frust_text::TextContext::new();
    root.layout_with_text(Size::new(400.0, 400.0), &mut text_ctx as &mut dyn Any);
    root.semantics()
        .nodes
        .iter()
        .map(|(_, node)| node.role())
        .filter(|role| *role != Role::Window)
        .collect()
}

#[test]
fn a_decorated_container_contributes_no_node_of_its_own() {
    // `text(...)` contributes exactly one `Role::Label` node (see
    // `tests/semantics_tree.rs`'s precedent) — a container wrapping it with
    // fill/border/radius set must add nothing beyond that single node.
    let roles = inspect_roles(|_: &mut ()| {
        container(text("hello"))
            .fill(Color::WHITE)
            .radius(8.0)
            .border(Color::BLACK, 1.0)
    });
    assert_eq!(
        roles.len(),
        1,
        "the container is purely decorative and forwards the child's node only"
    );
}

#[test]
fn a_childless_container_contributes_no_node() {
    let roles = inspect_roles(|_: &mut ()| colored_box().fill(Color::WHITE).expand());
    assert!(
        roles.is_empty(),
        "a childless container has nothing to forward and adds nothing itself"
    );
}