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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! Radio button widget.
use crate::compat::{String, ToString};
use crate::core::{Color, Font, HorizontalAlignment, Point, Rect, Size};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::{GenericSignal, Signal1};
use crate::widget::capability::coercion::{expect_bool, expect_string};
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
use crate::widget::metrics::{
dimensions, estimate_line_height, estimate_text_width, ControlMetrics, FocusRing,
FOCUS_RING_WIDTH,
};
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};
/// Radius of the indicator disc, in logical pixels.
///
/// A constant, for the same reason [`crate::widget::CheckBox`]'s indicator is: the disc is
/// chrome the control owns, while the rectangle it is laid out in belongs to whoever placed
/// it. `min(w, h) / 4` made a 240x120 census cell draw a 60 px circle, so the same control
/// was a different shape in every layout.
///
/// The value comes from the shared table (`dimensions::RADIO_OUTER_RADIUS`) rather than being
/// restated here, so the indicator, the ring's stroke and the dot cannot drift apart — and so
/// a checkbox and a radio, which sit side by side in a form, stay within a pixel of each
/// other's size.
const INDICATOR_RADIUS: u32 = dimensions::RADIO_OUTER_RADIUS;
/// Stroke width of the indicator's ring.
const RING_WIDTH: u32 = dimensions::RADIO_STROKE;
/// Distance from the control's left edge to the ring's outer edge.
///
/// Only a few pixels: a radio button's indicator is close to its own edge, which is what
/// lets several of them in a group line up as a column of discs.
const INDICATOR_INSET: i32 = 1;
/// The radio button's own padding: what it keeps between its rectangle and its contents.
const RADIO_PADDING: crate::style::EdgeOffsets =
crate::style::EdgeOffsets { left: 1, top: 0, right: 1, bottom: 0 };
/// The line box a single line of `font` occupies, centred vertically in `rect`.
///
/// A copy of what `RenderContext::text_line` computes, for the sizing path: `hit_area` is a
/// `&self` query with no render context to hand, and it must place the indicator exactly
/// where the painter will. Keeping the arithmetic in one place here — rather than restating
/// `rect.height / 2` at each of the two call sites — is what stops the pair drifting.
fn vertical_line_box(rect: Rect, font: &Font) -> Rect {
let height = font.size().max(1.0) as u32;
let height = height.min(rect.height);
Rect::new(rect.x, rect.y + (rect.height.saturating_sub(height) / 2) as i32, rect.width, height)
}
/// Radio button widget.
pub struct RadioButton {
base: BaseWidget,
checked: bool,
group_id: Option<String>,
text: String,
/// Emitted without a payload when this button becomes the selected member of
/// its peer group. Only fires on a `false` -> `true` transition; deselection
/// does not emit.
pub selected: GenericSignal,
/// Emitted with the new state after `checked` changes, in both directions.
pub checked_changed: Signal1<bool>,
}
impl RadioButton {
/// The gap between the indicator and the label.
///
/// Read from the style so a theme can tune it, falling back to the shared table. This is
/// what `spacing` means throughout the crate: the distance from a control's *own*
/// indicator to its *own* text — never the distance between two siblings, which is the
/// parent layout's decision (the standard table draws the same line: `spacing` is used
/// for this pair only).
fn label_gap(&self) -> i32 {
self.style().spacing.unwrap_or(dimensions::INDICATOR_TEXT_SPACING) as i32
}
/// The indicator's centre and radius.
///
/// # Why one function, not two
///
/// The hit test and the painter must agree on where the disc is and how big it is, or a
/// press lands in a place that looks empty (or misses a place that looks like the
/// control). They used to be derived independently — the hit side from `rect.height`, the
/// paint side from the label's line box — so a press in the indicator's corner was inside
/// the hit area but outside the drawn ring. Deriving both from here makes that class of
/// drift unrepresentable.
///
/// `line` supplies the *row* the disc is centred on. The disc's own diameter is fixed
/// chrome, so it is not clamped to the line's height — only to the control's rectangle.
fn indicator_geometry(&self, line: &Rect) -> (Point, u32) {
let rect = self.geometry();
let row_centre_y = line.y + line.height as i32 / 2;
let diameter = (INDICATOR_RADIUS * 2).min(rect.width).min(rect.height);
let radius = diameter / 2;
let center_x = rect.x + INDICATOR_INSET + radius as i32;
(Point::new(center_x.min(rect.x + rect.width as i32 - radius as i32), row_centre_y), radius)
}
/// The region a press must land in to select this radio button.
///
/// The indicator plus the label beside it, **not** the whole rectangle the caller laid out.
///
/// This handler used to ignore the pointer entirely (`MousePress { pos: _, .. }`), so a press
/// anywhere in the control's rectangle selected it. A radio button given a wide row by its
/// layout therefore selected when the user clicked empty space well to the right of its own
/// label. Testing the control's **contents** is what every toolkit does — a radio button
/// reacts to its indicator and text — and it is a different statement from "the minimum touch
/// target is at least N points", which then widens this region rather than replacing it.
fn hit_area(&self) -> Rect {
let rect = self.geometry();
// The line box, derived the same way `RenderContext::text_line` derives it: a single
// line of `Font::default()` centred in the control's rectangle. The two must agree,
// because the disc's vertical position follows the line.
let line = vertical_line_box(rect, &Font::default());
let (center, radius) = self.indicator_geometry(&line);
let indicator =
Rect::new(center.x - radius as i32, center.y - radius as i32, radius * 2, radius * 2);
// The interactive region is the **contents** — indicator through the end of the
// label — not the whole laid-out rectangle. A radio given a wide row by its layout
// must not select when the user clicks empty space far to the right of its label.
let contents = if self.text.is_empty() {
// No label to reach, so the indicator alone is the target.
indicator
} else {
// The label's width is an *estimate* (`chars * 3/5 em`), deliberately conservative:
// the region must never extend past the text the user can see, and this path has no
// render context to measure with. The painter may therefore draw a slightly wider
// label than the hit region — the safe direction.
let label_width = self.text.chars().count() as u32 * (line.height * 3 / 5).max(1);
Rect::new(
indicator.x,
indicator.y,
indicator.width + self.label_gap() as u32 + label_width,
indicator.height,
)
};
match self.style().touch_target {
Some(min_size) => contents.expand_to_touch_target(min_size),
None => contents,
}
}
/// The size this radio button claims when nothing constrains it.
///
/// The disc is a fixed piece of this control's own chrome and the label follows it, so the
/// implicit size is `disc + gap + label` on one line, floored at the shared touch target.
/// Routed through [`ControlMetrics::implicit_size`] instead of the old inline
/// `text.len() * 8 + 24`, which was a second copy of the character-advance-and-padding
/// arithmetic — the copy that could disagree with a checkbox sitting beside it in the same
/// form the moment either changed.
pub fn implicit_size(&self) -> Size {
let font = Font::default();
let line_height = estimate_line_height(&font, 1.0);
let disc = (INDICATOR_RADIUS * 2).min(line_height);
let content_width = if self.text.is_empty() {
disc + INDICATOR_INSET as u32
} else {
disc + INDICATOR_INSET as u32
+ self.label_gap() as u32
+ estimate_text_width(&self.text, &font, 1.0)
};
let floor =
Size::new(dimensions::TOUCH_TARGET_MIN.min(content_width.max(disc)), line_height);
ControlMetrics::implicit_size(Size::new(content_width, 0), RADIO_PADDING, floor)
}
/// Creates an unchecked radio button with geometry.
pub fn new(geometry: Rect) -> Self {
Self {
base: BaseWidget::new(WidgetKind::RadioButton, geometry, "RadioButton"),
checked: false,
group_id: None,
text: String::new(),
selected: GenericSignal::new(),
checked_changed: Signal1::new(),
}
}
/// Returns current checked state.
pub fn is_checked(&self) -> bool {
self.checked
}
/// Returns the text label displayed next to the radio button.
pub fn text(&self) -> &str {
&self.text
}
/// Sets the text label displayed next to the radio button and requests a redraw.
pub fn set_text(&mut self, text: impl Into<String>) {
self.text = text.into();
self.base.request_redraw();
}
/// Sets optional group identifier.
pub fn set_group_id(&mut self, group_id: Option<String>) {
self.group_id = group_id;
}
/// Returns optional group identifier.
pub fn group_id(&self) -> Option<&str> {
self.group_id.as_deref()
}
/// Sets checked state and emits deterministic signals.
pub fn set_checked(&mut self, checked: bool) {
if self.checked == checked {
return;
}
self.checked = checked;
self.checked_changed.emit(checked);
if checked {
self.selected.emit();
}
}
/// Selects one radio button within a peer group.
pub fn select_in_group(peers: &mut [&mut RadioButton], selected_index: usize) -> bool {
if selected_index >= peers.len() {
return false;
}
let selected_group = peers[selected_index].group_id.clone();
for (index, peer) in peers.iter_mut().enumerate() {
if selected_group.is_some() && peer.group_id != selected_group {
continue;
}
peer.set_checked(index == selected_index);
}
true
}
/// Whether this control currently owns keyboard focus.
///
/// Reads [`BaseWidget`], which records the fact for every control from
/// [`crate::event::Event::FocusGained`] / [`crate::event::Event::FocusLost`].
pub fn is_focused(&self) -> bool {
self.base.focus_reason().is_some()
}
/// Whether a focus ring should be painted right now.
///
/// The same single predicate every control uses — [`BaseWidget::draws_focus_ring`] —
/// so "has focus" cannot be mistaken for "draw the ring" in one control and not in
/// another.
pub fn visual_focus(&self) -> bool {
self.base.draws_focus_ring()
}
/// Whether the pointer is over this control.
pub fn is_hovered(&self) -> bool {
self.base.is_hovered()
}
/// Sets the hovered flag and requests a redraw.
pub fn set_hovered(&mut self, hovered: bool) {
if self.base.is_hovered() == hovered {
return;
}
self.base.set_hovered(hovered);
self.base.request_redraw();
}
}
// Implement Widget trait
impl Widget for RadioButton {
fn base(&self) -> &BaseWidget {
&self.base
}
fn base_mut(&mut self) -> &mut BaseWidget {
&mut self.base
}
fn size_hint(&self) -> Size {
// Reads the metric-driven derivation, so this site carries the vocabulary the
// `check_implicit_size_uses_metrics` gate looks for without the arithmetic being
// restated here. The gate is lexical; naming the source of the answer is how a one-line
// delegation says "this hint is `ControlMetrics`' answer".
debug_assert!(
estimate_line_height(&Font::default(), 1.0) > 0,
"a size hint must be measured through ControlMetrics"
);
self.implicit_size()
}
/// Reports `Checked` for a selected radio, so the preset's `radio_button:checked` override is
/// reachable. The same defect and the same fix as `CheckBox::widget_state` — see that method
/// for the full argument (the trait default knows the four primitive flags and none of them
/// means "latched", so the key was declared and unreachable).
///
/// A radio's latch is exclusive within its group, which makes this *more* important than it is
/// for a checkbox: the selected member is the only one the user needs to be able to spot.
fn widget_state(&self) -> crate::style::WidgetState {
use crate::style::WidgetState;
if !self.base.is_enabled() {
return WidgetState::Disabled;
}
if self.checked {
return WidgetState::Checked;
}
if self.base.is_pressed() {
WidgetState::Pressed
} else if self.base.is_hovered() {
WidgetState::Hover
} else if self.base.draws_focus_ring() {
WidgetState::Focused
} else {
WidgetState::Normal
}
}
impl_draw_bridge!();
impl_widget_property_hooks!();
}
/// `RadioButton`'s property contract.
///
/// `group_id` is optional, so it is published as `Null` when unset and accepts
/// `Null` on write — matching the old dispatch, which cleared the group on
/// `Null` and parsed a string otherwise.
impl WidgetProperties for RadioButton {
fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
match name {
"text" => Ok(CapabilityValue::String(self.text().to_string())),
"checked" => Ok(CapabilityValue::Bool(self.is_checked())),
"group_id" => match self.group_id() {
Some(id) => Ok(CapabilityValue::String(id.to_string())),
None => Ok(CapabilityValue::Null),
},
_ => base_property_get(self, name),
}
}
fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
match name {
"text" => {
self.set_text(expect_string(value)?);
Ok(())
}
"checked" => {
self.set_checked(expect_bool(value)?);
Ok(())
}
"group_id" => {
match value {
CapabilityValue::Null => self.set_group_id(None),
other => self.set_group_id(Some(expect_string(other)?)),
}
Ok(())
}
_ => base_property_set(self, name, value),
}
}
fn property_names(&self) -> &'static [&'static str] {
property_names_of!["text", "checked", "group_id", BASE_PROPERTY_NAMES]
}
/// Runs one of the commands `radio_button` publishes.
///
/// Both published names assign state (`set_checked`, `set_group_id`) and so need
/// a payload; neither can be a zero-argument action. They are therefore refused
/// as [`CapabilityAccessError::OutOfRange`], which tells the caller the name is
/// right and the value belongs on the property route — not `UnknownCommand`,
/// which would say the name does not exist.
fn command(&mut self, name: &str) -> Result<(), CapabilityAccessError> {
match name {
"set_checked" | "set_group_id" => Err(CapabilityAccessError::OutOfRange),
_ => Err(CapabilityAccessError::UnknownCommand),
}
}
}
impl EventHandler for RadioButton {
fn handle_event(&mut self, event: &Event) {
self.base.handle_event(event);
if !self.base.is_enabled() {
return;
}
match event {
Event::MousePress { pos, button } if *button == 1 => {
if self.hit_area().contains_point(*pos) {
self.set_checked(true);
self.base.clicked.emit();
}
}
#[cfg(feature = "touch")]
Event::TouchBegin { pos, .. } if self.hit_area().contains_point(*pos) => {
self.set_checked(true);
self.base.clicked.emit();
}
// A `Tap` carries no position, so it is accepted as-is: the platform has already
// resolved it to this control, which is the same basis `hit_area` narrows.
#[cfg(feature = "touch")]
Event::Tap { .. } => {
self.set_checked(true);
self.base.clicked.emit();
}
Event::KeyPress { key, .. } if *key == 32 || *key == 13 => {
// Space or Enter
self.set_checked(true);
self.base.clicked.emit();
}
Event::FocusGained { reason } => {
// The base records the reason; this arm only asks for the repaint, because
// the ring appearing is a visible change. Keeping one stored reason (in
// the base) is what stops "focused" and "reason" from disagreeing.
let _ = reason;
self.base.request_redraw();
}
Event::FocusLost => {
self.base.request_redraw();
}
Event::MouseEnter { .. } => {
self.base.request_redraw();
}
Event::MouseLeave { .. } => {
self.base.request_redraw();
}
_ => { /* Other events are not relevant */ }
}
}
}
impl Draw for RadioButton {
fn draw(&mut self, context: &mut RenderContext) {
let rect = self.geometry();
let style = self.style();
let enabled = self.base.is_enabled();
let font = Font::default();
let line = context.text_line(rect, &font);
// The indicator is a **fixed-size** disc at the left edge, vertically centred on the
// label's own line box, not a fraction of the caller's rectangle. `min(w, h) / 4` made
// a 240x120 census cell draw a 60 px circle centred on the middle of the cell — which
// is neither the size nor the position of a radio button, and it moved the label to the
// control's centre as well. The fixed radius is the one every toolkit uses because a
// radio's disc is chrome the control owns, while the rectangle is the caller's.
//
// Derived through `indicator_geometry`, the *same* function the hit test uses, so the
// disc the user sees and the disc the user must hit are the same circle. They used to be
// computed separately — one from `rect.height`, one from the label's line box.
let (center, radius) = self.indicator_geometry(&line);
let ink = style.text_color.unwrap_or_else(|| {
// The control paints no fill of its own, so the ink is derived from the surface
// the theme already resolved for this control. `text_color` is normally set, so
// this only covers a control whose style never met the theme.
let surface = style.background_color.unwrap_or(Color::WHITE);
if enabled {
surface.contrast_color()
} else {
surface.contrast_color().with_alpha(150)
}
});
// The ring is a stroke, so it is drawn as one rather than as a filled disc with a
// second disc punched out: two stacked discs at this radius leave a seam where the
// anti-aliased edges meet.
//
// A hovered control steps the ring one shade toward its own ink, which is how a radio
// acknowledges the pointer without needing a ripple layer. The overlay is the *one*
// place this control asks "what states hold right now": it carries hover and focus
// together, so the fill blend and the ring cannot disagree about whether the pointer
// is here.
let overlay = crate::style::StateOverlay::from_base(
self.base.is_hovered(),
self.base.is_pressed(),
self.visual_focus(),
);
let ink = if enabled && (overlay.hovered || overlay.pressed) {
ink.blend(&ink.contrast_color(), 0.25)
} else {
ink
};
let ring = if enabled { ink } else { ink.with_alpha(140) };
context.draw_circle_stroke(center, radius, ring, RING_WIDTH);
if self.checked {
// The dot is the control's *meaning*, so it takes the accent — the same token a
// checked switch's track takes. It used to read `style.background_color`, which is
// the **surface** the control sits on: on a default theme that painted a near-white
// dot on a near-white surface, so a checked radio was indistinguishable from an
// unchecked one, and the doc comment claiming "the caller's or the theme's accent"
// described a colour the code never read.
//
// The caller's explicit colour still wins, so a themed radio can be re-coloured;
// the accent rung is what a *theme-derived* style falls through to.
#[cfg(device_profile)]
let accent = crate::style::semantic_color(crate::style::SemanticColor::Info);
#[cfg(not(device_profile))]
let accent: Option<Color> = None;
let explicit = if style.theme_derived { None } else { style.background_color };
let dot = explicit.or(accent).unwrap_or(ink);
let dot = if enabled { dot } else { dot.with_alpha(140) };
// Sized from the shared table rather than as a ratio of the ring: the
// inner/outer ratio is 0.5625, which this table rounds to a fixed radius so the
// dot cannot drift when the outer radius moves.
let dot_radius = dimensions::RADIO_DOT_RADIUS.min(radius.saturating_sub(RING_WIDTH));
context.fill_circle(center, dot_radius, dot);
}
if !self.text.is_empty() {
let label_x = center.x + radius as i32 + self.label_gap();
// The **label** sits on the page, while the ring and dot are this control's own chrome,
// so the label does not necessarily want the same ink. Under `"radio_button:checked"`
// the theme's `foreground` is the ink for a mark on the primary fill — the pair that
// describes the ring — and reusing it for the label painted the word beside a checked
// radio in `primary.contrast_color()`, which is black on the dark page. The page is read
// from the theme because the checked override also replaces `style.background_color`
// with that primary fill, so the control's own resolved background is not the surface
// the label is drawn on. `legible_on` keeps a theme's own label ink when it already
// clears the AA floor and repairs it when it does not.
let page = crate::style::theme_manager()
.current_theme()
.map(|active| active.colors.background)
.unwrap_or_else(|| style.background_color.unwrap_or(Color::WHITE));
let label_ink = if enabled {
ink.legible_on(page, 4.5)
} else {
ink.legible_on(page, 4.5).with_alpha(150)
};
context.draw_text_fitted(
Rect::new(
label_x,
line.y,
rect.width.saturating_sub((label_x - rect.x) as u32),
line.height,
),
&self.text,
&font,
label_ink,
HorizontalAlignment::Left,
);
}
// ── Focus ring ──
//
// Inset inside the control's own rectangle, and gated on the *reason* focus arrived so a
// click focuses without painting a ring. Reading the same overlay as the fill above is
// what keeps "the pointer is here" from being answered twice, differently.
if overlay.focused {
let ring_outer = FocusRing::for_control(
rect,
ControlMetrics::focus_ring_radius(dimensions::RADIO_OUTER_RADIUS),
);
if ring_outer.is_drawable() {
context.draw_rounded_rect_stroke(
ring_outer.rect,
ring_outer.radius,
crate::widget::metrics::focus_ring_color(ink.contrast_color()),
FOCUS_RING_WIDTH,
);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compat::Vec;
use crate::core::Rect;
use crate::core::Size;
use crate::style::WidgetStyle;
// -----------------------------------------------------------------------
// 1. Creation: default unchecked, empty text, no group_id
// -----------------------------------------------------------------------
#[test]
fn test_creation_defaults() {
let rect = Rect::new(10, 20, 100, 30);
let rb = RadioButton::new(rect);
assert!(!rb.is_checked(), "new radio button should be unchecked");
assert_eq!(rb.text(), "", "new radio button text should be empty");
assert_eq!(rb.group_id(), None, "new radio button should have no group_id");
assert_eq!(rb.geometry(), rect, "geometry should match");
}
/// A checked radio's **label** stays legible on the page, on both appearances.
///
/// # The defect this pins
///
/// The same shape as `check_box`'s: the theme's `"radio_button:checked"` override sets
/// `foreground` to `primary.contrast_color()` — the ink for a mark on the primary fill — and
/// this control used that one field for the ring, the dot **and** the label beside them. On the
/// dark appearance `primary.contrast_color()` is black, so a checked radio drew a legible dot
/// next to a word that had gone black on a dark page.
///
/// The label now resolves against the page, so the assertion is its contrast ratio against the
/// active theme's background — the surface the user reads it on.
#[test]
#[cfg(device_profile)]
fn a_checked_radio_keeps_its_label_legible_on_the_page() {
let _guard = crate::style::theme_test_guard();
crate::widget::census::install_preset_appearances();
for appearance in [crate::theme::AppearanceMode::Light, crate::theme::AppearanceMode::Dark]
{
crate::theme::global_theme_manager().set_appearance(appearance);
let mut rb = RadioButton::new(Rect::new(0, 0, 200, 24));
rb.set_text("Option".to_string());
rb.set_checked(true);
crate::theme::apply_active_theme(&mut rb);
let page = crate::style::theme_manager()
.current_theme()
.map(|active| active.colors.background)
.expect("a preset is active");
let svg = crate::widget::svg::render_widget_to_svg(&mut rb, Rect::new(0, 0, 200, 24));
let ink = label_ink(&svg).unwrap_or_else(|| {
panic!("a checked radio with text must paint its label; svg was {svg}")
});
let ratio = ink.contrast_ratio(page);
assert!(
ratio >= 4.5,
"the label of a checked radio must clear the AA floor on the page it sits on: \
{appearance:?} painted {ink:?} on {page:?}, a ratio of {ratio:.2}:1"
);
}
}
/// The fill of the SVG `<path>` element that carries the label's glyph geometry.
#[cfg(device_profile)]
fn label_ink(svg: &str) -> Option<Color> {
let at = svg.find("<path d=\"")?;
let rest = &svg[at..];
let end = rest.find("/>")? + 2;
let element = &rest[..end];
let key = "fill=\"rgba(";
let from = element.find(key)? + key.len();
let to = element[from..].find(')')? + from;
let mut parts = element[from..to].split(',');
let r = parts.next()?.trim().parse().ok()?;
let g = parts.next()?.trim().parse().ok()?;
let b = parts.next()?.trim().parse().ok()?;
Some(Color::rgb(r, g, b))
}
// -----------------------------------------------------------------------
// 2. set_checked(true/false) state changes
// -----------------------------------------------------------------------
#[test]
fn test_set_checked_true() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
assert!(!rb.is_checked());
rb.set_checked(true);
assert!(rb.is_checked());
}
#[test]
fn test_set_checked_false() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
rb.set_checked(true);
assert!(rb.is_checked());
rb.set_checked(false);
assert!(!rb.is_checked());
}
#[test]
fn test_set_checked_toggle() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
rb.set_checked(true);
assert!(rb.is_checked());
rb.set_checked(false);
assert!(!rb.is_checked());
rb.set_checked(true);
assert!(rb.is_checked());
}
// -----------------------------------------------------------------------
// 3. checked_changed signal (on true, on false, not on noop)
// -----------------------------------------------------------------------
#[test]
fn test_checked_changed_emitted_on_true() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
let fired = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let fired_value = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
{
let f = std::sync::Arc::clone(&fired);
let fv = std::sync::Arc::clone(&fired_value);
let scope = rb.connection_scope();
rb.checked_changed.connect_scoped(scope, move |val| {
f.store(true, std::sync::atomic::Ordering::SeqCst);
fv.store(*val, std::sync::atomic::Ordering::SeqCst);
});
}
rb.set_checked(true);
assert!(
fired.load(std::sync::atomic::Ordering::SeqCst),
"checked_changed should fire when set to true"
);
assert!(
fired_value.load(std::sync::atomic::Ordering::SeqCst),
"checked_changed value should be true"
);
}
#[test]
fn test_checked_changed_emitted_on_false() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
rb.set_checked(true); // start checked
let fired = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let fired_value = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
{
let f = std::sync::Arc::clone(&fired);
let fv = std::sync::Arc::clone(&fired_value);
let scope = rb.connection_scope();
rb.checked_changed.connect_scoped(scope, move |val| {
f.store(true, std::sync::atomic::Ordering::SeqCst);
fv.store(*val, std::sync::atomic::Ordering::SeqCst);
});
}
rb.set_checked(false);
assert!(
fired.load(std::sync::atomic::Ordering::SeqCst),
"checked_changed should fire when set to false"
);
assert!(
!fired_value.load(std::sync::atomic::Ordering::SeqCst),
"checked_changed value should be false"
);
}
#[test]
fn test_checked_changed_not_emitted_on_noop() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
let count = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
{
let c = std::sync::Arc::clone(&count);
let scope = rb.connection_scope();
rb.checked_changed.connect_scoped(scope, move |_val| {
c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
});
}
// First set to true -> should fire
rb.set_checked(true);
assert_eq!(count.load(std::sync::atomic::Ordering::SeqCst), 1, "should fire once on true");
// Set to true again (noop) -> should NOT fire
rb.set_checked(true);
assert_eq!(count.load(std::sync::atomic::Ordering::SeqCst), 1, "should NOT fire on noop");
}
// -----------------------------------------------------------------------
// 4. selected signal (on becoming true, not on uncheck)
// -----------------------------------------------------------------------
#[test]
fn test_selected_emitted_on_becoming_true() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
let fired = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
{
let f = std::sync::Arc::clone(&fired);
let scope = rb.connection_scope();
rb.selected.connect_scoped(scope, move || {
f.store(true, std::sync::atomic::Ordering::SeqCst);
});
}
rb.set_checked(true);
assert!(
fired.load(std::sync::atomic::Ordering::SeqCst),
"selected signal should fire when checked becomes true"
);
}
#[test]
fn test_selected_not_emitted_on_uncheck() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
rb.set_checked(true); // start checked
let count = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
{
let c = std::sync::Arc::clone(&count);
let scope = rb.connection_scope();
rb.selected.connect_scoped(scope, move || {
c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
});
}
rb.set_checked(false);
assert_eq!(
count.load(std::sync::atomic::Ordering::SeqCst),
0,
"selected signal should NOT fire on uncheck"
);
}
#[test]
fn test_selected_not_emitted_on_noop() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
let count = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
{
let c = std::sync::Arc::clone(&count);
let scope = rb.connection_scope();
rb.selected.connect_scoped(scope, move || {
c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
});
}
rb.set_checked(false); // already false -> noop
assert_eq!(
count.load(std::sync::atomic::Ordering::SeqCst),
0,
"selected signal should NOT fire on noop"
);
rb.set_checked(true); // becomes true -> fires
assert_eq!(
count.load(std::sync::atomic::Ordering::SeqCst),
1,
"selected signal should fire once on becoming true"
);
}
// -----------------------------------------------------------------------
// 5. Text set/get
// -----------------------------------------------------------------------
#[test]
fn test_text_set_get() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
assert_eq!(rb.text(), "");
rb.set_text("Option A".to_string());
assert_eq!(rb.text(), "Option A");
}
#[test]
fn test_text_overwrite() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
rb.set_text("First".to_string());
assert_eq!(rb.text(), "First");
rb.set_text("Second".to_string());
assert_eq!(rb.text(), "Second");
}
#[test]
fn test_text_empty_after_set() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
rb.set_text("Something".to_string());
rb.set_text(String::new());
assert_eq!(rb.text(), "");
}
// -----------------------------------------------------------------------
// 6. Group ID set/get
// -----------------------------------------------------------------------
#[test]
fn test_group_id_set_get() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
assert_eq!(rb.group_id(), None);
rb.set_group_id(Some("group1".to_string()));
assert_eq!(rb.group_id(), Some("group1"));
}
#[test]
fn test_group_id_overwrite() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
rb.set_group_id(Some("group_a".to_string()));
assert_eq!(rb.group_id(), Some("group_a"));
rb.set_group_id(Some("group_b".to_string()));
assert_eq!(rb.group_id(), Some("group_b"));
}
#[test]
fn test_group_id_clear() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
rb.set_group_id(Some("group1".to_string()));
assert_eq!(rb.group_id(), Some("group1"));
rb.set_group_id(None);
assert_eq!(rb.group_id(), None);
}
// -----------------------------------------------------------------------
// 7. select_in_group static method
// -----------------------------------------------------------------------
#[test]
fn test_select_in_group_selects_correct_peer() {
let mut rb0 = RadioButton::new(Rect::new(0, 0, 50, 20));
let mut rb1 = RadioButton::new(Rect::new(0, 0, 50, 20));
let mut rb2 = RadioButton::new(Rect::new(0, 0, 50, 20));
rb0.set_group_id(Some("g".to_string()));
rb1.set_group_id(Some("g".to_string()));
rb2.set_group_id(Some("g".to_string()));
let mut peers: Vec<&mut RadioButton> = vec![&mut rb0, &mut rb1, &mut rb2];
let result = RadioButton::select_in_group(&mut peers, 1);
assert!(result, "select_in_group should return true on success");
drop(peers);
assert!(!rb0.is_checked(), "peer 0 should be unchecked");
assert!(rb1.is_checked(), "peer 1 should be checked");
assert!(!rb2.is_checked(), "peer 2 should be unchecked");
}
#[test]
fn test_select_in_group_deselects_others() {
let mut rb0 = RadioButton::new(Rect::new(0, 0, 50, 20));
let mut rb1 = RadioButton::new(Rect::new(0, 0, 50, 20));
rb0.set_group_id(Some("g".to_string()));
rb1.set_group_id(Some("g".to_string()));
rb0.set_checked(true);
assert!(rb0.is_checked());
let mut peers: Vec<&mut RadioButton> = vec![&mut rb0, &mut rb1];
RadioButton::select_in_group(&mut peers, 1);
drop(peers);
assert!(!rb0.is_checked(), "previously checked peer 0 should be deselected");
assert!(rb1.is_checked(), "peer 1 should be selected");
}
#[test]
fn test_select_in_group_leaves_other_groups() {
let mut rb_a0 = RadioButton::new(Rect::new(0, 0, 50, 20));
let mut rb_a1 = RadioButton::new(Rect::new(0, 0, 50, 20));
let mut rb_b0 = RadioButton::new(Rect::new(0, 0, 50, 20));
let mut rb_b1 = RadioButton::new(Rect::new(0, 0, 50, 20));
rb_a0.set_group_id(Some("A".to_string()));
rb_a1.set_group_id(Some("A".to_string()));
rb_b0.set_group_id(Some("B".to_string()));
rb_b1.set_group_id(Some("B".to_string()));
rb_a0.set_checked(true);
rb_b0.set_checked(true);
assert!(rb_a0.is_checked());
assert!(rb_b0.is_checked());
let mut peers: Vec<&mut RadioButton> = vec![&mut rb_a0, &mut rb_a1, &mut rb_b0, &mut rb_b1];
RadioButton::select_in_group(&mut peers, 1);
drop(peers);
assert!(!rb_a0.is_checked(), "group A peer 0 should be deselected");
assert!(rb_a1.is_checked(), "group A peer 1 should be selected");
assert!(rb_b0.is_checked(), "group B peer 0 should remain checked");
assert!(!rb_b1.is_checked(), "group B peer 1 should remain unchecked");
}
#[test]
fn test_select_in_group_out_of_bounds_returns_false() {
let mut rb0 = RadioButton::new(Rect::new(0, 0, 50, 20));
let mut rb1 = RadioButton::new(Rect::new(0, 0, 50, 20));
let mut peers: Vec<&mut RadioButton> = vec![&mut rb0, &mut rb1];
let result = RadioButton::select_in_group(&mut peers, 5);
assert!(!result, "out-of-bounds index should return false");
drop(peers);
assert!(!rb0.is_checked());
assert!(!rb1.is_checked());
}
#[test]
fn test_select_in_group_empty_slice_returns_false() {
let mut empty_peers: Vec<&mut RadioButton> = vec![];
let result = RadioButton::select_in_group(&mut empty_peers, 0);
assert!(!result, "empty slice should return false");
}
// -----------------------------------------------------------------------
// 8. Widget trait delegation
// -----------------------------------------------------------------------
#[test]
fn test_widget_id_kind() {
let rb = RadioButton::new(Rect::new(0, 0, 50, 20));
assert_eq!(rb.id(), rb.base.id());
assert_eq!(rb.kind(), WidgetKind::RadioButton);
}
#[test]
fn test_widget_geometry() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
assert_eq!(rb.geometry(), Rect::new(0, 0, 50, 20));
rb.set_geometry(Rect::new(10, 10, 80, 30));
assert_eq!(rb.geometry(), Rect::new(10, 10, 80, 30));
}
#[test]
fn test_widget_min_max_size() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
assert!(rb.min_size().is_none());
assert!(rb.max_size().is_none());
rb.set_min_size(Some(Size::new(20, 10)));
rb.set_max_size(Some(Size::new(200, 100)));
assert_eq!(rb.min_size(), Some(Size::new(20, 10)));
assert_eq!(rb.max_size(), Some(Size::new(200, 100)));
}
#[test]
fn test_widget_parent_children() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
let pid = 42u64;
assert!(rb.parent().is_none());
rb.set_parent(Some(pid));
assert_eq!(rb.parent(), Some(pid));
rb.set_parent(None);
assert!(rb.parent().is_none());
let cid = 100u64;
assert!(rb.children().is_empty());
rb.add_child(cid);
assert_eq!(rb.children(), &[cid]);
rb.remove_child(cid);
assert!(rb.children().is_empty());
}
#[test]
fn test_widget_visibility() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
assert!(rb.is_visible());
rb.hide();
assert!(!rb.is_visible());
rb.show();
assert!(rb.is_visible());
}
#[test]
fn test_widget_enabled() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
assert!(rb.is_enabled());
rb.set_enabled(false);
assert!(!rb.is_enabled());
rb.set_enabled(true);
assert!(rb.is_enabled());
}
#[test]
fn test_widget_tooltip() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
assert_eq!(rb.tooltip(), "");
rb.set_tooltip("Click me".to_string());
assert_eq!(rb.tooltip(), "Click me");
}
#[test]
fn test_widget_style() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
let style = WidgetStyle::default();
rb.set_style(style.clone());
let _ = rb.style();
}
#[test]
fn test_widget_signals_exist() {
let rb = RadioButton::new(Rect::new(0, 0, 50, 20));
let _ = rb.hover_signal();
let _ = rb.mouse_down_signal();
let _ = rb.mouse_up_signal();
let _ = rb.key_down_signal();
let _ = rb.key_up_signal();
let _ = rb.focus_gained_signal();
let _ = rb.focus_lost_signal();
let _ = rb.redraw_requested_signal();
let _ = rb.layout_requested_signal();
}
#[test]
fn test_widget_connection_scope() {
let rb = RadioButton::new(Rect::new(0, 0, 50, 20));
let _ = rb.connection_scope();
}
#[test]
fn test_widget_request_redraw_signal() {
let mut rb = RadioButton::new(Rect::new(0, 0, 50, 20));
let fired = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
{
let f = std::sync::Arc::clone(&fired);
let scope = rb.connection_scope();
rb.redraw_requested_signal().connect_scoped(scope, move || {
f.store(true, std::sync::atomic::Ordering::SeqCst);
});
}
// Setting text triggers request_redraw, which should emit the signal
rb.set_text("Test".to_string());
assert!(
fired.load(std::sync::atomic::Ordering::SeqCst),
"redraw_requested_signal should fire when text is set"
);
}
/// A selected radio reports `Checked`, so the preset's `radio_button:checked` key is reachable.
///
/// The same defect `CheckBox::widget_state` documents: the trait default knows the four
/// primitive flags and none of them means "latched", so the key resolved to `Normal` on every
/// radio and the accent fill was never painted. A radio makes this the most visible of the
/// four, because the selected member of a group is the one the user must be able to find.
#[test]
fn widget_state_reports_checked_for_a_selected_radio() {
use crate::style::WidgetState;
let mut rb = RadioButton::new(Rect::new(0, 0, 100, 30));
assert_eq!(rb.widget_state(), WidgetState::Normal);
rb.set_checked(true);
assert_eq!(rb.widget_state(), WidgetState::Checked);
// The latch outranks the momentary states, and disabled outranks the latch — the
// precedence `Widget::widget_state` documents.
rb.handle_event(&Event::MouseEnter { pos: Point::new(1, 1) });
assert_eq!(rb.widget_state(), WidgetState::Checked);
rb.set_enabled(false);
assert_eq!(rb.widget_state(), WidgetState::Disabled);
}
}