1use presentar_core::{
4 widget::{
5 AccessibleRole, Brick, BrickAssertion, BrickBudget, BrickVerification, LayoutResult,
6 TextStyle,
7 },
8 Canvas, Color, Constraints, Event, MouseButton, Point, Rect, Size, TypeId, Widget,
9};
10use serde::{Deserialize, Serialize};
11use std::any::Any;
12use std::time::Duration;
13
14#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
16pub struct RadioOption {
17 pub value: String,
19 pub label: String,
21 pub disabled: bool,
23}
24
25impl RadioOption {
26 #[must_use]
28 pub fn new(value: impl Into<String>, label: impl Into<String>) -> Self {
29 Self {
30 value: value.into(),
31 label: label.into(),
32 disabled: false,
33 }
34 }
35
36 #[must_use]
38 pub const fn disabled(mut self) -> Self {
39 self.disabled = true;
40 self
41 }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct RadioChanged {
47 pub value: String,
49 pub index: usize,
51}
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
55pub enum RadioOrientation {
56 #[default]
58 Vertical,
59 Horizontal,
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct RadioGroup {
66 options: Vec<RadioOption>,
68 selected: Option<usize>,
70 orientation: RadioOrientation,
72 spacing: f32,
74 radio_size: f32,
76 label_gap: f32,
78 border_color: Color,
80 fill_color: Color,
82 label_color: Color,
84 disabled_color: Color,
86 accessible_name_value: Option<String>,
88 test_id_value: Option<String>,
90 #[serde(skip)]
92 bounds: Rect,
93}
94
95impl Default for RadioGroup {
96 fn default() -> Self {
97 Self {
98 options: Vec::new(),
99 selected: None,
100 orientation: RadioOrientation::Vertical,
101 spacing: 8.0,
102 radio_size: 20.0,
103 label_gap: 8.0,
104 border_color: Color::new(0.6, 0.6, 0.6, 1.0),
105 fill_color: Color::new(0.2, 0.47, 0.96, 1.0),
106 label_color: Color::new(0.1, 0.1, 0.1, 1.0),
107 disabled_color: Color::new(0.6, 0.6, 0.6, 1.0),
108 accessible_name_value: None,
109 test_id_value: None,
110 bounds: Rect::default(),
111 }
112 }
113}
114
115impl RadioGroup {
116 #[must_use]
118 pub fn new() -> Self {
119 Self::default()
120 }
121
122 #[must_use]
124 pub fn option(mut self, option: RadioOption) -> Self {
125 self.options.push(option);
126 self
127 }
128
129 #[must_use]
131 pub fn options(mut self, options: impl IntoIterator<Item = RadioOption>) -> Self {
132 self.options.extend(options);
133 self
134 }
135
136 #[must_use]
138 pub fn selected(mut self, value: &str) -> Self {
139 self.selected = self.options.iter().position(|o| o.value == value);
140 self
141 }
142
143 #[must_use]
145 pub fn selected_index(mut self, index: usize) -> Self {
146 if index < self.options.len() {
147 self.selected = Some(index);
148 }
149 self
150 }
151
152 #[must_use]
154 pub const fn orientation(mut self, orientation: RadioOrientation) -> Self {
155 self.orientation = orientation;
156 self
157 }
158
159 #[must_use]
161 pub fn spacing(mut self, spacing: f32) -> Self {
162 self.spacing = spacing.max(0.0);
163 self
164 }
165
166 #[must_use]
168 pub fn radio_size(mut self, size: f32) -> Self {
169 self.radio_size = size.max(12.0);
170 self
171 }
172
173 #[must_use]
175 pub fn label_gap(mut self, gap: f32) -> Self {
176 self.label_gap = gap.max(0.0);
177 self
178 }
179
180 #[must_use]
182 pub const fn border_color(mut self, color: Color) -> Self {
183 self.border_color = color;
184 self
185 }
186
187 #[must_use]
189 pub const fn fill_color(mut self, color: Color) -> Self {
190 self.fill_color = color;
191 self
192 }
193
194 #[must_use]
196 pub const fn label_color(mut self, color: Color) -> Self {
197 self.label_color = color;
198 self
199 }
200
201 #[must_use]
203 pub fn accessible_name(mut self, name: impl Into<String>) -> Self {
204 self.accessible_name_value = Some(name.into());
205 self
206 }
207
208 #[must_use]
210 pub fn test_id(mut self, id: impl Into<String>) -> Self {
211 self.test_id_value = Some(id.into());
212 self
213 }
214
215 #[must_use]
217 pub fn get_options(&self) -> &[RadioOption] {
218 &self.options
219 }
220
221 #[must_use]
223 pub fn get_selected(&self) -> Option<&str> {
224 self.selected
225 .and_then(|i| self.options.get(i))
226 .map(|o| o.value.as_str())
227 }
228
229 #[must_use]
231 pub const fn get_selected_index(&self) -> Option<usize> {
232 self.selected
233 }
234
235 #[must_use]
237 pub fn get_selected_option(&self) -> Option<&RadioOption> {
238 self.selected.and_then(|i| self.options.get(i))
239 }
240
241 #[must_use]
243 pub fn is_selected(&self, value: &str) -> bool {
244 self.get_selected() == Some(value)
245 }
246
247 #[must_use]
249 pub fn is_index_selected(&self, index: usize) -> bool {
250 self.selected == Some(index)
251 }
252
253 #[must_use]
255 pub const fn has_selection(&self) -> bool {
256 self.selected.is_some()
257 }
258
259 #[must_use]
261 pub fn option_count(&self) -> usize {
262 self.options.len()
263 }
264
265 #[must_use]
267 pub fn is_empty(&self) -> bool {
268 self.options.is_empty()
269 }
270
271 pub fn set_selected(&mut self, value: &str) {
273 if let Some(index) = self.options.iter().position(|o| o.value == value) {
274 if !self.options[index].disabled {
275 self.selected = Some(index);
276 }
277 }
278 }
279
280 pub fn set_selected_index(&mut self, index: usize) {
282 if index < self.options.len() && !self.options[index].disabled {
283 self.selected = Some(index);
284 }
285 }
286
287 pub fn clear_selection(&mut self) {
289 self.selected = None;
290 }
291
292 pub fn select_next(&mut self) {
294 if self.options.is_empty() {
295 return;
296 }
297 let start = self.selected.map_or(0, |i| i + 1);
298 for offset in 0..self.options.len() {
299 let idx = (start + offset) % self.options.len();
300 if !self.options[idx].disabled {
301 self.selected = Some(idx);
302 return;
303 }
304 }
305 }
306
307 pub fn select_prev(&mut self) {
309 if self.options.is_empty() {
310 return;
311 }
312 let start = self.selected.map_or(self.options.len() - 1, |i| {
313 if i == 0 {
314 self.options.len() - 1
315 } else {
316 i - 1
317 }
318 });
319 for offset in 0..self.options.len() {
320 let idx = if start >= offset {
321 start - offset
322 } else {
323 self.options.len() - (offset - start)
324 };
325 if !self.options[idx].disabled {
326 self.selected = Some(idx);
327 return;
328 }
329 }
330 }
331
332 fn item_size(&self) -> Size {
334 let label_width = 100.0;
336 Size::new(
337 self.radio_size + self.label_gap + label_width,
338 self.radio_size.max(20.0),
339 )
340 }
341
342 fn option_rect(&self, index: usize) -> Rect {
344 let item = self.item_size();
345 match self.orientation {
346 RadioOrientation::Vertical => {
347 let y = (index as f32).mul_add(item.height + self.spacing, self.bounds.y);
348 Rect::new(self.bounds.x, y, self.bounds.width, item.height)
349 }
350 RadioOrientation::Horizontal => {
351 let x = (index as f32).mul_add(item.width + self.spacing, self.bounds.x);
352 Rect::new(x, self.bounds.y, item.width, item.height)
353 }
354 }
355 }
356
357 fn option_at_point(&self, x: f32, y: f32) -> Option<usize> {
359 for (i, _) in self.options.iter().enumerate() {
360 let rect = self.option_rect(i);
361 if x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height {
362 return Some(i);
363 }
364 }
365 None
366 }
367}
368
369impl Widget for RadioGroup {
370 fn type_id(&self) -> TypeId {
371 TypeId::of::<Self>()
372 }
373
374 fn measure(&self, constraints: Constraints) -> Size {
375 let item = self.item_size();
376 let count = self.options.len() as f32;
377
378 let preferred = match self.orientation {
379 RadioOrientation::Vertical => {
380 let total_spacing = if count > 1.0 {
381 self.spacing * (count - 1.0)
382 } else {
383 0.0
384 };
385 Size::new(item.width, count.mul_add(item.height, total_spacing))
386 }
387 RadioOrientation::Horizontal => {
388 let total_spacing = if count > 1.0 {
389 self.spacing * (count - 1.0)
390 } else {
391 0.0
392 };
393 Size::new(count.mul_add(item.width, total_spacing), item.height)
394 }
395 };
396
397 constraints.constrain(preferred)
398 }
399
400 fn layout(&mut self, bounds: Rect) -> LayoutResult {
401 self.bounds = bounds;
402 LayoutResult {
403 size: bounds.size(),
404 }
405 }
406
407 fn paint(&self, canvas: &mut dyn Canvas) {
408 for (i, option) in self.options.iter().enumerate() {
409 let rect = self.option_rect(i);
410 let is_selected = self.selected == Some(i);
411
412 let cx = rect.x + self.radio_size / 2.0;
414 let cy = rect.y + rect.height / 2.0;
415 let radius = self.radio_size / 2.0;
416
417 let border_rect = Rect::new(cx - radius, cy - radius, self.radio_size, self.radio_size);
419 let border_color = if option.disabled {
420 self.disabled_color
421 } else if is_selected {
422 self.fill_color
423 } else {
424 self.border_color
425 };
426 canvas.stroke_rect(border_rect, border_color, 2.0);
427
428 if is_selected {
430 let inner_radius = radius * 0.5;
431 let inner_rect = Rect::new(
432 cx - inner_radius,
433 cy - inner_radius,
434 inner_radius * 2.0,
435 inner_radius * 2.0,
436 );
437 canvas.fill_rect(inner_rect, self.fill_color);
438 }
439
440 let text_color = if option.disabled {
442 self.disabled_color
443 } else {
444 self.label_color
445 };
446
447 let text_style = TextStyle {
448 size: 14.0,
449 color: text_color,
450 ..TextStyle::default()
451 };
452
453 canvas.draw_text(
454 &option.label,
455 Point::new(rect.x + self.radio_size + self.label_gap, cy),
456 &text_style,
457 );
458 }
459 }
460
461 fn event(&mut self, event: &Event) -> Option<Box<dyn Any + Send>> {
462 if let Event::MouseDown {
463 position,
464 button: MouseButton::Left,
465 } = event
466 {
467 if let Some(index) = self.option_at_point(position.x, position.y) {
468 if !self.options[index].disabled && self.selected != Some(index) {
469 self.selected = Some(index);
470 return Some(Box::new(RadioChanged {
471 value: self.options[index].value.clone(),
472 index,
473 }));
474 }
475 }
476 }
477 None
478 }
479
480 fn children(&self) -> &[Box<dyn Widget>] {
481 &[]
482 }
483
484 fn children_mut(&mut self) -> &mut [Box<dyn Widget>] {
485 &mut []
486 }
487
488 fn is_interactive(&self) -> bool {
489 !self.options.is_empty()
490 }
491
492 fn is_focusable(&self) -> bool {
493 !self.options.is_empty()
494 }
495
496 fn accessible_name(&self) -> Option<&str> {
497 self.accessible_name_value.as_deref()
498 }
499
500 fn accessible_role(&self) -> AccessibleRole {
501 AccessibleRole::RadioGroup
502 }
503
504 fn test_id(&self) -> Option<&str> {
505 self.test_id_value.as_deref()
506 }
507}
508
509impl Brick for RadioGroup {
511 fn brick_name(&self) -> &'static str {
512 "RadioGroup"
513 }
514
515 fn assertions(&self) -> &[BrickAssertion] {
516 &[BrickAssertion::MaxLatencyMs(16)]
517 }
518
519 fn budget(&self) -> BrickBudget {
520 BrickBudget::uniform(16)
521 }
522
523 fn verify(&self) -> BrickVerification {
524 BrickVerification {
525 passed: self.assertions().to_vec(),
526 failed: vec![],
527 verification_time: Duration::from_micros(10),
528 }
529 }
530
531 fn to_html(&self) -> String {
532 r#"<div class="brick-radiogroup"></div>"#.to_string()
533 }
534
535 fn to_css(&self) -> String {
536 ".brick-radiogroup { display: block; }".to_string()
537 }
538}
539
540#[cfg(test)]
541mod tests {
542 use super::*;
543
544 #[test]
547 fn test_radio_option_new() {
548 let opt = RadioOption::new("val", "Label");
549 assert_eq!(opt.value, "val");
550 assert_eq!(opt.label, "Label");
551 assert!(!opt.disabled);
552 }
553
554 #[test]
555 fn test_radio_option_disabled() {
556 let opt = RadioOption::new("val", "Label").disabled();
557 assert!(opt.disabled);
558 }
559
560 #[test]
561 fn test_radio_option_equality() {
562 let opt1 = RadioOption::new("a", "A");
563 let opt2 = RadioOption::new("a", "A");
564 let opt3 = RadioOption::new("b", "B");
565 assert_eq!(opt1, opt2);
566 assert_ne!(opt1, opt3);
567 }
568
569 #[test]
572 fn test_radio_changed() {
573 let msg = RadioChanged {
574 value: "option1".to_string(),
575 index: 1,
576 };
577 assert_eq!(msg.value, "option1");
578 assert_eq!(msg.index, 1);
579 }
580
581 #[test]
584 fn test_radio_orientation_default() {
585 assert_eq!(RadioOrientation::default(), RadioOrientation::Vertical);
586 }
587
588 #[test]
591 fn test_radio_group_new() {
592 let group = RadioGroup::new();
593 assert!(group.is_empty());
594 assert_eq!(group.option_count(), 0);
595 assert!(!group.has_selection());
596 }
597
598 #[test]
599 fn test_radio_group_builder() {
600 let group = RadioGroup::new()
601 .option(RadioOption::new("a", "Option A"))
602 .option(RadioOption::new("b", "Option B"))
603 .option(RadioOption::new("c", "Option C"))
604 .selected("b")
605 .orientation(RadioOrientation::Horizontal)
606 .spacing(12.0)
607 .radio_size(24.0)
608 .label_gap(10.0)
609 .accessible_name("Choose option")
610 .test_id("radio-test");
611
612 assert_eq!(group.option_count(), 3);
613 assert_eq!(group.get_selected(), Some("b"));
614 assert_eq!(group.get_selected_index(), Some(1));
615 assert_eq!(Widget::accessible_name(&group), Some("Choose option"));
616 assert_eq!(Widget::test_id(&group), Some("radio-test"));
617 }
618
619 #[test]
620 fn test_radio_group_options_iter() {
621 let opts = vec![RadioOption::new("x", "X"), RadioOption::new("y", "Y")];
622 let group = RadioGroup::new().options(opts);
623 assert_eq!(group.option_count(), 2);
624 }
625
626 #[test]
627 fn test_radio_group_selected_index() {
628 let group = RadioGroup::new()
629 .option(RadioOption::new("a", "A"))
630 .option(RadioOption::new("b", "B"))
631 .selected_index(1);
632
633 assert_eq!(group.get_selected(), Some("b"));
634 }
635
636 #[test]
637 fn test_radio_group_selected_not_found() {
638 let group = RadioGroup::new()
639 .option(RadioOption::new("a", "A"))
640 .selected("nonexistent");
641
642 assert!(!group.has_selection());
643 }
644
645 #[test]
648 fn test_radio_group_get_selected_option() {
649 let group = RadioGroup::new()
650 .option(RadioOption::new("first", "First"))
651 .option(RadioOption::new("second", "Second"))
652 .selected("second");
653
654 let opt = group.get_selected_option().unwrap();
655 assert_eq!(opt.value, "second");
656 assert_eq!(opt.label, "Second");
657 }
658
659 #[test]
660 fn test_radio_group_is_selected() {
661 let group = RadioGroup::new()
662 .option(RadioOption::new("a", "A"))
663 .option(RadioOption::new("b", "B"))
664 .selected("a");
665
666 assert!(group.is_selected("a"));
667 assert!(!group.is_selected("b"));
668 }
669
670 #[test]
671 fn test_radio_group_is_index_selected() {
672 let group = RadioGroup::new()
673 .option(RadioOption::new("a", "A"))
674 .option(RadioOption::new("b", "B"))
675 .selected_index(0);
676
677 assert!(group.is_index_selected(0));
678 assert!(!group.is_index_selected(1));
679 }
680
681 #[test]
684 fn test_radio_group_set_selected() {
685 let mut group = RadioGroup::new()
686 .option(RadioOption::new("a", "A"))
687 .option(RadioOption::new("b", "B"));
688
689 group.set_selected("b");
690 assert_eq!(group.get_selected(), Some("b"));
691 }
692
693 #[test]
694 fn test_radio_group_set_selected_disabled() {
695 let mut group = RadioGroup::new()
696 .option(RadioOption::new("a", "A"))
697 .option(RadioOption::new("b", "B").disabled());
698
699 group.set_selected("b");
700 assert!(!group.has_selection()); }
702
703 #[test]
704 fn test_radio_group_set_selected_index() {
705 let mut group = RadioGroup::new()
706 .option(RadioOption::new("a", "A"))
707 .option(RadioOption::new("b", "B"));
708
709 group.set_selected_index(1);
710 assert_eq!(group.get_selected_index(), Some(1));
711 }
712
713 #[test]
714 fn test_radio_group_set_selected_index_out_of_bounds() {
715 let mut group = RadioGroup::new().option(RadioOption::new("a", "A"));
716
717 group.set_selected_index(10);
718 assert!(!group.has_selection());
719 }
720
721 #[test]
722 fn test_radio_group_clear_selection() {
723 let mut group = RadioGroup::new()
724 .option(RadioOption::new("a", "A"))
725 .selected("a");
726
727 assert!(group.has_selection());
728 group.clear_selection();
729 assert!(!group.has_selection());
730 }
731
732 #[test]
735 fn test_radio_group_select_next() {
736 let mut group = RadioGroup::new()
737 .option(RadioOption::new("a", "A"))
738 .option(RadioOption::new("b", "B"))
739 .option(RadioOption::new("c", "C"))
740 .selected_index(0);
741
742 group.select_next();
743 assert_eq!(group.get_selected_index(), Some(1));
744
745 group.select_next();
746 assert_eq!(group.get_selected_index(), Some(2));
747
748 group.select_next(); assert_eq!(group.get_selected_index(), Some(0));
750 }
751
752 #[test]
753 fn test_radio_group_select_next_skip_disabled() {
754 let mut group = RadioGroup::new()
755 .option(RadioOption::new("a", "A"))
756 .option(RadioOption::new("b", "B").disabled())
757 .option(RadioOption::new("c", "C"))
758 .selected_index(0);
759
760 group.select_next();
761 assert_eq!(group.get_selected_index(), Some(2));
762 }
763
764 #[test]
765 fn test_radio_group_select_next_no_selection() {
766 let mut group = RadioGroup::new()
767 .option(RadioOption::new("a", "A"))
768 .option(RadioOption::new("b", "B"));
769
770 group.select_next();
771 assert_eq!(group.get_selected_index(), Some(0));
772 }
773
774 #[test]
775 fn test_radio_group_select_prev() {
776 let mut group = RadioGroup::new()
777 .option(RadioOption::new("a", "A"))
778 .option(RadioOption::new("b", "B"))
779 .option(RadioOption::new("c", "C"))
780 .selected_index(2);
781
782 group.select_prev();
783 assert_eq!(group.get_selected_index(), Some(1));
784
785 group.select_prev();
786 assert_eq!(group.get_selected_index(), Some(0));
787
788 group.select_prev(); assert_eq!(group.get_selected_index(), Some(2));
790 }
791
792 #[test]
793 fn test_radio_group_select_prev_skip_disabled() {
794 let mut group = RadioGroup::new()
795 .option(RadioOption::new("a", "A"))
796 .option(RadioOption::new("b", "B").disabled())
797 .option(RadioOption::new("c", "C"))
798 .selected_index(2);
799
800 group.select_prev();
801 assert_eq!(group.get_selected_index(), Some(0));
802 }
803
804 #[test]
807 fn test_radio_group_spacing_min() {
808 let group = RadioGroup::new().spacing(-5.0);
809 assert_eq!(group.spacing, 0.0);
810 }
811
812 #[test]
813 fn test_radio_group_radio_size_min() {
814 let group = RadioGroup::new().radio_size(5.0);
815 assert_eq!(group.radio_size, 12.0);
816 }
817
818 #[test]
819 fn test_radio_group_label_gap_min() {
820 let group = RadioGroup::new().label_gap(-5.0);
821 assert_eq!(group.label_gap, 0.0);
822 }
823
824 #[test]
827 fn test_radio_group_type_id() {
828 let group = RadioGroup::new();
829 assert_eq!(Widget::type_id(&group), TypeId::of::<RadioGroup>());
830 }
831
832 #[test]
833 fn test_radio_group_measure_vertical() {
834 let group = RadioGroup::new()
835 .option(RadioOption::new("a", "A"))
836 .option(RadioOption::new("b", "B"))
837 .option(RadioOption::new("c", "C"))
838 .orientation(RadioOrientation::Vertical)
839 .radio_size(20.0)
840 .spacing(8.0);
841
842 let size = group.measure(Constraints::loose(Size::new(500.0, 500.0)));
843 assert!(size.height > 0.0);
845 assert!(size.width > 0.0);
846 }
847
848 #[test]
849 fn test_radio_group_measure_horizontal() {
850 let group = RadioGroup::new()
851 .option(RadioOption::new("a", "A"))
852 .option(RadioOption::new("b", "B"))
853 .orientation(RadioOrientation::Horizontal)
854 .spacing(8.0);
855
856 let size = group.measure(Constraints::loose(Size::new(500.0, 500.0)));
857 assert!(size.width > 0.0);
858 assert!(size.height > 0.0);
859 }
860
861 #[test]
862 fn test_radio_group_layout() {
863 let mut group = RadioGroup::new().option(RadioOption::new("a", "A"));
864 let bounds = Rect::new(10.0, 20.0, 200.0, 100.0);
865 let result = group.layout(bounds);
866 assert_eq!(result.size, Size::new(200.0, 100.0));
867 assert_eq!(group.bounds, bounds);
868 }
869
870 #[test]
871 fn test_radio_group_children() {
872 let group = RadioGroup::new();
873 assert!(group.children().is_empty());
874 }
875
876 #[test]
877 fn test_radio_group_is_interactive() {
878 let group = RadioGroup::new();
879 assert!(!group.is_interactive()); let group = RadioGroup::new().option(RadioOption::new("a", "A"));
882 assert!(group.is_interactive());
883 }
884
885 #[test]
886 fn test_radio_group_is_focusable() {
887 let group = RadioGroup::new();
888 assert!(!group.is_focusable()); let group = RadioGroup::new().option(RadioOption::new("a", "A"));
891 assert!(group.is_focusable());
892 }
893
894 #[test]
895 fn test_radio_group_accessible_role() {
896 let group = RadioGroup::new();
897 assert_eq!(group.accessible_role(), AccessibleRole::RadioGroup);
898 }
899
900 #[test]
901 fn test_radio_group_accessible_name() {
902 let group = RadioGroup::new().accessible_name("Select size");
903 assert_eq!(Widget::accessible_name(&group), Some("Select size"));
904 }
905
906 #[test]
907 fn test_radio_group_test_id() {
908 let group = RadioGroup::new().test_id("size-radio");
909 assert_eq!(Widget::test_id(&group), Some("size-radio"));
910 }
911
912 #[test]
915 fn test_radio_group_click_selects() {
916 let mut group = RadioGroup::new()
917 .option(RadioOption::new("a", "A"))
918 .option(RadioOption::new("b", "B"))
919 .radio_size(20.0)
920 .spacing(8.0);
921 group.bounds = Rect::new(0.0, 0.0, 200.0, 56.0);
922
923 let event = Event::MouseDown {
925 position: Point::new(10.0, 38.0),
926 button: MouseButton::Left,
927 };
928
929 let result = group.event(&event);
930 assert!(result.is_some());
931 assert_eq!(group.get_selected_index(), Some(1));
932
933 let msg = result.unwrap().downcast::<RadioChanged>().unwrap();
934 assert_eq!(msg.value, "b");
935 assert_eq!(msg.index, 1);
936 }
937
938 #[test]
939 fn test_radio_group_click_disabled_no_change() {
940 let mut group = RadioGroup::new()
941 .option(RadioOption::new("a", "A"))
942 .option(RadioOption::new("b", "B").disabled())
943 .radio_size(20.0)
944 .spacing(8.0);
945 group.bounds = Rect::new(0.0, 0.0, 200.0, 56.0);
946
947 let event = Event::MouseDown {
949 position: Point::new(10.0, 38.0),
950 button: MouseButton::Left,
951 };
952
953 let result = group.event(&event);
954 assert!(result.is_none());
955 assert!(!group.has_selection());
956 }
957
958 #[test]
959 fn test_radio_group_click_same_no_event() {
960 let mut group = RadioGroup::new()
961 .option(RadioOption::new("a", "A"))
962 .option(RadioOption::new("b", "B"))
963 .selected_index(0)
964 .radio_size(20.0);
965 group.bounds = Rect::new(0.0, 0.0, 200.0, 56.0);
966
967 let event = Event::MouseDown {
969 position: Point::new(10.0, 10.0),
970 button: MouseButton::Left,
971 };
972
973 let result = group.event(&event);
974 assert!(result.is_none());
975 }
976
977 #[test]
980 fn test_radio_group_colors() {
981 let group = RadioGroup::new()
982 .border_color(Color::RED)
983 .fill_color(Color::GREEN)
984 .label_color(Color::BLUE);
985
986 assert_eq!(group.border_color, Color::RED);
987 assert_eq!(group.fill_color, Color::GREEN);
988 assert_eq!(group.label_color, Color::BLUE);
989 }
990
991 #[test]
992 fn test_radio_group_right_click_no_select() {
993 let mut group = RadioGroup::new()
994 .option(RadioOption::new("a", "A"))
995 .radio_size(20.0);
996 group.bounds = Rect::new(0.0, 0.0, 200.0, 28.0);
997
998 let result = group.event(&Event::MouseDown {
999 position: Point::new(10.0, 10.0),
1000 button: MouseButton::Right,
1001 });
1002 assert!(group.selected.is_none());
1003 assert!(result.is_none());
1004 }
1005
1006 #[test]
1007 fn test_radio_group_click_outside_no_select() {
1008 let mut group = RadioGroup::new()
1009 .option(RadioOption::new("a", "A"))
1010 .radio_size(20.0);
1011 group.bounds = Rect::new(0.0, 0.0, 200.0, 28.0);
1012
1013 let result = group.event(&Event::MouseDown {
1014 position: Point::new(10.0, 100.0),
1015 button: MouseButton::Left,
1016 });
1017 assert!(group.selected.is_none());
1018 assert!(result.is_none());
1019 }
1020
1021 #[test]
1022 fn test_radio_group_click_with_offset_bounds() {
1023 let mut group = RadioGroup::new()
1024 .option(RadioOption::new("a", "A"))
1025 .option(RadioOption::new("b", "B"))
1026 .radio_size(20.0)
1027 .spacing(8.0);
1028 group.bounds = Rect::new(50.0, 100.0, 200.0, 56.0);
1029
1030 let result = group.event(&Event::MouseDown {
1031 position: Point::new(60.0, 138.0), button: MouseButton::Left,
1033 });
1034 assert_eq!(group.selected, Some(1));
1035 assert!(result.is_some());
1036 }
1037}