herogpui-components 0.14.0

HeroUI-style component library for GPUI
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
//! Per-component `radius` builders under the narrow parity exception: the
//! default stays the owning `util` helper, and the builder replaces it.
//!
//! Proved on the painted scene, the way `radius_painted.rs` proves the
//! components that need no open surface: this file reaches the overlay
//! panels (opened through their controlled/default-open builders or real
//! pointer input), the field-family boxes (which paint through the shared
//! field chrome), and the composites whose inner parts must keep their own
//! shape while the root takes the override.

use crate::harness;

use std::rc::Rc;
use std::time::Duration;

use gpui::{
    point, prelude::*, px, AnyElement, App, Modifiers, MouseButton, Pixels, TestAppContext,
    VisualTestContext,
};
use harness::{open_host, painted, press, settle, still, wait_real, Painted};
use herogpui_components::extend::{
    container_radius, control_radius, field_radius, key_radius, small_radius, soft_radius,
};
use herogpui_components::{
    clear_toasts, Accordion, AccordionItem, AccordionVariant, AlertDialog, Autocomplete, Badge,
    BadgeAnchor, Button, Checkbox, CloseButton, ColorField, ComboBox, DateField, Dropdown, Input,
    InputGroup, InputOTP, InputState, Link, MenuItem, Modal, OtpState, PickerColor, PickerItem,
    Popover, RadioGroup, RadioOption, Select, Size, Table, Tag, TagGroup, TimeField, TimeState,
    Toast, ToastViewport, ToggleButton, Tooltip, Typography,
};

/// An override no helper or token produces, and small enough that no box
/// under test clamps it to half its side.
const OVERRIDE: f32 = 1.75;

type Helper = fn(&App) -> Pixels;
type Build = Rc<dyn Fn(Option<Pixels>) -> AnyElement>;

/// One component whose default paints `helper` and whose `.radius(..)`
/// replaces it. `drive` runs after the window opens (a hover, a Tab) and
/// `ring` says the observed box is the offset focus ring's gap band, whose
/// corners sit `ring_offset_width` outside the control's own.
struct Case {
    name: &'static str,
    helper: Helper,
    ring: bool,
    build: Build,
    drive: fn(&mut VisualTestContext),
    setup: Option<fn(Option<Pixels>, &mut App)>,
}

fn case(name: &'static str, helper: Helper, build: Build) -> Case {
    Case {
        name,
        helper,
        ring: false,
        build,
        drive: settle,
        setup: None,
    }
}

fn with<T>(el: T, radius: Option<Pixels>, set: impl FnOnce(T, Pixels) -> T) -> T {
    match radius {
        Some(r) => set(el, r),
        None => el,
    }
}

fn items() -> Vec<PickerItem> {
    vec![PickerItem::new("a", "Alpha"), PickerItem::new("b", "Beta")]
}

fn host(build: Build, radius: Option<Pixels>) -> impl Fn() -> AnyElement {
    move || {
        gpui::div()
            .p(px(24.))
            .w(px(480.))
            .h(px(600.))
            .child(build(radius))
            .into_any_element()
    }
}

/// The painted scene of one render, plus the value the default must paint.
fn paint(cx: &mut TestAppContext, case: &Case, radius: Option<Pixels>) -> (Painted, f32) {
    still();
    if let Some(setup) = case.setup {
        cx.update(|cx| {
            clear_toasts(cx);
            setup(radius, cx);
        });
    }
    let vcx = open_host(cx, host(case.build.clone(), radius));
    (case.drive)(vcx);
    let scene = painted(vcx);
    let helper = case.helper;
    let ring = case.ring;
    let value = vcx.update(|_, cx| {
        f32::from(helper(cx))
            + if ring {
                f32::from(herogpui_theme::ActiveTheme::layout(cx).ring_offset_width)
            } else {
                0.
            }
    });
    (scene, value)
}

fn hover_trigger(cx: &mut VisualTestContext) {
    settle(cx);
    cx.simulate_mouse_move(point(px(60.), px(40.)), None, Modifiers::none());
    settle(cx);
    cx.executor().advance_clock(Duration::from_millis(50));
    settle(cx);
}

fn tab_in(cx: &mut VisualTestContext) {
    settle(cx);
    press(cx, "tab");
    settle(cx);
}

fn cases(cx: &mut TestAppContext) -> Vec<Case> {
    let input = cx.new(|cx| InputState::new(cx));
    let grouped = cx.new(|cx| InputState::new(cx));
    let date = cx.new(|cx| InputState::new(cx));
    let combo = cx.new(|cx| InputState::new(cx));
    let auto = cx.new(|cx| InputState::new(cx));
    let time = cx.new(|cx| TimeState::new(cx));
    let otp = cx.new(|cx| OtpState::with_length(cx, 4));
    vec![
        Case {
            drive: hover_trigger,
            ..case(
                "Tooltip",
                small_radius,
                Rc::new(|r| {
                    with(
                        Tooltip::new("Saved")
                            .id("tt")
                            .delay(0)
                            .child(gpui::div().w(px(120.)).h(px(36.))),
                        r,
                        Tooltip::radius,
                    )
                    .into_any_element()
                }),
            )
        },
        case(
            "Modal",
            container_radius,
            Rc::new(|r| {
                with(
                    Modal::new().id("md").is_open(true).child("Body"),
                    r,
                    Modal::radius,
                )
                .into_any_element()
            }),
        ),
        Case {
            setup: Some(|r, cx| {
                with(
                    Toast::new("Saved").timeout(Duration::ZERO),
                    r,
                    Toast::radius,
                )
                .push(None, cx);
            }),
            ..case(
                "Toast",
                container_radius,
                Rc::new(|_| ToastViewport::new().into_any_element()),
            )
        },
        case(
            "Popover",
            container_radius,
            Rc::new(|r| {
                with(
                    Popover::new(gpui::div().w(px(40.)).h(px(36.)))
                        .id("pop")
                        .is_open(true)
                        .child("Body"),
                    r,
                    Popover::radius,
                )
                .into_any_element()
            }),
        ),
        case(
            "Dropdown",
            container_radius,
            Rc::new(|r| {
                with(
                    Dropdown::new(
                        "dd",
                        Button::new("dd-t").label("Open"),
                        vec![MenuItem::new("a", "Alpha")],
                        true,
                    ),
                    r,
                    Dropdown::radius,
                )
                .into_any_element()
            }),
        ),
        case(
            "Input",
            field_radius,
            Rc::new(move |r| with(Input::new(input.clone()), r, Input::radius).into_any_element()),
        ),
        case(
            "InputOTP",
            field_radius,
            Rc::new(move |r| {
                with(InputOTP::new(otp.clone()), r, InputOTP::radius).into_any_element()
            }),
        ),
        case(
            "TimeField",
            field_radius,
            Rc::new(move |r| {
                with(TimeField::new(time.clone()), r, TimeField::radius).into_any_element()
            }),
        ),
        case(
            "DateField",
            field_radius,
            Rc::new(move |r| {
                with(DateField::new(date.clone()), r, DateField::radius).into_any_element()
            }),
        ),
        case(
            "ColorField",
            field_radius,
            Rc::new(|r| {
                with(
                    ColorField::new("cf", PickerColor::from_hex("#FF0000")),
                    r,
                    ColorField::radius,
                )
                .into_any_element()
            }),
        ),
        case(
            "Select (panel)",
            container_radius,
            Rc::new(|r| {
                with(
                    Select::new("sel", items()).default_open(true),
                    r,
                    Select::radius,
                )
                .into_any_element()
            }),
        ),
        case(
            "ComboBox (panel)",
            container_radius,
            Rc::new(move |r| {
                with(
                    ComboBox::new(combo.clone(), items()).default_open(true),
                    r,
                    ComboBox::radius,
                )
                .into_any_element()
            }),
        ),
        case(
            "Autocomplete (panel)",
            container_radius,
            Rc::new(move |r| {
                with(
                    Autocomplete::new(auto.clone(), items()).default_open(true),
                    r,
                    Autocomplete::radius,
                )
                .into_any_element()
            }),
        ),
        case(
            "InputGroup",
            field_radius,
            Rc::new(move |r| {
                with(
                    InputGroup::new().input(Input::new(grouped.clone())),
                    r,
                    InputGroup::radius,
                )
                .into_any_element()
            }),
        ),
        case(
            "AlertDialog",
            container_radius,
            Rc::new(|r| {
                with(
                    AlertDialog::new("Delete?").id("ad").is_open(true),
                    r,
                    AlertDialog::radius,
                )
                .into_any_element()
            }),
        ),
        Case {
            ring: true,
            drive: tab_in,
            ..case(
                "Link (focus ring)",
                small_radius,
                Rc::new(|r| {
                    with(
                        Link::new("lnk").label("Docs").href("#/docs"),
                        r,
                        Link::radius,
                    )
                    .into_any_element()
                }),
            )
        },
    ]
}

#[gpui::test]
fn radius_builders_override_their_helper_defaults(cx: &mut TestAppContext) {
    let mut checked = 0;
    for case in cases(cx) {
        let name = case.name;
        let ring = if case.ring { 2. } else { 0. };
        let (scene, value) = paint(cx, &case, None);
        let helper_boxes = scene.rounded(value).len();
        assert!(
            scene.rounded_exact(OVERRIDE + ring).is_empty() && helper_boxes > 0,
            "{name}: the default must paint the helper's {value}px, not the override\n{}",
            scene.describe()
        );
        checked += 1;

        let (scene, _) = paint(cx, &case, Some(px(OVERRIDE)));
        let remaining = scene.rounded(value).len();
        assert!(
            !scene.rounded_exact(OVERRIDE + ring).is_empty() && remaining < helper_boxes,
            "{name}: `.radius({OVERRIDE})` must replace the painted {value}px \
             ({helper_boxes} helper boxes before, {remaining} after)"
        );
        checked += 1;
    }
    assert_eq!(checked, 32, "all 16 cases ran both of their checks");
}

/// Select, ComboBox and Autocomplete round their detached *panel*; the
/// trigger box stays the shared field chrome's, so a closed picker with an
/// override still paints `field_radius` and nothing at the override.
#[gpui::test]
fn detached_panel_overrides_do_not_reach_the_trigger_box(cx: &mut TestAppContext) {
    let combo = cx.new(|cx| InputState::new(cx));
    let triggers: Vec<(&str, Build)> = vec![
        (
            "Select",
            Rc::new(|_| {
                Select::new("sel-c", items())
                    .radius(px(OVERRIDE))
                    .into_any_element()
            }),
        ),
        (
            "ComboBox",
            Rc::new(move |_| {
                ComboBox::new(combo.clone(), items())
                    .radius(px(OVERRIDE))
                    .into_any_element()
            }),
        ),
    ];
    for (name, build) in triggers {
        still();
        let vcx = open_host(cx, host(build, None));
        let scene = painted(vcx);
        let field = vcx.update(|_, cx| f32::from(field_radius(cx)));
        assert!(
            scene.rounded_exact(OVERRIDE).is_empty() && !scene.rounded(field).is_empty(),
            "{name}: the closed trigger must keep the field chrome's {field}px\n{}",
            scene.describe()
        );
    }
}

/// `Autocomplete::radius` documents the same split — "the trigger is a field
/// box of its own, painted by the shared field chrome — `--field-radius`,
/// not this value". Until 0.13 the render resolved the trigger radius from
/// the override; the 0.13 test conversion found it (the source-text check it
/// replaced looked only for the literal `Some(radius),`).
#[gpui::test]
fn autocomplete_panel_override_does_not_reach_the_trigger_box(cx: &mut TestAppContext) {
    let auto = cx.new(|cx| InputState::new(cx));
    still();
    let vcx = open_host(cx, move || {
        Autocomplete::new(auto.clone(), items())
            .radius(px(OVERRIDE))
            .into_any_element()
    });
    let scene = painted(vcx);
    let field = vcx.update(|_, cx| f32::from(field_radius(cx)));
    assert!(
        scene.rounded_exact(OVERRIDE).is_empty() && !scene.rounded(field).is_empty(),
        "Autocomplete: the closed trigger must keep the field chrome's {field}px\n{}",
        scene.describe()
    );
}

/// `Table::radius` replaces the wrapper shell's helper value, which paints
/// only where the shell's corner shows: the `Primary` tray paints its own
/// pinned radius over both, and in `Secondary` the last row's fill rounds
/// its bottom corners to the shell's. Hover that row and read them.
#[gpui::test]
fn table_override_reaches_the_shell_corner_the_rows_round_to(cx: &mut TestAppContext) {
    use herogpui_components::TableVariant;
    for radius in [None, Some(px(OVERRIDE))] {
        still();
        let vcx = open_host(cx, move || {
            gpui::div()
                .p(px(24.))
                .w(px(480.))
                .child(with(
                    Table::new(vec!["Name".into()])
                        .id("tbl")
                        .variant(TableVariant::Secondary)
                        .row(vec![gpui::div().child("Ada").into_any_element()]),
                    radius,
                    Table::radius,
                ))
                .into_any_element()
        });
        settle(vcx);
        let before = painted(vcx);
        vcx.simulate_mouse_move(point(px(240.), px(80.)), None, Modifiers::none());
        let scene = painted(vcx);
        let helper = vcx.update(|_, cx| f32::from(container_radius(cx)));
        let fills = scene
            .quads
            .iter()
            .filter(|q| q.background.as_solid().is_some_and(|c| c.a > 0.))
            .filter(|q| {
                !before
                    .quads
                    .iter()
                    .any(|b| b.bounds == q.bounds && b.background == q.background)
            })
            .collect::<Vec<_>>();
        assert!(
            !fills.is_empty(),
            "hovering the row must paint its fill\n{}",
            scene.describe()
        );
        let want = match radius {
            Some(r) => f32::from(r),
            None => helper,
        };
        let bottom_round = fills.iter().any(|q| {
            let b = scene.bounds(q);
            let want = want.min(f32::from(b.size.height) / 2.);
            let [tl, tr, br, bl] = scene.corners(q);
            tl < 0.05 && tr < 0.05 && (br - want).abs() < 0.05 && (bl - want).abs() < 0.05
        });
        assert!(
            bottom_round,
            "radius {radius:?}: the last row's fill must round its bottom corners \
             to the shell's {want}px\n{}",
            scene.describe()
        );
    }
}

/// `ColorField`'s editable path renders the inner `Input`'s own box, so the
/// override rides along with the field box.
#[gpui::test]
fn color_field_forwards_the_override_to_its_editable_input(cx: &mut TestAppContext) {
    for radius in [None, Some(px(OVERRIDE))] {
        let state = cx.new(|cx| InputState::new(cx));
        still();
        let vcx = open_host(cx, move || {
            let field =
                ColorField::new("cf-edit", PickerColor::from_hex("#00FF00")).state(state.clone());
            with(field, radius, ColorField::radius).into_any_element()
        });
        let scene = painted(vcx);
        let field = vcx.update(|_, cx| f32::from(field_radius(cx)));
        match radius {
            None => assert!(
                !scene.rounded(field).is_empty() && scene.rounded_exact(OVERRIDE).is_empty(),
                "the editable ColorField must paint the field chrome's {field}px"
            ),
            Some(_) => assert!(
                !scene.rounded_exact(OVERRIDE).is_empty(),
                "the editable ColorField must forward the override to its input box"
            ),
        }
    }
}

/// An `InputGroup` child is `rounded-none`: the group owns the one outline,
/// so a fill the caller gives the inner field shows square corners inside
/// the group's rounded shell rather than a second set of corners.
#[gpui::test]
fn grouped_input_uses_the_group_shell_radius_only(cx: &mut TestAppContext) {
    let state = cx.new(|cx| InputState::new(cx));
    let fill = gpui::rgb(0x336699).into();
    still();
    let vcx = open_host(cx, move || {
        InputGroup::new()
            .radius(px(5.))
            .input(Input::new(state.clone()).sx(move |s| s.bg(fill)))
            .into_any_element()
    });
    let scene = painted(vcx);
    let inner = scene.filled(fill);
    assert!(!inner.is_empty(), "the inner field's fill must paint");
    for quad in inner {
        assert!(
            scene.corners(quad).iter().all(|c| c.abs() < 0.05),
            "the grouped input must not round its own box, got {:?}",
            scene.corners(quad)
        );
    }
    assert!(
        !scene.rounded(5.).is_empty(),
        "the group shell must paint the group's radius"
    );
}

/// The dialog's panel is painted by its resting chain and, with motion on,
/// re-rounded every frame by the entry zoom; the settled zoom must land on
/// the resolved override, not on the helper.
#[gpui::test]
fn alert_dialog_entry_zoom_lands_on_the_resolved_radius(cx: &mut TestAppContext) {
    for radius in [None, Some(px(OVERRIDE))] {
        let vcx = open_host(cx, move || {
            gpui::div()
                .p(px(24.))
                .child(with(
                    AlertDialog::new("Delete?").id("ad-zoom").is_open(true),
                    radius,
                    AlertDialog::radius,
                ))
                .into_any_element()
        });
        wait_real(vcx, 500);
        let scene = painted(vcx);
        let helper = vcx.update(|_, cx| f32::from(container_radius(cx)));
        match radius {
            None => assert!(
                !scene.rounded(helper).is_empty() && scene.rounded_exact(OVERRIDE).is_empty(),
                "the settled dialog must paint the container helper's {helper}px"
            ),
            Some(_) => assert!(
                !scene.rounded_exact(OVERRIDE).is_empty(),
                "the settled entry zoom must paint the override, not the helper"
            ),
        }
    }
}

/// Only the `Surface` variant paints a card, so the override is inert on the
/// flush default variant. (Its separators are 1px rules: any radius clamps
/// to half a pixel there, so which radius they read is not a visible fact.)
#[gpui::test]
fn accordion_override_is_inert_on_the_flush_default_variant(cx: &mut TestAppContext) {
    for variant in [AccordionVariant::Default, AccordionVariant::Surface] {
        still();
        let vcx = open_host(cx, move || {
            Accordion::new(vec![
                AccordionItem::new("a", "One"),
                AccordionItem::new("b", "Two"),
            ])
            .variant(variant)
            .radius(px(OVERRIDE))
            .into_any_element()
        });
        let scene = painted(vcx);
        let painted_override = !scene.rounded_exact(OVERRIDE).is_empty();
        assert_eq!(
            painted_override,
            variant == AccordionVariant::Surface,
            "{variant:?}: only the Surface card takes the override\n{}",
            scene.describe()
        );
    }
}

/// Holds the left button down at `at` and settles, without releasing.
fn hold(cx: &mut VisualTestContext, at: gpui::Point<Pixels>) {
    cx.simulate_mouse_move(at, None, Modifiers::none());
    settle(cx);
    cx.simulate_mouse_down(at, MouseButton::Left, Modifiers::none());
    settle(cx);
}

/// The close button's box is its whole shape, so its pressed skin scales the
/// resolved radius — the override's, when one is set.
#[gpui::test]
fn close_button_press_scale_follows_the_override(cx: &mut TestAppContext) {
    for radius in [None, Some(px(10.))] {
        still();
        let vcx = open_host(cx, move || {
            gpui::div()
                .p(px(24.))
                .child(with(CloseButton::new("cb"), radius, CloseButton::radius))
                .into_any_element()
        });
        settle(vcx);
        let resting = vcx
            .debug_bounds("Name(\"cb\")")
            .expect("the close button must be laid out");
        hold(vcx, resting.center());
        let scene = painted(vcx);
        let value = match radius {
            Some(r) => f32::from(r),
            None => vcx.update(|_, cx| f32::from(small_radius(cx))),
        };
        let pressed = value * 0.93;
        assert!(
            !scene.rounded(pressed).is_empty(),
            "radius {radius:?}: the pressed skin must paint {value} x 0.93 = {pressed}"
        );
        vcx.simulate_mouse_up(resting.center(), MouseButton::Left, Modifiers::none());
    }
}

/// `is_round` is documented shape semantics: the override replaces the
/// helper's value on the square control only, and the round one stays a
/// circle.
#[gpui::test]
fn checkbox_override_never_overrides_the_round_circle(cx: &mut TestAppContext) {
    for round in [false, true] {
        still();
        let vcx = open_host(cx, move || {
            gpui::div()
                .p(px(24.))
                .child(Checkbox::new("cb").is_round(round).radius(px(OVERRIDE)))
                .into_any_element()
        });
        let scene = painted(vcx);
        let circles = scene
            .quads
            .iter()
            .filter(|q| {
                let b = scene.bounds(q);
                let side = f32::from(b.size.width);
                (side - f32::from(b.size.height)).abs() < 0.5
                    && side >= 12.
                    && scene.is_uniform(q, side / 2.)
            })
            .count();
        if round {
            assert!(
                scene.rounded_exact(OVERRIDE).is_empty() && circles > 0,
                "a round checkbox must stay a circle under an override"
            );
        } else {
            assert!(
                !scene.rounded_exact(OVERRIDE).is_empty(),
                "a square checkbox must take the override"
            );
        }
    }
}

/// A pressed radio control scales the resolved radius, so it does not snap
/// back to the helper; the selected dot inside is an inner part and keeps
/// `key_radius`.
#[gpui::test]
fn radio_control_press_follows_the_override_and_the_dot_does_not(cx: &mut TestAppContext) {
    const R: f32 = 3.;
    // Motion on: under reduced motion the press refinement is skipped.
    let vcx = open_host(cx, || {
        gpui::div()
            .p(px(24.))
            .child(
                RadioGroup::new("rg", vec![RadioOption::new("One")])
                    .default_value("One")
                    .radius(px(R)),
            )
            .into_any_element()
    });
    let resting = painted(vcx);
    let control = resting
        .rounded_exact(R)
        .first()
        .map(|q| resting.bounds(q))
        .expect("the selected control must paint the override");
    hold(vcx, control.center());
    wait_real(vcx, 400);
    let scene = painted(vcx);
    assert!(
        !scene.rounded_exact(R * 0.95).is_empty(),
        "the pressed control must scale the override (x0.95), not the helper\n{}",
        scene.describe()
    );
    let key = vcx.update(|_, cx| f32::from(key_radius(cx)));
    let dot = scene
        .quads
        .iter()
        .filter(|q| contains(control, scene.bounds(q)))
        .filter(|q| f32::from(scene.bounds(q).size.width) <= 8.5)
        .collect::<Vec<_>>();
    assert!(
        !dot.is_empty(),
        "the selected dot must paint inside the control"
    );
    for q in dot {
        assert!(
            scene.is_uniform(q, key) && !scene.is_uniform(q, R),
            "the dot keeps key_radius ({key}px), not the control's override"
        );
    }
}

fn contains(outer: gpui::Bounds<Pixels>, inner: gpui::Bounds<Pixels>) -> bool {
    harness::contains(outer, inner)
}

/// The `Code` chip is the only kind that paints a box, so the override is
/// inert on every other kind — a heading's `radius` would otherwise lie.
#[gpui::test]
fn typography_override_sits_inside_the_code_branch(cx: &mut TestAppContext) {
    for code in [false, true] {
        still();
        let vcx = open_host(cx, move || {
            let t = if code {
                Typography::code("let x")
            } else {
                Typography::heading(2, "Title")
            };
            t.radius(px(OVERRIDE)).into_any_element()
        });
        let scene = painted(vcx);
        assert_eq!(
            !scene.rounded_exact(OVERRIDE).is_empty(),
            code,
            "code={code}: only the `Code` chip rounds"
        );
    }
}

fn badge(size: Size, radius: Option<Pixels>) -> AnyElement {
    gpui::div()
        .flex()
        .items_start()
        .p(px(40.))
        .child(
            BadgeAnchor::new()
                .child(gpui::div().w(px(64.)).h(px(64.)))
                .child(with(
                    Badge::new().size(size).child("3"),
                    radius,
                    Badge::radius,
                )),
        )
        .into_any_element()
}

/// Both size-stepped boxes keep their step as the fallback — each size paints
/// its own helper — and the override replaces the step's value; the tag's
/// remove button is an inner part that stays a circle under an override.
#[gpui::test]
fn badge_and_tag_steps_stay_the_fallback_and_inner_parts_keep_their_own(cx: &mut TestAppContext) {
    let steps: [(Size, Helper, Helper); 3] = [
        (Size::Sm, small_radius, small_radius),
        (Size::Md, control_radius, small_radius),
        (Size::Lg, soft_radius, soft_radius),
    ];
    for (size, badge_helper, tag_helper) in steps {
        for radius in [None, Some(px(OVERRIDE))] {
            still();
            let vcx = open_host(cx, move || badge(size, radius));
            let scene = painted(vcx);
            let step = vcx.update(|_, cx| f32::from(badge_helper(cx)));
            match radius {
                None => assert!(
                    !scene.rounded(step).is_empty() && scene.rounded_exact(OVERRIDE).is_empty(),
                    "Badge {size:?}: the step's {step}px must be the fallback"
                ),
                Some(_) => assert!(
                    !scene.rounded_exact(OVERRIDE).is_empty(),
                    "Badge {size:?}: the override must replace the step"
                ),
            }

            still();
            let vcx = open_host(cx, move || {
                gpui::div()
                    .p(px(24.))
                    .child(with(
                        TagGroup::new("tg", vec![Tag::new("a", "Alpha")]).size(size),
                        radius,
                        TagGroup::radius,
                    ))
                    .into_any_element()
            });
            let scene = painted(vcx);
            let step = vcx.update(|_, cx| f32::from(tag_helper(cx)));
            match radius {
                None => assert!(
                    !scene.rounded(step).is_empty() && scene.rounded_exact(OVERRIDE).is_empty(),
                    "Tag {size:?}: the step's {step}px must be the fallback"
                ),
                Some(_) => assert!(
                    !scene.rounded_exact(OVERRIDE).is_empty(),
                    "Tag {size:?}: the override must replace the step"
                ),
            }
        }
    }

    // The remove button's hover disc is `rounded-full` whatever the tag's
    // radius: hover it and the 12px disc paints as a circle.
    still();
    let vcx = open_host(cx, || {
        gpui::div()
            .p(px(24.))
            .child(
                TagGroup::new("tg-rm", vec![Tag::new("a", "Alpha")])
                    .radius(px(OVERRIDE))
                    .on_remove(|_, _, _| {}),
            )
            .into_any_element()
    });
    let resting = painted(vcx);
    let chip = resting
        .rounded_exact(OVERRIDE)
        .first()
        .map(|q| resting.bounds(q))
        .expect("the tag chip must paint the override");
    // The remove button trails the label inside the chip's end padding.
    let target = point(
        chip.origin.x + chip.size.width - px(14.),
        chip.origin.y + chip.size.height / 2.,
    );
    vcx.simulate_mouse_move(target, None, Modifiers::none());
    let scene = painted(vcx);
    let discs = scene
        .quads
        .iter()
        .filter(|q| {
            let b = scene.bounds(q);
            (f32::from(b.size.width) - 12.).abs() < 0.5
                && (f32::from(b.size.height) - 12.).abs() < 0.5
        })
        .collect::<Vec<_>>();
    assert!(
        !discs.is_empty(),
        "hovering the remove button must paint its disc"
    );
    for q in discs {
        assert!(
            scene.is_uniform(q, 6.),
            "the remove disc stays a circle under the tag's override"
        );
    }
}

/// Both the skin and its animated hover fill consume the pixel corners an
/// `sx` refinement names, and the pressed skin scales those corners: an `sx`
/// top-left corner survives resting, hover and press on all three buttons.
#[gpui::test]
fn button_shapes_reconcile_sx_on_skin_fill_and_press(cx: &mut TestAppContext) {
    const TL: f32 = 7.;
    type Shape = (&'static str, fn() -> AnyElement, f32);
    let shapes: [Shape; 3] = [
        (
            "Button",
            || {
                Button::new("b")
                    .label("Save")
                    .sx(|s| s.rounded_tl(px(TL)))
                    .into_any_element()
            },
            0.97,
        ),
        (
            "ToggleButton",
            || {
                ToggleButton::new("t")
                    .label("Bold")
                    .sx(|s| s.rounded_tl(px(TL)))
                    .into_any_element()
            },
            0.97,
        ),
        (
            "CloseButton",
            || {
                CloseButton::new("c")
                    .sx(|s| s.rounded_tl(px(TL)))
                    .into_any_element()
            },
            0.93,
        ),
    ];
    for (name, build, scale) in shapes {
        // Motion on: the hover fill only exists while a fade is mounted.
        let vcx = open_host(cx, move || {
            gpui::div().p(px(24.)).child(build()).into_any_element()
        });
        let resting = painted(vcx);
        let skin = resting
            .quads
            .iter()
            .find(|q| (resting.corners(q)[0] - TL).abs() < 0.05)
            .map_or_else(
                || panic!("{name}: the resting skin must take the sx corner"),
                |q| resting.bounds(q),
            );

        vcx.simulate_mouse_move(skin.center(), None, Modifiers::none());
        wait_real(vcx, 300);
        let hovered = painted(vcx);
        let layers = hovered
            .quads
            .iter()
            .filter(|q| {
                let b = hovered.bounds(q);
                (f32::from(b.size.width) - f32::from(skin.size.width)).abs() < 0.5
                    && (f32::from(b.size.height) - f32::from(skin.size.height)).abs() < 0.5
                    && q.background.as_solid().is_some_and(|c| c.a > 0.)
            })
            .collect::<Vec<_>>();
        assert!(
            layers.len() >= 2,
            "{name}: hovering must paint the skin and its fade fill, got {}",
            layers.len()
        );
        for q in layers {
            assert!(
                (hovered.corners(q)[0] - TL).abs() < 0.05,
                "{name}: every hover layer must keep the sx corner, got {:?}",
                hovered.corners(q)
            );
        }

        vcx.simulate_mouse_down(skin.center(), MouseButton::Left, Modifiers::none());
        wait_real(vcx, 400);
        let pressed = painted(vcx);
        let want = TL * scale;
        assert!(
            pressed
                .quads
                .iter()
                .any(|q| (pressed.corners(q)[0] - want).abs() < 0.05),
            "{name}: the pressed skin must scale the sx corner to {want}"
        );
        vcx.simulate_mouse_up(skin.center(), MouseButton::Left, Modifiers::none());
        settle(vcx);
    }
}