frust-material 0.5.1

Material 3 (Expressive) design-system catalog for Frust: HCT seed-color tokens, shape morphing, icons, widgets and bundled Roboto fonts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
//! The M3 `Radio` button: a generic value/group-value single-selection
//! control with an optional label slot, an error flavor, and an inner-dot
//! scale-in animation.
//!
//! Source: `paadevelopments/material_3_expressive` v1.0.8 (MIT, © 2026 Paa
//! Developments — see `plugins/material/NOTICE`'s root-crate MIT section),
//! `lib/components/radio_button/{m3e_radio_button.dart,
//! styles/m3e_radio_theme.dart}`'s `M3ERadio<T>`/`M3ERadioTheme` (retrieved
//! 2026-08-19). No license changes; the generic-`T` selection model, the
//! optional label slot, and the dot scale-in are all preserved from the Dart
//! source — see the sections below for what changed in translation.
//!
//! # Naming: plain `radio`/`Radio`, deliberately
//!
//! Baseline `frust-widgets` already ships a non-generic `radio`/[`Radio`]
//! ([`frust::radio`]) taking a plain `bool` `selected`. This module reuses
//! the same plain names in `frust_material` on purpose — the same
//! cross-crate naming call `frust_material::switch`/[`super::switch::Switch`]
//! already made against a baseline widget with no direct counterpart, now
//! extended to a name baseline *does* have one for. Two catalogs (or a
//! catalog and the baseline set) legitimately reuse a vocabulary word; an app
//! picks exactly one by import path (`frust_material::radio` vs.
//! `frust::radio`), never both in the same call site. This crate's own
//! `radio` is a strict superset of what the baseline widget can express
//! (generic `T`, group-value selection, optional label, error flavor,
//! animation) — recorded here as the deliberate decision, not an oversight.
//!
//! # Generic selection model
//!
//! [`radio`] takes an owned `value: T` and `group_value: T` (`T: PartialEq +
//! Clone + 'static`, matching the Dart source's `T`/`T?` pair) and computes
//! `selected = value == group_value` once, at `View::build`/`rebuild` — the
//! same shape `M3ERadio<T>`'s `_selected` getter uses. [`RadioView::on_changed`]
//! attaches a single `Fn(&mut State, T) + 'static` callback shared by every
//! member of a group (mirroring Dart's `ValueChanged<T>`): a release inside
//! bounds fires `on_changed(state, value.clone())` — reporting *this* radio's
//! own value, not a derived index — and never self-mutates. `T` is erased out
//! of the retained [`RadioWidget`] entirely: `on_changed` is bound with
//! `value` already captured into a plain [`frust::authoring::ErasedCallback`]
//! at build/rebuild time, so the widget itself carries no generic parameter
//! (the same type-erase-to-avoid-a-generic idiom `docs/CODE_STANDARDS.md`'s
//! Language Idioms describes for a downstream-crate dependency, applied here
//! to a generic instead).
//!
//! # Enabled/disabled
//!
//! Mirroring the Dart source's `_enabled => onChanged != null` (and
//! `M3ETappable`'s `_isInteractive = enabled && onTap != null`, which for
//! `M3ERadio` reduces to the same condition since it never sets `enabled`
//! itself): a radio built with no [`RadioView::on_changed`] call is
//! **disabled** — it paints its ring/dot dimmed to
//! [`crate::interaction::DISABLED_CONTENT_OPACITY`] (also the Dart source's
//! own `disabledOpacity: 0.38`, so the values already agree) over `on_surface`
//! and ignores every pointer event outright, never entering a pressed/captured
//! state. A disabled radio still tracks `selected` (and animates its dot)
//! when the *group's* value changes out from under it via a sibling's own
//! `on_changed` — the Dart source's `AnimatedScale` is keyed on `_selected`
//! alone, independent of `_enabled`, and this port preserves that. The one
//! documented simplification: the optional label's text color does not
//! itself dim when disabled (the Dart source multiplies `onSurface`'s alpha
//! for the label style) — this crate's `frust::text` has no themed-role
//! variant carrying a fixed alpha multiplier, only a plain
//! [`frust::authoring::ThemeTextColor`] role or an explicit
//! [`frust::authoring::AnyView`]-time [`Color`] override neither `build` nor
//! `rebuild` can theme-resolve (see `docs/CODE_STANDARDS.md`'s Theming
//! conventions: only `LayoutCtx`/`PaintCtx` thread a theme). The primary
//! disabled signal — the ring/dot dimming — carries the state correctly; a
//! themed-role-at-reduced-alpha text API is future work, not something this
//! module can reach for.
//!
//! # Dot scale-in: duration + curve, not a spring
//!
//! The reference's `AnimatedScale(scale: _selected ? 1 : 0, duration:
//! M3EMotion.short4, curve: M3EMotion.emphasizedDecelerate)` is a
//! **duration-driven**, not spring-driven, implicit animation — unlike this
//! crate's other toggle-style controls ([`super::switch`], `NavigationBar`'s
//! selection pill), which all animate via
//! [`frust::AnimationController::fling`] against a [`frust::SpringDesc`].
//! This module instead drives [`RadioWidget::anim`] with
//! [`frust::AnimationController::forward`]/[`frust::AnimationController::reverse`]
//! against a fixed duration+curve built once from
//! [`crate::tokens::MaterialMotion::SHORT_4`]/
//! [`crate::tokens::MaterialMotion::EMPHASIZED_DECELERATE`] — both compile-time
//! crate constants, not a per-instance `Theme` lookup, so (unlike `switch`'s
//! themed spring, which must wait for `paint` to have a theme in scope) the
//! motion can start directly in `rebuild` the moment `selected` actually
//! changes. Flutter's `AnimatedScale` applies the *same* curve shape whether
//! animating toward `1` or back toward `0` (not a mirrored/reversed curve);
//! [`frust::AnimationController`]'s own duration-driven interpolation
//! (`value = start + (target - start) * curve.transform(elapsed / duration)`)
//! has the identical property, so `forward`/`reverse` reproduce the Dart
//! source's both-directions animated deselect exactly — a verified fact, not
//! an assumption (`crates/frust-core/src/anim.rs`'s `Drive::Duration` arm).
//!
//! An initially-selected radio never plays a pop-in on first mount (the same
//! contract Flutter's `ImplicitlyAnimatedWidget` gives `AnimatedScale`, and
//! this crate's own [`super::navbar`] indicator pill and [`super::switch`]
//! thumb already establish for their own first-mount snap): [`View::build`]
//! settles [`RadioWidget::anim`] to `1.0` with two `advance` calls — the
//! first only seeds the controller's clock (a documented no-op per
//! [`frust::AnimationController::advance`]), the second covers the whole
//! `SHORT_4` span in one step, so no intermediate frame is ever painted.
//!
//! # Haptics: fires the hook, with the reference's own choice — none
//!
//! [`crate::interaction::MaterialHaptics`] is fired on every successful
//! select — but the Dart source's own `M3ETappable(haptic: ...)` parameter is
//! never passed by `M3ERadio` (verified: neither the widget nor any example
//! usage in the reference package overrides it), so it takes
//! `M3ETappable`'s own default, `M3EHapticFeedback.none`. This module
//! preserves that choice exactly — [`crate::interaction::HapticSignal::None`]
//! is documented as an explicit no-op, identical to firing nothing at all —
//! while still wiring the call site through the shared hook (this crate's
//! first real [`MaterialHaptics::fire`] consumer), so a future upstream
//! change to `M3ERadio`'s own haptic wiring has exactly one call site to
//! update here.

use std::rc::Rc;

use frust::authoring::{Action, Role};
use frust::authoring::{
    AnyView, BoxConstraints, BuildCtx, ChangeFlags, ChildPod, EventCtx, EventResult, InputEvent,
    LayoutCtx, PaintCtx, PaintScene, PointerPhase, SemanticsCtx, ThemeTextColor, ThemeTextType,
    TypedArgCallback, View, Widget, any,
};
use frust::{AnimationController, FrameTime, Theme};
use kurbo::{Circle, Point, Rect, Shape, Size};
use peniko::{Brush, Color};

use super::press::presses;
use super::state_layer::StateLayer;
use crate::interaction::{DISABLED_CONTENT_OPACITY, HapticSignal, MaterialHaptics};
use crate::tokens::MaterialMotion;

/// The control's own tap-target/state-layer diameter, in logical px
/// (`M3ERadioTheme.hitSize`) — also the width the control column reserves in
/// layout, wider than the visible ring so the state layer can extend past it.
const HIT_SIZE: f64 = 40.0;
/// The visible outer ring's diameter, in logical px (`M3ERadioTheme.ringSize`).
const RING_SIZE: f64 = 20.0;
/// The inner dot's fully-scaled-in diameter, in logical px
/// (`M3ERadioTheme.dotSize`).
const DOT_SIZE: f64 = 10.0;
/// The ring's stroke width, in logical px (`M3ERadioTheme.borderWidth`).
const BORDER_WIDTH: f64 = 2.0;
/// Gap between the control and an optional label, in logical px
/// (`M3ERadioTheme.labelGap`).
const LABEL_GAP: f64 = 8.0;
/// Flattening tolerance for the ring's stroked-circle path — a
/// visually-lossless value for on-screen radii, matching
/// [`super::card`]'s `STROKE_TOLERANCE` precedent (the same primitive-choice
/// rationale: no stroked-circle primitive exists on
/// [`frust::authoring::PaintScene`], only `stroke_path` over a `kurbo`
/// [`kurbo::Shape`]).
const STROKE_TOLERANCE: f64 = 0.1;

/// Unthemed fallback selected-ring/dot color (a theme resolves this from
/// `colors.primary`).
const PRIMARY: Color = Color::from_rgb8(0x67, 0x50, 0xA4);
/// Unthemed fallback unselected-ring color (a theme resolves this from
/// `colors.on_surface_variant`).
const ON_SURFACE_VARIANT: Color = Color::from_rgb8(0x49, 0x45, 0x4F);
/// Unthemed fallback state-layer base color, both states share the ring's
/// selected color only when selected (a theme resolves this from
/// `colors.on_surface`).
const ON_SURFACE: Color = Color::from_rgb8(0x1D, 0x1B, 0x20);
/// Unthemed fallback error-flavor ring/dot color (a theme resolves this from
/// `colors.error`).
const ERROR: Color = Color::from_rgb8(0xB3, 0x26, 0x1E);

/// Return `color` with its alpha channel replaced by `alpha` (mirrors
/// [`super::state_layer`]'s helper of the same shape).
fn with_alpha(color: Color, alpha: f32) -> Color {
    let c = color.components;
    Color::new([c[0], c[1], c[2], alpha])
}

/// The resolved `(ring_color, state_layer_color)` pair.
///
/// Ring/dot (`M3ERadioTheme.color`): disabled wins over everything —
/// `on_surface` at [`DISABLED_CONTENT_OPACITY`]; else error wins over
/// selection — `colors.error`; else `primary` if selected, `on_surface_variant`
/// otherwise. State layer (`M3ERadioTheme.stateLayerColor`): `primary` if
/// selected, `on_surface` otherwise — **not** conditioned on `error`, exactly
/// mirroring the Dart source (the state-layer overlay tints the same either
/// way; only the ring/dot recolor for the error flavor).
fn resolve_colors(
    theme: Option<&Theme>,
    selected: bool,
    error: bool,
    enabled: bool,
) -> (Color, Color) {
    match theme {
        Some(theme) => {
            let scheme = theme.scheme();
            let ring = if !enabled {
                with_alpha(scheme.on_surface, DISABLED_CONTENT_OPACITY)
            } else if error {
                scheme.error
            } else if selected {
                scheme.primary
            } else {
                scheme.on_surface_variant
            };
            let state_layer = if selected {
                scheme.primary
            } else {
                scheme.on_surface
            };
            (ring, state_layer)
        }
        None => {
            let ring = if !enabled {
                with_alpha(ON_SURFACE, DISABLED_CONTENT_OPACITY)
            } else if error {
                ERROR
            } else if selected {
                PRIMARY
            } else {
                ON_SURFACE_VARIANT
            };
            let state_layer = if selected { PRIMARY } else { ON_SURFACE };
            (ring, state_layer)
        }
    }
}

/// Whether a widget-local `pos` lies within a `size`-sized box anchored at
/// the origin (mirrors [`super::switch`]'s helper of the same shape).
fn inside(pos: Point, size: Size) -> bool {
    pos.x >= 0.0 && pos.y >= 0.0 && pos.x < size.width && pos.y < size.height
}

/// Build the optional label's type-erased child view, themed `on_surface`
/// (`frust::text`'s default body style), its family following the live
/// theme's `bodyLarge` role — see the module docs' Enabled/disabled section
/// for why this doesn't dim when the radio is disabled.
fn label_view<State: 'static>(label: String) -> AnyView<State> {
    any::<State, _>(
        frust::text(label)
            .themed_role(ThemeTextColor::OnSurface)
            .themed_family(ThemeTextType::BodyLarge),
    )
}

/// Erase a view-held, generic `Fn(&mut State, T)` group handler into a plain
/// [`frust::authoring::ErasedCallback`] with `value` already bound — see the
/// module docs' Generic selection model section for why this is what lets
/// [`RadioWidget`] carry no generic parameter of its own.
fn bind_on_changed<State: 'static, T: Clone + 'static>(
    on_changed: &TypedArgCallback<State, T>,
    value: T,
) -> frust::authoring::ErasedCallback {
    let on_changed = on_changed.clone();
    Box::new(move |ctx: &mut EventCtx| {
        let state = ctx.state_mut::<State>();
        on_changed(state, value.clone());
    })
}

/// A declarative M3 radio button. See the [module docs](self).
pub struct RadioView<State: 'static, T: PartialEq + Clone + 'static> {
    value: T,
    group_value: T,
    on_changed: Option<TypedArgCallback<State, T>>,
    label: Option<String>,
    error: bool,
}

/// Create a radio for `value`, reflecting `selected = value == group_value`.
/// Attach a shared group handler with [`RadioView::on_changed`]; a radio with
/// none is **disabled** — see the [module docs](self)' Enabled/disabled
/// section.
pub fn radio<State: 'static, T: PartialEq + Clone + 'static>(
    value: T,
    group_value: T,
) -> RadioView<State, T> {
    RadioView {
        value,
        group_value,
        on_changed: None,
        label: None,
        error: false,
    }
}

/// PascalCase alias for [`radio`].
#[allow(non_snake_case)]
pub fn Radio<State: 'static, T: PartialEq + Clone + 'static>(
    value: T,
    group_value: T,
) -> RadioView<State, T> {
    radio(value, group_value)
}

impl<State: 'static, T: PartialEq + Clone + 'static> RadioView<State, T> {
    /// Fire `on_changed(state, value.clone())` on release inside this
    /// radio's bounds (whole control **and** an attached [`Self::label`]) —
    /// unconditionally, even if already selected, mirroring the Dart
    /// source's `onTap`. Never self-mutates. Omitting this call leaves the
    /// radio disabled — see the [module docs](self).
    pub fn on_changed<F: Fn(&mut State, T) + 'static>(mut self, on_changed: F) -> Self {
        self.on_changed = Some(Rc::new(on_changed));
        self
    }

    /// Attach an optional text label beside the control; tapping it also
    /// selects (the whole control+label bounds is one press target).
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Set the error flavor: recolors the ring/dot to `colors.error` while
    /// enabled (a disabled radio ignores this — see [`resolve_colors`]).
    pub fn error(mut self, error: bool) -> Self {
        self.error = error;
        self
    }
}

/// The retained widget for a [`RadioView`] — carries no generic parameter;
/// see the module docs' Generic selection model section.
pub struct RadioWidget {
    selected: bool,
    enabled: bool,
    error: bool,
    /// Drives the inner dot's `0.0` (hidden) .. `1.0` (fully scaled in)
    /// fraction — duration+curve, not a spring; see the module docs.
    anim: AnimationController,
    state_layer: StateLayer,
    label_pod: Option<ChildPod>,
    /// The label text, retained for the semantics node's accessible name.
    label_text: Option<String>,
    /// The pressed *visual* state; follows the cursor in/out while captured.
    pressed: bool,
    /// Armed by a `Down`, cleared on `Up`/`Cancel`.
    captured: bool,
    on_changed: Option<frust::authoring::ErasedCallback>,
}

impl<State: 'static, T: PartialEq + Clone + 'static> View<State> for RadioView<State, T> {
    type Element = RadioWidget;

    fn build(&self, ctx: &mut BuildCtx<'_>) -> RadioWidget {
        let selected = self.value == self.group_value;
        let enabled = self.on_changed.is_some();

        let label_pod = self
            .label
            .as_ref()
            .map(|label| frust::authoring::build_child(&label_view::<State>(label.clone()), ctx));

        let mut anim = AnimationController::new(MaterialMotion::SHORT_4)
            .with_curve(MaterialMotion::EMPHASIZED_DECELERATE);
        if selected {
            anim.forward();
            // Settle instantly on first mount — see the module docs' Dot
            // scale-in section for why two `advance` calls are needed.
            anim.advance(FrameTime::ZERO);
            anim.advance(FrameTime::from_nanos(
                MaterialMotion::SHORT_4.as_nanos() as u64
            ));
        }

        RadioWidget {
            selected,
            enabled,
            error: self.error,
            anim,
            state_layer: StateLayer::new(),
            label_pod,
            label_text: self.label.clone(),
            pressed: false,
            captured: false,
            on_changed: self
                .on_changed
                .as_ref()
                .map(|cb| bind_on_changed::<State, T>(cb, self.value.clone())),
        }
    }

    fn rebuild(
        &self,
        prev: &Self,
        element: &mut RadioWidget,
        ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        let mut flags = ChangeFlags::NONE;

        // Closures aren't comparable — always reinstall the adapter.
        element.on_changed = self
            .on_changed
            .as_ref()
            .map(|cb| bind_on_changed::<State, T>(cb, self.value.clone()));

        let selected = self.value == self.group_value;
        if element.selected != selected {
            element.selected = selected;
            // Compile-time duration+curve — no theme needed, so (unlike
            // `switch`'s themed spring) this can start right here rather
            // than waiting for `paint`. See the module docs.
            if selected {
                element.anim.forward();
            } else {
                element.anim.reverse();
            }
            flags |= ChangeFlags::PAINT;
        }

        let enabled = self.on_changed.is_some();
        if element.enabled != enabled {
            element.enabled = enabled;
            if !enabled {
                // A radio disabled mid-press keeps neither the press nor the
                // capture it was holding (mirrors
                // `button::ButtonWidget::rebuild`'s identical clear).
                element.pressed = false;
                element.captured = false;
                element.state_layer.set_pressed(false);
            }
            flags |= ChangeFlags::PAINT;
        }

        if element.error != self.error {
            element.error = self.error;
            flags |= ChangeFlags::PAINT;
        }

        match (&prev.label, &self.label) {
            (None, None) => {}
            (Some(p), Some(n)) if p == n => {}
            (Some(_), Some(n)) => {
                element.label_text = Some(n.clone());
                let pod = element.label_pod.as_mut().expect("label pod present");
                let prev_view = label_view::<State>(prev.label.clone().unwrap_or_default());
                let next_view = label_view::<State>(n.clone());
                flags |= frust::authoring::rebuild_child(&prev_view, &next_view, pod, ctx);
            }
            (None, Some(n)) => {
                element.label_text = Some(n.clone());
                element.label_pod = Some(frust::authoring::build_child(
                    &label_view::<State>(n.clone()),
                    ctx,
                ));
                flags |= ChangeFlags::LAYOUT | ChangeFlags::PAINT;
            }
            (Some(p), None) => {
                element.label_text = None;
                let mut pod = element.label_pod.take().expect("label pod present");
                let prev_view = label_view::<State>(p.clone());
                frust::authoring::teardown_child(&prev_view, &mut pod, ctx);
                flags |= ChangeFlags::LAYOUT | ChangeFlags::PAINT;
            }
        }

        flags
    }

    fn teardown(&self, element: &mut RadioWidget, ctx: &mut BuildCtx<'_>) {
        if let (Some(label), Some(pod)) = (&self.label, element.label_pod.as_mut()) {
            let label_view = label_view::<State>(label.clone());
            frust::authoring::teardown_child(&label_view, pod, ctx);
        }
    }
}

impl Widget for RadioWidget {
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        let Some(label) = self.label_pod.as_mut() else {
            return bc.constrain(Size::new(HIT_SIZE, HIT_SIZE));
        };
        let label_max = Size::new(
            (bc.max().width - HIT_SIZE - LABEL_GAP).max(0.0),
            bc.max().height,
        );
        let label_size = label.layout_child(ctx, &BoxConstraints::loose(label_max));
        let height = label_size.height.max(HIT_SIZE);
        // Vertically centre the label against the control column.
        label.set_origin(Point::new(
            HIT_SIZE + LABEL_GAP,
            (height - label_size.height) / 2.0,
        ));
        bc.constrain(Size::new(HIT_SIZE + LABEL_GAP + label_size.width, height))
    }

    fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut dyn PaintScene) {
        let theme = Theme::from_paint_ctx(ctx);
        let (ring_color, state_layer_color) =
            resolve_colors(theme, self.selected, self.error, self.enabled);

        if self.anim.advance(ctx.frame_time()) {
            ctx.request_frame();
        }
        // Duration+curve motion is already bounded 0.0..=1.0 (see
        // `AnimationController::value`'s own docs) — no clamping needed.
        let progress = self.anim.value();

        let o = ctx.origin();
        let cy = ctx.size().height / 2.0;
        let center_local = Point::new(HIT_SIZE / 2.0, cy);
        let center_abs = Point::new(o.x + HIT_SIZE / 2.0, o.y + cy);

        // The pod's hover link is authoritative; re-synced every paint — the
        // pointer's departure never reaches `event`, so this is what drops
        // the hover tint the frame it leaves (mirrors `checkbox`/`list_item`'s
        // identical self-correction). Enabled-gated the same way
        // `button::core`'s identical self-correction is: a claim latched
        // before a mid-hover disable must still be dropped here.
        self.state_layer
            .set_hovered(self.enabled && ctx.is_hovered());

        self.state_layer.paint(
            ctx,
            scene,
            Rect::from_center_size(center_abs, Size::new(HIT_SIZE, HIT_SIZE)),
            HIT_SIZE / 2.0,
            state_layer_color,
        );

        let ring_radius = (RING_SIZE - BORDER_WIDTH) / 2.0;
        let ring_path = Circle::new(center_local, ring_radius).to_path(STROKE_TOLERANCE);
        scene.stroke_path(o, &ring_path, BORDER_WIDTH, &Brush::Solid(ring_color));

        let dot_diam = DOT_SIZE * progress;
        scene.fill_rounded_rect(
            Point::new(center_abs.x - dot_diam / 2.0, center_abs.y - dot_diam / 2.0),
            Size::new(dot_diam, dot_diam),
            dot_diam / 2.0,
            ring_color,
        );

        if let Some(label) = self.label_pod.as_mut() {
            label.paint_child(ctx, scene);
        }
    }

    fn event(&mut self, ctx: &mut EventCtx, event: &InputEvent) -> EventResult {
        let InputEvent::Pointer(p) = event else {
            return EventResult::Ignored;
        };
        // A disabled radio (`M3ETappable`'s `_isInteractive` reducing to
        // `_enabled` for this widget — see the module docs) ignores every
        // pointer phase outright, never arming a press.
        if !self.enabled {
            return EventResult::Ignored;
        }
        match p.phase {
            PointerPhase::Down => {
                if !presses(p) {
                    return EventResult::Ignored;
                }
                self.pressed = true;
                self.captured = true;
                self.state_layer.set_pressed(true);
                ctx.capture_pointer();
                ctx.request_redraw();
                EventResult::Handled
            }
            PointerPhase::Move => {
                if !self.captured {
                    // No capture: this is the hover pass — claim whenever
                    // the pointer is inside, mirroring `list_item`'s
                    // reference hover-claim implementation (see the module
                    // docs).
                    let over = inside(p.position, ctx.size());
                    if over {
                        ctx.claim_hover();
                    }
                    if self.state_layer.set_hovered(over) {
                        ctx.request_redraw();
                    }
                    return EventResult::Ignored;
                }
                let inside_now = inside(p.position, ctx.size());
                self.pressed = inside_now;
                self.state_layer.set_pressed(inside_now);
                ctx.request_redraw();
                EventResult::Handled
            }
            PointerPhase::Up => {
                if !self.captured {
                    return EventResult::Ignored;
                }
                if inside(p.position, ctx.size())
                    && let Some(on_changed) = self.on_changed.as_mut()
                {
                    // The reference's own choice — see the module docs'
                    // Haptics section.
                    MaterialHaptics::fire(HapticSignal::None);
                    (on_changed)(ctx);
                }
                self.pressed = false;
                self.captured = false;
                self.state_layer.set_pressed(false);
                ctx.request_redraw();
                EventResult::Handled
            }
            PointerPhase::Cancel => {
                if !self.captured {
                    return EventResult::Ignored;
                }
                self.pressed = false;
                self.captured = false;
                self.state_layer.set_pressed(false);
                ctx.request_redraw();
                EventResult::Handled
            }
        }
    }

    fn semantics(&self, ctx: &mut SemanticsCtx) {
        ctx.push_node(Role::RadioButton, |node| {
            if let Some(label) = &self.label_text {
                node.set_label(label.as_str());
            }
            node.set_selected(self.selected);
            if self.enabled {
                node.add_action(Action::Click);
            }
        });
    }

    frust::authoring::visit_children!(label_pod);
}

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

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    enum Freq {
        Daily,
        Weekly,
        Monthly,
    }

    struct GroupState {
        selected: Freq,
        changes: u32,
    }

    fn on_changed(s: &mut GroupState, v: Freq) {
        s.selected = v;
        s.changes += 1;
    }

    fn built(value: Freq, group_value: Freq, enabled: bool) -> RadioWidget {
        let view = radio::<GroupState, Freq>(value, group_value);
        let view = if enabled {
            view.on_changed(on_changed)
        } else {
            view
        };
        let mut counter = 0u64;
        View::<GroupState>::build(&view, &mut BuildCtx::new(&mut counter))
    }

    fn ev(phase: PointerPhase, x: f64, y: f64) -> InputEvent {
        InputEvent::Pointer(frust::authoring::PointerEvent {
            phase,
            position: Point::new(x, y),
            button: frust::authoring::PointerButton::Primary,
        })
    }

    fn secondary_ev(phase: PointerPhase, x: f64, y: f64) -> InputEvent {
        InputEvent::Pointer(frust::authoring::PointerEvent {
            phase,
            position: Point::new(x, y),
            button: frust::authoring::PointerButton::Secondary,
        })
    }

    fn dispatch(w: &mut RadioWidget, state: &mut GroupState, event: &InputEvent) {
        let state_any: &mut dyn Any = state;
        let mut ctx = EventCtx::new(state_any, Point::ZERO, Size::new(HIT_SIZE, HIT_SIZE));
        w.event(&mut ctx, event);
    }

    // ---- Acceptance 1: select/deselect across a 3-member group -----------

    #[test]
    fn select_and_deselect_across_a_three_member_group() {
        let mut state = GroupState {
            selected: Freq::Daily,
            changes: 0,
        };

        // Round 1: group starts on Daily.
        let daily = built(Freq::Daily, state.selected, true);
        let mut weekly = built(Freq::Weekly, state.selected, true);
        let monthly = built(Freq::Monthly, state.selected, true);
        assert!(daily.selected);
        assert!(!weekly.selected);
        assert!(!monthly.selected);

        // Tap "weekly": fires the shared handler with its own value.
        dispatch(&mut weekly, &mut state, &ev(PointerPhase::Down, 5.0, 12.0));
        dispatch(&mut weekly, &mut state, &ev(PointerPhase::Up, 5.0, 12.0));
        assert_eq!(state.selected, Freq::Weekly);
        assert_eq!(state.changes, 1);
        assert!(
            !weekly.selected,
            "must not self-mutate — still false until the app rebuilds"
        );

        // The app rebuilds all three against the new group value.
        let daily = built(Freq::Daily, state.selected, true);
        let weekly = built(Freq::Weekly, state.selected, true);
        let monthly = built(Freq::Monthly, state.selected, true);
        assert!(!daily.selected, "Daily deselected");
        assert!(weekly.selected, "Weekly now selected");
        assert!(!monthly.selected);

        // Round 2: tap "monthly" next — deselects Weekly, selects Monthly.
        let mut monthly = monthly;
        dispatch(&mut monthly, &mut state, &ev(PointerPhase::Down, 5.0, 12.0));
        dispatch(&mut monthly, &mut state, &ev(PointerPhase::Up, 5.0, 12.0));
        assert_eq!(state.selected, Freq::Monthly);
        assert_eq!(state.changes, 2);

        let daily = built(Freq::Daily, state.selected, true);
        let weekly = built(Freq::Weekly, state.selected, true);
        let monthly = built(Freq::Monthly, state.selected, true);
        assert!(!daily.selected);
        assert!(!weekly.selected, "Weekly deselected");
        assert!(monthly.selected, "Monthly now selected");
    }

    #[test]
    fn already_selected_still_fires() {
        let mut w = built(Freq::Daily, Freq::Daily, true);
        let mut state = GroupState {
            selected: Freq::Daily,
            changes: 0,
        };
        dispatch(&mut w, &mut state, &ev(PointerPhase::Down, 5.0, 12.0));
        dispatch(&mut w, &mut state, &ev(PointerPhase::Up, 5.0, 12.0));
        assert_eq!(state.changes, 1);
        assert!(w.selected, "still selected until the app rebuilds it");
    }

    #[test]
    fn up_outside_does_not_fire() {
        let mut w = built(Freq::Weekly, Freq::Daily, true);
        let mut state = GroupState {
            selected: Freq::Daily,
            changes: 0,
        };
        dispatch(&mut w, &mut state, &ev(PointerPhase::Down, 5.0, 12.0));
        dispatch(&mut w, &mut state, &ev(PointerPhase::Move, 500.0, 12.0));
        dispatch(&mut w, &mut state, &ev(PointerPhase::Up, 500.0, 12.0));
        assert_eq!(state.changes, 0);
    }

    #[test]
    fn hover_move_without_down_is_ignored_noop() {
        let mut w = built(Freq::Weekly, Freq::Daily, true);
        let mut state = GroupState {
            selected: Freq::Daily,
            changes: 0,
        };
        let state_any: &mut dyn Any = &mut state;
        let mut ctx = EventCtx::new(state_any, Point::ZERO, Size::new(HIT_SIZE, HIT_SIZE));
        let result = w.event(&mut ctx, &ev(PointerPhase::Move, 5.0, 12.0));
        assert!(matches!(result, EventResult::Ignored));
        assert!(!w.pressed);
        assert_eq!(state.changes, 0);
    }

    #[test]
    fn hover_move_inside_claims_hover_and_requests_redraw_once() {
        let mut w = built(Freq::Weekly, Freq::Daily, true);
        let mut state = GroupState {
            selected: Freq::Daily,
            changes: 0,
        };
        let state_any: &mut dyn Any = &mut state;
        let mut ctx = EventCtx::new(state_any, Point::ZERO, Size::new(HIT_SIZE, HIT_SIZE));
        w.event(&mut ctx, &ev(PointerPhase::Move, 5.0, 12.0));
        assert!(ctx.needs_redraw(), "hover gain must request a redraw");
        assert_eq!(w.state_layer.opacity(), crate::interaction::HOVER_OPACITY);
    }

    #[test]
    fn paint_self_corrects_hover_off_when_the_pod_link_is_gone() {
        let mut w = built(Freq::Weekly, Freq::Daily, true);
        let mut state = GroupState {
            selected: Freq::Daily,
            changes: 0,
        };
        let state_any: &mut dyn Any = &mut state;
        let mut ctx = EventCtx::new(state_any, Point::ZERO, Size::new(HIT_SIZE, HIT_SIZE));
        w.event(&mut ctx, &ev(PointerPhase::Move, 5.0, 12.0));
        assert!(w.state_layer.opacity() > 0.0, "hover claimed");

        // `PaintCtx::is_hovered` reads false with no pod link recorded — the
        // pointer left with no event ever telling this widget so; paint is
        // what drops the stale flag.
        let mut paint_ctx = PaintCtx::new(Point::ZERO, Size::new(HIT_SIZE, HIT_SIZE));
        let mut rec = Recorder::default();
        w.paint(&mut paint_ctx, &mut rec);
        assert_eq!(
            w.state_layer.opacity(),
            0.0,
            "paint self-corrected the flag"
        );
    }

    #[test]
    fn cancel_clears_armed_state() {
        let mut w = built(Freq::Weekly, Freq::Daily, true);
        let mut state = GroupState {
            selected: Freq::Daily,
            changes: 0,
        };
        dispatch(&mut w, &mut state, &ev(PointerPhase::Down, 5.0, 12.0));
        assert!(w.captured);
        dispatch(&mut w, &mut state, &ev(PointerPhase::Cancel, 5.0, 12.0));
        assert!(!w.captured, "Cancel disarms the press");
        dispatch(&mut w, &mut state, &ev(PointerPhase::Up, 5.0, 12.0));
        assert_eq!(state.changes, 0);
    }

    #[test]
    fn a_secondary_press_never_presses_captures_or_fires() {
        let mut w = built(Freq::Weekly, Freq::Daily, true);
        let mut state = GroupState {
            selected: Freq::Daily,
            changes: 0,
        };
        dispatch(
            &mut w,
            &mut state,
            &secondary_ev(PointerPhase::Down, 5.0, 12.0),
        );
        assert!(!w.pressed);
        assert!(!w.captured);
        dispatch(
            &mut w,
            &mut state,
            &secondary_ev(PointerPhase::Up, 5.0, 12.0),
        );
        assert_eq!(state.changes, 0);

        // The primary gesture is untouched by the guard.
        dispatch(&mut w, &mut state, &ev(PointerPhase::Down, 5.0, 12.0));
        assert!(w.pressed);
        dispatch(&mut w, &mut state, &ev(PointerPhase::Up, 5.0, 12.0));
        assert_eq!(state.changes, 1);
    }

    #[test]
    fn disabled_radio_ignores_every_pointer_phase() {
        let mut w = built(Freq::Weekly, Freq::Daily, false);
        assert!(!w.enabled);
        let mut state = GroupState {
            selected: Freq::Daily,
            changes: 0,
        };
        let state_any: &mut dyn Any = &mut state;
        let mut ctx = EventCtx::new(state_any, Point::ZERO, Size::new(HIT_SIZE, HIT_SIZE));
        let down = w.event(&mut ctx, &ev(PointerPhase::Down, 5.0, 12.0));
        assert!(matches!(down, EventResult::Ignored));
        assert!(!w.pressed);
        assert!(!w.captured);
    }

    // ---- Acceptance 2: dot animation progress at t=0/mid/1 ---------------

    fn paint_rec(w: &mut RadioWidget, theme: Option<&Theme>) -> Recorder {
        let mut rec = Recorder::default();
        let mut ctx = match theme {
            Some(t) => PaintCtx::new(Point::ZERO, Size::new(HIT_SIZE, HIT_SIZE)).with_theme(t),
            None => PaintCtx::new(Point::ZERO, Size::new(HIT_SIZE, HIT_SIZE)),
        };
        w.paint(&mut ctx, &mut rec);
        rec
    }

    #[derive(Default)]
    struct Recorder {
        /// `fill_rounded_rect` calls, in paint order: the state layer (only
        /// while active) then the dot.
        rrects: Vec<(Point, Size, f64, Color)>,
        /// `stroke_path` calls' solid brush color, in paint order (the ring).
        strokes: Vec<Color>,
    }

    impl PaintScene for Recorder {
        fn fill_rect(&mut self, _o: Point, _s: Size, _c: Color) {}
        fn draw_text(&mut self, _o: Point, _t: &str) {}
        fn fill_rounded_rect(&mut self, o: Point, s: Size, radius: f64, color: Color) {
            self.rrects.push((o, s, radius, color));
        }
        fn stroke_path(&mut self, _o: Point, _path: &kurbo::BezPath, _width: f64, brush: &Brush) {
            if let Brush::Solid(c) = brush {
                self.strokes.push(*c);
            }
        }
    }

    #[test]
    fn dot_scale_in_progress_observable_at_zero_mid_and_settled() {
        let mut counter = 0u64;
        let prev = radio::<GroupState, Freq>(Freq::Daily, Freq::Weekly).on_changed(on_changed);
        let mut w = View::<GroupState>::build(&prev, &mut BuildCtx::new(&mut counter));
        assert!(!w.selected);
        assert_eq!(w.anim.value(), 0.0);

        let next = radio::<GroupState, Freq>(Freq::Daily, Freq::Daily).on_changed(on_changed);
        let flags =
            View::<GroupState>::rebuild(&next, &prev, &mut w, &mut BuildCtx::new(&mut counter));
        assert!(w.selected);
        assert!(flags.needs_paint());
        assert!(w.anim.is_animating(), "rebuild started the forward drive");

        // t=0: the first `advance` (inside `paint`) only seeds the clock.
        let rec0 = paint_rec(&mut w, None);
        assert_eq!(
            rec0.rrects[0].1,
            Size::ZERO,
            "dot has not started scaling in yet"
        );

        // t=mid: advance halfway through SHORT_4 directly on the controller
        // (mirroring `super::switch`'s own test pattern — `PaintCtx::new`
        // always reads `FrameTime::ZERO`, so a real elapsed span is driven
        // by hand), then paint again purely to record the result.
        let half_ns = (MaterialMotion::SHORT_4.as_nanos() / 2) as u64;
        w.anim.advance(FrameTime::from_nanos(half_ns));
        let rec_mid = paint_rec(&mut w, None);
        let mid_diam = rec_mid.rrects[0].1.width;
        assert!(
            mid_diam > 0.0 && mid_diam < DOT_SIZE,
            "dot mid-animation diameter should be strictly between 0 and {DOT_SIZE}, got {mid_diam}"
        );

        // t=1: advance to (and past) the full SHORT_4 span — settles exactly.
        let full_ns = MaterialMotion::SHORT_4.as_nanos() as u64;
        w.anim.advance(FrameTime::from_nanos(full_ns));
        assert!(!w.anim.is_animating(), "fling settled");
        let rec_done = paint_rec(&mut w, None);
        assert_eq!(rec_done.rrects[0].1, Size::new(DOT_SIZE, DOT_SIZE));
    }

    #[test]
    fn deselect_also_animates_both_ways() {
        let mut counter = 0u64;
        let prev = radio::<GroupState, Freq>(Freq::Daily, Freq::Daily).on_changed(on_changed);
        let mut w = View::<GroupState>::build(&prev, &mut BuildCtx::new(&mut counter));
        // Settle to fully selected first (build's own instant-settle path).
        assert_eq!(w.anim.value(), 1.0);

        let next = radio::<GroupState, Freq>(Freq::Daily, Freq::Weekly).on_changed(on_changed);
        View::<GroupState>::rebuild(&next, &prev, &mut w, &mut BuildCtx::new(&mut counter));
        assert!(!w.selected);
        assert!(w.anim.is_animating(), "rebuild started the reverse drive");

        let full_ns = MaterialMotion::SHORT_4.as_nanos() as u64;
        w.anim.advance(FrameTime::ZERO); // seed
        w.anim.advance(FrameTime::from_nanos(full_ns));
        assert!(!w.anim.is_animating());
        assert_eq!(w.anim.value(), 0.0);
        let rec = paint_rec(&mut w, None);
        assert_eq!(rec.rrects[0].1, Size::ZERO, "dot fully scaled back out");
    }

    // ---- Acceptance 3: error flavor colors --------------------------------

    #[test]
    fn error_flavor_recolors_ring_unthemed() {
        let mut w = built(Freq::Weekly, Freq::Daily, true);
        w.error = true;
        let rec = paint_rec(&mut w, None);
        assert_eq!(rec.strokes, vec![ERROR]);
    }

    #[test]
    fn error_flavor_recolors_ring_themed() {
        let theme = crate::baseline();
        let scheme = theme.scheme();
        let mut w = built(Freq::Weekly, Freq::Daily, true);
        w.error = true;
        let rec = paint_rec(&mut w, Some(&theme));
        assert_eq!(rec.strokes, vec![scheme.error]);

        // A selected+error radio still reads `error`, not `primary` — error
        // wins over selection (matches `M3ERadioTheme.color`).
        let mut selected = built(Freq::Daily, Freq::Daily, true);
        selected.error = true;
        let rec = paint_rec(&mut selected, Some(&theme));
        assert_eq!(rec.strokes, vec![scheme.error]);

        // The state layer never recolors for error — always primary/on_surface.
        selected.state_layer.set_pressed(true);
        let rec = paint_rec(&mut selected, Some(&theme));
        assert_eq!(rec.rrects[0].3, with_alpha(scheme.primary, 0.10));
    }

    #[test]
    fn disabled_wins_over_error() {
        let theme = crate::baseline();
        let scheme = theme.scheme();
        let mut w = built(Freq::Weekly, Freq::Daily, false);
        w.error = true;
        let rec = paint_rec(&mut w, Some(&theme));
        assert_eq!(rec.strokes, vec![with_alpha(scheme.on_surface, 0.38)]);
    }

    // ---- Paint: unthemed/themed role resolution ---------------------------

    #[test]
    fn unthemed_paint_uses_fallback_constants() {
        let mut off = built(Freq::Weekly, Freq::Daily, true);
        let rec = paint_rec(&mut off, None);
        assert_eq!(rec.strokes, vec![ON_SURFACE_VARIANT]);

        let mut on = built(Freq::Daily, Freq::Daily, true);
        let rec = paint_rec(&mut on, None);
        assert_eq!(rec.strokes, vec![PRIMARY]);
        assert_eq!(rec.rrects[0].3, PRIMARY, "the fully-settled dot is PRIMARY");
    }

    #[test]
    fn themed_paint_resolves_roles() {
        let theme = crate::baseline();
        let scheme = theme.scheme();
        let mut off = built(Freq::Weekly, Freq::Daily, true);
        let rec = paint_rec(&mut off, Some(&theme));
        assert_eq!(rec.strokes, vec![scheme.on_surface_variant]);

        let mut on = built(Freq::Daily, Freq::Daily, true);
        let rec = paint_rec(&mut on, Some(&theme));
        assert_eq!(rec.strokes, vec![scheme.primary]);
    }

    // ---- rebuild ------------------------------------------------------------

    #[test]
    fn rebuild_adopts_new_selected_value() {
        let mut counter = 0u64;
        let prev = radio::<GroupState, Freq>(Freq::Weekly, Freq::Daily).on_changed(on_changed);
        let mut w = View::<GroupState>::build(&prev, &mut BuildCtx::new(&mut counter));
        assert!(!w.selected);
        let next = radio::<GroupState, Freq>(Freq::Weekly, Freq::Weekly).on_changed(on_changed);
        let flags =
            View::<GroupState>::rebuild(&next, &prev, &mut w, &mut BuildCtx::new(&mut counter));
        assert!(w.selected);
        assert!(flags.needs_paint());
    }

    #[test]
    fn rebuild_toggles_enabled_state() {
        let mut counter = 0u64;
        let prev = radio::<GroupState, Freq>(Freq::Weekly, Freq::Daily);
        let mut w = View::<GroupState>::build(&prev, &mut BuildCtx::new(&mut counter));
        assert!(!w.enabled);
        let next = radio::<GroupState, Freq>(Freq::Weekly, Freq::Daily).on_changed(on_changed);
        let flags =
            View::<GroupState>::rebuild(&next, &prev, &mut w, &mut BuildCtx::new(&mut counter));
        assert!(w.enabled);
        assert!(flags.needs_paint());
    }

    #[test]
    fn disabling_mid_press_clears_the_press_and_the_capture() {
        let mut counter = 0u64;
        let prev = radio::<GroupState, Freq>(Freq::Weekly, Freq::Daily).on_changed(on_changed);
        let mut w = View::<GroupState>::build(&prev, &mut BuildCtx::new(&mut counter));
        w.pressed = true;
        w.captured = true;
        w.state_layer.set_pressed(true);

        let next = radio::<GroupState, Freq>(Freq::Weekly, Freq::Daily);
        let flags =
            View::<GroupState>::rebuild(&next, &prev, &mut w, &mut BuildCtx::new(&mut counter));
        assert!(!w.pressed);
        assert!(!w.captured);
        assert_eq!(
            w.state_layer.opacity(),
            0.0,
            "the state-layer's own pressed flag clears too"
        );
        assert!(flags.needs_paint());
    }

    // ---- through a real RenderRoot: paint self-correction with a live claim -
    //
    // Hover is an authoritative read off `PaintCtx`, seeded by the pod chain, so
    // a bare `PaintCtx` can never fake a *standing* claim — only a real tree can
    // (the pattern `list_item.rs`'s/`button/core.rs`'s own hover harnesses use).

    const HOVER_DISABLE_WINDOW: Size = Size::new(200.0, 200.0);

    struct HoverDisableHarness {
        root: frust_core::RenderRoot<GroupState, RadioView<GroupState, Freq>>,
        state: GroupState,
        tcx: frust::authoring::text::TextContext,
        enabled: bool,
    }

    impl HoverDisableHarness {
        fn new(enabled: bool) -> Self {
            let mut h = HoverDisableHarness {
                root: frust_core::RenderRoot::new(),
                state: GroupState {
                    selected: Freq::Daily,
                    changes: 0,
                },
                tcx: frust::authoring::text::TextContext::new(),
                enabled,
            };
            h.sync();
            h
        }

        fn sync(&mut self) {
            let enabled = self.enabled;
            let mut app = move |_: &mut GroupState| {
                let view = radio::<GroupState, Freq>(Freq::Weekly, Freq::Daily);
                if enabled {
                    view.on_changed(on_changed)
                } else {
                    view
                }
            };
            self.root.rebuild(&mut app, &mut self.state);
            self.root
                .layout_with_text(HOVER_DISABLE_WINDOW, &mut self.tcx as &mut dyn Any);
        }

        fn dispatch(&mut self, phase: PointerPhase, x: f64, y: f64) {
            self.root.event(&mut self.state, &ev(phase, x, y));
        }

        fn paint(&mut self) -> Recorder {
            let mut rec = Recorder::default();
            self.root.paint(&mut rec, frust::FrameTime::ZERO);
            rec
        }
    }

    #[test]
    fn a_disabled_radio_drops_a_standing_hover_claim_at_paint() {
        let mut h = HoverDisableHarness::new(true);

        // Claim hover while enabled — the pod's hover link is now standing.
        h.dispatch(PointerPhase::Move, 5.0, 12.0);
        let rec = h.paint();
        assert_eq!(
            rec.rrects.len(),
            2,
            "hovered+enabled radio paints the state-layer overlay plus the dot"
        );

        // Disable via rebuild — no `Down`/`Up`/`Cancel` runs, so nothing ever
        // tells the widget's own event handler to drop the claim; the pointer's
        // hover link is still standing (`ctx.is_hovered()` would read `true`)
        // when the next paint runs.
        h.enabled = false;
        h.sync();

        let rec = h.paint();
        assert_eq!(
            rec.rrects.len(),
            1,
            "paint's self-correction must gate on `enabled`: state_layer.opacity() \
             drops to 0.0 (the overlay fill is skipped entirely), so only the dot paints"
        );
    }

    // ---- label: presence/text reconciliation -------------------------------

    #[test]
    fn label_presence_change_builds_and_tears_down_the_child() {
        let mut counter = 0u64;
        let prev = radio::<GroupState, Freq>(Freq::Weekly, Freq::Daily).on_changed(on_changed);
        let mut w = View::<GroupState>::build(&prev, &mut BuildCtx::new(&mut counter));
        assert!(w.label_pod.is_none());

        let next = radio::<GroupState, Freq>(Freq::Weekly, Freq::Daily)
            .on_changed(on_changed)
            .label("Weekly");
        let flags =
            View::<GroupState>::rebuild(&next, &prev, &mut w, &mut BuildCtx::new(&mut counter));
        assert!(w.label_pod.is_some());
        assert_eq!(w.label_text.as_deref(), Some("Weekly"));
        assert!(flags.needs_layout());

        let prev2 = next;
        let next2 = radio::<GroupState, Freq>(Freq::Weekly, Freq::Daily).on_changed(on_changed);
        let flags =
            View::<GroupState>::rebuild(&next2, &prev2, &mut w, &mut BuildCtx::new(&mut counter));
        assert!(w.label_pod.is_none());
        assert!(w.label_text.is_none());
        assert!(flags.needs_layout());
    }

    // ---- semantics ----------------------------------------------------------

    #[test]
    fn semantics_reports_role_label_selected_and_bounds() {
        fn logic(_s: &mut ()) -> RadioView<(), Freq> {
            radio::<(), Freq>(Freq::Daily, Freq::Daily)
                .on_changed(|_, _| {})
                .label("daily")
        }
        let mut root: frust_core::RenderRoot<(), RadioView<(), Freq>> =
            frust_core::RenderRoot::new();
        let mut state = ();
        root.rebuild(&mut logic, &mut state);
        let mut tcx = frust::authoring::text::TextContext::new();
        root.layout_with_text(Size::new(200.0, 200.0), &mut tcx as &mut dyn Any);
        let update = root.semantics();
        let (_, node) = update
            .nodes
            .iter()
            .find(|(_, n)| n.role() == Role::RadioButton)
            .expect("radio contributes a Role::RadioButton node");
        assert_eq!(node.label(), Some("daily"));
        assert_eq!(node.is_selected(), Some(true));
        assert!(node.supports_action(Action::Click));
        let bounds = node.bounds().expect("radio node has bounds");
        assert_eq!((bounds.x0, bounds.y0), (0.0, 0.0));
    }

    #[test]
    fn semantics_omits_click_action_when_disabled() {
        fn logic(_s: &mut ()) -> RadioView<(), Freq> {
            radio::<(), Freq>(Freq::Daily, Freq::Weekly)
        }
        let mut root: frust_core::RenderRoot<(), RadioView<(), Freq>> =
            frust_core::RenderRoot::new();
        let mut state = ();
        root.rebuild(&mut logic, &mut state);
        let mut tcx = frust::authoring::text::TextContext::new();
        root.layout_with_text(Size::new(200.0, 200.0), &mut tcx as &mut dyn Any);
        let update = root.semantics();
        let (_, node) = update
            .nodes
            .iter()
            .find(|(_, n)| n.role() == Role::RadioButton)
            .expect("radio contributes a Role::RadioButton node");
        assert!(!node.supports_action(Action::Click));
        assert_eq!(node.is_selected(), Some(false));
    }
}