1#![allow(deprecated)]
7
8use std::fmt::{self, Debug};
9
10use waft_embedder::Color;
11use waft_foundation::{App, Handle, OptionRetained, Ref, ValueChanged};
12use waft_gestures::GestureTapCallback;
13use waft_painting::{AnyBorderSide, EdgeInsetsGeometry, ShapeBorderRef};
14use waft_services::MouseCursorRef;
15use waft_widgets::{
16 AnyFocusNode, AnyRadioClient, BuildContext, ExcludeFocus, FocusNode, FocusNodeLeaf,
17 FocusNodeRef, IntoWidget, KeyRef, MergeSemantics, RadioClient, RadioClientData, RadioGroup,
18 RadioGroupRegistry, RadioGroupRegistryRef, State, StateData, StatefulWidget, Transform,
19 WidgetRef, WidgetState, WidgetStatePropertyRef, WidgetStates, WidgetStatesController,
20};
21
22use crate::list_tile::{ListTile, ListTileControlAffinity, ListTileTitleAlignment};
23use crate::list_tile_theme::ListTileTheme;
24use crate::radio::Radio;
25use crate::radio_theme::RadioTheme;
26use crate::theme::Theme;
27use crate::theme_data::{MaterialTapTargetSize, VisualDensity};
28
29#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31enum RadioType {
32 Material,
33 Adaptive,
34}
35
36pub struct RadioListTile<T: Clone + PartialEq + 'static> {
92 pub key: Option<KeyRef>,
93 pub value: T,
95 pub group_value: Option<T>,
104 pub on_changed: Option<ValueChanged<Option<T>>>,
121 pub mouse_cursor: Option<MouseCursorRef>,
134 pub toggleable: bool,
151 pub active_color: Option<Color>,
155 pub fill_color: Option<WidgetStatePropertyRef<Option<Color>>>,
166 pub material_tap_target_size: Option<MaterialTapTargetSize>,
170 pub hover_color: Option<Color>,
172 pub overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>,
185 pub splash_radius: Option<f64>,
190 pub title: Option<WidgetRef>,
194 pub subtitle: Option<WidgetRef>,
198 pub secondary: Option<WidgetRef>,
202 pub is_three_line: Option<bool>,
208 pub dense: Option<bool>,
212 pub selected: bool,
221 pub control_affinity: Option<ListTileControlAffinity>,
223 pub autofocus: bool,
226 pub content_padding: Option<EdgeInsetsGeometry>,
233 pub shape: Option<ShapeBorderRef>,
236 pub tile_color: Option<Color>,
239 pub selected_tile_color: Option<Color>,
241 pub visual_density: Option<VisualDensity>,
243 pub focus_node: Option<FocusNodeRef>,
245 pub states_controller: Option<Ref<WidgetStatesController>>,
247 pub on_focus_change: Option<ValueChanged<bool>>,
252 pub enable_feedback: Option<bool>,
258 pub horizontal_title_gap: Option<f64>,
260 pub min_vertical_padding: Option<f64>,
262 pub min_leading_width: Option<f64>,
264 pub min_tile_height: Option<f64>,
266 radio_type: RadioType,
267 pub title_alignment: Option<ListTileTitleAlignment>,
280 pub internal_add_semantic_for_on_tap: bool,
283 pub use_cupertino_checkmark_style: bool,
293 pub radio_scale_factor: f64,
297 pub enabled: Option<bool>,
308 pub radio_background_color: Option<WidgetStatePropertyRef<Option<Color>>>,
317 pub radio_side: Option<AnyBorderSide>,
330 pub radio_inner_radius: Option<WidgetStatePropertyRef<Option<f64>>>,
338}
339
340macro_rules! radio_list_tile_setters {
342 ($($field:ident: $ty:ty => $name:literal),* $(,)?) => {
343 $(
344 #[doc = concat!("Dart `RadioListTile(", $name, ":)`.")]
345 pub fn $field(mut self, $field: impl Into<$ty>) -> RadioListTile<T> {
346 self.$field = Some($field.into());
347 self
348 }
349 )*
350 };
351}
352
353macro_rules! radio_list_tile_widget_setters {
355 ($($field:ident => $name:literal),* $(,)?) => {
356 $(
357 #[doc = concat!("Dart `RadioListTile(", $name, ":)`.")]
358 pub fn $field<K>(mut self, $field: impl IntoWidget<K>) -> RadioListTile<T> {
359 self.$field = Some($field.into_widget());
360 self
361 }
362 )*
363 };
364}
365
366impl<T: Clone + PartialEq + 'static> RadioListTile<T> {
367 pub fn new(value: T) -> RadioListTile<T> {
375 RadioListTile::with_type(value, RadioType::Material)
376 }
377
378 pub fn adaptive(value: T) -> RadioListTile<T> {
385 RadioListTile::with_type(value, RadioType::Adaptive)
386 }
387
388 fn with_type(value: T, radio_type: RadioType) -> RadioListTile<T> {
389 RadioListTile {
390 key: None,
391 value,
392 group_value: None,
393 on_changed: None,
394 mouse_cursor: None,
395 toggleable: false,
396 active_color: None,
397 fill_color: None,
398 material_tap_target_size: None,
399 hover_color: None,
400 overlay_color: None,
401 splash_radius: None,
402 title: None,
403 subtitle: None,
404 secondary: None,
405 is_three_line: None,
406 dense: None,
407 selected: false,
408 control_affinity: None,
409 autofocus: false,
410 content_padding: None,
411 shape: None,
412 tile_color: None,
413 selected_tile_color: None,
414 visual_density: None,
415 focus_node: None,
416 states_controller: None,
417 on_focus_change: None,
418 enable_feedback: None,
419 horizontal_title_gap: None,
420 min_vertical_padding: None,
421 min_leading_width: None,
422 min_tile_height: None,
423 radio_type,
424 title_alignment: None,
425 internal_add_semantic_for_on_tap: false,
426 use_cupertino_checkmark_style: false,
427 radio_scale_factor: 1.0,
428 enabled: None,
429 radio_background_color: None,
430 radio_side: None,
431 radio_inner_radius: None,
432 }
433 }
434
435 pub fn key(mut self, key: KeyRef) -> RadioListTile<T> {
437 self.key = Some(key);
438 self
439 }
440
441 #[deprecated(
443 note = "Use a RadioGroup ancestor to manage group value instead. This feature was \
444 deprecated after v3.32.0-0.0.pre."
445 )]
446 pub fn group_value(mut self, group_value: T) -> RadioListTile<T> {
447 self.group_value = Some(group_value);
448 self
449 }
450
451 #[deprecated(
453 note = "Use RadioGroup to handle value change instead. This feature was deprecated \
454 after v3.32.0-0.0.pre."
455 )]
456 pub fn on_changed(mut self, on_changed: ValueChanged<Option<T>>) -> RadioListTile<T> {
457 self.on_changed = Some(on_changed);
458 self
459 }
460
461 radio_list_tile_setters!(
462 mouse_cursor: MouseCursorRef => "mouseCursor",
463 active_color: Color => "activeColor",
464 fill_color: WidgetStatePropertyRef<Option<Color>> => "fillColor",
465 hover_color: Color => "hoverColor",
466 overlay_color: WidgetStatePropertyRef<Option<Color>> => "overlayColor",
467 splash_radius: f64 => "splashRadius",
468 material_tap_target_size: MaterialTapTargetSize => "materialTapTargetSize",
469 is_three_line: bool => "isThreeLine",
470 dense: bool => "dense",
471 control_affinity: ListTileControlAffinity => "controlAffinity",
472 content_padding: EdgeInsetsGeometry => "contentPadding",
473 shape: ShapeBorderRef => "shape",
474 tile_color: Color => "tileColor",
475 selected_tile_color: Color => "selectedTileColor",
476 visual_density: VisualDensity => "visualDensity",
477 focus_node: FocusNodeRef => "focusNode",
478 states_controller: Ref<WidgetStatesController> => "statesController",
479 on_focus_change: ValueChanged<bool> => "onFocusChange",
480 enable_feedback: bool => "enableFeedback",
481 horizontal_title_gap: f64 => "horizontalTitleGap",
482 min_vertical_padding: f64 => "minVerticalPadding",
483 min_leading_width: f64 => "minLeadingWidth",
484 min_tile_height: f64 => "minTileHeight",
485 title_alignment: ListTileTitleAlignment => "titleAlignment",
486 enabled: bool => "enabled",
487 radio_background_color: WidgetStatePropertyRef<Option<Color>> => "radioBackgroundColor",
488 radio_side: AnyBorderSide => "radioSide",
489 radio_inner_radius: WidgetStatePropertyRef<Option<f64>> => "radioInnerRadius",
490 );
491
492 radio_list_tile_widget_setters!(
493 title => "title",
494 subtitle => "subtitle",
495 secondary => "secondary",
496 );
497
498 pub fn toggleable(mut self, toggleable: bool) -> RadioListTile<T> {
500 self.toggleable = toggleable;
501 self
502 }
503
504 pub fn selected(mut self, selected: bool) -> RadioListTile<T> {
506 self.selected = selected;
507 self
508 }
509
510 pub fn autofocus(mut self, autofocus: bool) -> RadioListTile<T> {
512 self.autofocus = autofocus;
513 self
514 }
515
516 pub fn internal_add_semantic_for_on_tap(
518 mut self,
519 internal_add_semantic_for_on_tap: bool,
520 ) -> RadioListTile<T> {
521 self.internal_add_semantic_for_on_tap = internal_add_semantic_for_on_tap;
522 self
523 }
524
525 pub fn radio_scale_factor(mut self, radio_scale_factor: f64) -> RadioListTile<T> {
527 self.radio_scale_factor = radio_scale_factor;
528 self
529 }
530
531 pub fn use_cupertino_checkmark_style(
533 mut self,
534 use_cupertino_checkmark_style: bool,
535 ) -> RadioListTile<T> {
536 self.use_cupertino_checkmark_style = use_cupertino_checkmark_style;
537 self
538 }
539
540 pub fn effective_use_cupertino_checkmark_style(&self) -> bool {
544 match self.radio_type {
545 RadioType::Material => false,
546 RadioType::Adaptive => self.use_cupertino_checkmark_style,
547 }
548 }
549
550 #[deprecated(
555 note = "Use RadioGroup.groupValue to find which radio is checked. This feature was \
556 deprecated after v3.32.0-0.0.pre."
557 )]
558 pub fn checked(&self) -> bool {
559 self.group_value.as_ref() == Some(&self.value)
560 }
561}
562
563impl<T: Clone + PartialEq + 'static> Debug for RadioListTile<T> {
564 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
565 f.debug_struct("RadioListTile")
566 .field("toggleable", &self.toggleable)
567 .field("selected", &self.selected)
568 .finish_non_exhaustive()
569 }
570}
571
572impl<T: Clone + PartialEq + 'static> StatefulWidget for RadioListTile<T> {
573 type State = RadioListTileState<T>;
574
575 fn key(&self) -> Option<&KeyRef> {
576 self.key.as_ref()
577 }
578
579 fn debug_check_constructor(&self) {
580 debug_assert!(self.is_three_line != Some(true) || self.subtitle.is_some());
581 }
582
583 fn create_state(&self) -> RadioListTileState<T> {
584 RadioListTileState {
585 state: StateData::new(),
586 radio_client: RadioClientData::new(),
587 internal_focus_node: None,
588 radio_registry: None,
589 }
590 }
591}
592
593pub struct RadioListTileState<T: Clone + PartialEq + 'static> {
596 state: StateData<RadioListTile<T>>,
597 radio_client: RadioClientData<T>,
598 internal_focus_node: Option<Ref<FocusNode>>,
599 radio_registry: Option<Ref<RadioRegistry<T>>>,
600}
601
602impl<T: Clone + PartialEq + 'static> RadioListTileState<T> {
603 fn focus_node(self: Handle<Self>, app: &mut App) -> FocusNodeRef {
606 if let Some(focus_node) = self.widget(app).focus_node.clone() {
607 return focus_node;
608 }
609 if let Some(focus_node) = app.get(self).internal_focus_node.as_ref() {
610 return focus_node.erase();
611 }
612 let focus_node = FocusNode::new(app);
613 app.get_mut(self).internal_focus_node = Some(focus_node.clone());
614 focus_node.into_erased()
615 }
616
617 fn radio_registry(self: Handle<Self>, app: &mut App) -> RadioGroupRegistryRef<T> {
619 let registry = match app.get(self).radio_registry.as_ref().cloned() {
620 Some(registry) => registry,
621 None => {
622 let registry = app.create(RadioRegistry { state: self });
623 app.get_mut(self).radio_registry = Some(registry.clone());
624 registry
625 }
626 };
627 registry.erase()
628 }
629
630 fn checked(self: Handle<Self>, app: &App) -> bool {
631 Some(self.radio_value(app)) == self.effective_group_value(app)
632 }
633
634 fn effective_group_value(self: Handle<Self>, app: &App) -> Option<T> {
635 RadioClient::registry(self, app)
636 .and_then(|registry| registry.group_value(app))
637 .or_else(|| self.widget(app).group_value.clone())
638 }
639
640 fn handle_list_tile_tap(self: Handle<Self>, app: &mut App) {
641 if !self.widget(app).toggleable && self.checked(app) {
642 return;
643 }
644 let new_value = if self.checked(app) {
645 None
646 } else {
647 Some(self.radio_value(app))
648 };
649 self.handle_change(app, new_value);
650 }
651
652 fn handle_change(self: Handle<Self>, app: &mut App, value: Option<T>) {
653 if let Some(registry) = RadioClient::registry(self, app) {
654 registry.on_changed(app, value.clone());
655 }
656
657 if let Some(on_changed) = self.widget(app).on_changed.clone() {
658 on_changed(app, value);
659 }
660 }
661
662 fn build_radio(self: Handle<Self>, app: &mut App) -> Radio<T> {
664 let radio_value = self.radio_value(app);
665 let radio_registry = self.radio_registry(app);
666 let group_value = radio_registry.group_value(app);
667 let enabled = RadioClient::enabled(self, app);
668 let widget = self.widget(app);
669 let mut radio = match widget.radio_type {
670 RadioType::Material => Radio::new(radio_value),
671 RadioType::Adaptive => Radio::adaptive(radio_value)
672 .use_cupertino_checkmark_style(widget.effective_use_cupertino_checkmark_style()),
673 }
674 .toggleable(widget.toggleable)
675 .material_tap_target_size(
676 widget
677 .material_tap_target_size
678 .unwrap_or(MaterialTapTargetSize::ShrinkWrap),
679 )
680 .autofocus(widget.autofocus)
681 .enabled(enabled)
682 .group_registry(radio_registry);
683 radio.group_value = group_value;
684 radio.active_color = widget.active_color;
685 radio.fill_color = widget.fill_color.clone();
686 radio.mouse_cursor = widget.mouse_cursor.clone();
687 radio.hover_color = widget.hover_color;
688 radio.overlay_color = widget.overlay_color.clone();
689 radio.splash_radius = widget.splash_radius;
690 radio.background_color = widget.radio_background_color.clone();
691 radio.side = widget.radio_side.clone();
692 radio.inner_radius = widget.radio_inner_radius.clone();
693 radio
694 }
695}
696
697impl<T: Clone + PartialEq + 'static> RadioClient<T> for RadioListTileState<T> {
698 waft_widgets::radio_client_accessors!(T);
699
700 fn tristate(self: Handle<Self>, app: &App) -> bool {
701 self.widget(app).toggleable
702 }
703
704 fn radio_value(self: Handle<Self>, app: &App) -> T {
705 self.widget(app).value.clone()
706 }
707
708 fn enabled(self: Handle<Self>, app: &App) -> bool {
710 let widget = self.widget(app);
711 widget
712 .enabled
713 .unwrap_or(widget.on_changed.is_some() || RadioClient::registry(self, app).is_some())
714 }
715
716 fn focus_node(self: Handle<Self>, app: &App) -> AnyFocusNode {
717 self.widget(app)
718 .focus_node
719 .handle()
720 .or_else(|| {
721 app.get(self)
722 .internal_focus_node
723 .as_ref()
724 .map(|node| AnyFocusNode::from(node.handle()))
725 })
726 .expect("the tile has built, which makes its focus node")
727 }
728}
729
730impl<T: Clone + PartialEq + 'static> State for RadioListTileState<T> {
731 type Widget = RadioListTile<T>;
732 waft_widgets::state_accessors!();
733
734 fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
735 let context = self.context(app);
736 let registry = RadioGroup::<T>::maybe_of(app, context);
737 self.set_registry(app, registry);
738 }
739
740 fn dispose(self: Handle<Self>, app: &mut App) {
741 self.set_registry(app, None);
742 if let Some(focus_node) = app.get(self).internal_focus_node.handle() {
743 focus_node.dispose(app);
744 }
745 }
747
748 fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
749 debug_assert!(
750 !self.widget(app).enabled.unwrap_or(false)
751 || self.widget(app).on_changed.is_some()
752 || RadioGroup::<T>::maybe_of(app, context).is_some(),
753 "Radio is enabled but has no RadioListTile.onChange or registry above"
754 );
755 let mut control = ExcludeFocus::new(self.build_radio(app)).into_widget();
756
757 let radio_scale_factor = self.widget(app).radio_scale_factor;
758 if radio_scale_factor != 1.0 {
759 control = Transform::scale(Some(radio_scale_factor), None, None)
760 .child(control)
761 .into_widget();
762 }
763
764 let list_tile_theme = ListTileTheme::of(app, context);
765 let widget = self.widget(app);
766 let effective_control_affinity = widget
767 .control_affinity
768 .or(list_tile_theme.control_affinity)
769 .unwrap_or(ListTileControlAffinity::Platform);
770 let (leading, trailing) = match effective_control_affinity {
771 ListTileControlAffinity::Leading | ListTileControlAffinity::Platform => {
772 (Some(control), widget.secondary.clone())
773 }
774 ListTileControlAffinity::Trailing => (widget.secondary.clone(), Some(control)),
775 };
776 let theme = Theme::of(app, context);
777 let radio_theme_data = RadioTheme::of(app, context);
778 let widget = self.widget(app);
779 let mut states = WidgetStates::new();
780 if widget.selected {
781 states.insert(WidgetState::Selected);
782 }
783 let effective_active_color = widget
784 .active_color
785 .or_else(|| {
786 radio_theme_data
787 .fill_color
788 .as_ref()
789 .and_then(|fill_color| fill_color.resolve(&states))
790 })
791 .unwrap_or(theme.color_scheme.secondary());
792 let enabled = RadioClient::enabled(self, app);
793 let focus_node = self.focus_node(app);
794 let widget = self.widget(app);
795 let mut list_tile = ListTile::new()
796 .selected_color(effective_active_color)
797 .enabled(enabled)
798 .selected(widget.selected)
799 .autofocus(widget.autofocus)
800 .focus_node(focus_node);
801 list_tile.leading = leading;
802 list_tile.title = widget.title.clone();
803 list_tile.subtitle = widget.subtitle.clone();
804 list_tile.trailing = trailing;
805 list_tile.is_three_line = widget.is_three_line;
806 list_tile.dense = widget.dense;
807 list_tile.shape = widget.shape.clone();
808 list_tile.tile_color = widget.tile_color;
809 list_tile.selected_tile_color = widget.selected_tile_color;
810 list_tile.on_tap = if enabled {
811 Some(GestureTapCallback::handle_method(
812 self,
813 Self::handle_list_tile_tap,
814 ))
815 } else {
816 None
817 };
818 list_tile.content_padding = widget.content_padding;
819 list_tile.visual_density = widget.visual_density;
820 list_tile.states_controller = widget.states_controller.clone();
821 list_tile.on_focus_change = widget.on_focus_change.clone();
822 list_tile.enable_feedback = widget.enable_feedback;
823 list_tile.horizontal_title_gap = widget.horizontal_title_gap;
824 list_tile.min_vertical_padding = widget.min_vertical_padding;
825 list_tile.min_leading_width = widget.min_leading_width;
826 list_tile.min_tile_height = widget.min_tile_height;
827 list_tile.title_alignment = widget.title_alignment;
828 list_tile.internal_add_semantic_for_on_tap = widget.internal_add_semantic_for_on_tap;
829 MergeSemantics::new().child(list_tile).into_widget()
830 }
831}
832
833struct RadioRegistry<T: Clone + PartialEq + 'static> {
836 state: Handle<RadioListTileState<T>>,
837}
838
839impl<T: Clone + PartialEq + 'static> RadioGroupRegistry<T> for RadioRegistry<T> {
840 fn group_value(self: Handle<Self>, app: &App) -> Option<T> {
841 app.get(self).state.effective_group_value(app)
842 }
843
844 fn register_client(self: Handle<Self>, _app: &mut App, _radio: AnyRadioClient<T>) {}
845
846 fn unregister_client(self: Handle<Self>, _app: &mut App, _radio: AnyRadioClient<T>) {}
847
848 fn on_changed(self: Handle<Self>, app: &mut App, value: Option<T>) {
849 let state = app.get(self).state;
850 state.handle_change(app, value);
851 }
852}
853
854#[cfg(test)]
855#[allow(deprecated)]
856mod tests {
857 use std::cell::{Cell, RefCell};
858 use std::rc::Rc;
859
860 use waft_embedder::{Matrix4, Offset, PointerChange, Size, TargetPlatform};
861 use waft_foundation::K_IS_WEB;
862 use waft_painting::{BorderSide, EdgeInsets};
863 use waft_services::SystemMouseCursors;
864 use waft_widgets::{
865 Center, Container, FocusHighlightStrategy, GlobalKey, ListView, SizedBox, StatefulBuilder,
866 Text, WidgetStateProperty,
867 };
868
869 use waft_rendering::testing::PaintCall;
870 use waft_widgets::testing::{TestPointer, WidgetTester, find, test_widgets};
871
872 use super::*;
873 use crate::checkbox_list_tile::tests::{
874 IS_THREE_LINE_FRAMES, check_control_is_skipped_by_traversal,
875 check_default_horizontal_title_gap, check_horizontal_title_gap_0,
876 check_horizontal_title_gap_with_visual_density, check_min_leading_width_60,
877 check_min_tile_height, check_min_vertical_padding_80, check_on_focus_change,
878 check_title_alignment, expected_three_line_rects, focus_of, has_primary_focus,
879 ink_well_custom_border, ink_well_enable_feedback, is_three_line_rects, nth,
880 pump_in_forbidden_region, pump_in_material, pump_wrapped, rect_of, right_rounded_border,
881 };
882 use crate::colors::Colors;
883 use crate::constants::K_RADIAL_REACTION_ALPHA;
884 use crate::list_tile::tests::{m2, m3};
885 use crate::list_tile_theme::ListTileThemeData;
886 use crate::radio::tests::{
887 CircleArguments, any_rect, assert_paints, center_of_radio, circle, colors, hover_at, no_op,
888 press_at, radio_material,
889 };
890 use crate::radio_theme::RadioThemeData;
891 use crate::scaffold::Scaffold;
892 use crate::test_support::PaintInspection;
893 use crate::theme_data::ThemeData;
894 use crate::tooltip::tests::pump_app;
895
896 fn log_into(log: &Rc<RefCell<Vec<Option<i32>>>>) -> ValueChanged<Option<i32>> {
897 let log = Rc::clone(log);
898 Rc::new(move |_app, value| log.borrow_mut().push(value))
899 }
900
901 fn any_circle() -> PaintCall {
903 PaintCall::circle(None)
904 }
905
906 fn three_tiles(
910 selected_value: &Rc<Cell<Option<i32>>>,
911 log: &Rc<RefCell<Vec<Option<i32>>>>,
912 toggleable: bool,
913 ) -> WidgetRef {
914 let (selected_value, log) = (Rc::clone(selected_value), Rc::clone(log));
915 StatefulBuilder::new(move |_app, _context, set_state| {
916 let (selected_value, log) = (Rc::clone(&selected_value), Rc::clone(&log));
917 Scaffold::new()
918 .body(
919 ListView::builder(move |_app, _context, index| {
920 let (selected_now, log) = (Rc::clone(&selected_value), Rc::clone(&log));
921 let set_state = Rc::clone(&set_state);
922 let mut tile = RadioListTile::new(index)
923 .on_changed(Rc::new(move |app: &mut App, value: Option<i32>| {
924 log.borrow_mut().push(value);
925 let selected = Rc::clone(&selected_now);
926 set_state(app, &mut |_app| selected.set(value));
927 }))
928 .toggleable(toggleable)
929 .title(Text::new(index.to_string()));
930 tile.group_value = selected_value.get();
931 Some(tile.into_widget())
932 })
933 .item_count(3),
934 )
935 .into_widget()
936 })
937 .into_widget()
938 }
939
940 fn tap_radio(tester: &mut WidgetTester, index: usize) {
941 let at = rect_of::<Radio<i32>>(tester, index).center();
942 tester.tap_at(at, 1);
943 }
944
945 #[test]
948 fn radio_list_tile_can_be_toggled_when_toggleable_is_set() {
949 test_widgets(|tester| {
950 let theme = m3(&tester.cell.borrow());
951 let key = GlobalKey::new();
952 let log: Rc<RefCell<Vec<Option<i32>>>> = Rc::default();
953 let wrap = |radio: Radio<i32>| {
954 crate::theme::Theme::new(
955 theme.clone(),
956 crate::material::Material::new()
957 .child(Center::new().child(radio.key(key.clone().into()))),
958 )
959 .into_widget()
960 };
961
962 tester.pump_widget(wrap(
963 Radio::new(1)
964 .group_value(2)
965 .on_changed(log_into(&log))
966 .toggleable(true),
967 ));
968 tester.tap_at(tester.get_center(&key), 1);
969 assert_eq!(*log.borrow(), [Some(1)]);
970 log.borrow_mut().clear();
971
972 tester.pump_widget(wrap(
973 Radio::new(1)
974 .group_value(1)
975 .on_changed(log_into(&log))
976 .toggleable(true),
977 ));
978 tester.tap_at(tester.get_center(&key), 2);
979 assert_eq!(*log.borrow(), [None]);
980 log.borrow_mut().clear();
981
982 tester.pump_widget(wrap(
983 Radio::new(1).on_changed(log_into(&log)).toggleable(true),
984 ));
985 tester.tap_at(tester.get_center(&key), 3);
986 assert_eq!(*log.borrow(), [Some(1)]);
987 });
988 }
989
990 #[test]
991 fn radio_list_tile_should_initialize_according_to_group_value() {
992 test_widgets(|tester| {
993 let selected_value = Rc::new(Cell::new(None));
994 let log = Rc::new(RefCell::new(Vec::new()));
995 let checked = |tester: &WidgetTester| -> Vec<bool> {
996 (0..3)
997 .map(|index| {
998 tester.widget(
999 nth::<RadioListTile<i32>>(tester, index),
1000 |tile: &RadioListTile<i32>| tile.checked(),
1001 )
1002 })
1003 .collect()
1004 };
1005 let frame = three_tiles(&selected_value, &log, false);
1006 pump_wrapped(tester, |_app| frame);
1007 assert_eq!(checked(tester), [false, false, false]);
1008
1009 selected_value.set(Some(1));
1010 let frame = three_tiles(&selected_value, &log, false);
1011 pump_wrapped(tester, |_app| frame);
1012 assert_eq!(checked(tester), [false, true, false]);
1013 });
1014 }
1015
1016 #[test]
1017 fn radio_list_tile_forwards_states_controller_to_list_tile() {
1018 test_widgets(|tester| {
1019 let controller = WidgetStatesController::new(&mut tester.cell.borrow_mut(), None);
1020 pump_wrapped(tester, |_app| {
1021 RadioListTile::new(1)
1022 .group_value(1)
1023 .on_changed(no_op())
1024 .states_controller(controller.clone())
1025 .into_widget()
1026 });
1027 let forwarded = tester.widget(nth::<ListTile>(tester, 0), |tile: &ListTile| {
1028 tile.states_controller.clone()
1029 });
1030 assert_eq!(forwarded, Some(controller));
1031 });
1032 }
1033
1034 #[test]
1035 fn radio_list_tile_simple_control_test() {
1036 test_widgets(|tester| {
1037 let key = GlobalKey::new();
1038 let title_key = GlobalKey::new();
1039 let log = Rc::new(RefCell::new(Vec::new()));
1040 let frame =
1041 |tester: &mut WidgetTester, group_value: i32, on_changed: bool, tap_title: bool| {
1042 let (tile_key, tile_title_key, log) =
1043 (key.clone(), title_key.clone(), Rc::clone(&log));
1044 pump_wrapped(tester, move |_app| {
1045 let mut tile = RadioListTile::new(1)
1046 .key(tile_key.into())
1047 .group_value(group_value)
1048 .title(Text::new("Title").key(tile_title_key.into()));
1049 if on_changed {
1050 tile = tile.on_changed(log_into(&log));
1051 }
1052 tile.into_widget()
1053 });
1054 let at = if tap_title {
1055 tester.get_center(&title_key)
1056 } else {
1057 tester.get_center(&key)
1058 };
1059 tester.tap_at(at, 1);
1060 };
1061
1062 frame(tester, 2, true, false);
1063 assert_eq!(*log.borrow(), [Some(1)]);
1064 log.borrow_mut().clear();
1065
1066 frame(tester, 1, true, false);
1067 assert!(log.borrow().is_empty());
1068
1069 frame(tester, 2, false, false);
1070 assert!(log.borrow().is_empty());
1071
1072 frame(tester, 2, true, true);
1073 assert_eq!(*log.borrow(), [Some(1)]);
1074 });
1075 }
1076
1077 #[test]
1078 fn radio_list_tile_control_tests() {
1079 test_widgets(|tester| {
1080 let selected_value = Rc::new(Cell::new(None));
1081 let log = Rc::new(RefCell::new(Vec::new()));
1082 let separator = || log.borrow_mut().push(Some(-1));
1083 let frame = |tester: &mut WidgetTester| {
1084 let frame = three_tiles(&selected_value, &log, false);
1085 pump_wrapped(tester, |_app| frame);
1086 };
1087
1088 frame(tester);
1090 tester.tap(find::text("1"));
1091 separator();
1092 tap_radio(tester, 2);
1093 assert_eq!(*log.borrow(), [Some(1), Some(-1), Some(2)]);
1094 separator();
1095 tester.tap(find::text("1"));
1096
1097 log.borrow_mut().clear();
1098 selected_value.set(None);
1099
1100 frame(tester);
1102 tap_radio(tester, 1);
1103 separator();
1104 tap_radio(tester, 2);
1105 assert_eq!(*log.borrow(), [Some(1), Some(-1), Some(2)]);
1106
1107 log.borrow_mut().clear();
1108 selected_value.set(None);
1109
1110 frame(tester);
1112 tester.tap(find::text("1"));
1113 separator();
1114 tester.tap(find::text("2"));
1115 assert_eq!(*log.borrow(), [Some(1), Some(-1), Some(2)]);
1116 });
1117 }
1118
1119 fn tap_selected_tile(
1122 tester: &mut WidgetTester,
1123 toggleable: bool,
1124 expected: [&[Option<i32>]; 3],
1125 ) {
1126 let selected_value = Rc::new(Cell::new(None));
1128 let log = Rc::new(RefCell::new(Vec::new()));
1129 let frame = three_tiles(&selected_value, &log, toggleable);
1130 pump_wrapped(tester, |_app| frame);
1131
1132 tester.tap(find::text("0"));
1133 tester.pump();
1134 assert_eq!(*log.borrow(), expected[0]);
1135
1136 tester.tap(find::text("0"));
1137 tester.pump();
1138 assert_eq!(*log.borrow(), expected[1]);
1139
1140 tap_radio(tester, 0);
1141 tester.pump();
1142 assert_eq!(*log.borrow(), expected[2]);
1143 }
1144
1145 #[test]
1146 fn selected_radio_list_tile_should_not_trigger_on_changed() {
1147 test_widgets(|tester| {
1148 tap_selected_tile(tester, false, [&[Some(0)], &[Some(0)], &[Some(0)]]);
1149 });
1150 }
1151
1152 #[test]
1153 fn selected_radio_list_tile_should_trigger_on_changed_when_toggleable() {
1154 test_widgets(|tester| {
1155 tap_selected_tile(
1156 tester,
1157 true,
1158 [&[Some(0)], &[Some(0), None], &[Some(0), None, Some(0)]],
1159 );
1160 });
1161 }
1162
1163 #[test]
1164 fn radio_list_tile_can_autofocus_unless_disabled() {
1165 test_widgets(|tester| {
1166 let child_key = GlobalKey::new();
1167 for (enabled, focused) in [(true, true), (false, false)] {
1168 let key = child_key.clone();
1169 pump_wrapped(tester, move |_app| {
1170 let mut tile = RadioListTile::new(1)
1171 .group_value(2)
1172 .title(Text::new("Title").key(key.into()))
1173 .autofocus(true);
1174 if enabled {
1175 tile = tile.on_changed(no_op());
1176 }
1177 tile.into_widget()
1178 });
1179 tester.pump();
1180 let node = focus_of(tester, &child_key);
1181 assert_eq!(has_primary_focus(tester, node), focused);
1182 }
1183 });
1184 }
1185
1186 #[test]
1187 fn radio_list_tile_content_padding_test() {
1188 test_widgets(|tester| {
1189 pump_wrapped(tester, |_app| {
1190 Center::new()
1191 .child(
1192 RadioListTile::new(true)
1193 .group_value(true)
1194 .title(Text::new("Title"))
1195 .on_changed(no_op())
1196 .content_padding(EdgeInsets::from_ltrb(8.0, 10.0, 15.0, 20.0)),
1197 )
1198 .into_widget()
1199 });
1200 let padding_rect = rect_of::<waft_widgets::SafeArea>(tester, 0);
1201 let radio_rect = rect_of::<Radio<bool>>(tester, 0);
1202 let title_rect = tester.get_rect(find::text("Title"));
1203
1204 let taller_rect = if radio_rect.height() > title_rect.height() {
1206 radio_rect
1207 } else {
1208 title_rect
1209 };
1210
1211 let extra_height = 56.0 - taller_rect.height();
1213
1214 assert_eq!(
1216 padding_rect.top,
1217 taller_rect.top - extra_height / 2.0 - 10.0
1218 );
1219 assert_eq!(
1220 padding_rect.bottom,
1221 taller_rect.bottom + extra_height / 2.0 + 20.0
1222 );
1223
1224 assert_eq!(padding_rect.left, radio_rect.left - 8.0);
1226 assert_eq!(padding_rect.right, title_rect.right + 15.0);
1227 });
1228 }
1229
1230 #[test]
1231 fn radio_list_tile_respects_shape() {
1232 test_widgets(|tester| {
1233 pump_in_material(tester, m3, |_app| {
1234 RadioListTile::new(true)
1235 .group_value(true)
1236 .title(Text::new("Title"))
1237 .shape(ShapeBorderRef::new(right_rounded_border()))
1238 .into_widget()
1239 });
1240 let shape: ShapeBorderRef = ShapeBorderRef::new(right_rounded_border());
1241 assert!(ink_well_custom_border(tester).as_deref() == Some(&*shape));
1242 });
1243 }
1244
1245 #[test]
1246 fn radio_list_tile_respects_tile_color() {
1247 test_widgets(|tester| {
1248 let tile_color = Colors::RED.shade500();
1249 pump_wrapped(tester, |_app| {
1250 Center::new()
1251 .child(
1252 RadioListTile::new(false)
1253 .group_value(true)
1254 .title(Text::new("Title"))
1255 .tile_color(tile_color),
1256 )
1257 .into_widget()
1258 });
1259 waft_rendering::testing::assert_paints(
1260 tester.ink_list().ops(),
1261 &[waft_rendering::testing::PaintCall::rect(Some(tile_color))],
1262 );
1263 });
1264 }
1265
1266 #[test]
1267 fn radio_list_tile_respects_selected_tile_color() {
1268 test_widgets(|tester| {
1269 let selected_tile_color = Colors::GREEN.shade500();
1270 pump_wrapped(tester, |_app| {
1271 Center::new()
1272 .child(
1273 RadioListTile::new(false)
1274 .group_value(true)
1275 .title(Text::new("Title"))
1276 .selected(true)
1277 .selected_tile_color(selected_tile_color),
1278 )
1279 .into_widget()
1280 });
1281 waft_rendering::testing::assert_paints(
1282 tester.ink_list().ops(),
1283 &[waft_rendering::testing::PaintCall::rect(Some(
1284 selected_tile_color,
1285 ))],
1286 );
1287 });
1288 }
1289
1290 #[test]
1291 fn radio_list_tile_selected_item_text_color() {
1292 test_widgets(|tester| {
1293 const ACTIVE_COLOR: Color = Color::new(0xff00ff00);
1295 for (active_color, fill_color) in
1296 [(None, Some(ACTIVE_COLOR)), (Some(ACTIVE_COLOR), None)]
1297 {
1298 pump_app(
1299 tester,
1300 move |app| {
1301 ThemeData::builder()
1302 .radio_theme(RadioThemeData::new().fill_color(
1303 <dyn WidgetStateProperty<Option<Color>>>::resolve_with(
1304 move |states| {
1305 if states.contains(&WidgetState::Selected) {
1306 fill_color
1307 } else {
1308 None
1309 }
1310 },
1311 ),
1312 ))
1313 .build(app)
1314 },
1315 move |_app| {
1316 let mut tile = RadioListTile::new(false)
1317 .selected(true)
1318 .title(Text::new("title"))
1319 .group_value(true)
1320 .on_changed(no_op());
1321 tile.active_color = active_color;
1322 Scaffold::new()
1323 .body(Center::new().child(tile))
1324 .into_widget()
1325 },
1326 );
1327 assert_eq!(tester.text_color("title"), ACTIVE_COLOR);
1328 }
1329 });
1330 }
1331
1332 #[test]
1333 fn radio_list_tile_respects_visual_density() {
1334 test_widgets(|tester| {
1335 pump_wrapped(tester, |_app| {
1336 Center::new()
1337 .child(
1338 RadioListTile::new(false)
1339 .group_value(true)
1340 .on_changed(no_op())
1341 .autofocus(true)
1342 .visual_density(VisualDensity::STANDARD),
1343 )
1344 .into_widget()
1345 });
1346 tester.pump_and_settle();
1347 assert_eq!(
1348 rect_of::<RadioListTile<bool>>(tester, 0).size(),
1349 Size::new(800.0, 56.0)
1350 );
1351 });
1352 }
1353
1354 #[test]
1355 fn radio_list_tile_respects_focus_node() {
1356 test_widgets(|tester| {
1357 let child_key = GlobalKey::new();
1358 let key = child_key.clone();
1359 pump_wrapped(tester, move |_app| {
1360 Center::new()
1361 .child(
1362 RadioListTile::new(false)
1363 .group_value(true)
1364 .title(Text::new("A").key(key.into()))
1365 .on_changed(no_op()),
1366 )
1367 .into_widget()
1368 });
1369 tester.pump();
1370 let tile_node = focus_of(tester, &child_key);
1371 tile_node.request_focus(&mut tester.cell.borrow_mut(), None);
1372 tester.pump(); let node = focus_of(tester, &child_key);
1374 assert!(has_primary_focus(tester, node));
1375 assert!(has_primary_focus(tester, tile_node));
1376 });
1377 }
1378
1379 #[test]
1380 fn radio_list_tile_on_focus_change_callback() {
1381 test_widgets(|tester| {
1382 check_on_focus_change(tester, |node, on_focus_change| {
1383 RadioListTile::new(true)
1384 .focus_node(node)
1385 .on_focus_change(on_focus_change)
1386 .on_changed(no_op())
1387 .group_value(true)
1388 .into_widget()
1389 });
1390 });
1391 }
1392
1393 #[test]
1394 fn radio_changes_mouse_cursor_when_hovered() {
1395 test_widgets(|tester| {
1396 pump_in_forbidden_region(
1398 tester,
1399 RadioListTile::new(1)
1400 .mouse_cursor(MouseCursorRef::from(SystemMouseCursors::TEXT))
1401 .on_changed(no_op())
1402 .group_value(2)
1403 .into_widget(),
1404 );
1405 let mut gesture = TestPointer::mouse(1, 0);
1406 let center = center_of_radio::<i32>(tester);
1407 tester.add_pointer(&mut gesture, center);
1408 tester.pump();
1409 assert_eq!(tester.active_cursor(1), SystemMouseCursors::TEXT.into());
1410
1411 pump_in_forbidden_region(
1413 tester,
1414 RadioListTile::new(1)
1415 .on_changed(no_op())
1416 .group_value(2)
1417 .into_widget(),
1418 );
1419 let expected: MouseCursorRef = if K_IS_WEB {
1420 SystemMouseCursors::CLICK.into()
1421 } else {
1422 SystemMouseCursors::BASIC.into()
1423 };
1424 assert_eq!(Some(tester.active_cursor(1)), Some(expected));
1425
1426 pump_in_forbidden_region(tester, RadioListTile::new(1).group_value(2).into_widget());
1428 assert_eq!(tester.active_cursor(1), SystemMouseCursors::BASIC.into());
1429 tester.send(&gesture, PointerChange::Remove);
1430 });
1431 }
1432
1433 fn stateful_tile(
1436 group_value: &Rc<Cell<Option<i32>>>,
1437 enabled: bool,
1438 configure: impl Fn(RadioListTile<i32>) -> RadioListTile<i32> + 'static,
1439 ) -> WidgetRef {
1440 let group_value = Rc::clone(group_value);
1441 StatefulBuilder::new(move |_app, _context, set_state| {
1442 let mut tile = configure(RadioListTile::new(0));
1443 tile.group_value = group_value.get();
1444 if enabled {
1445 let group_value = Rc::clone(&group_value);
1446 tile = tile.on_changed(Rc::new(move |app: &mut App, value: Option<i32>| {
1447 let group_value = Rc::clone(&group_value);
1448 set_state(app, &mut |_app| group_value.set(value));
1449 }));
1450 }
1451 tile.into_widget()
1452 })
1453 .into_widget()
1454 }
1455
1456 #[test]
1457 fn radio_list_tile_respects_fill_color_in_enabled_disabled_states() {
1458 test_widgets(|tester| {
1459 const ACTIVE_ENABLED_FILL_COLOR: Color = Color::new(0xFF000001);
1460 const ACTIVE_DISABLED_FILL_COLOR: Color = Color::new(0xFF000002);
1461 const INACTIVE_ENABLED_FILL_COLOR: Color = Color::new(0xFF000003);
1462 const INACTIVE_DISABLED_FILL_COLOR: Color = Color::new(0xFF000004);
1463 let fill_color = colors(|states| {
1464 if states.contains(&WidgetState::Disabled) {
1465 if states.contains(&WidgetState::Selected) {
1466 return ACTIVE_DISABLED_FILL_COLOR;
1467 }
1468 return INACTIVE_DISABLED_FILL_COLOR;
1469 }
1470 if states.contains(&WidgetState::Selected) {
1471 return ACTIVE_ENABLED_FILL_COLOR;
1472 }
1473 INACTIVE_ENABLED_FILL_COLOR
1474 });
1475 let group_value = Rc::new(Cell::new(Some(0)));
1476 let selected = |color: Color| [circle(color), circle(color)];
1478 let unselected = |color: Color| [circle(color).stroke_width(2.0)];
1479 for (value, enabled, expected) in [
1480 (0, true, selected(ACTIVE_ENABLED_FILL_COLOR).to_vec()),
1481 (1, true, unselected(INACTIVE_ENABLED_FILL_COLOR).to_vec()),
1482 (0, false, selected(ACTIVE_DISABLED_FILL_COLOR).to_vec()),
1483 (1, false, unselected(INACTIVE_DISABLED_FILL_COLOR).to_vec()),
1484 ] {
1485 group_value.set(Some(value));
1486 let fill_color = Rc::clone(&fill_color);
1487 let host = stateful_tile(&group_value, enabled, move |tile| {
1488 tile.fill_color(Rc::clone(&fill_color))
1489 });
1490 pump_wrapped(tester, |_app| host);
1491 tester.pump_and_settle();
1492 assert_paints(tester, radio_material::<i32>(tester), &expected);
1493 }
1494 });
1495 }
1496
1497 #[test]
1498 fn radio_list_tile_respects_fill_color_in_hovered_state() {
1499 test_widgets(|tester| {
1500 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
1501 const HOVERED_FILL_COLOR: Color = Color::new(0xFF000001);
1502 let fill_color = colors(|states| {
1503 if states.contains(&WidgetState::Hovered) {
1504 return HOVERED_FILL_COLOR;
1505 }
1506 Colors::TRANSPARENT
1507 });
1508 let group_value = Rc::new(Cell::new(Some(0)));
1509 let host = stateful_tile(&group_value, true, move |tile| {
1510 tile.fill_color(Rc::clone(&fill_color))
1511 });
1512 pump_wrapped(tester, |_app| host);
1513 tester.pump_and_settle();
1514
1515 let center = center_of_radio::<i32>(tester);
1517 hover_at(tester, center);
1518 tester.pump_and_settle();
1519 assert_paints(
1520 tester,
1521 radio_material::<i32>(tester),
1522 &[any_rect(), any_circle(), circle(HOVERED_FILL_COLOR)],
1523 );
1524 });
1525 }
1526
1527 #[test]
1528 fn material3_radio_list_tile_respects_hover_color() {
1529 test_widgets(|tester| {
1530 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
1531 let hover_color = Colors::ORANGE.shade500();
1532 let theme = m3(&tester.cell.borrow());
1533 let group_value = Rc::new(Cell::new(Some(0)));
1534 let frame = |tester: &mut WidgetTester, enabled: bool| {
1535 let host = stateful_tile(&group_value, enabled, move |tile| {
1536 tile.hover_color(hover_color)
1537 });
1538 let theme = theme.clone();
1542 pump_in_material(tester, move |_app| theme, |_app| host);
1543 tester.pump();
1544 tester.pump_and_settle();
1545 };
1546
1547 frame(tester, true);
1548 let primary = theme.color_scheme.primary();
1549 assert_paints(
1551 tester,
1552 radio_material::<i32>(tester),
1553 &[circle(primary), circle(primary)],
1554 );
1555
1556 let center = center_of_radio::<i32>(tester);
1558 hover_at(tester, center);
1559
1560 group_value.set(Some(1));
1562 frame(tester, true);
1563 assert_paints(
1564 tester,
1565 radio_material::<i32>(tester),
1566 &[any_rect(), circle(hover_color)],
1567 );
1568
1569 group_value.set(Some(0));
1571 frame(tester, false);
1572 let disabled = theme.color_scheme.on_surface().with_opacity(0.38);
1573 assert_paints(
1574 tester,
1575 radio_material::<i32>(tester),
1576 &[circle(disabled), circle(disabled)],
1577 );
1578 });
1579 }
1580
1581 #[test]
1582 fn material3_radio_list_tile_respects_overlay_color_in_active_pressed_hovered_states() {
1583 test_widgets(|tester| {
1584 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
1585 const FILL_COLOR: Color = Color::new(0xFF000000);
1586 const ACTIVE_PRESSED_OVERLAY_COLOR: Color = Color::new(0xFF000001);
1587 const INACTIVE_PRESSED_OVERLAY_COLOR: Color = Color::new(0xFF000002);
1588 const HOVER_OVERLAY_COLOR: Color = Color::new(0xFF000003);
1589 const HOVER_COLOR: Color = Color::new(0xFF000005);
1590 let overlay = <dyn WidgetStateProperty<Option<Color>>>::resolve_with(|states| {
1591 if states.contains(&WidgetState::Pressed) {
1592 if states.contains(&WidgetState::Selected) {
1593 return Some(ACTIVE_PRESSED_OVERLAY_COLOR);
1594 }
1595 return Some(INACTIVE_PRESSED_OVERLAY_COLOR);
1596 }
1597 if states.contains(&WidgetState::Hovered) {
1598 return Some(HOVER_OVERLAY_COLOR);
1599 }
1600 None
1601 });
1602 let frame = |tester: &mut WidgetTester, active: bool, use_overlay: bool| {
1603 let overlay = Rc::clone(&overlay);
1604 pump_in_material(tester, m3, move |_app| {
1605 let mut tile = RadioListTile::new(active)
1606 .group_value(true)
1607 .on_changed(no_op())
1608 .fill_color(<dyn WidgetStateProperty<Option<Color>>>::all(Some(
1609 FILL_COLOR,
1610 )))
1611 .hover_color(HOVER_COLOR);
1612 if use_overlay {
1613 tile = tile.overlay_color(overlay);
1614 }
1615 tile.into_widget()
1616 });
1617 };
1618 let reaction = FILL_COLOR.with_alpha(K_RADIAL_REACTION_ALPHA);
1619 let pressed = |color: Color| {
1620 [
1621 PaintCall::rect(Some(Color::new(0x66bcbcbc))),
1622 circle(color).radius(20.0),
1623 ]
1624 };
1625 for (pointer, (active, use_overlay, expected)) in [
1626 (false, false, reaction),
1627 (true, false, reaction),
1628 (false, true, INACTIVE_PRESSED_OVERLAY_COLOR),
1629 (true, true, ACTIVE_PRESSED_OVERLAY_COLOR),
1630 ]
1631 .into_iter()
1632 .enumerate()
1633 {
1634 frame(tester, active, use_overlay);
1635 let center = center_of_radio::<bool>(tester);
1636 press_at(tester, center, pointer as i64 + 2);
1637 tester.pump_and_settle();
1638 assert_paints(tester, radio_material::<bool>(tester), &pressed(expected));
1639 }
1640
1641 let center = center_of_radio::<bool>(tester);
1643 hover_at(tester, center);
1644 tester.pump_and_settle();
1645
1646 tester.pump_widget(Container::new().into_widget());
1647 frame(tester, false, true);
1648 tester.pump_and_settle();
1649 assert_paints(
1650 tester,
1651 radio_material::<bool>(tester),
1652 &[
1653 PaintCall::rect(Some(Color::new(0x0a000000))),
1654 circle(HOVER_OVERLAY_COLOR).radius(20.0),
1655 ],
1656 );
1657 });
1658 }
1659
1660 #[test]
1661 fn material2_radio_list_tile_respects_overlay_color_in_active_pressed_hovered_states() {
1662 test_widgets(|tester| {
1663 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
1664 const FILL_COLOR: Color = Color::new(0xFF000000);
1665 const ACTIVE_PRESSED_OVERLAY_COLOR: Color = Color::new(0xFF000001);
1666 const INACTIVE_PRESSED_OVERLAY_COLOR: Color = Color::new(0xFF000002);
1667 const HOVER_OVERLAY_COLOR: Color = Color::new(0xFF000003);
1668 const HOVER_COLOR: Color = Color::new(0xFF000005);
1669 let overlay = <dyn WidgetStateProperty<Option<Color>>>::resolve_with(|states| {
1670 if states.contains(&WidgetState::Pressed) {
1671 if states.contains(&WidgetState::Selected) {
1672 return Some(ACTIVE_PRESSED_OVERLAY_COLOR);
1673 }
1674 return Some(INACTIVE_PRESSED_OVERLAY_COLOR);
1675 }
1676 if states.contains(&WidgetState::Hovered) {
1677 return Some(HOVER_OVERLAY_COLOR);
1678 }
1679 None
1680 });
1681 let frame = |tester: &mut WidgetTester, active: bool, use_overlay: bool| {
1682 let overlay = Rc::clone(&overlay);
1683 pump_in_material(tester, m2, move |_app| {
1684 let mut tile = RadioListTile::new(active)
1685 .group_value(true)
1686 .on_changed(no_op())
1687 .fill_color(<dyn WidgetStateProperty<Option<Color>>>::all(Some(
1688 FILL_COLOR,
1689 )))
1690 .hover_color(HOVER_COLOR);
1691 if use_overlay {
1692 tile = tile.overlay_color(overlay);
1693 }
1694 tile.into_widget()
1695 });
1696 };
1697 let reaction = FILL_COLOR.with_alpha(K_RADIAL_REACTION_ALPHA);
1698 for (pointer, (active, use_overlay, expected)) in [
1699 (false, false, reaction),
1700 (true, false, reaction),
1701 (false, true, INACTIVE_PRESSED_OVERLAY_COLOR),
1702 ]
1703 .into_iter()
1704 .enumerate()
1705 {
1706 frame(tester, active, use_overlay);
1707 let center = center_of_radio::<bool>(tester);
1708 press_at(tester, center, pointer as i64 + 2);
1709 tester.pump_and_settle();
1710 assert_paints(
1711 tester,
1712 radio_material::<bool>(tester),
1713 &[any_circle(), circle(expected).radius(20.0)],
1714 );
1715 }
1716
1717 let center = center_of_radio::<bool>(tester);
1719 hover_at(tester, center);
1720 tester.pump_and_settle();
1721
1722 tester.pump_widget(Container::new().into_widget());
1723 frame(tester, false, true);
1724 tester.pump_and_settle();
1725 assert_paints(
1726 tester,
1727 radio_material::<bool>(tester),
1728 &[circle(HOVER_OVERLAY_COLOR).radius(20.0)],
1729 );
1730 });
1731 }
1732
1733 #[test]
1734 fn material2_radio_list_tile_respects_hover_color() {
1735 test_widgets(|tester| {
1736 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
1737 let hover_color = Colors::ORANGE.shade500();
1738 let group_value = Rc::new(Cell::new(Some(0)));
1739 let frame = |tester: &mut WidgetTester, enabled: bool| {
1740 let host = stateful_tile(&group_value, enabled, move |tile| {
1741 tile.hover_color(hover_color)
1742 });
1743 pump_in_material(tester, m2, |_app| host);
1745 tester.pump();
1746 tester.pump_and_settle();
1747 };
1748
1749 frame(tester, true);
1750 let blue = Color::new(0xff2196f3);
1751 assert_paints(
1753 tester,
1754 radio_material::<i32>(tester),
1755 &[circle(blue), circle(blue)],
1756 );
1757
1758 let center = center_of_radio::<i32>(tester);
1760 hover_at(tester, center);
1761
1762 group_value.set(Some(1));
1764 frame(tester, true);
1765 assert_paints(
1766 tester,
1767 radio_material::<i32>(tester),
1768 &[any_rect(), circle(hover_color)],
1769 );
1770
1771 group_value.set(Some(0));
1773 frame(tester, false);
1774 let disabled = Color::new(0x61000000);
1775 assert_paints(
1776 tester,
1777 radio_material::<i32>(tester),
1778 &[circle(disabled), circle(disabled)],
1779 );
1780 });
1781 }
1782
1783 #[test]
1784 fn radio_list_tile_respects_splash_radius() {
1785 test_widgets(|tester| {
1786 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
1787 const SPLASH_RADIUS: f64 = 30.0;
1788 let orange = Colors::ORANGE.shade500();
1789 pump_wrapped(tester, |_app| {
1790 RadioListTile::new(0)
1791 .on_changed(no_op())
1792 .hover_color(orange)
1793 .group_value(0)
1794 .splash_radius(SPLASH_RADIUS)
1795 .into_widget()
1796 });
1797 tester.pump_and_settle();
1798 let center = center_of_radio::<i32>(tester);
1799 hover_at(tester, center);
1800 tester.pump_and_settle();
1801 assert_paints(
1802 tester,
1803 radio_material::<i32>(tester),
1804 &[circle(orange).radius(SPLASH_RADIUS)],
1805 );
1806 });
1807 }
1808
1809 #[test]
1810 fn radio_respects_material_tap_target_size() {
1811 test_widgets(|tester| {
1812 pump_wrapped(tester, |_app| {
1813 RadioListTile::new(true)
1814 .group_value(true)
1815 .on_changed(no_op())
1816 .into_widget()
1817 });
1818 assert_eq!(
1820 rect_of::<Radio<bool>>(tester, 0).size(),
1821 Size::new(40.0, 40.0)
1822 );
1823
1824 pump_wrapped(tester, |_app| {
1825 RadioListTile::new(true)
1826 .material_tap_target_size(MaterialTapTargetSize::Padded)
1827 .group_value(true)
1828 .on_changed(no_op())
1829 .into_widget()
1830 });
1831 assert_eq!(
1832 rect_of::<Radio<bool>>(tester, 0).size(),
1833 Size::new(48.0, 48.0)
1834 );
1835 });
1836 }
1837
1838 #[test]
1839 fn radio_list_tile_control_widget_should_not_request_focus_on_traversal() {
1840 test_widgets(|tester| {
1841 check_control_is_skipped_by_traversal(tester, |title| {
1842 let mut tile = RadioListTile::new(true)
1843 .group_value(true)
1844 .on_changed(no_op());
1845 tile.title = Some(title);
1846 tile.into_widget()
1847 });
1848 });
1849 }
1850
1851 #[test]
1852 fn radio_list_tile_adaptive_shows_the_correct_radio_platform_widget() {
1853 test_widgets(|tester| {
1854 for (platform, cupertino) in [
1855 (TargetPlatform::IOS, 1),
1856 (TargetPlatform::MacOS, 1),
1857 (TargetPlatform::Android, 0),
1858 (TargetPlatform::Fuchsia, 0),
1859 (TargetPlatform::Linux, 0),
1860 (TargetPlatform::Windows, 0),
1861 ] {
1862 pump_in_material(
1863 tester,
1864 move |app| ThemeData::builder().platform(platform).build(app),
1865 |_app| {
1866 Center::new()
1867 .child(
1868 RadioListTile::adaptive(1)
1869 .group_value(2)
1870 .on_changed(no_op()),
1871 )
1872 .into_widget()
1873 },
1874 );
1875 tester.pump_and_settle();
1876 assert_eq!(
1877 find::by_type::<waft_cupertino::CupertinoRadio<i32>>()
1878 .evaluate(tester)
1879 .len(),
1880 cupertino,
1881 "{platform:?}"
1882 );
1883 }
1884 });
1885 }
1886
1887 #[test]
1888 fn radio_list_tile_respects_enable_feedback() {
1889 test_widgets(|tester| {
1890 for enable_feedback in [false, true] {
1891 pump_wrapped(tester, |_app| {
1892 Center::new()
1893 .child(
1894 RadioListTile::new(false)
1895 .group_value(true)
1896 .selected(true)
1897 .on_changed(no_op())
1898 .enable_feedback(enable_feedback),
1899 )
1900 .into_widget()
1901 });
1902 assert_eq!(ink_well_enable_feedback(tester), enable_feedback);
1903 }
1904 });
1905 }
1906
1907 #[test]
1908 fn radio_list_tile_uses_list_tile_theme_control_affinity() {
1909 test_widgets(|tester| {
1910 for (control_affinity, expected) in [
1911 (ListTileControlAffinity::Leading, Offset::new(72.0, 16.0)),
1912 (ListTileControlAffinity::Trailing, Offset::new(16.0, 16.0)),
1913 (ListTileControlAffinity::Platform, Offset::new(72.0, 16.0)),
1914 ] {
1915 pump_in_material(tester, m3, |_app| {
1916 ListTileTheme::new(
1917 RadioListTile::new(0.5)
1918 .group_value(1.0)
1919 .title(Text::new("RadioListTile"))
1920 .on_changed(no_op()),
1921 )
1922 .data(ListTileThemeData::new().control_affinity(control_affinity))
1923 .into_widget()
1924 });
1925 assert_eq!(
1926 tester.get_rect(find::text("RadioListTile")).top_left(),
1927 expected
1928 );
1929 }
1930 });
1931 }
1932
1933 #[test]
1934 fn radio_list_tile_renders_with_default_scale() {
1935 test_widgets(|tester| {
1936 pump_in_material(tester, m3, |_app| {
1937 RadioListTile::new(false).group_value(false).into_widget()
1938 });
1939 let radio = nth::<Radio<bool>>(tester, 0);
1940 let app = tester.cell.borrow();
1941 assert!(!tester.ancestors(radio).into_iter().any(|ancestor| {
1942 waft_widgets::downcast_widget::<Transform>(&**ancestor.widget(&app)).is_some()
1943 }));
1944 });
1945 }
1946
1947 #[test]
1948 fn radio_list_tile_respects_radio_scale_factor() {
1949 test_widgets(|tester| {
1950 const SCALE: f64 = 1.4;
1951 pump_in_material(tester, m3, |_app| {
1952 RadioListTile::new(false)
1953 .group_value(false)
1954 .radio_scale_factor(SCALE)
1955 .into_widget()
1956 });
1957 let transform = tester.element(
1958 find::ancestor(
1959 find::by_type::<Radio<bool>>().at(0),
1960 find::by_type::<Transform>(),
1961 )
1962 .first(),
1963 );
1964 let matrix = tester.widget(transform, |transform: &Transform| transform.transform);
1965 assert_eq!(matrix, Matrix4::scale(SCALE as f32, SCALE as f32));
1966 });
1967 }
1968
1969 fn three_line_tile(is_three_line: Option<bool>, subtitle: &str) -> WidgetRef {
1970 three_line_tile_of(RadioListTile::new(0), is_three_line, subtitle)
1971 }
1972
1973 fn adaptive_three_line_tile(is_three_line: Option<bool>, subtitle: &str) -> WidgetRef {
1974 three_line_tile_of(RadioListTile::adaptive(0), is_three_line, subtitle)
1975 }
1976
1977 fn three_line_tile_of(
1978 tile: RadioListTile<i32>,
1979 is_three_line: Option<bool>,
1980 subtitle: &str,
1981 ) -> WidgetRef {
1982 let group_value = if subtitle == "A" { 2 } else { 1 };
1983 let mut tile = tile
1984 .title(Text::new("A"))
1985 .subtitle(Text::new(subtitle))
1986 .group_value(group_value);
1987 tile.is_three_line = is_three_line;
1988 tile.into_widget()
1989 }
1990
1991 #[test]
1992 fn radio_list_tile_is_three_line() {
1993 test_widgets(|tester| {
1994 for (frame, three_line) in IS_THREE_LINE_FRAMES {
1995 assert_eq!(
1996 is_three_line_rects::<Radio<i32>, RadioListTile<i32>>(
1997 tester,
1998 TargetPlatform::Android,
1999 frame,
2000 three_line_tile
2001 ),
2002 expected_three_line_rects(
2003 three_line,
2004 16.0,
2005 Size::new(40.0, 40.0),
2006 [130.0, 300.0 + 16.0]
2007 ),
2008 "{frame:?}"
2009 );
2010 }
2011 });
2012 }
2013
2014 #[test]
2015 fn radio_list_tile_adaptive_is_three_line() {
2016 test_widgets(|tester| {
2017 for (frame, three_line) in IS_THREE_LINE_FRAMES {
2018 assert_eq!(
2019 is_three_line_rects::<Radio<i32>, RadioListTile<i32>>(
2020 tester,
2021 TargetPlatform::IOS,
2022 frame,
2023 adaptive_three_line_tile
2024 ),
2025 expected_three_line_rects(
2026 three_line,
2027 16.0,
2028 Size::new(18.0, 18.0),
2029 [141.0, 300.0 + 27.0]
2030 ),
2031 "{frame:?}"
2032 );
2033 }
2034 });
2035 }
2036
2037 fn title_alignment_tile(
2038 title_alignment: Option<ListTileTitleAlignment>,
2039 secondary: WidgetRef,
2040 with_subtitle: bool,
2041 ) -> WidgetRef {
2042 let mut tile = RadioListTile::new(true)
2043 .group_value(true)
2044 .on_changed(no_op())
2045 .control_affinity(ListTileControlAffinity::Leading)
2046 .title(SizedBox::new().width(20.0).height(50.0))
2047 .secondary(secondary);
2048 if with_subtitle {
2049 tile = tile.subtitle(SizedBox::new().width(20.0).height(50.0));
2050 }
2051 tile.title_alignment = title_alignment;
2052 tile.into_widget()
2053 }
2054
2055 #[test]
2056 fn title_alignment_position_with_title_widget() {
2057 test_widgets(|tester| {
2058 check_title_alignment::<Radio<bool>>(tester, title_alignment_tile, false);
2059 });
2060 }
2061
2062 #[test]
2063 fn title_alignment_position_with_title_and_subtitle_widgets() {
2064 test_widgets(|tester| {
2065 check_title_alignment::<Radio<bool>>(tester, title_alignment_tile, true);
2066 });
2067 }
2068
2069 fn grouped_tile(
2072 group_value: &Rc<Cell<Option<i32>>>,
2073 configure: impl Fn(RadioListTile<i32>) -> RadioListTile<i32> + 'static,
2074 ) -> WidgetRef {
2075 let group_value = Rc::clone(group_value);
2076 StatefulBuilder::new(move |_app, _context, set_state| {
2077 let group_value_now = Rc::clone(&group_value);
2078 let mut group = RadioGroup::new(
2079 Rc::new(move |app: &mut App, value: Option<i32>| {
2080 let group_value = Rc::clone(&group_value_now);
2081 set_state(app, &mut |_app| group_value.set(value));
2082 }),
2083 configure(RadioListTile::new(0)),
2084 );
2085 group.group_value = group_value.get();
2086 group.into_widget()
2087 })
2088 .into_widget()
2089 }
2090
2091 #[test]
2092 fn radio_list_tile_respects_radio_background_color_in_enabled_disabled_states() {
2093 test_widgets(|tester| {
2094 const ACTIVE_ENABLED_BACKGROUND_COLOR: Color = Color::new(0xFF000001);
2095 const ACTIVE_DISABLED_BACKGROUND_COLOR: Color = Color::new(0xFF000002);
2096 const INACTIVE_ENABLED_BACKGROUND_COLOR: Color = Color::new(0xFF000003);
2097 const INACTIVE_DISABLED_BACKGROUND_COLOR: Color = Color::new(0xFF000004);
2098 let background_color = colors(|states| {
2099 if states.contains(&WidgetState::Disabled) {
2100 if states.contains(&WidgetState::Selected) {
2101 return ACTIVE_DISABLED_BACKGROUND_COLOR;
2102 }
2103 return INACTIVE_DISABLED_BACKGROUND_COLOR;
2104 }
2105 if states.contains(&WidgetState::Selected) {
2106 return ACTIVE_ENABLED_BACKGROUND_COLOR;
2107 }
2108 INACTIVE_ENABLED_BACKGROUND_COLOR
2109 });
2110 let group_value = Rc::new(Cell::new(Some(0)));
2111 for (value, enabled, expected) in [
2112 (0, true, ACTIVE_ENABLED_BACKGROUND_COLOR),
2113 (1, true, INACTIVE_ENABLED_BACKGROUND_COLOR),
2114 (0, false, ACTIVE_DISABLED_BACKGROUND_COLOR),
2115 (1, false, INACTIVE_DISABLED_BACKGROUND_COLOR),
2116 ] {
2117 group_value.set(Some(value));
2118 let background_color = Rc::clone(&background_color);
2119 let host = grouped_tile(&group_value, move |tile| {
2120 tile.radio_background_color(Rc::clone(&background_color))
2121 .enabled(enabled)
2122 });
2123 pump_wrapped(tester, |_app| host);
2124 tester.pump_and_settle();
2125 assert_paints(tester, radio_material::<i32>(tester), &[circle(expected)]);
2127 }
2128 });
2129 }
2130
2131 #[test]
2132 fn radio_list_tile_respects_radio_background_color_in_hovered_state() {
2133 test_widgets(|tester| {
2134 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
2135 const HOVERED_BACKGROUND_COLOR: Color = Color::new(0xFF000001);
2136 let background_color = colors(|states| {
2137 if states.contains(&WidgetState::Hovered) {
2138 return HOVERED_BACKGROUND_COLOR;
2139 }
2140 Colors::TRANSPARENT
2141 });
2142 pump_in_material(tester, m3, |_app| {
2143 let mut group = RadioGroup::new(
2144 Rc::new(|_app: &mut App, _value: Option<i32>| {}),
2145 RadioListTile::new(0).radio_background_color(background_color),
2146 );
2147 group.group_value = Some(0);
2148 group.into_widget()
2149 });
2150 tester.pump_and_settle();
2151
2152 let center = center_of_radio::<i32>(tester);
2154 hover_at(tester, center);
2155 tester.pump_and_settle();
2156 assert_paints(
2157 tester,
2158 radio_material::<i32>(tester),
2159 &[any_rect(), any_circle(), circle(HOVERED_BACKGROUND_COLOR)],
2160 );
2161 });
2162 }
2163
2164 #[test]
2165 fn radio_side_is_passed_to_the_radio() {
2166 test_widgets(|tester| {
2167 let side = BorderSide {
2168 color: Colors::RED.primary(),
2169 width: 3.0,
2170 ..BorderSide::default()
2171 };
2172 pump_in_material(tester, m3, |_app| {
2173 Center::new()
2174 .child(RadioListTile::new(true).radio_side(side))
2175 .into_widget()
2176 });
2177 let passed = tester.widget(nth::<Radio<bool>>(tester, 0), |radio: &Radio<bool>| {
2178 radio.side.clone()
2179 });
2180 assert!(passed == Some(side.into()));
2181 });
2182 }
2183
2184 #[test]
2185 fn radio_inner_radius_is_passed_to_the_radio() {
2186 test_widgets(|tester| {
2187 let inner_radius = <dyn WidgetStateProperty<Option<f64>>>::all(Some(6.0));
2188 let given = Rc::clone(&inner_radius);
2189 pump_in_material(tester, m3, |_app| {
2190 Center::new()
2191 .child(RadioListTile::new(true).radio_inner_radius(given))
2192 .into_widget()
2193 });
2194 let passed = tester.widget(nth::<Radio<bool>>(tester, 0), |radio: &Radio<bool>| {
2195 radio.inner_radius.clone()
2196 });
2197 assert!(passed.is_some_and(|passed| Rc::ptr_eq(&passed, &inner_radius)));
2198 });
2199 }
2200
2201 #[test]
2202 fn radio_list_tile_does_not_crash_at_zero_area() {
2203 test_widgets(|tester| {
2204 pump_app(tester, m3, |_app| {
2205 Scaffold::new()
2206 .body(Center::new().child(SizedBox::shrink().child(RadioListTile::new(true))))
2207 .into_widget()
2208 });
2209 assert_eq!(rect_of::<RadioListTile<bool>>(tester, 0).size(), Size::ZERO);
2210 });
2211 }
2212
2213 fn leading_title_tile() -> RadioListTile<i32> {
2214 RadioListTile::new(1)
2215 .group_value(1)
2216 .on_changed(no_op())
2217 .control_affinity(ListTileControlAffinity::Leading)
2218 .title(Text::new("title"))
2219 }
2220
2221 #[test]
2222 fn radio_list_tile_horizontal_title_gap_0() {
2223 test_widgets(|tester| {
2224 check_horizontal_title_gap_0(tester, [56.0, 744.0], |gap| {
2225 let mut tile = leading_title_tile();
2226 tile.horizontal_title_gap = gap;
2227 tile.into_widget()
2228 });
2229 });
2230 }
2231
2232 #[test]
2233 fn radio_list_tile_default_horizontal_title_gap_and_min_leading_width() {
2234 test_widgets(|tester| {
2235 check_default_horizontal_title_gap(tester, [72.0, 728.0], || {
2236 leading_title_tile().into_widget()
2237 });
2238 });
2239 }
2240
2241 #[test]
2242 fn radio_list_tile_horizontal_title_gap_with_visual_density() {
2243 test_widgets(|tester| {
2244 check_horizontal_title_gap_with_visual_density(tester, 58.0, || {
2245 leading_title_tile()
2246 .visual_density(VisualDensity::new(VisualDensity::MINIMUM_DENSITY, 0.0))
2247 .horizontal_title_gap(10.0)
2248 .into_widget()
2249 });
2250 });
2251 }
2252
2253 fn min_vertical_padding_tile(padding: Option<f64>) -> WidgetRef {
2254 let mut tile = RadioListTile::new(1)
2255 .group_value(1)
2256 .on_changed(no_op())
2257 .title(Text::new("title"));
2258 tile.min_vertical_padding = padding;
2259 tile.into_widget()
2260 }
2261
2262 #[test]
2263 fn radio_list_tile_min_vertical_padding_80_material_3() {
2264 test_widgets(|tester| {
2265 check_min_vertical_padding_80(tester, m3, 184.0, min_vertical_padding_tile);
2267 });
2268 }
2269
2270 #[test]
2271 fn radio_list_tile_min_vertical_padding_80_material_2() {
2272 test_widgets(|tester| {
2273 check_min_vertical_padding_80(tester, m2, 176.0, min_vertical_padding_tile);
2275 });
2276 }
2277
2278 #[test]
2279 fn radio_list_tile_min_leading_width_60() {
2280 test_widgets(|tester| {
2281 check_min_leading_width_60(tester, |width| {
2282 let mut tile = leading_title_tile();
2283 tile.min_leading_width = width;
2284 tile.into_widget()
2285 });
2286 });
2287 }
2288
2289 #[test]
2290 fn radio_list_tile_min_tile_height() {
2291 test_widgets(|tester| {
2292 check_min_tile_height(tester, |min_tile_height| {
2293 let mut tile = RadioListTile::new(1).group_value(1).on_changed(no_op());
2294 tile.min_tile_height = min_tile_height;
2295 tile.into_widget()
2296 });
2297 });
2298 }
2299}