1#![allow(deprecated)]
4
5use std::any::Any;
6use std::fmt::{self, Debug};
7use std::rc::Rc;
8
9use waft_embedder::{Brightness, Color, Offset, Size, TextBaseline, TextDirection};
10use waft_foundation::{
11 App, DiagnosticPropertiesBuilder, DiagnosticsNode, DiagnosticsProperty, DoubleProperty,
12 FlagProperty, Handle, Ref, ValueChanged,
13};
14use waft_gestures::{GestureLongPressCallback, GestureTapCallback};
15use waft_painting::{
16 AnyColor, Border, BoxDecoration, ColorProperty, EdgeInsets, EdgeInsetsGeometry, ShapeBorderRef,
17 ShapeDecoration, TextStyle,
18};
19use waft_rendering::{
20 AnyRenderBox, AnyRenderObject, BaselineOffset, BoxConstraints, BoxHitTestResult, BoxParentData,
21 ChildBaselineGetter, ChildLayoutHelper, ChildLayouter, DecorationPosition, PaintingContext,
22 PipelineOwner, RenderBox, RenderBoxData, RenderHandle, RenderObject, RenderObjectData,
23};
24use waft_services::MouseCursorRef;
25use waft_widgets::{
26 AnimatedDefaultTextStyle, BuildContext, ColoredBox, DecoratedBox, Directionality, FocusNodeRef,
27 IconTheme, IconThemeData, IntoWidget, KeyRef, RenderObjectWidget, SafeArea, Semantics,
28 SizedBox, SlottedContainerRenderObjectData, SlottedContainerRenderObjectMixin,
29 SlottedMultiChildRenderObjectWidget, StatelessWidget, WidgetRef, WidgetState, WidgetStateColor,
30 WidgetStateMouseCursor, WidgetStateProperty, WidgetStatePropertyAll, WidgetStatePropertyRef,
31 WidgetStates, downcast_widget,
32};
33
34use crate::colors::Colors;
35use crate::constants::K_THEME_CHANGE_DURATION;
36use crate::debug::debug_check_has_material;
37use crate::divider::Divider;
38use crate::generated::list_tile_defaults_m3::ListTileDefaultsM3;
39use crate::icon_button::IconButton;
40use crate::icon_button_theme::{IconButtonTheme, IconButtonThemeData};
41use crate::ink_decoration::Ink;
42use crate::ink_well::InkWell;
43use crate::list_tile_theme::{ListTileTheme, ListTileThemeData, ListTileThemeDataGetters};
44use crate::material::Material;
45use crate::material_state::MaterialStatesController;
46use crate::theme::Theme;
47use crate::theme_data::{ThemeData, VisualDensity};
48use crate::theme_data_getters::DefaultsContext;
49
50struct Sizes {
52 title_y: f64,
53 text_constraints: BoxConstraints,
54 tile_size: Size,
55}
56
57type PositionChild = fn(&mut App, AnyRenderBox, Offset);
59
60#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
66pub enum ListTileStyle {
67 List,
69
70 Drawer,
72}
73
74#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
85pub enum ListTileControlAffinity {
86 Leading,
89
90 Trailing,
93
94 Platform,
98}
99
100#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
113pub enum ListTileTitleAlignment {
114 ThreeLine,
121
122 TitleHeight,
133
134 Top,
137
138 Center,
141
142 Bottom,
146}
147
148impl ListTileTitleAlignment {
149 fn y_offset_for(
152 self,
153 app: &App,
154 child_height: f64,
155 tile_height: f64,
156 list_tile: RenderHandle<RenderListTile>,
157 is_leading: bool,
158 ) -> f64 {
159 match self {
160 ListTileTitleAlignment::ThreeLine => {
161 if list_tile.is_three_line(app) {
162 ListTileTitleAlignment::Top.y_offset_for(
163 app,
164 child_height,
165 tile_height,
166 list_tile,
167 is_leading,
168 )
169 } else {
170 ListTileTitleAlignment::Center.y_offset_for(
171 app,
172 child_height,
173 tile_height,
174 list_tile,
175 is_leading,
176 )
177 }
178 }
179 ListTileTitleAlignment::TitleHeight if tile_height > 72.0 => 16.0,
186 ListTileTitleAlignment::TitleHeight => {
190 if is_leading {
191 ((tile_height - child_height) / 2.0).min(16.0)
192 } else {
193 (tile_height - child_height) / 2.0
194 }
195 }
196 ListTileTitleAlignment::Top => list_tile.min_vertical_padding(app),
197 ListTileTitleAlignment::Center => (tile_height - child_height) / 2.0,
198 ListTileTitleAlignment::Bottom => {
199 tile_height - child_height - list_tile.min_vertical_padding(app)
200 }
201 }
202 }
203}
204
205#[derive(Clone)]
297pub struct ListTile {
298 pub key: Option<KeyRef>,
299
300 pub leading: Option<WidgetRef>,
304
305 pub title: Option<WidgetRef>,
312
313 pub subtitle: Option<WidgetRef>,
334
335 pub trailing: Option<WidgetRef>,
345
346 pub is_three_line: Option<bool>,
362
363 pub dense: Option<bool>,
372
373 pub visual_density: Option<VisualDensity>,
380
381 pub shape: Option<ShapeBorderRef>,
391
392 pub selected_color: Option<Color>,
402
403 pub icon_color: Option<AnyColor>,
424
425 pub text_color: Option<AnyColor>,
447
448 pub title_text_style: Option<TextStyle>,
456
457 pub subtitle_text_style: Option<TextStyle>,
464
465 pub leading_and_trailing_text_style: Option<TextStyle>,
474
475 pub style: Option<ListTileStyle>,
485
486 pub content_padding: Option<EdgeInsetsGeometry>,
497
498 pub enabled: bool,
504
505 pub on_tap: Option<GestureTapCallback>,
509
510 pub on_long_press: Option<GestureLongPressCallback>,
514
515 pub on_focus_change: Option<ValueChanged<bool>>,
520
521 pub mouse_cursor: Option<MouseCursorRef>,
533
534 pub selected: bool,
540
541 pub focus_color: Option<Color>,
543
544 pub hover_color: Option<Color>,
546
547 pub splash_color: Option<Color>,
549
550 pub focus_node: Option<FocusNodeRef>,
552
553 pub autofocus: bool,
556
557 pub tile_color: Option<Color>,
566
567 pub selected_tile_color: Option<Color>,
573
574 pub enable_feedback: Option<bool>,
585
586 pub horizontal_title_gap: Option<f64>,
591
592 pub min_vertical_padding: Option<f64>,
597
598 pub min_leading_width: Option<f64>,
603
604 pub min_tile_height: Option<f64>,
611
612 pub title_alignment: Option<ListTileTitleAlignment>,
625
626 pub internal_add_semantic_for_on_tap: bool,
631
632 pub states_controller: Option<Ref<MaterialStatesController>>,
636}
637
638impl Default for ListTile {
639 fn default() -> ListTile {
640 ListTile {
641 key: None,
642 leading: None,
643 title: None,
644 subtitle: None,
645 trailing: None,
646 is_three_line: None,
647 dense: None,
648 visual_density: None,
649 shape: None,
650 selected_color: None,
651 icon_color: None,
652 text_color: None,
653 title_text_style: None,
654 subtitle_text_style: None,
655 leading_and_trailing_text_style: None,
656 style: None,
657 content_padding: None,
658 enabled: true,
659 on_tap: None,
660 on_long_press: None,
661 on_focus_change: None,
662 mouse_cursor: None,
663 selected: false,
664 focus_color: None,
665 hover_color: None,
666 splash_color: None,
667 focus_node: None,
668 autofocus: false,
669 tile_color: None,
670 selected_tile_color: None,
671 enable_feedback: None,
672 horizontal_title_gap: None,
673 min_vertical_padding: None,
674 min_leading_width: None,
675 min_tile_height: None,
676 title_alignment: None,
677 internal_add_semantic_for_on_tap: true,
678 states_controller: None,
679 }
680 }
681}
682
683macro_rules! list_tile_setters {
685 ($($field:ident: $ty:ty => $name:literal),* $(,)?) => {
686 $(
687 #[doc = concat!("Dart `ListTile(", $name, ":)`.")]
688 pub fn $field(mut self, $field: impl Into<$ty>) -> ListTile {
689 self.$field = Some($field.into());
690 self
691 }
692 )*
693 };
694}
695
696macro_rules! list_tile_widget_setters {
698 ($($field:ident => $name:literal),* $(,)?) => {
699 $(
700 #[doc = concat!("Dart `ListTile(", $name, ":)`.")]
701 pub fn $field<K>(mut self, $field: impl IntoWidget<K>) -> ListTile {
702 self.$field = Some($field.into_widget());
703 self
704 }
705 )*
706 };
707}
708
709impl ListTile {
710 pub fn new() -> ListTile {
717 ListTile::default()
718 }
719
720 pub fn key(mut self, key: KeyRef) -> ListTile {
722 self.key = Some(key);
723 self
724 }
725
726 list_tile_widget_setters!(
727 leading => "leading",
728 title => "title",
729 subtitle => "subtitle",
730 trailing => "trailing",
731 );
732
733 pub fn is_three_line(mut self, is_three_line: bool) -> ListTile {
735 self.is_three_line = Some(is_three_line);
736 self
737 }
738
739 list_tile_setters!(
740 dense: bool => "dense",
741 visual_density: VisualDensity => "visualDensity",
742 shape: ShapeBorderRef => "shape",
743 style: ListTileStyle => "style",
744 selected_color: Color => "selectedColor",
745 icon_color: AnyColor => "iconColor",
746 text_color: AnyColor => "textColor",
747 title_text_style: TextStyle => "titleTextStyle",
748 subtitle_text_style: TextStyle => "subtitleTextStyle",
749 leading_and_trailing_text_style: TextStyle => "leadingAndTrailingTextStyle",
750 content_padding: EdgeInsetsGeometry => "contentPadding",
751 on_tap: GestureTapCallback => "onTap",
752 on_long_press: GestureLongPressCallback => "onLongPress",
753 on_focus_change: ValueChanged<bool> => "onFocusChange",
754 mouse_cursor: MouseCursorRef => "mouseCursor",
755 focus_color: Color => "focusColor",
756 hover_color: Color => "hoverColor",
757 splash_color: Color => "splashColor",
758 focus_node: FocusNodeRef => "focusNode",
759 tile_color: Color => "tileColor",
760 selected_tile_color: Color => "selectedTileColor",
761 enable_feedback: bool => "enableFeedback",
762 horizontal_title_gap: f64 => "horizontalTitleGap",
763 min_vertical_padding: f64 => "minVerticalPadding",
764 min_leading_width: f64 => "minLeadingWidth",
765 min_tile_height: f64 => "minTileHeight",
766 title_alignment: ListTileTitleAlignment => "titleAlignment",
767 states_controller: Ref<MaterialStatesController> => "statesController",
768 );
769
770 pub fn enabled(mut self, enabled: bool) -> ListTile {
772 self.enabled = enabled;
773 self
774 }
775
776 pub fn selected(mut self, selected: bool) -> ListTile {
778 self.selected = selected;
779 self
780 }
781
782 pub fn autofocus(mut self, autofocus: bool) -> ListTile {
784 self.autofocus = autofocus;
785 self
786 }
787
788 pub fn internal_add_semantic_for_on_tap(
790 mut self,
791 internal_add_semantic_for_on_tap: bool,
792 ) -> ListTile {
793 self.internal_add_semantic_for_on_tap = internal_add_semantic_for_on_tap;
794 self
795 }
796
797 pub fn divide_tiles(
804 app: &mut App,
805 context: Option<BuildContext>,
806 tiles: impl IntoIterator<Item = WidgetRef>,
807 color: Option<Color>,
808 ) -> Vec<WidgetRef> {
809 debug_assert!(color.is_some() || context.is_some());
810 let tiles: Vec<WidgetRef> = tiles.into_iter().collect();
811
812 if tiles.is_empty() || tiles.len() == 1 {
813 return tiles;
814 }
815
816 let mut wrap_tile = |tile: WidgetRef| -> WidgetRef {
817 DecoratedBox::new(BoxDecoration::new().border(Border {
818 bottom: Divider::create_border_side(app, context, color, None),
819 ..Border::default()
820 }))
821 .position(DecorationPosition::Foreground)
822 .child(tile)
823 .into_widget()
824 };
825
826 let last = tiles.len() - 1;
827 let mut divided: Vec<WidgetRef> =
828 tiles[..last].iter().cloned().map(&mut wrap_tile).collect();
829 divided.push(tiles[last].clone());
830 divided
831 }
832
833 fn is_dense_layout(&self, theme: &ThemeData, tile_theme: &ListTileThemeData) -> bool {
834 self.dense
835 .or(tile_theme.dense)
836 .or(theme.list_tile_theme.dense)
837 .unwrap_or(false)
838 }
839
840 fn debug_check_background_is_hidden(&self, app: &App, context: BuildContext) -> bool {
841 if cfg!(debug_assertions)
842 && let Some((intermediate_widget, intermediate_debug)) =
843 ListTile::find_intermediate_widget(app, context)
844 {
845 panic!(
846 "ListTile background color or ink splashes may be invisible.\nThe ListTile is \
847 wrapped in a {intermediate_widget} that has a background color. Because \
848 ListTile paints its background and ink splashes on the nearest Material \
849 ancestor, this {intermediate_widget} will hide those effects.\nTo fix this, \
850 wrap the ListTile in its own Material widget, or remove the background color \
851 from the intermediate {intermediate_widget}.\nListTile: {self:?}\n\
852 {intermediate_widget}: {intermediate_debug}"
853 );
854 }
855 true
856 }
857
858 fn find_intermediate_widget(
861 app: &App,
862 context: BuildContext,
863 ) -> Option<(&'static str, String)> {
864 let mut intermediate_widget = None;
865 context.visit_ancestor_elements(app, &mut |ancestor| {
866 let widget = ancestor.widget(app);
867 if downcast_widget::<Material>(&**widget).is_some() {
868 return false;
869 }
870 let found = if let Some(colored_box) = downcast_widget::<ColoredBox>(&**widget) {
871 Some(("ColoredBox", Some(colored_box.color.color())))
872 } else if let Some(decorated_box) = downcast_widget::<DecoratedBox>(&**widget) {
873 let decoration = decorated_box.decoration.as_any();
874 let color = if let Some(decoration) = decoration.downcast_ref::<BoxDecoration>() {
875 decoration.color.as_ref().map(AnyColor::color)
876 } else if let Some(decoration) = decoration.downcast_ref::<ShapeDecoration>() {
877 decoration.color.as_ref().map(AnyColor::color)
878 } else {
879 None
880 };
881 Some(("DecoratedBox", color))
882 } else {
883 None
884 };
885 if let Some((name, Some(color))) = found
886 && color.a > 0.0
887 {
888 intermediate_widget = Some((name, format!("{widget:?}")));
889 return false;
890 }
891 true
892 });
893 intermediate_widget
894 }
895}
896
897impl StatelessWidget for ListTile {
898 fn key(&self) -> Option<&KeyRef> {
899 self.key.as_ref()
900 }
901
902 fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
903 properties.add(
905 FlagProperty::new("isThreeLine", self.is_three_line)
906 .if_true("THREE_LINE")
907 .if_false("TWO_LINE")
908 .with_show_name(true)
909 .default_value(None),
910 );
911 properties.add(
912 FlagProperty::new("dense", self.dense)
913 .if_true("true")
914 .if_false("false")
915 .with_show_name(true)
916 .default_value(None),
917 );
918 properties.add(
919 DiagnosticsProperty::<crate::theme_data::VisualDensity>::new(
920 "visualDensity",
921 self.visual_density,
922 )
923 .default_value(None),
924 );
925 properties.add(
926 DiagnosticsProperty::<ShapeBorderRef>::new("shape", self.shape.clone())
927 .default_value(None),
928 );
929 properties.add(
930 DiagnosticsProperty::<waft_foundation::DartEnum<ListTileStyle>>::new(
931 "style",
932 self.style.map(waft_foundation::DartEnum),
933 )
934 .default_value(None),
935 );
936 properties
937 .add(ColorProperty::new("selectedColor", self.selected_color).default_value(None));
938 properties.add(
939 DiagnosticsProperty::<AnyColor>::new("iconColor", self.icon_color.clone())
940 .default_value(None),
941 );
942 properties.add(
943 DiagnosticsProperty::<AnyColor>::new("textColor", self.text_color.clone())
944 .default_value(None),
945 );
946 properties.add(
947 DiagnosticsProperty::<TextStyle>::new("titleTextStyle", self.title_text_style.clone())
948 .default_value(None),
949 );
950 properties.add(
951 DiagnosticsProperty::<TextStyle>::new(
952 "subtitleTextStyle",
953 self.subtitle_text_style.clone(),
954 )
955 .default_value(None),
956 );
957 properties.add(
958 DiagnosticsProperty::<TextStyle>::new(
959 "leadingAndTrailingTextStyle",
960 self.leading_and_trailing_text_style.clone(),
961 )
962 .default_value(None),
963 );
964 properties.add(
965 DiagnosticsProperty::<EdgeInsetsGeometry>::new("contentPadding", self.content_padding)
966 .default_value(None),
967 );
968 properties.add(
969 FlagProperty::new("enabled", Some(self.enabled))
970 .if_true("true")
971 .if_false("false")
972 .with_show_name(true)
973 .default_value(Some(true)),
974 );
975 properties.add(
976 DiagnosticsProperty::<String>::new(
977 "mouseCursor",
978 self.mouse_cursor
979 .as_ref()
980 .map(|cursor| cursor.debug_description()),
981 )
982 .default_value(None),
983 );
984 properties.add(
985 FlagProperty::new("selected", Some(self.selected))
986 .if_true("true")
987 .if_false("false")
988 .with_show_name(true)
989 .default_value(Some(false)),
990 );
991 properties.add(ColorProperty::new("focusColor", self.focus_color).default_value(None));
992 properties.add(ColorProperty::new("hoverColor", self.hover_color).default_value(None));
993 properties.add(
994 DiagnosticsProperty::<String>::new(
995 "focusNode",
996 self.focus_node.as_ref().map(|node| node.to_string(app)),
997 )
998 .default_value(None),
999 );
1000 properties.add(
1001 FlagProperty::new("autofocus", Some(self.autofocus))
1002 .if_true("true")
1003 .if_false("false")
1004 .with_show_name(true)
1005 .default_value(Some(false)),
1006 );
1007 properties.add(ColorProperty::new("tileColor", self.tile_color).default_value(None));
1008 properties.add(
1009 ColorProperty::new("selectedTileColor", self.selected_tile_color).default_value(None),
1010 );
1011 properties.add(
1012 FlagProperty::new("enableFeedback", self.enable_feedback)
1013 .if_true("true")
1014 .if_false("false")
1015 .with_show_name(true)
1016 .default_value(None),
1017 );
1018 properties.add(
1019 DoubleProperty::new("horizontalTitleGap", self.horizontal_title_gap)
1020 .default_value(None),
1021 );
1022 properties.add(
1023 DoubleProperty::new("minVerticalPadding", self.min_vertical_padding)
1024 .default_value(None),
1025 );
1026 properties.add(
1027 DoubleProperty::new("minLeadingWidth", self.min_leading_width).default_value(None),
1028 );
1029 properties.add(
1030 DiagnosticsProperty::<waft_foundation::DartEnum<ListTileTitleAlignment>>::new(
1031 "titleAlignment",
1032 self.title_alignment.map(waft_foundation::DartEnum),
1033 )
1034 .default_value(None),
1035 );
1036 }
1037
1038 fn debug_check_constructor(&self) {
1039 debug_assert!(self.is_three_line != Some(true) || self.subtitle.is_some());
1040 }
1041
1042 fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
1043 debug_assert!(debug_check_has_material(app, context));
1044 let theme = Theme::of(app, context);
1045 let icon_button_theme = IconButtonTheme::of(app, context);
1046 let tile_theme = ListTileTheme::of(app, context);
1047 let list_tile_style = self
1048 .style
1049 .or(tile_theme.style)
1050 .or(theme.list_tile_theme.style)
1051 .unwrap_or(ListTileStyle::List);
1052 let defaults: Box<dyn ListTileThemeDataGetters> = if theme.use_material3 {
1053 Box::new(ListTileDefaultsM3::new(context))
1054 } else {
1055 Box::new(LisTileDefaultsM2::new(context, list_tile_style))
1056 };
1057
1058 let background_color = self
1059 .tile_color
1060 .or(tile_theme.tile_color)
1061 .or(theme.list_tile_theme.tile_color)
1062 .unwrap_or_else(|| {
1063 defaults
1064 .tile_color(app)
1065 .expect("the defaults have a tile color")
1066 });
1067 let selected_background_color = self
1068 .selected_tile_color
1069 .or(tile_theme.selected_tile_color)
1070 .or(theme.list_tile_theme.selected_tile_color)
1071 .unwrap_or_else(|| {
1072 defaults
1073 .tile_color(app)
1074 .expect("the defaults have a tile color")
1075 });
1076 let effective_tile_color = if self.selected {
1077 selected_background_color
1078 } else {
1079 background_color
1080 };
1081 let has_opaque_background =
1082 background_color.alpha() > 0 || selected_background_color.alpha() > 0;
1083 if self.on_tap.is_some() || self.on_long_press.is_some() || has_opaque_background {
1084 debug_assert!(self.debug_check_background_is_hidden(app, context));
1085 }
1086 let mut states = WidgetStates::new();
1087 if !self.enabled {
1088 states.insert(WidgetState::Disabled);
1089 }
1090 if self.selected {
1091 states.insert(WidgetState::Selected);
1092 }
1093
1094 let resolve_color = |explicit_color: Option<&AnyColor>,
1095 selected_color: Option<Color>,
1096 enabled_color: Option<&AnyColor>,
1097 disabled_color: Option<Color>|
1098 -> Option<Color> {
1099 IndividualOverrides {
1100 explicit_color: explicit_color.cloned(),
1101 enabled_color: enabled_color.map(AnyColor::color),
1102 selected_color,
1103 disabled_color,
1104 }
1105 .resolve(&states)
1106 };
1107
1108 let mut effective_icon_color = resolve_color(
1109 self.icon_color.as_ref(),
1110 self.selected_color,
1111 self.icon_color.as_ref(),
1112 None,
1113 )
1114 .or_else(|| {
1115 resolve_color(
1116 tile_theme.icon_color.as_ref(),
1117 tile_theme.selected_color,
1118 tile_theme.icon_color.as_ref(),
1119 None,
1120 )
1121 })
1122 .or_else(|| {
1123 resolve_color(
1124 theme.list_tile_theme.icon_color.as_ref(),
1125 theme.list_tile_theme.selected_color,
1126 theme.list_tile_theme.icon_color.as_ref(),
1127 None,
1128 )
1129 });
1130
1131 let default_icon_color = defaults.icon_color(app);
1132 let default_effective_icon_color = resolve_color(
1133 default_icon_color.as_ref(),
1134 defaults.selected_color(app),
1135 default_icon_color.as_ref(),
1136 Some(theme.disabled_color),
1137 );
1138
1139 let effective_icon_button_color = effective_icon_color
1140 .or_else(|| {
1141 icon_button_theme
1142 .style
1143 .as_ref()
1144 .and_then(|style| style.foreground_color.as_ref())
1145 .and_then(|foreground_color| foreground_color.resolve(&states))
1146 })
1147 .or(default_effective_icon_color);
1148
1149 effective_icon_color = effective_icon_color.or(default_effective_icon_color);
1150
1151 let effective_color = resolve_color(
1152 self.text_color.as_ref(),
1153 self.selected_color,
1154 self.text_color.as_ref(),
1155 None,
1156 )
1157 .or_else(|| {
1158 resolve_color(
1159 tile_theme.text_color.as_ref(),
1160 tile_theme.selected_color,
1161 tile_theme.text_color.as_ref(),
1162 None,
1163 )
1164 })
1165 .or_else(|| {
1166 resolve_color(
1167 theme.list_tile_theme.text_color.as_ref(),
1168 theme.list_tile_theme.selected_color,
1169 theme.list_tile_theme.text_color.as_ref(),
1170 None,
1171 )
1172 })
1173 .or_else(|| {
1174 let default_text_color = defaults.text_color(app);
1175 resolve_color(
1176 default_text_color.as_ref(),
1177 defaults.selected_color(app),
1178 default_text_color.as_ref(),
1179 Some(theme.disabled_color),
1180 )
1181 });
1182 let mut icon_theme_data = IconThemeData::new();
1183 if let Some(effective_icon_color) = effective_icon_color {
1184 icon_theme_data = icon_theme_data.color(effective_icon_color);
1185 }
1186 let icon_button_theme_data =
1187 IconButtonThemeData::new().style(match IconButtonTheme::of(app, context).style {
1188 Some(style) => {
1189 style
1190 .copy_with()
1191 .foreground_color(Rc::new(WidgetStatePropertyAll::new(
1192 effective_icon_button_color,
1193 ))
1194 as WidgetStatePropertyRef<Option<Color>>)
1195 }
1196 None => {
1197 let mut style_from = IconButton::style_from();
1198 if let Some(effective_icon_button_color) = effective_icon_button_color {
1199 style_from = style_from.foreground_color(effective_icon_button_color);
1200 }
1201 style_from.build()
1202 }
1203 });
1204
1205 let mut leading_and_trailing_style: Option<TextStyle> = None;
1206 if self.leading.is_some() || self.trailing.is_some() {
1207 let style = self
1208 .leading_and_trailing_text_style
1209 .clone()
1210 .or(tile_theme.leading_and_trailing_text_style.clone())
1211 .unwrap_or_else(|| {
1212 defaults
1213 .leading_and_trailing_text_style(app)
1214 .expect("the defaults have a leading and trailing text style")
1215 });
1216 let leading_and_trailing_text_color = effective_color;
1217 leading_and_trailing_style = Some(with_color(&style, leading_and_trailing_text_color));
1218 }
1219
1220 let mut leading_icon: Option<WidgetRef> = None;
1221 if let Some(leading) = &self.leading {
1222 leading_icon = Some(
1223 AnimatedDefaultTextStyle::new(
1224 leading.clone(),
1225 leading_and_trailing_style
1226 .clone()
1227 .expect("a leading widget has a style"),
1228 K_THEME_CHANGE_DURATION,
1229 )
1230 .into_widget(),
1231 );
1232 }
1233
1234 let title_style = self
1235 .title_text_style
1236 .clone()
1237 .or(tile_theme.title_text_style.clone())
1238 .unwrap_or_else(|| {
1239 defaults
1240 .title_text_style(app)
1241 .expect("the defaults have a title text style")
1242 });
1243 let title_color = effective_color;
1244 let mut title_style = with_color(&title_style, title_color);
1245 if self.is_dense_layout(&theme, &tile_theme) {
1246 title_style = title_style.font_size(13.0);
1247 }
1248 let title_text = AnimatedDefaultTextStyle::new(
1249 self.title
1250 .clone()
1251 .unwrap_or_else(|| SizedBox::new().into_widget()),
1252 title_style.clone(),
1253 K_THEME_CHANGE_DURATION,
1254 )
1255 .into_widget();
1256
1257 let mut subtitle_text: Option<WidgetRef> = None;
1258 let mut subtitle_style: Option<TextStyle> = None;
1259 if let Some(subtitle) = &self.subtitle {
1260 let style = self
1261 .subtitle_text_style
1262 .clone()
1263 .or(tile_theme.subtitle_text_style.clone())
1264 .unwrap_or_else(|| {
1265 defaults
1266 .subtitle_text_style(app)
1267 .expect("the defaults have a subtitle text style")
1268 });
1269 let subtitle_color = effective_color;
1270 let mut style = with_color(&style, subtitle_color);
1271 if self.is_dense_layout(&theme, &tile_theme) {
1272 style = style.font_size(12.0);
1273 }
1274 subtitle_text = Some(
1275 AnimatedDefaultTextStyle::new(
1276 subtitle.clone(),
1277 style.clone(),
1278 K_THEME_CHANGE_DURATION,
1279 )
1280 .into_widget(),
1281 );
1282 subtitle_style = Some(style);
1283 }
1284
1285 let mut trailing_icon: Option<WidgetRef> = None;
1286 if let Some(trailing) = &self.trailing {
1287 trailing_icon = Some(
1288 AnimatedDefaultTextStyle::new(
1289 trailing.clone(),
1290 leading_and_trailing_style
1291 .clone()
1292 .expect("a trailing widget has a style"),
1293 K_THEME_CHANGE_DURATION,
1294 )
1295 .into_widget(),
1296 );
1297 }
1298
1299 let text_direction = Directionality::of(app, context);
1300 let resolved_content_padding: EdgeInsets = self
1301 .content_padding
1302 .map(|padding| padding.resolve(Some(text_direction)))
1303 .or_else(|| {
1304 tile_theme
1305 .content_padding
1306 .map(|padding| padding.resolve(Some(text_direction)))
1307 })
1308 .unwrap_or_else(|| {
1309 defaults
1310 .content_padding(app)
1311 .expect("the defaults have a content padding")
1312 .resolve(Some(text_direction))
1313 });
1314
1315 let mut mouse_states = WidgetStates::new();
1317 if !self.enabled || (self.on_tap.is_none() && self.on_long_press.is_none()) {
1318 mouse_states.insert(WidgetState::Disabled);
1319 }
1320 let effective_mouse_cursor: MouseCursorRef =
1321 <dyn WidgetStateProperty<MouseCursorRef>>::resolve_as(
1322 &self.mouse_cursor,
1323 &mouse_states,
1324 )
1325 .or_else(|| {
1326 tile_theme
1327 .mouse_cursor
1328 .as_ref()
1329 .and_then(|mouse_cursor| mouse_cursor.resolve(&mouse_states))
1330 })
1331 .unwrap_or_else(|| WidgetStateMouseCursor::clickable().resolve(&mouse_states));
1332
1333 let effective_title_alignment = self
1334 .title_alignment
1335 .or(tile_theme.title_alignment)
1336 .unwrap_or(if theme.use_material3 {
1337 ListTileTitleAlignment::ThreeLine
1338 } else {
1339 ListTileTitleAlignment::TitleHeight
1340 });
1341
1342 let is_dense = self.is_dense_layout(&theme, &tile_theme);
1343 let raw_list_tile = RawListTile {
1344 leading: leading_icon,
1345 title: title_text,
1346 subtitle: subtitle_text,
1347 trailing: trailing_icon,
1348 is_dense,
1349 visual_density: self
1350 .visual_density
1351 .or(tile_theme.visual_density)
1352 .unwrap_or(theme.visual_density),
1353 is_three_line: self
1354 .is_three_line
1355 .or(tile_theme.is_three_line)
1356 .or(theme.list_tile_theme.is_three_line)
1357 .unwrap_or(false),
1358 text_direction,
1359 title_baseline_type: title_style.text_baseline.unwrap_or_else(|| {
1360 defaults
1361 .title_text_style(app)
1362 .as_ref()
1363 .and_then(|style| style.text_baseline)
1364 .expect("the default title text style has a text baseline")
1365 }),
1366 subtitle_baseline_type: Some(
1367 subtitle_style
1368 .as_ref()
1369 .and_then(|style| style.text_baseline)
1370 .unwrap_or_else(|| {
1371 defaults
1372 .subtitle_text_style(app)
1373 .as_ref()
1374 .and_then(|style| style.text_baseline)
1375 .expect("the default subtitle text style has a text baseline")
1376 }),
1377 ),
1378 horizontal_title_gap: self
1379 .horizontal_title_gap
1380 .or(tile_theme.horizontal_title_gap)
1381 .unwrap_or(16.0),
1382 min_vertical_padding: self
1383 .min_vertical_padding
1384 .or(tile_theme.min_vertical_padding)
1385 .unwrap_or_else(|| {
1386 defaults
1387 .min_vertical_padding(app)
1388 .expect("the defaults have a min vertical padding")
1389 }),
1390 min_leading_width: self
1391 .min_leading_width
1392 .or(tile_theme.min_leading_width)
1393 .unwrap_or_else(|| {
1394 defaults
1395 .min_leading_width(app)
1396 .expect("the defaults have a min leading width")
1397 }),
1398 min_tile_height: self.min_tile_height.or(tile_theme.min_tile_height),
1399 title_alignment: effective_title_alignment,
1400 };
1401
1402 let mut ink_well = InkWell::new()
1403 .mouse_cursor(effective_mouse_cursor)
1404 .can_request_focus(self.enabled)
1405 .autofocus(self.autofocus)
1406 .enable_feedback(
1407 self.enable_feedback
1408 .or(tile_theme.enable_feedback)
1409 .unwrap_or(true),
1410 )
1411 .child(
1412 Semantics::new()
1413 .button(
1414 self.internal_add_semantic_for_on_tap
1415 && (self.on_tap.is_some() || self.on_long_press.is_some()),
1416 )
1417 .selected(self.selected)
1418 .enabled(self.enabled)
1419 .child(
1420 Ink::new()
1421 .decoration(
1422 ShapeDecoration::new(
1423 self.shape
1424 .clone()
1425 .or(tile_theme.shape.clone())
1426 .unwrap_or_else(|| ShapeBorderRef::new(Border::default())),
1427 )
1428 .color(effective_tile_color),
1429 )
1430 .child(
1431 SafeArea::new(IconTheme::merge(
1432 None,
1433 icon_theme_data,
1434 IconButtonTheme::new(icon_button_theme_data, raw_list_tile),
1435 ))
1436 .top(false)
1437 .bottom(false)
1438 .minimum(resolved_content_padding),
1439 ),
1440 ),
1441 );
1442 ink_well.ink_response.custom_border = self.shape.clone().or(tile_theme.shape.clone());
1443 ink_well.ink_response.on_tap = if self.enabled {
1444 self.on_tap.clone()
1445 } else {
1446 None
1447 };
1448 ink_well.ink_response.on_long_press = if self.enabled {
1449 self.on_long_press.clone()
1450 } else {
1451 None
1452 };
1453 ink_well.ink_response.on_focus_change = self.on_focus_change.clone();
1454 ink_well.ink_response.focus_node = self.focus_node.clone();
1455 ink_well.ink_response.focus_color = self.focus_color;
1456 ink_well.ink_response.hover_color = self.hover_color;
1457 ink_well.ink_response.splash_color = self.splash_color;
1458 ink_well.ink_response.states_controller = self.states_controller.clone();
1459 ink_well.into_widget()
1460 }
1461}
1462
1463fn with_color(style: &TextStyle, color: Option<Color>) -> TextStyle {
1465 match color {
1466 Some(color) => style.copy_with().color(color),
1467 None => style.copy_with(),
1468 }
1469}
1470
1471impl Debug for ListTile {
1472 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1473 let mut properties = f.debug_struct("ListTile");
1474 properties.field(
1475 "is_three_line",
1476 &match self.is_three_line {
1477 Some(true) => "THREE_LINE",
1478 Some(false) => "TWO_LINE",
1479 None => "null",
1480 },
1481 );
1482 properties.field("dense", &self.dense);
1483 if let Some(visual_density) = &self.visual_density {
1484 properties.field("visual_density", visual_density);
1485 }
1486 if let Some(shape) = &self.shape {
1487 properties.field("shape", shape);
1488 }
1489 if let Some(style) = &self.style {
1490 properties.field("style", style);
1491 }
1492 if let Some(selected_color) = &self.selected_color {
1493 properties.field("selected_color", selected_color);
1494 }
1495 if let Some(icon_color) = &self.icon_color {
1496 properties.field("icon_color", icon_color);
1497 }
1498 if let Some(text_color) = &self.text_color {
1499 properties.field("text_color", text_color);
1500 }
1501 if let Some(title_text_style) = &self.title_text_style {
1502 properties.field("title_text_style", title_text_style);
1503 }
1504 if let Some(subtitle_text_style) = &self.subtitle_text_style {
1505 properties.field("subtitle_text_style", subtitle_text_style);
1506 }
1507 if let Some(style) = &self.leading_and_trailing_text_style {
1508 properties.field("leading_and_trailing_text_style", style);
1509 }
1510 if let Some(content_padding) = &self.content_padding {
1511 properties.field("content_padding", content_padding);
1512 }
1513 if !self.enabled {
1514 properties.field("enabled", &false);
1515 }
1516 if let Some(on_tap) = &self.on_tap {
1517 properties.field("on_tap", on_tap);
1518 }
1519 if let Some(on_long_press) = &self.on_long_press {
1520 properties.field("on_long_press", on_long_press);
1521 }
1522 if let Some(mouse_cursor) = &self.mouse_cursor {
1523 properties.field("mouse_cursor", mouse_cursor);
1524 }
1525 if self.selected {
1526 properties.field("selected", &true);
1527 }
1528 if let Some(focus_color) = &self.focus_color {
1529 properties.field("focus_color", focus_color);
1530 }
1531 if let Some(hover_color) = &self.hover_color {
1532 properties.field("hover_color", hover_color);
1533 }
1534 if let Some(focus_node) = &self.focus_node {
1535 properties.field("focus_node", focus_node);
1536 }
1537 if self.autofocus {
1538 properties.field("autofocus", &true);
1539 }
1540 if let Some(tile_color) = &self.tile_color {
1541 properties.field("tile_color", tile_color);
1542 }
1543 if let Some(selected_tile_color) = &self.selected_tile_color {
1544 properties.field("selected_tile_color", selected_tile_color);
1545 }
1546 properties.field("enable_feedback", &self.enable_feedback);
1547 if let Some(horizontal_title_gap) = &self.horizontal_title_gap {
1548 properties.field("horizontal_title_gap", horizontal_title_gap);
1549 }
1550 if let Some(min_vertical_padding) = &self.min_vertical_padding {
1551 properties.field("min_vertical_padding", min_vertical_padding);
1552 }
1553 if let Some(min_leading_width) = &self.min_leading_width {
1554 properties.field("min_leading_width", min_leading_width);
1555 }
1556 if let Some(title_alignment) = &self.title_alignment {
1557 properties.field("title_alignment", title_alignment);
1558 }
1559 properties.finish()
1560 }
1561}
1562
1563#[derive(Debug)]
1565struct IndividualOverrides {
1566 explicit_color: Option<AnyColor>,
1567 enabled_color: Option<Color>,
1568 selected_color: Option<Color>,
1569 disabled_color: Option<Color>,
1570}
1571
1572impl WidgetStateProperty<Option<Color>> for IndividualOverrides {
1573 fn resolve(&self, states: &WidgetStates) -> Option<Color> {
1574 if let Some(explicit_color) = &self.explicit_color
1575 && explicit_color.extension::<WidgetStateColor>().is_some()
1576 {
1577 return <dyn WidgetStateProperty<Color>>::resolve_as(&self.explicit_color, states)
1578 .map(|color| color.color());
1579 }
1580 if states.contains(&WidgetState::Disabled) {
1581 return self.disabled_color;
1582 }
1583 if states.contains(&WidgetState::Selected) {
1584 return self.selected_color;
1585 }
1586 self.enabled_color
1587 }
1588
1589 fn as_any(&self) -> &dyn Any {
1590 self
1591 }
1592}
1593
1594#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1596enum ListTileSlot {
1597 Leading,
1598 Title,
1599 Subtitle,
1600 Trailing,
1601}
1602
1603#[derive(Debug)]
1605struct RawListTile {
1606 leading: Option<WidgetRef>,
1607 title: WidgetRef,
1608 subtitle: Option<WidgetRef>,
1609 trailing: Option<WidgetRef>,
1610 is_three_line: bool,
1611 is_dense: bool,
1612 visual_density: VisualDensity,
1613 text_direction: TextDirection,
1614 title_baseline_type: TextBaseline,
1615 subtitle_baseline_type: Option<TextBaseline>,
1616 horizontal_title_gap: f64,
1617 min_vertical_padding: f64,
1618 min_leading_width: f64,
1619 min_tile_height: Option<f64>,
1620 title_alignment: ListTileTitleAlignment,
1621}
1622
1623impl SlottedMultiChildRenderObjectWidget for RawListTile {
1624 type Slot = ListTileSlot;
1625
1626 fn slots(&self) -> Vec<ListTileSlot> {
1627 vec![
1628 ListTileSlot::Leading,
1629 ListTileSlot::Title,
1630 ListTileSlot::Subtitle,
1631 ListTileSlot::Trailing,
1632 ]
1633 }
1634
1635 fn child_for_slot(&self, slot: &ListTileSlot) -> Option<WidgetRef> {
1636 match slot {
1637 ListTileSlot::Leading => self.leading.clone(),
1638 ListTileSlot::Title => Some(self.title.clone()),
1639 ListTileSlot::Subtitle => self.subtitle.clone(),
1640 ListTileSlot::Trailing => self.trailing.clone(),
1641 }
1642 }
1643}
1644
1645impl RenderObjectWidget for RawListTile {
1646 type RenderObject = RenderListTile;
1647
1648 fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
1649 RenderListTile::new(
1650 app,
1651 self.is_dense,
1652 self.visual_density,
1653 self.is_three_line,
1654 self.text_direction,
1655 self.title_baseline_type,
1656 self.subtitle_baseline_type,
1657 self.horizontal_title_gap,
1658 self.min_vertical_padding,
1659 self.min_leading_width,
1660 self.min_tile_height,
1661 self.title_alignment,
1662 )
1663 .as_object()
1664 }
1665
1666 fn update_render_object(
1667 &self,
1668 app: &mut App,
1669 _context: BuildContext,
1670 render_object: RenderHandle<RenderListTile>,
1671 ) {
1672 render_object.set_is_three_line(app, self.is_three_line);
1673 render_object.set_is_dense(app, self.is_dense);
1674 render_object.set_visual_density(app, self.visual_density);
1675 render_object.set_text_direction(app, self.text_direction);
1676 render_object.set_title_baseline_type(app, self.title_baseline_type);
1677 render_object.set_subtitle_baseline_type(app, self.subtitle_baseline_type);
1678 render_object.set_horizontal_title_gap(app, self.horizontal_title_gap);
1679 render_object.set_min_leading_width(app, self.min_leading_width);
1680 render_object.set_min_tile_height(app, self.min_tile_height);
1681 render_object.set_min_vertical_padding(app, self.min_vertical_padding);
1682 render_object.set_title_alignment(app, self.title_alignment);
1683 }
1684}
1685
1686struct RenderListTile {
1688 render_object: RenderObjectData,
1689 render_box: RenderBoxData,
1690 slotted: SlottedContainerRenderObjectData<ListTileSlot, AnyRenderBox>,
1691 is_dense: bool,
1692 visual_density: VisualDensity,
1693 is_three_line: bool,
1694 text_direction: TextDirection,
1695 title_baseline_type: TextBaseline,
1696 subtitle_baseline_type: Option<TextBaseline>,
1697 horizontal_title_gap: f64,
1698 min_vertical_padding: f64,
1699 min_leading_width: f64,
1700 min_tile_height: Option<f64>,
1701 title_alignment: ListTileTitleAlignment,
1702}
1703
1704macro_rules! render_list_tile_property {
1707 ($($field:ident, $setter:ident: $ty:ty;)*) => {
1708 $(
1709 fn $field(self: RenderHandle<Self>, app: &App) -> $ty {
1710 self.get(app).$field
1711 }
1712
1713 fn $setter(self: RenderHandle<Self>, app: &mut App, value: $ty) {
1714 if self.get(app).$field == value {
1715 return;
1716 }
1717 self.get_mut(app).$field = value;
1718 self.mark_needs_layout(app);
1719 }
1720 )*
1721 };
1722}
1723
1724impl RenderListTile {
1725 #[allow(clippy::too_many_arguments)]
1726 fn new(
1727 app: &mut App,
1728 is_dense: bool,
1729 visual_density: VisualDensity,
1730 is_three_line: bool,
1731 text_direction: TextDirection,
1732 title_baseline_type: TextBaseline,
1733 subtitle_baseline_type: Option<TextBaseline>,
1734 horizontal_title_gap: f64,
1735 min_vertical_padding: f64,
1736 min_leading_width: f64,
1737 min_tile_height: Option<f64>,
1738 title_alignment: ListTileTitleAlignment,
1739 ) -> RenderHandle<RenderListTile> {
1740 RenderHandle::new_box(
1741 app,
1742 RenderListTile {
1743 render_object: RenderObjectData::new(),
1744 render_box: RenderBoxData::new(),
1745 slotted: SlottedContainerRenderObjectData::new(),
1746 is_dense,
1747 visual_density,
1748 is_three_line,
1749 text_direction,
1750 title_baseline_type,
1751 subtitle_baseline_type,
1752 horizontal_title_gap,
1753 min_vertical_padding,
1754 min_leading_width,
1755 min_tile_height,
1756 title_alignment,
1757 },
1758 )
1759 }
1760
1761 fn leading(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1762 self.child_for_slot(app, &ListTileSlot::Leading)
1763 }
1764
1765 fn title(self: RenderHandle<Self>, app: &App) -> AnyRenderBox {
1766 self.child_for_slot(app, &ListTileSlot::Title)
1767 .expect("a list tile always has a title")
1768 }
1769
1770 fn subtitle(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1771 self.child_for_slot(app, &ListTileSlot::Subtitle)
1772 }
1773
1774 fn trailing(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1775 self.child_for_slot(app, &ListTileSlot::Trailing)
1776 }
1777
1778 render_list_tile_property! {
1779 is_dense, set_is_dense: bool;
1780 visual_density, set_visual_density: VisualDensity;
1781 is_three_line, set_is_three_line: bool;
1782 text_direction, set_text_direction: TextDirection;
1783 title_baseline_type, set_title_baseline_type: TextBaseline;
1784 subtitle_baseline_type, set_subtitle_baseline_type: Option<TextBaseline>;
1785 horizontal_title_gap, set_horizontal_title_gap: f64;
1786 min_vertical_padding, set_min_vertical_padding: f64;
1787 min_leading_width, set_min_leading_width: f64;
1788 min_tile_height, set_min_tile_height: Option<f64>;
1789 title_alignment, set_title_alignment: ListTileTitleAlignment;
1790 }
1791
1792 fn effective_horizontal_title_gap(self: RenderHandle<Self>, app: &App) -> f64 {
1793 self.horizontal_title_gap(app) + self.visual_density(app).horizontal * 2.0
1794 }
1795
1796 fn min_width(app: &mut App, r#box: Option<AnyRenderBox>, height: f64) -> f64 {
1797 match r#box {
1798 None => 0.0,
1799 Some(r#box) => r#box.get_min_intrinsic_width(app, height),
1800 }
1801 }
1802
1803 fn max_width(app: &mut App, r#box: Option<AnyRenderBox>, height: f64) -> f64 {
1804 match r#box {
1805 None => 0.0,
1806 Some(r#box) => r#box.get_max_intrinsic_width(app, height),
1807 }
1808 }
1809
1810 fn default_tile_height(self: RenderHandle<Self>, app: &App) -> f64 {
1812 let base_density = self.visual_density(app).base_size_adjustment();
1813 let is_dense = self.is_dense(app);
1814 base_density.dy()
1815 + match (self.is_three_line(app), self.subtitle(app).is_some()) {
1816 (true, _) => {
1817 if is_dense {
1818 76.0
1819 } else {
1820 88.0
1821 }
1822 } (false, true) => {
1824 if is_dense {
1825 64.0
1826 } else {
1827 72.0
1828 }
1829 } (false, false) => {
1831 if is_dense {
1832 48.0
1833 } else {
1834 56.0
1835 }
1836 } }
1838 }
1839
1840 fn target_tile_height(self: RenderHandle<Self>, app: &App) -> f64 {
1841 self.min_tile_height(app)
1842 .unwrap_or_else(|| self.default_tile_height(app))
1843 }
1844
1845 fn max_icon_height_constraint(self: RenderHandle<Self>, app: &App) -> BoxConstraints {
1846 BoxConstraints {
1847 max_height: (if self.is_dense(app) { 48.0 } else { 56.0 })
1853 + self.visual_density(app).base_size_adjustment().dy(),
1854 ..BoxConstraints::new()
1855 }
1856 }
1857
1858 fn position_box(app: &mut App, r#box: AnyRenderBox, offset: Offset) {
1859 r#box.parent_data_of_mut::<BoxParentData>(app).offset = offset;
1860 }
1861
1862 fn compute_sizes(
1868 self: RenderHandle<Self>,
1869 app: &mut App,
1870 get_baseline: ChildBaselineGetter,
1871 get_size: ChildLayouter,
1872 constraints: BoxConstraints,
1873 position_child: Option<PositionChild>,
1874 ) -> Sizes {
1875 let loose_constraints = constraints.loosen();
1876 let tile_width = loose_constraints.max_width;
1877 let icon_constraints = loose_constraints.enforce(self.max_icon_height_constraint(app));
1878 let leading = self.leading(app);
1879 let trailing = self.trailing(app);
1880
1881 let leading_size = leading.map(|leading| get_size(app, leading, icon_constraints));
1882 let trailing_size = trailing.map(|trailing| get_size(app, trailing, icon_constraints));
1883
1884 if cfg!(debug_assertions) && tile_width != 0.0 {
1885 let overflowed_widget = if Some(tile_width) == leading_size.map(|size| size.width()) {
1886 Some("Leading")
1887 } else if Some(tile_width) == trailing_size.map(|size| size.width()) {
1888 Some("Trailing")
1889 } else {
1890 None
1891 };
1892
1893 if let Some(overflowed_widget) = overflowed_widget {
1894 panic!(
1895 "{overflowed_widget} widget consumes the entire tile width (including \
1896 ListTile.contentPadding).\nEither resize the tile width so that the {} \
1897 widget plus any content padding do not exceed the tile width, or use a \
1898 sized widget, or consider replacing ListTile with a custom widget.\nSee \
1899 also: https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.4",
1900 overflowed_widget.to_lowercase()
1901 );
1902 }
1903 }
1904
1905 let title_start = match leading_size {
1906 None => 0.0,
1907 Some(leading_size) => {
1908 self.min_leading_width(app).max(leading_size.width())
1909 + self.effective_horizontal_title_gap(app)
1910 }
1911 };
1912
1913 let adjusted_trailing_width = match trailing_size {
1914 None => 0.0,
1915 Some(trailing_size) => {
1916 (trailing_size.width() + self.effective_horizontal_title_gap(app)).max(32.0)
1917 }
1918 };
1919
1920 let text_constraints = loose_constraints.tighten(
1921 Some(tile_width - title_start - adjusted_trailing_width),
1922 None,
1923 );
1924
1925 let subtitle = self.subtitle(app);
1926 let title = self.title(app);
1927 let title_height = get_size(app, title, text_constraints).height();
1928
1929 let is_ltr = match self.text_direction(app) {
1930 TextDirection::Ltr => true,
1931 TextDirection::Rtl => false,
1932 };
1933
1934 let title_y;
1935 let tile_height;
1936 let min_vertical_padding = self.min_vertical_padding(app);
1937 match subtitle {
1938 None => {
1939 tile_height = self
1940 .target_tile_height(app)
1941 .max(title_height + 2.0 * min_vertical_padding);
1942 title_y = (tile_height - title_height) / 2.0;
1943 }
1944 Some(subtitle) => {
1945 let subtitle_height = get_size(app, subtitle, text_constraints).height();
1946 let title_baseline_type = self.title_baseline_type(app);
1947 let title_baseline =
1948 get_baseline(app, title, text_constraints, title_baseline_type)
1949 .unwrap_or(title_height);
1950 let subtitle_baseline_type = self
1951 .subtitle_baseline_type(app)
1952 .expect("a list tile with a subtitle has a subtitle baseline type");
1953 let subtitle_baseline =
1954 get_baseline(app, subtitle, text_constraints, subtitle_baseline_type)
1955 .unwrap_or(subtitle_height);
1956
1957 let is_three_line = self.is_three_line(app);
1958 let is_dense = self.is_dense(app);
1959 let target_title_y = (if is_three_line {
1960 if is_dense { 22.0 } else { 28.0 }
1961 } else if is_dense {
1962 28.0
1963 } else {
1964 32.0
1965 }) - title_baseline;
1966 let target_subtitle_y = (if is_three_line {
1967 if is_dense { 42.0 } else { 48.0 }
1968 } else if is_dense {
1969 48.0
1970 } else {
1971 52.0
1972 }) + self.visual_density(app).vertical * 2.0
1973 - subtitle_baseline;
1974 let half_overlap =
1977 (target_title_y + title_height - target_subtitle_y).max(0.0) / 2.0;
1978 let ideal_title_y = target_title_y - half_overlap;
1979 let ideal_subtitle_y = target_subtitle_y + half_overlap;
1980 let target_tile_height = self.target_tile_height(app);
1982 let compact = ideal_title_y < min_vertical_padding
1983 || ideal_subtitle_y + subtitle_height + min_vertical_padding
1984 > target_tile_height;
1985
1986 if let Some(position_child) = position_child {
1988 position_child(
1989 app,
1990 subtitle,
1991 Offset::new(
1992 if is_ltr {
1993 title_start
1994 } else {
1995 adjusted_trailing_width
1996 },
1997 if compact {
1998 min_vertical_padding + title_height
1999 } else {
2000 ideal_subtitle_y
2001 },
2002 ),
2003 );
2004 }
2005 tile_height = if compact {
2006 2.0 * min_vertical_padding + title_height + subtitle_height
2007 } else {
2008 target_tile_height
2009 };
2010 title_y = if compact {
2011 min_vertical_padding
2012 } else {
2013 ideal_title_y
2014 };
2015 }
2016 }
2017
2018 if let Some(position_child) = position_child {
2019 position_child(
2020 app,
2021 title,
2022 Offset::new(
2023 if is_ltr {
2024 title_start
2025 } else {
2026 adjusted_trailing_width
2027 },
2028 title_y,
2029 ),
2030 );
2031
2032 let title_alignment = self.title_alignment(app);
2033 if let (Some(leading), Some(leading_size)) = (leading, leading_size) {
2034 let dy = title_alignment.y_offset_for(
2035 app,
2036 leading_size.height(),
2037 tile_height,
2038 self,
2039 true,
2040 );
2041 position_child(
2042 app,
2043 leading,
2044 Offset::new(
2045 if is_ltr {
2046 0.0
2047 } else {
2048 tile_width - leading_size.width()
2049 },
2050 dy,
2051 ),
2052 );
2053 }
2054
2055 if let (Some(trailing), Some(trailing_size)) = (trailing, trailing_size) {
2056 let dy = title_alignment.y_offset_for(
2057 app,
2058 trailing_size.height(),
2059 tile_height,
2060 self,
2061 false,
2062 );
2063 position_child(
2064 app,
2065 trailing,
2066 Offset::new(
2067 if is_ltr {
2068 tile_width - trailing_size.width()
2069 } else {
2070 0.0
2071 },
2072 dy,
2073 ),
2074 );
2075 }
2076 }
2077
2078 Sizes {
2079 title_y,
2080 text_constraints,
2081 tile_size: Size::new(tile_width, tile_height),
2082 }
2083 }
2084}
2085
2086impl SlottedContainerRenderObjectMixin<ListTileSlot> for RenderListTile {
2087 type ChildType = AnyRenderBox;
2088
2089 fn slotted_container_data(
2090 self: RenderHandle<Self>,
2091 app: &App,
2092 ) -> &SlottedContainerRenderObjectData<ListTileSlot, AnyRenderBox> {
2093 &self.get(app).slotted
2094 }
2095
2096 fn slotted_container_data_mut(
2097 self: RenderHandle<Self>,
2098 app: &mut App,
2099 ) -> &mut SlottedContainerRenderObjectData<ListTileSlot, AnyRenderBox> {
2100 &mut self.get_mut(app).slotted
2101 }
2102
2103 fn children(self: RenderHandle<Self>, app: &App) -> Vec<AnyRenderBox> {
2105 let title = self.child_for_slot(app, &ListTileSlot::Title);
2106 [
2107 self.leading(app),
2108 title,
2109 self.subtitle(app),
2110 self.trailing(app),
2111 ]
2112 .into_iter()
2113 .flatten()
2114 .collect()
2115 }
2116}
2117
2118impl RenderObject for RenderListTile {
2119 waft_rendering::render_object_accessors!();
2120
2121 fn debug_describe_children(
2122 self: RenderHandle<Self>,
2123 app: &App,
2124 ) -> Vec<Rc<dyn DiagnosticsNode>> {
2125 SlottedContainerRenderObjectMixin::debug_describe_children(self, app)
2126 }
2127
2128 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
2129 SlottedContainerRenderObjectMixin::did_attach(self, app, owner);
2130 }
2131
2132 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
2133 SlottedContainerRenderObjectMixin::did_detach(self, app);
2134 }
2135
2136 fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
2137 SlottedContainerRenderObjectMixin::redepth_children(self, app);
2138 }
2139
2140 fn visit_children(
2141 self: RenderHandle<Self>,
2142 app: &App,
2143 visitor: &mut dyn FnMut(AnyRenderObject),
2144 ) {
2145 SlottedContainerRenderObjectMixin::visit_children(self, app, visitor);
2146 }
2147
2148 fn sized_by_parent(self: RenderHandle<Self>, _app: &App) -> bool {
2149 false
2150 }
2151
2152 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
2153 let constraints = self.constraints(app);
2154 let tile_size = self
2155 .compute_sizes(
2156 app,
2157 ChildLayoutHelper::get_baseline,
2158 ChildLayoutHelper::layout_child,
2159 constraints,
2160 Some(RenderListTile::position_box),
2161 )
2162 .tile_size;
2163
2164 let size = constraints.constrain(tile_size);
2165 self.set_size(app, size);
2166 debug_assert!(size.width() == constraints.constrain_width(tile_size.width()));
2167 debug_assert!(size.height() == constraints.constrain_height(tile_size.height()));
2168 }
2169
2170 fn paint(
2171 self: RenderHandle<Self>,
2172 app: &mut App,
2173 context: &mut PaintingContext,
2174 offset: Offset,
2175 ) {
2176 let mut do_paint = |app: &mut App, child: Option<AnyRenderBox>| {
2177 if let Some(child) = child {
2178 let child_offset = child.box_parent_data(app).offset;
2179 context.paint_child(app, child.as_object(), child_offset + offset);
2180 }
2181 };
2182
2183 do_paint(app, self.leading(app));
2184 do_paint(app, Some(self.title(app)));
2185 do_paint(app, self.subtitle(app));
2186 do_paint(app, self.trailing(app));
2187 }
2188}
2189
2190impl RenderBox for RenderListTile {
2191 waft_rendering::render_box_accessors!();
2192
2193 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
2194 let leading_width = match self.leading(app) {
2195 Some(leading) => {
2196 leading
2197 .get_min_intrinsic_width(app, height)
2198 .max(self.min_leading_width(app))
2199 + self.effective_horizontal_title_gap(app)
2200 }
2201 None => 0.0,
2202 };
2203 let title = Some(self.title(app));
2204 let subtitle = self.subtitle(app);
2205 let trailing = self.trailing(app);
2206 leading_width
2207 + RenderListTile::min_width(app, title, height)
2208 .max(RenderListTile::min_width(app, subtitle, height))
2209 + RenderListTile::max_width(app, trailing, height)
2210 }
2211
2212 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
2213 let leading_width = match self.leading(app) {
2214 Some(leading) => {
2215 leading
2216 .get_max_intrinsic_width(app, height)
2217 .max(self.min_leading_width(app))
2218 + self.effective_horizontal_title_gap(app)
2219 }
2220 None => 0.0,
2221 };
2222 let title = Some(self.title(app));
2223 let subtitle = self.subtitle(app);
2224 let trailing = self.trailing(app);
2225 leading_width
2226 + RenderListTile::max_width(app, title, height)
2227 .max(RenderListTile::max_width(app, subtitle, height))
2228 + RenderListTile::max_width(app, trailing, height)
2229 }
2230
2231 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
2232 let title_min_height = self.title(app).get_min_intrinsic_height(app, width);
2233 let subtitle_min_height = self
2234 .subtitle(app)
2235 .map(|subtitle| subtitle.get_min_intrinsic_height(app, width));
2236
2237 const TOP_AND_BOTTOM_PADDING_MULTIPLIER: f64 = 2.0;
2238 let content_height = title_min_height
2239 + subtitle_min_height.unwrap_or(0.0)
2240 + TOP_AND_BOTTOM_PADDING_MULTIPLIER * self.min_vertical_padding(app);
2241
2242 self.target_tile_height(app).max(content_height)
2243 }
2244
2245 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
2246 self.as_box().get_min_intrinsic_height(app, width)
2247 }
2248
2249 fn compute_distance_to_actual_baseline(
2250 self: RenderHandle<Self>,
2251 app: &mut App,
2252 baseline: TextBaseline,
2253 ) -> Option<f64> {
2254 let title = self.title(app);
2255 let offset_dy = title.box_parent_data(app).offset.dy();
2256 let offset =
2257 BaselineOffset(title.get_distance_to_actual_baseline(app, baseline)) + offset_dy;
2258 offset.offset()
2259 }
2260
2261 fn compute_dry_baseline(
2262 self: RenderHandle<Self>,
2263 app: &mut App,
2264 constraints: BoxConstraints,
2265 baseline: TextBaseline,
2266 ) -> Option<f64> {
2267 let sizes = self.compute_sizes(
2268 app,
2269 ChildLayoutHelper::get_dry_baseline,
2270 ChildLayoutHelper::dry_layout_child,
2271 constraints,
2272 None,
2273 );
2274 let title_baseline = BaselineOffset(self.title(app).get_dry_baseline(
2275 app,
2276 sizes.text_constraints,
2277 baseline,
2278 )) + sizes.title_y;
2279 title_baseline.offset()
2280 }
2281
2282 fn compute_dry_layout(
2283 self: RenderHandle<Self>,
2284 app: &mut App,
2285 constraints: BoxConstraints,
2286 ) -> Size {
2287 constraints.constrain(
2288 self.compute_sizes(
2289 app,
2290 ChildLayoutHelper::get_dry_baseline,
2291 ChildLayoutHelper::dry_layout_child,
2292 constraints,
2293 None,
2294 )
2295 .tile_size,
2296 )
2297 }
2298
2299 fn hit_test_self(self: RenderHandle<Self>, _app: &App, _position: Offset) -> bool {
2300 true
2301 }
2302
2303 fn hit_test_children(
2304 self: RenderHandle<Self>,
2305 app: &mut App,
2306 result: &mut BoxHitTestResult<'_>,
2307 position: Offset,
2308 ) -> bool {
2309 for child in SlottedContainerRenderObjectMixin::children(self, app) {
2310 let offset = child.box_parent_data(app).offset;
2311 let is_hit =
2312 result.add_with_paint_offset(Some(offset), position, |result, transformed| {
2313 debug_assert!(transformed == position - offset);
2314 child.hit_test(app, result, transformed)
2315 });
2316 if is_hit {
2317 return true;
2318 }
2319 }
2320 false
2321 }
2322}
2323
2324struct LisTileDefaultsM2 {
2326 list_tile_theme_data: ListTileThemeData,
2328 context: DefaultsContext,
2329}
2330
2331impl LisTileDefaultsM2 {
2332 fn new(context: BuildContext, style: ListTileStyle) -> LisTileDefaultsM2 {
2333 LisTileDefaultsM2 {
2334 list_tile_theme_data: ListTileThemeData::new()
2335 .content_padding(EdgeInsets::symmetric(0.0, 16.0))
2336 .min_leading_width(40.0)
2337 .min_vertical_padding(4.0)
2338 .shape(Border::default())
2339 .style(style),
2340 context: DefaultsContext::new(context),
2341 }
2342 }
2343}
2344
2345impl ListTileThemeDataGetters for LisTileDefaultsM2 {
2346 fn list_tile_theme_data(&self) -> &ListTileThemeData {
2347 &self.list_tile_theme_data
2348 }
2349
2350 fn tile_color(&self, _app: &mut App) -> Option<Color> {
2351 Some(Colors::TRANSPARENT)
2352 }
2353
2354 fn title_text_style(&self, app: &mut App) -> Option<TextStyle> {
2355 let text_theme = self.context.text_theme(app);
2356 match self
2357 .list_tile_theme_data
2358 .style
2359 .expect("the constructor sets a style")
2360 {
2361 ListTileStyle::Drawer => text_theme.body_large.clone(),
2362 ListTileStyle::List => text_theme.title_medium.clone(),
2363 }
2364 }
2365
2366 fn subtitle_text_style(&self, app: &mut App) -> Option<TextStyle> {
2367 let text_theme = self.context.text_theme(app);
2368 let body_medium = text_theme
2369 .body_medium
2370 .as_ref()
2371 .expect("the text theme has a body medium style");
2372 let body_small = text_theme
2373 .body_small
2374 .as_ref()
2375 .expect("the text theme has a body small style");
2376 Some(match &body_small.color {
2377 Some(color) => body_medium.copy_with().color(color.clone()),
2378 None => body_medium.copy_with(),
2379 })
2380 }
2381
2382 fn leading_and_trailing_text_style(&self, app: &mut App) -> Option<TextStyle> {
2383 self.context.text_theme(app).body_medium.clone()
2384 }
2385
2386 fn selected_color(&self, app: &mut App) -> Option<Color> {
2387 Some(self.context.colors(app).primary())
2388 }
2389
2390 fn icon_color(&self, app: &mut App) -> Option<AnyColor> {
2391 match self.context.theme(app).brightness() {
2392 Brightness::Light => Some(AnyColor::new(Colors::BLACK45)),
2395 Brightness::Dark => None,
2397 }
2398 }
2399}
2400
2401#[cfg(test)]
2402pub(crate) mod tests {
2403 use std::cell::{Cell, RefCell};
2404 use std::rc::Rc;
2405 use std::time::Duration;
2406 use waft_painting::OutlinedBorderRef;
2407
2408 use waft_embedder::{RRect, Rect};
2409 use waft_foundation::{Listenable, Listener};
2410 use waft_painting::{
2411 AlignmentGeometry, EdgeInsetsDirectional, RoundedRectangleBorder, StadiumBorder, TextScaler,
2412 };
2413 use waft_services::SystemMouseCursors;
2414 use waft_widgets::{
2415 Builder, Center, Container, FocusHighlightStrategy, FocusNode, GlobalKey, ListView,
2416 MediaQuery, MediaQueryData, MouseRegion, StatefulBuilder, Text,
2417 };
2418
2419 use waft_widgets::testing::{
2420 TestPointer, WidgetTester, expect, find, finds_one_widget, test_widgets,
2421 };
2422
2423 use super::*;
2424 use crate::colors::Colors;
2425 use crate::scaffold::Scaffold;
2426 use crate::test_support::{PaintInspection, material_app};
2427 use waft_rendering::testing::{PaintCall, assert_paints, draws, paints_exactly};
2428 use waft_widgets::AnyFocusNode;
2429
2430 pub(crate) fn m3(app: &App) -> ThemeData {
2432 ThemeData::builder().build(app)
2433 }
2434
2435 pub(crate) fn m2(app: &App) -> ThemeData {
2437 ThemeData::builder().use_material3(false).build(app)
2438 }
2439
2440 pub(crate) fn pump_app(
2442 tester: &mut WidgetTester,
2443 theme: impl FnOnce(&App) -> ThemeData,
2444 home: impl FnOnce(&mut App) -> WidgetRef,
2445 ) {
2446 let home = home(&mut tester.cell.borrow_mut());
2447 let theme = theme(&tester.cell.borrow());
2448 tester.pump_widget(material_app(theme, home));
2449 }
2450
2451 pub(crate) fn pump_unique_app(
2454 tester: &mut WidgetTester,
2455 theme: impl FnOnce(&App) -> ThemeData,
2456 home: impl FnOnce(&mut App) -> WidgetRef,
2457 ) {
2458 let home = home(&mut tester.cell.borrow_mut());
2459 let theme = theme(&tester.cell.borrow());
2460 tester.pump_widget(
2461 crate::app::MaterialApp::new()
2462 .key(waft_foundation::UniqueKey::new().into())
2463 .theme(theme)
2464 .home(home),
2465 );
2466 }
2467
2468 pub(crate) fn tile_rect(tester: &WidgetTester, index: usize) -> Rect {
2470 tester.get_rect(tester.element(find::by_type::<ListTile>().at(index)))
2471 }
2472
2473 pub(crate) fn key_rect(tester: &WidgetTester, key: &GlobalKey) -> Rect {
2475 tester.get_rect(key)
2476 }
2477
2478 pub(crate) fn sized(key: &GlobalKey, width: f64, height: f64) -> WidgetRef {
2479 SizedBox::new()
2480 .key(key.clone().into())
2481 .width(width)
2482 .height(height)
2483 .into_widget()
2484 }
2485
2486 pub(crate) fn noop() -> Listener {
2487 Listener::new(|_app| {})
2488 }
2489
2490 #[derive(Clone, Copy)]
2492 struct GeometryFrame {
2493 dense: bool,
2494 is_two_line: bool,
2495 is_three_line: bool,
2496 text_scale: f64,
2497 subtitle_scale: Option<f64>,
2498 }
2499
2500 impl GeometryFrame {
2501 const PLAIN: GeometryFrame = GeometryFrame {
2502 dense: false,
2503 is_two_line: false,
2504 is_three_line: false,
2505 text_scale: 1.0,
2506 subtitle_scale: None,
2507 };
2508 }
2509
2510 const LEFT_PADDING: f64 = 10.0;
2511 const RIGHT_PADDING: f64 = 20.0;
2512
2513 fn check_ltr_geometry(
2518 tester: &mut WidgetTester,
2519 [leading_key, trailing_key]: &[GlobalKey; 2],
2520 theme: fn(&App) -> ThemeData,
2521 frame: GeometryFrame,
2522 expected_height: f64,
2523 right_content_padding: f64,
2524 title_offset: f64,
2525 ) {
2526 let has_subtitle = frame.is_two_line || frame.is_three_line;
2527 let subtitle_scale = frame.subtitle_scale.unwrap_or(frame.text_scale);
2528 let mut tile = ListTile::new()
2529 .leading(sized(leading_key, 24.0, 24.0))
2530 .title(Text::new("title"))
2531 .trailing(sized(trailing_key, 24.0, 24.0))
2532 .dense(frame.dense);
2533 if has_subtitle {
2534 tile = tile
2535 .subtitle(Text::new("subtitle").text_scaler(TextScaler::linear(subtitle_scale)));
2536 }
2537 tile = tile.is_three_line(frame.is_three_line);
2538 let tree = bare(
2539 theme(&tester.cell.borrow()),
2540 MediaQuery::new(
2541 MediaQueryData::new()
2542 .padding(EdgeInsets::only(LEFT_PADDING, 0.0, RIGHT_PADDING, 0.0))
2543 .text_scaler(TextScaler::linear(frame.text_scale)),
2544 Material::new().child(Center::new().child(tile)),
2545 )
2546 .into_widget(),
2547 );
2548 tester.pump_widget(tree);
2549 let tester = &*tester;
2550
2551 let leading = key_rect(tester, leading_key);
2556 let trailing = key_rect(tester, trailing_key);
2557 let title = tester.get_rect(find::text("title"));
2558 assert_eq!(leading.left, 16.0_f64.max(LEFT_PADDING));
2559 assert_eq!(title.left, title_offset + 16.0_f64.max(LEFT_PADDING));
2560 if has_subtitle {
2561 assert_eq!(
2562 tester.get_rect(find::text("subtitle")).left,
2563 title_offset + 16.0_f64.max(LEFT_PADDING)
2564 );
2565 }
2566 assert_eq!(title.left, leading.right + title_offset - 24.0);
2567 assert_eq!(
2568 trailing.right,
2569 800.0 - right_content_padding.max(RIGHT_PADDING)
2570 );
2571 assert_eq!(trailing.width(), 24.0);
2572
2573 let tile = tile_rect(tester, 0);
2574 assert_eq!(tile.size(), Size::new(800.0, expected_height));
2575 assert!(title.top >= tile.top);
2576 if has_subtitle {
2577 let subtitle = tester.get_rect(find::text("subtitle"));
2578 assert!(subtitle.top >= title.bottom);
2579 assert!(subtitle.bottom < tile.bottom);
2580 } else {
2581 assert_eq!(title.top, tile.top + (tile.height() - title.height()) / 2.0);
2582 }
2583 assert_eq!(trailing.height(), 24.0);
2584 }
2585
2586 #[test]
2589 fn list_tile_geometry_ltr() {
2590 test_widgets(|tester| {
2591 let frames = [
2592 (GeometryFrame::PLAIN, 56.0),
2593 (
2594 GeometryFrame {
2595 is_two_line: true,
2596 ..GeometryFrame::PLAIN
2597 },
2598 72.0,
2599 ),
2600 (
2601 GeometryFrame {
2602 is_three_line: true,
2603 ..GeometryFrame::PLAIN
2604 },
2605 88.0,
2606 ),
2607 (
2608 GeometryFrame {
2609 text_scale: 4.0,
2610 ..GeometryFrame::PLAIN
2611 },
2612 112.0,
2613 ),
2614 (
2615 GeometryFrame {
2616 is_two_line: true,
2617 text_scale: 4.0,
2618 ..GeometryFrame::PLAIN
2619 },
2620 192.0,
2621 ),
2622 (
2624 GeometryFrame {
2625 is_two_line: true,
2626 text_scale: 0.5,
2627 subtitle_scale: Some(4.0),
2628 ..GeometryFrame::PLAIN
2629 },
2630 108.0,
2631 ),
2632 (
2633 GeometryFrame {
2634 is_three_line: true,
2635 text_scale: 4.0,
2636 ..GeometryFrame::PLAIN
2637 },
2638 192.0,
2639 ),
2640 ];
2641 let keys = [GlobalKey::new(), GlobalKey::new()];
2642 for (frame, expected_height) in frames {
2643 check_ltr_geometry(tester, &keys, m3, frame, expected_height, 24.0, 40.0);
2644 }
2645 });
2646 }
2647
2648 #[test]
2650 fn list_tile_geometry_rtl() {
2651 test_widgets(|tester| {
2652 pump_app(tester, m3, |_app| {
2653 MediaQuery::new(
2654 MediaQueryData::new().padding(EdgeInsets::only(
2655 LEFT_PADDING,
2656 0.0,
2657 RIGHT_PADDING,
2658 0.0,
2659 )),
2660 Directionality::new(
2661 TextDirection::Rtl,
2662 Material::new().child(
2663 Center::new().child(
2664 ListTile::new()
2665 .leading(Text::new("L"))
2666 .title(Text::new("title"))
2667 .trailing(Text::new("T")),
2668 ),
2669 ),
2670 ),
2671 )
2672 .into_widget()
2673 });
2674 assert_eq!(
2675 tester.get_rect(find::text("L")).right,
2676 800.0 - 16.0_f64.max(RIGHT_PADDING)
2677 );
2678 assert_eq!(
2679 tester.get_rect(find::text("title")).right,
2680 800.0 - 40.0 - 16.0_f64.max(RIGHT_PADDING)
2681 );
2682 assert_eq!(
2683 tester.get_rect(find::text("T")).left,
2684 24.0_f64.max(LEFT_PADDING)
2685 );
2686 });
2687 }
2688
2689 #[test]
2691 fn list_tile_divide_tiles() {
2692 test_widgets(|tester| {
2693 let titles = ["first", "second", "third"];
2694 pump_app(tester, m3, |_app| {
2695 Material::new()
2696 .child(Builder::new(move |app, context| {
2697 let tiles = titles
2698 .map(|title| ListTile::new().title(Text::new(title)).into_widget());
2699 ListView::new()
2700 .children(ListTile::divide_tiles(app, Some(context), tiles, None))
2701 .into_widget()
2702 }))
2703 .into_widget()
2704 });
2705 expect(tester, &find::text("first"), finds_one_widget());
2706 expect(tester, &find::text("second"), finds_one_widget());
2707 expect(tester, &find::text("third"), finds_one_widget());
2708 });
2709 }
2710
2711 #[test]
2713 fn list_tile_divide_tiles_with_empty_list() {
2714 test_widgets(|tester| {
2715 let output = ListTile::divide_tiles(
2716 &mut tester.cell.borrow_mut(),
2717 None,
2718 Vec::new(),
2719 Some(Colors::GREY.primary()),
2720 );
2721 assert!(output.is_empty());
2722 });
2723 }
2724
2725 #[test]
2727 fn list_tile_divide_tiles_with_single_item_list() {
2728 test_widgets(|tester| {
2729 let output = ListTile::divide_tiles(
2730 &mut tester.cell.borrow_mut(),
2731 None,
2732 vec![SizedBox::new().into_widget()],
2733 Some(Colors::GREY.primary()),
2734 );
2735 assert_eq!(output.len(), 1);
2736 assert!(downcast_widget::<SizedBox>(&*output[0]).is_some());
2737 });
2738 }
2739
2740 #[test]
2745 fn list_tile_divide_tiles_only_runs_the_generator_once() {
2746 test_widgets(|tester| {
2747 let call_count = Rc::new(Cell::new(0));
2749 let generator = || {
2750 let call_count = Rc::clone(&call_count);
2751 let mut yielded = 0;
2752 std::iter::from_fn(move || {
2753 if yielded == 0 {
2754 call_count.set(call_count.get() + 1);
2755 }
2756 (yielded < 2).then(|| {
2757 yielded += 1;
2758 Text::new("").into_widget()
2759 })
2760 })
2761 };
2762 let output = ListTile::divide_tiles(
2763 &mut tester.cell.borrow_mut(),
2764 None,
2765 generator(),
2766 Some(Colors::GREY.primary()),
2767 );
2768 assert_eq!(output.len(), 2);
2769 assert_eq!(call_count.get(), 1);
2770 });
2771 }
2772
2773 #[test]
2775 fn list_tile_content_padding() {
2776 test_widgets(|tester| {
2777 let check = |tester: &mut WidgetTester, text_direction: TextDirection| {
2778 tester.pump_widget(
2779 MediaQuery::new(
2780 MediaQueryData::new(),
2781 Directionality::new(
2782 text_direction,
2783 Material::new().child(
2784 Container::new()
2785 .alignment(AlignmentGeometry::TOP_LEFT)
2786 .child(
2787 ListTile::new()
2788 .content_padding(EdgeInsetsDirectional::only(
2789 10.0, 30.0, 20.0, 40.0,
2790 ))
2791 .leading(Text::new("L"))
2792 .title(Text::new("title"))
2793 .trailing(Text::new("T")),
2794 ),
2795 ),
2796 ),
2797 )
2798 .into_widget(),
2799 );
2800 };
2801
2802 check(tester, TextDirection::Ltr);
2803 assert_eq!(tile_rect(tester, 0).size(), Size::new(800.0, 126.0));
2805 assert_eq!(tester.get_rect(find::text("L")).left, 10.0); assert_eq!(tester.get_rect(find::text("T")).right, 780.0); check(tester, TextDirection::Rtl);
2809 assert_eq!(tile_rect(tester, 0).size(), Size::new(800.0, 126.0));
2811 assert_eq!(tester.get_rect(find::text("T")).left, 20.0); assert_eq!(tester.get_rect(find::text("L")).right, 790.0); });
2814 }
2815
2816 fn wide_leading(
2818 tester: &mut WidgetTester,
2819 theme: fn(&App) -> ThemeData,
2820 leading_width: f64,
2821 text_direction: TextDirection,
2822 ) -> GlobalKey {
2823 let leading_key = GlobalKey::new();
2824 pump_app(tester, theme, |_app| {
2825 Directionality::new(
2826 text_direction,
2827 Material::new().child(
2828 Container::new()
2829 .alignment(AlignmentGeometry::TOP_LEFT)
2830 .child(
2831 ListTile::new()
2832 .content_padding(EdgeInsets::ZERO)
2833 .leading(sized(&leading_key, leading_width, 32.0))
2834 .title(Text::new("title"))
2835 .subtitle(Text::new("subtitle")),
2836 ),
2837 ),
2838 )
2839 .into_widget()
2840 });
2841 leading_key
2842 }
2843
2844 fn check_wide_leading(
2845 tester: &mut WidgetTester,
2846 theme: fn(&App) -> ThemeData,
2847 leading_top: f64,
2848 narrow_title: f64,
2849 ) {
2850 let key = wide_leading(tester, theme, 24.0, TextDirection::Ltr);
2852 assert_eq!(tile_rect(tester, 0).size(), Size::new(800.0, 72.0));
2853 assert_eq!(
2854 key_rect(tester, &key).top_left(),
2855 Offset::new(0.0, leading_top)
2856 );
2857 assert_eq!(
2858 key_rect(tester, &key).bottom_right(),
2859 Offset::new(24.0, leading_top + 32.0)
2860 );
2861
2862 assert_eq!(tester.get_rect(find::text("title")).left, narrow_title);
2866 assert_eq!(tester.get_rect(find::text("subtitle")).left, narrow_title);
2867
2868 let key = wide_leading(tester, theme, 56.0, TextDirection::Ltr);
2871 assert_eq!(tile_rect(tester, 0).size(), Size::new(800.0, 72.0));
2872 assert_eq!(
2873 key_rect(tester, &key).top_left(),
2874 Offset::new(0.0, leading_top)
2875 );
2876 assert_eq!(
2877 key_rect(tester, &key).bottom_right(),
2878 Offset::new(56.0, leading_top + 32.0)
2879 );
2880 assert_eq!(tester.get_rect(find::text("title")).left, 72.0);
2881 assert_eq!(tester.get_rect(find::text("subtitle")).left, 72.0);
2882
2883 let key = wide_leading(tester, theme, 24.0, TextDirection::Rtl);
2886 assert_eq!(tile_rect(tester, 0).size(), Size::new(800.0, 72.0));
2887 assert_eq!(
2888 key_rect(tester, &key).top_right(),
2889 Offset::new(800.0, leading_top)
2890 );
2891 assert_eq!(
2892 key_rect(tester, &key).bottom_left(),
2893 Offset::new(800.0 - 24.0, leading_top + 32.0)
2894 );
2895 assert_eq!(
2896 tester.get_rect(find::text("title")).right,
2897 800.0 - narrow_title
2898 );
2899 assert_eq!(
2900 tester.get_rect(find::text("subtitle")).right,
2901 800.0 - narrow_title
2902 );
2903
2904 let key = wide_leading(tester, theme, 56.0, TextDirection::Rtl);
2905 assert_eq!(tile_rect(tester, 0).size(), Size::new(800.0, 72.0));
2906 assert_eq!(
2907 key_rect(tester, &key).top_right(),
2908 Offset::new(800.0, leading_top)
2909 );
2910 assert_eq!(
2911 key_rect(tester, &key).bottom_left(),
2912 Offset::new(800.0 - 56.0, leading_top + 32.0)
2913 );
2914 assert_eq!(tester.get_rect(find::text("title")).right, 800.0 - 72.0);
2915 assert_eq!(tester.get_rect(find::text("subtitle")).right, 800.0 - 72.0);
2916 }
2917
2918 #[test]
2920 fn list_tile_wide_leading_widget() {
2921 test_widgets(|tester| check_wide_leading(tester, m3, 20.0, 40.0));
2922 }
2923
2924 pub(crate) fn bare<K>(theme: ThemeData, home: impl IntoWidget<K>) -> WidgetRef {
2927 MediaQuery::new(
2928 MediaQueryData::new(),
2929 Directionality::new(TextDirection::Ltr, crate::theme::Theme::new(theme, home)),
2930 )
2931 .into_widget()
2932 }
2933
2934 fn two_tiles(tester: &mut WidgetTester, theme: fn(&App) -> ThemeData, tiles: [ListTile; 2]) {
2936 pump_app(tester, theme, |_app| {
2937 Material::new()
2938 .child(ListView::new().children(tiles.map(IntoWidget::into_widget)))
2939 .into_widget()
2940 })
2941 }
2942
2943 pub(crate) fn avatar(key: &GlobalKey) -> WidgetRef {
2945 sized(key, 40.0, 40.0)
2946 }
2947
2948 pub(crate) fn placeholder(key: &GlobalKey, width: f64, height: f64) -> WidgetRef {
2951 sized(key, width, height)
2952 }
2953
2954 pub(crate) const THIRTEEN_LINES: &str = "A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM";
2955
2956 #[test]
2958 fn list_tile_leading_and_trailing_positions() {
2959 test_widgets(|tester| {
2960 let keys: [GlobalKey; 4] = std::array::from_fn(|_| GlobalKey::new());
2961 let [avatar0, avatar1, placeholder0, placeholder1] = &keys;
2962
2963 two_tiles(
2965 tester,
2966 m3,
2967 [
2968 ListTile::new()
2969 .leading(avatar(avatar0))
2970 .trailing(placeholder(placeholder0, 24.0, 24.0))
2971 .title(Text::new(THIRTEEN_LINES)),
2972 ListTile::new()
2973 .leading(avatar(avatar1))
2974 .trailing(placeholder(placeholder1, 24.0, 24.0))
2975 .title(Text::new("A")),
2976 ],
2977 );
2978 tester.pump_by(Duration::from_secs(2)); assert_eq!(
2980 tile_rect(tester, 0),
2981 Rect::from_ltwh(0.0, 0.0, 800.0, 328.0)
2982 );
2983 assert_eq!(
2984 key_rect(tester, avatar0),
2985 Rect::from_ltwh(16.0, 144.0, 40.0, 40.0)
2986 );
2987 assert_eq!(
2988 key_rect(tester, placeholder0),
2989 Rect::from_ltwh(800.0 - 24.0 - 24.0, 152.0, 24.0, 24.0)
2990 );
2991 assert_eq!(
2992 tile_rect(tester, 1),
2993 Rect::from_ltwh(0.0, 328.0, 800.0, 56.0)
2994 );
2995 assert_eq!(
2996 key_rect(tester, avatar1),
2997 Rect::from_ltwh(16.0, 328.0 + 8.0, 40.0, 40.0)
2998 );
2999 assert_eq!(
3000 key_rect(tester, placeholder1),
3001 Rect::from_ltwh(800.0 - 24.0 - 24.0, 328.0 + 16.0, 24.0, 24.0)
3002 );
3003
3004 two_tiles(
3006 tester,
3007 m3,
3008 [
3009 ListTile::new()
3010 .leading(avatar(avatar0))
3011 .trailing(placeholder(placeholder0, 24.0, 24.0))
3012 .title(Text::new("A"))
3013 .subtitle(Text::new(THIRTEEN_LINES)),
3014 ListTile::new()
3015 .leading(avatar(avatar1))
3016 .trailing(placeholder(placeholder1, 24.0, 24.0))
3017 .title(Text::new("A"))
3018 .subtitle(Text::new("A")),
3019 ],
3020 );
3021 const HEIGHT: f64 = 300.0;
3022 const AVATAR_TOP: f64 = 130.0;
3023 const PLACEHOLDER_TOP: f64 = 138.0;
3024 assert_eq!(
3025 tile_rect(tester, 0),
3026 Rect::from_ltwh(0.0, 0.0, 800.0, HEIGHT)
3027 );
3028 assert_eq!(
3029 key_rect(tester, avatar0),
3030 Rect::from_ltwh(16.0, AVATAR_TOP, 40.0, 40.0)
3031 );
3032 assert_eq!(
3033 key_rect(tester, placeholder0),
3034 Rect::from_ltwh(800.0 - 24.0 - 24.0, PLACEHOLDER_TOP, 24.0, 24.0)
3035 );
3036 assert_eq!(
3037 tile_rect(tester, 1),
3038 Rect::from_ltwh(0.0, HEIGHT, 800.0, 72.0)
3039 );
3040 assert_eq!(
3041 key_rect(tester, avatar1),
3042 Rect::from_ltwh(16.0, HEIGHT + 16.0, 40.0, 40.0)
3043 );
3044 assert_eq!(
3045 key_rect(tester, placeholder1),
3046 Rect::from_ltwh(800.0 - 24.0 - 24.0, HEIGHT + 24.0, 24.0, 24.0)
3047 );
3048
3049 two_tiles(
3051 tester,
3052 m3,
3053 [
3054 ListTile::new()
3055 .leading(avatar(avatar0))
3056 .trailing(placeholder(placeholder0, 24.0, 24.0))
3057 .title(Text::new("A"))
3058 .subtitle(Text::new(THIRTEEN_LINES))
3059 .is_three_line(true),
3060 ListTile::new()
3061 .leading(avatar(avatar1))
3062 .trailing(placeholder(placeholder1, 24.0, 24.0))
3063 .title(Text::new("A"))
3064 .subtitle(Text::new("A"))
3065 .is_three_line(true),
3066 ],
3067 );
3068 assert_eq!(
3069 tile_rect(tester, 0),
3070 Rect::from_ltwh(0.0, 0.0, 800.0, HEIGHT)
3071 );
3072 assert_eq!(
3073 key_rect(tester, avatar0),
3074 Rect::from_ltwh(16.0, 8.0, 40.0, 40.0)
3075 );
3076 assert_eq!(
3077 key_rect(tester, placeholder0),
3078 Rect::from_ltwh(800.0 - 24.0 - 24.0, 8.0, 24.0, 24.0)
3079 );
3080 assert_eq!(
3081 tile_rect(tester, 1),
3082 Rect::from_ltwh(0.0, HEIGHT, 800.0, 88.0)
3083 );
3084 assert_eq!(
3085 key_rect(tester, avatar1),
3086 Rect::from_ltwh(16.0, HEIGHT + 8.0, 40.0, 40.0)
3087 );
3088 assert_eq!(
3089 key_rect(tester, placeholder1),
3090 Rect::from_ltwh(800.0 - 24.0 - 24.0, HEIGHT + 8.0, 24.0, 24.0)
3091 );
3092
3093 let [leading0, trailing0, leading1, trailing1] = &keys;
3095 two_tiles(
3096 tester,
3097 m3,
3098 [
3099 ListTile::new()
3100 .leading(placeholder(leading0, 24.0, 12.0))
3101 .trailing(placeholder(trailing0, 24.0, 24.0))
3102 .title(Text::new(THIRTEEN_LINES)),
3103 ListTile::new()
3104 .leading(placeholder(leading1, 24.0, 12.0))
3105 .trailing(placeholder(trailing1, 24.0, 24.0))
3106 .title(Text::new("A")),
3107 ],
3108 );
3109 tester.pump_by(Duration::from_secs(2)); assert_eq!(
3111 tile_rect(tester, 0),
3112 Rect::from_ltwh(0.0, 0.0, 800.0, 328.0)
3113 );
3114 assert_eq!(
3115 key_rect(tester, leading0),
3116 Rect::from_ltwh(16.0, 158.0, 24.0, 12.0)
3117 );
3118 assert_eq!(
3119 key_rect(tester, trailing0),
3120 Rect::from_ltwh(800.0 - 24.0 - 24.0, 152.0, 24.0, 24.0)
3121 );
3122 assert_eq!(
3123 tile_rect(tester, 1),
3124 Rect::from_ltwh(0.0, 328.0, 800.0, 56.0)
3125 );
3126 assert_eq!(
3127 key_rect(tester, leading1),
3128 Rect::from_ltwh(16.0, 328.0 + 22.0, 24.0, 12.0)
3129 );
3130 assert_eq!(
3131 key_rect(tester, trailing1),
3132 Rect::from_ltwh(800.0 - 24.0 - 24.0, 328.0 + 16.0, 24.0, 24.0)
3133 );
3134 });
3135 }
3136
3137 fn oversized_rects(
3140 tester: &mut WidgetTester,
3141 theme: fn(&App) -> ThemeData,
3142 leading: bool,
3143 subtitle: bool,
3144 is_three_line: bool,
3145 ) -> [Rect; 2] {
3146 oversized_rects_dense(tester, theme, leading, subtitle, is_three_line, false)
3147 }
3148
3149 fn oversized_rects_dense(
3151 tester: &mut WidgetTester,
3152 theme: fn(&App) -> ThemeData,
3153 leading: bool,
3154 subtitle: bool,
3155 is_three_line: bool,
3156 dense: bool,
3157 ) -> [Rect; 2] {
3158 let keys = [GlobalKey::new(), GlobalKey::new()];
3160 let tiles = [("A", &keys[0]), ("B", &keys[1])].map(|(text, key)| {
3161 let oversized = placeholder(key, 24.0, 80.0);
3162 let mut tile = ListTile::new().title(Text::new(text)).dense(dense);
3163 tile = if leading {
3164 tile.leading(oversized)
3165 } else {
3166 tile.trailing(oversized)
3167 };
3168 if subtitle {
3169 tile = tile.subtitle(Text::new(text));
3170 }
3171 tile.is_three_line(is_three_line)
3172 });
3173 two_tiles(tester, theme, tiles);
3174 [key_rect(tester, &keys[0]), key_rect(tester, &keys[1])]
3175 }
3176
3177 #[test]
3179 fn list_tile_leading_icon_height_does_not_exceed_list_tile_height() {
3180 test_widgets(|tester| {
3181 assert_eq!(
3183 oversized_rects(tester, m3, true, false, false),
3184 [
3185 Rect::from_ltwh(16.0, 0.0, 24.0, 56.0),
3186 Rect::from_ltwh(16.0, 56.0, 24.0, 56.0)
3187 ]
3188 );
3189 assert_eq!(
3191 oversized_rects(tester, m3, true, true, false),
3192 [
3193 Rect::from_ltwh(16.0, 8.0, 24.0, 56.0),
3194 Rect::from_ltwh(16.0, 72.0 + 8.0, 24.0, 56.0)
3195 ]
3196 );
3197 assert_eq!(
3199 oversized_rects(tester, m3, true, true, true),
3200 [
3201 Rect::from_ltwh(16.0, 8.0, 24.0, 56.0),
3202 Rect::from_ltwh(16.0, 88.0 + 8.0, 24.0, 56.0)
3203 ]
3204 );
3205 });
3206 }
3207
3208 #[test]
3210 fn list_tile_trailing_icon_height_does_not_exceed_list_tile_height() {
3211 test_widgets(|tester| {
3212 assert_eq!(
3214 oversized_rects(tester, m3, false, false, false),
3215 [
3216 Rect::from_ltwh(800.0 - 24.0 - 24.0, 0.0, 24.0, 56.0),
3217 Rect::from_ltwh(800.0 - 24.0 - 24.0, 56.0, 24.0, 56.0)
3218 ]
3219 );
3220 assert_eq!(
3222 oversized_rects(tester, m3, false, true, false),
3223 [
3224 Rect::from_ltwh(800.0 - 24.0 - 24.0, 8.0, 24.0, 56.0),
3225 Rect::from_ltwh(800.0 - 24.0 - 24.0, 72.0 + 8.0, 24.0, 56.0)
3226 ]
3227 );
3228 assert_eq!(
3230 oversized_rects(tester, m3, false, true, true),
3231 [
3232 Rect::from_ltwh(800.0 - 24.0 - 24.0, 8.0, 24.0, 56.0),
3233 Rect::from_ltwh(800.0 - 24.0 - 24.0, 88.0 + 8.0, 24.0, 56.0)
3234 ]
3235 );
3236 });
3237 }
3238
3239 fn focus_of(tester: &WidgetTester, key: &GlobalKey) -> AnyFocusNode {
3241 let mut app = tester.cell.borrow_mut();
3242 let context = key.current_context(&app).expect("mounted");
3243 waft_widgets::Focus::of(&mut app, context, false, true)
3244 }
3245
3246 #[test]
3248 fn list_tile_only_accepts_focus_when_enabled() {
3249 test_widgets(|tester| {
3250 let child_key = GlobalKey::new();
3251 let frame = |tester: &WidgetTester, enabled: bool| {
3252 bare(
3253 m3(&tester.cell.borrow()),
3254 Material::new().child(
3255 ListView::new().children([ListTile::new()
3256 .title(Text::new("A").key(child_key.clone().into()))
3257 .dense(true)
3258 .enabled(enabled)
3259 .on_tap(noop())
3260 .into_widget()]),
3261 ),
3262 )
3263 };
3264 let first = frame(tester, true);
3265 tester.pump_widget(first);
3266 tester.pump(); let tile_node = focus_of(tester, &child_key);
3269 tile_node.request_focus(&mut tester.cell.borrow_mut(), None);
3270 tester.pump(); assert!(focus_of(tester, &child_key).has_primary_focus(&tester.cell.borrow()));
3272
3273 assert!(tile_node.has_primary_focus(&tester.cell.borrow()));
3274 let second = frame(tester, false);
3275 tester.pump_widget(second);
3276
3277 assert_ne!(
3278 waft_widgets::primary_focus(&mut tester.cell.borrow_mut()),
3279 Some(tile_node)
3280 );
3281 assert!(!focus_of(tester, &child_key).has_primary_focus(&tester.cell.borrow()));
3282 });
3283 }
3284
3285 #[test]
3287 fn list_tile_can_autofocus_unless_disabled() {
3288 test_widgets(|tester| {
3289 let child_key = GlobalKey::new();
3290 let frame = |tester: &WidgetTester, enabled: bool| {
3291 bare(
3292 m3(&tester.cell.borrow()),
3293 Material::new().child(
3294 ListView::new().children([ListTile::new()
3295 .title(Text::new("A").key(child_key.clone().into()))
3296 .dense(true)
3297 .enabled(enabled)
3298 .autofocus(true)
3299 .on_tap(noop())
3300 .into_widget()]),
3301 ),
3302 )
3303 };
3304 let first = frame(tester, true);
3305 tester.pump_widget(first);
3306 tester.pump();
3307 assert!(focus_of(tester, &child_key).has_primary_focus(&tester.cell.borrow()));
3308
3309 let second = frame(tester, false);
3310 tester.pump_widget(second);
3311 tester.pump();
3312 assert!(!focus_of(tester, &child_key).has_primary_focus(&tester.cell.borrow()));
3313 });
3314 }
3315
3316 fn white_square(tile: ListTile) -> WidgetRef {
3318 Center::new()
3319 .child(
3320 SizedBox::new()
3321 .width(100.0)
3322 .height(100.0)
3323 .child(Material::new().color(Colors::WHITE).child(tile)),
3324 )
3325 .into_widget()
3326 }
3327
3328 const SQUARE: Rect = Rect::from_ltwh(0.0, 0.0, 100.0, 100.0);
3329
3330 #[test]
3335 fn list_tile_is_focusable_and_has_correct_focus_color() {
3336 test_widgets(|tester| {
3337 let focus_node =
3338 FocusNode::new(&mut tester.cell.borrow_mut()).into_erased::<AnyFocusNode>();
3339 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
3340 let frame = |tester: &WidgetTester, enabled: bool| {
3341 let mut tile = ListTile::new()
3342 .focus_color(Colors::ORANGE.shade500())
3343 .autofocus(true)
3344 .focus_node(focus_node.clone());
3345 if enabled {
3346 tile = tile.on_tap(noop());
3347 }
3348 bare(m3(&tester.cell.borrow()), white_square(tile))
3349 };
3350 let first = frame(tester, true);
3351 tester.pump_widget(first);
3352
3353 tester.pump_and_settle();
3354 assert!(focus_node.has_primary_focus(&tester.cell.borrow()));
3355 assert_eq!(ink_tile_color(tester), Some(Color::new(0x00000000)));
3356 paints_exactly(
3357 &draws(tester.ink_list().ops()),
3358 &[PaintCall::rect_at(SQUARE, Some(Colors::ORANGE.shade500()))],
3359 )
3360 .unwrap();
3361
3362 let second = frame(tester, false);
3364 tester.pump_widget(second);
3365 tester.pump_and_settle();
3366 assert!(!focus_node.has_primary_focus(&tester.cell.borrow()));
3367 assert_eq!(ink_tile_color(tester), Some(Color::new(0x00000000)));
3368 paints_exactly(&draws(tester.ink_list().ops()), &[]).unwrap();
3369 });
3370 }
3371
3372 #[test]
3376 fn list_tile_can_be_hovered_and_has_correct_hover_color() {
3377 test_widgets(|tester| {
3378 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
3379 let frame = |tester: &WidgetTester, enabled: bool| {
3380 let mut tile = ListTile::new()
3381 .hover_color(Colors::ORANGE.shade500())
3382 .autofocus(true);
3383 if enabled {
3384 tile = tile.on_tap(noop());
3385 }
3386 bare(m3(&tester.cell.borrow()), white_square(tile))
3387 };
3388 let first = frame(tester, true);
3389 tester.pump_widget(first);
3390
3391 tester.pump();
3392 tester.pump_and_settle();
3393 assert_eq!(ink_tile_color(tester), Some(Color::new(0x00000000)));
3394 paints_exactly(
3395 &draws(tester.ink_list().ops()),
3396 &[PaintCall::rect_at(SQUARE, Some(Color::new(0x1f000000)))],
3397 )
3398 .unwrap();
3399
3400 let mut gesture = TestPointer::mouse(1, 0);
3402 let center = tile_rect(tester, 0).center();
3403 tester.add_pointer(&mut gesture, Offset::ZERO);
3404 tester.move_to(&mut gesture, center, false);
3405
3406 let again = frame(tester, true);
3407 tester.pump_widget(again);
3408 tester.pump();
3409 tester.pump_and_settle();
3410 paints_exactly(
3411 &draws(tester.ink_list().ops()),
3412 &[
3413 PaintCall::rect_at(SQUARE, Some(Color::new(0x1f000000))),
3414 PaintCall::rect_at(SQUARE, Some(Colors::ORANGE.shade500())),
3415 ],
3416 )
3417 .unwrap();
3418
3419 let disabled = frame(tester, false);
3420 tester.pump_widget(disabled);
3421 tester.pump();
3422 tester.pump_and_settle();
3423 assert_eq!(ink_tile_color(tester), Some(Color::new(0x00000000)));
3424 paints_exactly(&draws(tester.ink_list().ops()), &[]).unwrap();
3425 });
3426 }
3427
3428 #[test]
3430 fn list_tile_can_be_splashed_and_has_correct_splash_color() {
3431 test_widgets(|tester| {
3432 pump_app(tester, m2, |_app| {
3433 Material::new()
3434 .child(
3435 Center::new().child(
3436 SizedBox::square(Some(100.0)).child(
3437 ListTile::new()
3438 .on_tap(noop())
3439 .splash_color(Color::new(0xff88ff88)),
3440 ),
3441 ),
3442 )
3443 .into_widget()
3444 });
3445 tester.pump_and_settle();
3446 let center = tile_rect(tester, 0).center();
3447 let mut gesture = TestPointer::touch(1);
3448 tester.down(&mut gesture, center);
3449 tester.pump_by(Duration::from_millis(200));
3450 tester.pump_by(Duration::from_millis(16));
3453 assert_paints(
3454 tester.ink_list().ops(),
3455 &[PaintCall::Circle {
3456 x: Some(50.0),
3457 y: Some(50.0),
3458 radius: None,
3459 color: Some(Color::new(0xff88ff88)),
3460 style: None,
3461 }],
3462 );
3463 tester.up(&gesture);
3464 });
3465 }
3466
3467 #[test]
3469 fn list_tile_responds_to_density_changes() {
3470 test_widgets(|tester| {
3471 let key = GlobalKey::new();
3472 let build_test = |tester: &mut WidgetTester, visual_density: VisualDensity| {
3473 let theme = m3(&tester.cell.borrow());
3474 tester.pump_widget(material_app(
3475 theme,
3476 Material::new()
3477 .child(
3478 Center::new().child(
3479 ListTile::new()
3480 .key(key.clone().into())
3481 .on_tap(noop())
3482 .autofocus(true)
3483 .visual_density(visual_density),
3484 ),
3485 )
3486 .into_widget(),
3487 ));
3488 };
3489
3490 build_test(tester, VisualDensity::STANDARD);
3491 let render = tester.render_object(&key).as_box().expect("a render box");
3492 let size = |tester: &WidgetTester| render.size(&tester.cell.borrow());
3493 tester.pump_and_settle();
3494 assert_eq!(size(tester), Size::new(800.0, 56.0));
3495
3496 build_test(tester, VisualDensity::new(3.0, 3.0));
3497 tester.pump_and_settle();
3498 assert_eq!(size(tester), Size::new(800.0, 68.0));
3499
3500 build_test(tester, VisualDensity::new(-3.0, -3.0));
3501 tester.pump_and_settle();
3502 assert_eq!(size(tester), Size::new(800.0, 44.0));
3503
3504 build_test(tester, VisualDensity::new(3.0, -3.0));
3505 tester.pump_and_settle();
3506 assert_eq!(size(tester), Size::new(800.0, 44.0));
3507 });
3508 }
3509
3510 #[test]
3512 fn list_tile_shape_is_painted_correctly() {
3513 test_widgets(|tester| {
3514 let tile_color = Colors::RED.shade500();
3516 let pump_shape = |tester: &mut WidgetTester, shape: ShapeBorderRef| {
3517 pump_app(tester, m3, |_app| {
3518 Material::new()
3519 .child(
3520 Center::new()
3521 .child(ListTile::new().shape(shape).tile_color(tile_color)),
3522 )
3523 .into_widget()
3524 })
3525 };
3526
3527 pump_shape(
3529 tester,
3530 ShapeBorderRef::new(RoundedRectangleBorder::default()),
3531 );
3532 let rect = tile_rect(tester, 0);
3533 assert_paints(
3535 tester.ink_list().ops(),
3536 &[PaintCall::rect_at(rect, Some(tile_color))],
3537 );
3538
3539 pump_shape(tester, ShapeBorderRef::new(StadiumBorder::default()));
3541 let rect = tile_rect(tester, 0);
3542 assert_paints(
3544 tester.ink_list().ops(),
3545 &[PaintCall::rrect_at(
3546 RRect::from_rect_and_radius(
3547 rect,
3548 waft_embedder::Radius::circular(rect.width().min(rect.height()) / 2.0),
3549 ),
3550 Some(tile_color),
3551 )],
3552 );
3553 });
3554 }
3555
3556 fn cursor_over(
3561 tester: &mut WidgetTester,
3562 gesture: &mut Option<TestPointer>,
3563 tile: ListTile,
3564 ) -> MouseCursorRef {
3565 pump_app(tester, m3, |_app| {
3566 Material::new()
3567 .child(
3568 Center::new().child(
3569 MouseRegion::new()
3570 .cursor(SystemMouseCursors::FORBIDDEN.into())
3571 .child(tile),
3572 ),
3573 )
3574 .into_widget()
3575 });
3576 if gesture.is_none() {
3577 let center = tile_rect(tester, 0).center();
3578 let mut mouse = TestPointer::mouse(1, 0);
3579 tester.add_pointer(&mut mouse, center);
3580 tester.pump();
3581 *gesture = Some(mouse);
3582 }
3583 tester.active_cursor(1)
3584 }
3585
3586 #[test]
3588 fn list_tile_changes_mouse_cursor_when_hovered() {
3589 test_widgets(|tester| {
3590 let text: MouseCursorRef = SystemMouseCursors::TEXT.into();
3591 let click: MouseCursorRef = SystemMouseCursors::CLICK.into();
3592 let basic: MouseCursorRef = SystemMouseCursors::BASIC.into();
3593
3594 let mut gesture = None;
3595
3596 let cursor = cursor_over(
3598 tester,
3599 &mut gesture,
3600 ListTile::new()
3601 .on_tap(noop())
3602 .mouse_cursor(SystemMouseCursors::TEXT),
3603 );
3604 assert!(*cursor == *text);
3605
3606 let cursor = cursor_over(tester, &mut gesture, ListTile::new().on_tap(noop()));
3608 assert!(*cursor == *click);
3609
3610 let cursor = cursor_over(tester, &mut gesture, ListTile::new().enabled(false));
3612 assert!(*cursor == *basic);
3613
3614 let cursor = cursor_over(tester, &mut gesture, ListTile::new());
3616 assert!(*cursor == *basic);
3617 });
3618 }
3619
3620 #[test]
3622 fn list_tile_on_focus_change_callback() {
3623 test_widgets(|tester| {
3624 let node = FocusNode::new(&mut tester.cell.borrow_mut()).into_erased::<AnyFocusNode>();
3625 let got_focus = Rc::new(Cell::new(false));
3626 let on_focus_change: ValueChanged<bool> = {
3627 let got_focus = Rc::clone(&got_focus);
3628 Rc::new(move |_app, focused| got_focus.set(focused))
3629 };
3630 let tree = bare(
3631 m3(&tester.cell.borrow()),
3632 Material::new().child(
3633 ListTile::new()
3634 .focus_node(node.clone())
3635 .on_focus_change(on_focus_change)
3636 .on_tap(noop()),
3637 ),
3638 );
3639 tester.pump_widget(tree);
3640
3641 node.request_focus(&mut tester.cell.borrow_mut(), None);
3642 tester.pump();
3643 assert!(got_focus.get());
3644 assert!(node.has_focus(&tester.cell.borrow_mut()));
3645
3646 node.unfocus(
3647 &mut tester.cell.borrow_mut(),
3648 waft_widgets::UnfocusDisposition::Scope,
3649 );
3650 tester.pump();
3651 assert!(!got_focus.get());
3652 assert!(!node.has_focus(&tester.cell.borrow_mut()));
3653 });
3654 }
3655
3656 pub(crate) fn ink_tile_color(tester: &WidgetTester) -> Option<Color> {
3659 let ink = tester.element(find::by_type::<Ink>().at(0));
3660 tester.widget(ink, |ink: &Ink| {
3661 ink.effective_decoration().and_then(|decoration| {
3662 decoration
3663 .as_any()
3664 .downcast_ref::<ShapeDecoration>()
3665 .and_then(|decoration| decoration.color.as_ref().map(AnyColor::color))
3666 })
3667 })
3668 }
3669
3670 pub(crate) fn toggling_tile(
3672 tester: &mut WidgetTester,
3673 theme: fn(&App) -> ThemeData,
3674 tile: impl Fn() -> ListTile + 'static,
3675 ) {
3676 let is_selected = Rc::new(Cell::new(false));
3677 pump_app(tester, theme, move |_app| {
3678 Material::new()
3679 .child(Center::new().child(StatefulBuilder::new(
3680 move |_app, _context, set_state| {
3681 let toggled = Rc::clone(&is_selected);
3682 tile()
3683 .selected(is_selected.get())
3684 .on_tap(Listener::new(move |app| {
3685 let toggled = Rc::clone(&toggled);
3686 set_state(app, &mut |_app| toggled.set(!toggled.get()));
3687 }))
3688 .title(Text::new("Title"))
3689 .into_widget()
3690 },
3691 )))
3692 .into_widget()
3693 })
3694 }
3695
3696 #[test]
3698 fn list_tile_respects_tile_color_and_selected_tile_color() {
3699 test_widgets(|tester| {
3700 let tile_color = Colors::GREEN.shade500();
3701 let selected_tile_color = Colors::RED.shade500();
3702 toggling_tile(tester, m3, move || {
3703 ListTile::new()
3704 .selected_tile_color(selected_tile_color)
3705 .tile_color(tile_color)
3706 });
3707
3708 assert_paints(
3710 tester.ink_list().ops(),
3711 &[PaintCall::rect(Some(tile_color))],
3712 );
3713
3714 let center = tile_rect(tester, 0).center();
3716 tester.tap_at(center, 1);
3717 tester.pump_and_settle();
3718
3719 assert_paints(
3721 tester.ink_list().ops(),
3722 &[PaintCall::rect(Some(selected_tile_color))],
3723 );
3724 });
3725 }
3726
3727 #[test]
3729 fn list_tile_shows_material_ripple_effects_on_top_of_tile_color() {
3730 test_widgets(|tester| {
3731 let tile_color = Colors::RED.shade500();
3733 pump_app(tester, m2, |_app| {
3734 Material::new()
3735 .child(
3736 Center::new().child(
3737 ListTile::new()
3738 .tile_color(tile_color)
3739 .on_tap(noop())
3740 .title(Text::new("Title")),
3741 ),
3742 )
3743 .into_widget()
3744 });
3745
3746 assert_paints(
3748 tester.ink_list().ops(),
3749 &[PaintCall::rect(Some(tile_color))],
3750 );
3751
3752 let center = tile_rect(tester, 0).center();
3754 tester.tap_at(center, 1);
3755 tester.pump_by(Duration::from_millis(200));
3756 tester.pump_by(Duration::from_millis(16));
3759
3760 assert_paints(
3762 tester.ink_list().ops(),
3763 &[PaintCall::rect(Some(tile_color)), PaintCall::circle(None)],
3764 );
3765 });
3766 }
3767
3768 #[test]
3771 fn list_tile_default_tile_color() {
3772 test_widgets(|tester| {
3773 toggling_tile(tester, m3, ListTile::new);
3774 assert_eq!(ink_tile_color(tester), Some(Colors::TRANSPARENT));
3775
3776 let center = tile_rect(tester, 0).center();
3778 tester.tap_at(center, 1);
3779 tester.pump_and_settle();
3780
3781 assert_eq!(ink_tile_color(tester), Some(Colors::TRANSPARENT));
3782 });
3783 }
3784
3785 #[test]
3787 fn list_tile_layout_at_zero_size() {
3788 test_widgets(|tester| {
3789 let key = GlobalKey::new();
3791 pump_app(tester, m3, |_app| {
3792 Scaffold::new()
3793 .body(
3794 SizedBox::shrink().child(
3795 ListTile::new()
3796 .key(key.clone().into())
3797 .tile_color(Colors::GREEN.primary()),
3798 ),
3799 )
3800 .into_widget()
3801 });
3802 let size = key_rect(tester, &key).size();
3803 assert_eq!(size.width(), 0.0);
3804 assert_eq!(size.height(), 0.0);
3805 });
3806 }
3807
3808 #[test]
3810 fn list_tile_mouse_cursor_overrides_list_tile_theme_mouse_cursor() {
3811 test_widgets(|tester| {
3812 let tile_key = GlobalKey::new();
3813 pump_app(tester, m3, |_app| {
3814 let clickable: WidgetStatePropertyRef<Option<MouseCursorRef>> =
3815 <dyn WidgetStateProperty<Option<MouseCursorRef>>>::resolve_with(|states| {
3816 Some(WidgetStateMouseCursor::clickable().resolve(states))
3817 });
3818 Material::new()
3819 .child(
3820 ListTileTheme::new(
3821 ListTile::new()
3822 .key(tile_key.clone().into())
3823 .mouse_cursor(WidgetStateMouseCursor::textable())
3824 .title(Text::new("Title"))
3825 .on_tap(noop()),
3826 )
3827 .data(ListTileThemeData::new().mouse_cursor(clickable)),
3828 )
3829 .into_widget()
3830 });
3831 let list_tile = key_rect(tester, &tile_key).center();
3832 let mut gesture = TestPointer::mouse(1, 0);
3833 tester.add_pointer(&mut gesture, Offset::ZERO);
3834 tester.move_to(&mut gesture, list_tile, false);
3835 tester.pump_and_settle();
3836 let text: MouseCursorRef = SystemMouseCursors::TEXT.into();
3837 assert!(*tester.active_cursor(1) == *text);
3838 });
3839 }
3840
3841 fn spacing_frame(
3845 tester: &mut WidgetTester,
3846 theme_data: ListTileThemeData,
3847 text_direction: TextDirection,
3848 tile: ListTile,
3849 ) {
3850 spacing_frame_in(tester, m3, theme_data, text_direction, tile)
3851 }
3852
3853 fn spacing_frame_in(
3855 tester: &mut WidgetTester,
3856 theme: fn(&App) -> ThemeData,
3857 theme_data: ListTileThemeData,
3858 text_direction: TextDirection,
3859 tile: ListTile,
3860 ) {
3861 let theme = theme(&tester.cell.borrow());
3862 tester.pump_widget(
3863 MediaQuery::new(
3864 MediaQueryData::new(),
3865 Directionality::new(
3866 text_direction,
3867 crate::theme::Theme::new(
3868 theme,
3869 Material::new().child(
3870 ListTileTheme::new(
3871 Container::new()
3872 .alignment(AlignmentGeometry::TOP_LEFT)
3873 .child(tile),
3874 )
3875 .data(theme_data),
3876 ),
3877 ),
3878 ),
3879 )
3880 .into_widget(),
3881 );
3882 }
3883
3884 fn l_title_t() -> ListTile {
3886 ListTile::new()
3887 .leading(Text::new("L"))
3888 .title(Text::new("title"))
3889 .trailing(Text::new("T"))
3890 }
3891
3892 fn title_edge(tester: &WidgetTester, text_direction: TextDirection) -> (f64, Size) {
3894 let title = tester.get_rect(find::text("title"));
3895 let edge = match text_direction {
3896 TextDirection::Ltr => title.left,
3897 TextDirection::Rtl => title.right,
3898 };
3899 (edge, tile_rect(tester, 0).size())
3900 }
3901
3902 #[test]
3904 fn list_tile_horizontal_title_gap_0() {
3905 test_widgets(|tester| {
3906 for (text_direction, edge) in [(TextDirection::Ltr, 40.0), (TextDirection::Rtl, 760.0)]
3907 {
3908 let frames = [
3909 (None, Some(0.0)),
3910 (Some(0.0), None),
3911 (Some(10.0), Some(0.0)),
3912 ];
3913 for (theme_gap, widget_gap) in frames {
3914 let mut theme_data = ListTileThemeData::new();
3915 theme_data.horizontal_title_gap = theme_gap;
3916 let mut tile = l_title_t();
3917 tile.horizontal_title_gap = widget_gap;
3918 spacing_frame(tester, theme_data, text_direction, tile);
3919 assert_eq!(
3920 title_edge(tester, text_direction),
3921 (edge, Size::new(800.0, 56.0))
3922 );
3923 }
3924 }
3925 });
3926 }
3927
3928 #[test]
3930 fn list_tile_default_horizontal_title_gap_and_min_leading_width() {
3931 test_widgets(|tester| {
3932 spacing_frame(
3933 tester,
3934 ListTileThemeData::new(),
3935 TextDirection::Ltr,
3936 l_title_t(),
3937 );
3938 assert_eq!(
3940 title_edge(tester, TextDirection::Ltr),
3941 (56.0, Size::new(800.0, 56.0))
3942 );
3943
3944 spacing_frame(
3945 tester,
3946 ListTileThemeData::new(),
3947 TextDirection::Rtl,
3948 l_title_t(),
3949 );
3950 assert_eq!(
3952 title_edge(tester, TextDirection::Rtl),
3953 (744.0, Size::new(800.0, 56.0))
3954 );
3955 });
3956 }
3957
3958 #[test]
3960 fn list_tile_horizontal_title_gap_with_visual_density() {
3961 test_widgets(|tester| {
3962 let frame = || {
3963 l_title_t()
3964 .visual_density(VisualDensity::new(VisualDensity::MINIMUM_DENSITY, 0.0))
3965 .horizontal_title_gap(10.0)
3966 };
3967 spacing_frame(
3968 tester,
3969 ListTileThemeData::new(),
3970 TextDirection::Ltr,
3971 frame(),
3972 );
3973 assert_eq!(
3974 title_edge(tester, TextDirection::Ltr),
3975 (42.0, Size::new(800.0, 56.0))
3976 );
3977
3978 let theme = m3(&tester.cell.borrow());
3982 tester.pump_widget(
3983 MediaQuery::new(
3984 MediaQueryData::new(),
3985 Directionality::new(
3986 TextDirection::Ltr,
3987 crate::theme::Theme::new(
3988 theme,
3989 Material::new().child(
3990 ListTileTheme::new(
3991 Container::new()
3992 .alignment(AlignmentGeometry::TOP_LEFT)
3993 .child(frame()),
3994 )
3995 .data(ListTileThemeData::new()),
3996 ),
3997 ),
3998 ),
3999 )
4000 .into_widget(),
4001 );
4002 assert_eq!(
4003 title_edge(tester, TextDirection::Ltr),
4004 (42.0, Size::new(800.0, 56.0))
4005 );
4006 });
4007 }
4008
4009 #[test]
4011 fn list_tile_min_vertical_padding_80() {
4012 test_widgets(|tester| {
4013 for text_direction in [TextDirection::Ltr, TextDirection::Rtl] {
4014 let frames = [
4015 (None, Some(80.0)),
4016 (Some(80.0), None),
4017 (Some(0.0), Some(80.0)),
4018 ];
4019 for (theme_padding, widget_padding) in frames {
4020 let mut theme_data = ListTileThemeData::new();
4021 theme_data.min_vertical_padding = theme_padding;
4022 let mut tile = l_title_t();
4023 tile.min_vertical_padding = widget_padding;
4024 spacing_frame(tester, theme_data, text_direction, tile);
4025 assert_eq!(tile_rect(tester, 0).size(), Size::new(800.0, 184.0));
4027 }
4028 }
4029 });
4030 }
4031
4032 #[test]
4034 fn list_tile_min_leading_width_60() {
4035 test_widgets(|tester| {
4036 for (text_direction, edge) in [(TextDirection::Ltr, 92.0), (TextDirection::Rtl, 708.0)]
4039 {
4040 let frames = [
4041 (None, Some(60.0)),
4042 (Some(60.0), None),
4043 (Some(0.0), Some(60.0)),
4044 ];
4045 for (theme_width, widget_width) in frames {
4046 let mut theme_data = ListTileThemeData::new();
4047 theme_data.min_leading_width = theme_width;
4048 let mut tile = l_title_t();
4049 tile.min_leading_width = widget_width;
4050 spacing_frame(tester, theme_data, text_direction, tile);
4051 assert_eq!(
4052 title_edge(tester, text_direction),
4053 (edge, Size::new(800.0, 56.0))
4054 );
4055 }
4056 }
4057 });
4058 }
4059
4060 #[test]
4062 fn list_tile_min_tile_height() {
4063 test_widgets(|tester| {
4064 let height = |tester: &mut WidgetTester, min_tile_height: Option<f64>| {
4065 let mut tile = ListTile::new();
4066 tile.min_tile_height = min_tile_height;
4067 spacing_frame(tester, ListTileThemeData::new(), TextDirection::Ltr, tile);
4068 tile_rect(tester, 0).size()
4069 };
4070 assert_eq!(height(tester, None), Size::new(800.0, 56.0));
4072 assert_eq!(height(tester, Some(30.0)), Size::new(800.0, 30.0));
4074 assert_eq!(height(tester, Some(60.0)), Size::new(800.0, 60.0));
4076 });
4077 }
4078
4079 #[test]
4082 fn list_tile_compute_min_intrinsic_height_respects_min_tile_height_and_padding() {
4083 test_widgets(|tester| {
4084 let column = || {
4085 waft_widgets::Expanded::new(
4086 waft_widgets::Column::new()
4087 .children([ListTile::new().title(Text::new("item.label")).into_widget()]),
4088 )
4089 .into_widget()
4090 };
4091 pump_app(
4092 tester,
4093 |app| {
4094 ThemeData::builder()
4095 .list_tile_theme(ListTileThemeData::new().min_tile_height(30.0))
4096 .build(app)
4097 },
4098 |_app| {
4099 Material::new()
4100 .r#type(crate::material::MaterialType::Transparency)
4101 .child(
4102 waft_widgets::Column::new().children([
4103 waft_widgets::IntrinsicHeight::new()
4104 .child(waft_widgets::Row::new().children([column(), column()]))
4105 .into_widget(),
4106 ]),
4107 )
4108 .into_widget()
4109 },
4110 );
4111 });
4112 }
4113
4114 pub(crate) fn test_text(text: &str, seen: &Rc<RefCell<Option<TextStyle>>>) -> WidgetRef {
4117 let text = text.to_owned();
4118 let seen = Rc::clone(seen);
4119 Builder::new(move |app, context| {
4120 *seen.borrow_mut() = Some(waft_widgets::DefaultTextStyle::of(app, context).style);
4121 Text::new(&text).into_widget()
4122 })
4123 .into_widget()
4124 }
4125
4126 pub(crate) fn test_icon(seen: &Rc<Cell<Option<Color>>>) -> WidgetRef {
4128 let seen = Rc::clone(seen);
4129 Builder::new(move |app, context| {
4130 let icon_theme = IconTheme::of(app, context);
4131 seen.set(icon_theme.color.as_ref().map(AnyColor::color));
4132 waft_widgets::Icon::new(Some(crate::icons::Icons::add())).into_widget()
4133 })
4134 .into_widget()
4135 }
4136
4137 pub(crate) fn style_color(seen: &Rc<RefCell<Option<TextStyle>>>) -> Option<Color> {
4138 seen.borrow()
4139 .as_ref()
4140 .and_then(|style| style.color.as_ref().map(AnyColor::color))
4141 }
4142
4143 #[test]
4145 fn colors_are_applied_to_leading_and_trailing_text_widgets() {
4146 test_widgets(|tester| {
4147 let check = |tester: &mut WidgetTester, enabled: bool, selected: bool| {
4148 let leading = Rc::new(RefCell::new(None));
4149 let trailing = Rc::new(RefCell::new(None));
4150 let title = Rc::new(RefCell::new(None));
4151 let theme = Rc::new(RefCell::new(None));
4152 pump_app(tester, m2, |_app| {
4153 let (leading, trailing, title, theme) = (
4154 Rc::clone(&leading),
4155 Rc::clone(&trailing),
4156 Rc::clone(&title),
4157 Rc::clone(&theme),
4158 );
4159 Material::new()
4160 .child(Center::new().child(Builder::new(move |app, context| {
4161 *theme.borrow_mut() = Some(Theme::of(app, context));
4162 ListTile::new()
4163 .enabled(enabled)
4164 .selected(selected)
4165 .leading(test_text("leading", &leading))
4166 .title(test_text("title", &title))
4167 .trailing(test_text("trailing", &trailing))
4168 .into_widget()
4169 })))
4170 .into_widget()
4171 });
4172 tester.pump_and_settle();
4174 let theme = theme.borrow_mut().take().expect("built");
4175 (style_color(&leading), style_color(&trailing), theme)
4176 };
4177
4178 let (leading, trailing, theme) = check(tester, true, false);
4179 let body_medium = theme
4181 .text_theme
4182 .body_medium
4183 .as_ref()
4184 .and_then(|style| style.color.as_ref().map(AnyColor::color));
4185 assert_eq!(leading, body_medium);
4186 assert_eq!(trailing, body_medium);
4187
4188 let (leading, trailing, theme) = check(tester, true, true);
4189 assert_eq!(leading, Some(theme.primary_color));
4191 assert_eq!(trailing, Some(theme.primary_color));
4192
4193 let (leading, trailing, theme) = check(tester, false, false);
4194 assert_eq!(leading, Some(theme.disabled_color));
4196 assert_eq!(trailing, Some(theme.disabled_color));
4197 });
4198 }
4199
4200 fn slot_icon_colors(
4202 tester: &mut WidgetTester,
4203 theme: fn(&App) -> ThemeData,
4204 selected: bool,
4205 ) -> [Option<Color>; 4] {
4206 let seen: [Rc<Cell<Option<Color>>>; 4] = std::array::from_fn(|_| Rc::new(Cell::new(None)));
4207 pump_app(tester, theme, |_app| {
4208 Material::new()
4209 .child(
4210 Center::new().child(
4211 ListTile::new()
4212 .selected(selected)
4213 .leading(test_icon(&seen[0]))
4214 .title(test_icon(&seen[1]))
4215 .subtitle(test_icon(&seen[2]))
4216 .trailing(test_icon(&seen[3])),
4217 ),
4218 )
4219 .into_widget()
4220 });
4221 seen.map(|seen| seen.get())
4222 }
4223
4224 #[test]
4226 fn selected_enabled_list_tile_default_icon_color() {
4227 test_widgets(|tester| {
4228 let color_scheme = m3(&tester.cell.borrow()).color_scheme;
4229 assert_eq!(
4230 slot_icon_colors(tester, m3, true),
4231 [Some(color_scheme.primary()); 4]
4232 );
4233 assert_eq!(
4234 slot_icon_colors(tester, m3, false),
4235 [Some(color_scheme.on_surface_variant()); 4]
4236 );
4237 });
4238 }
4239
4240 fn slot_text_styles(tester: &mut WidgetTester, theme: fn(&App) -> ThemeData) -> [TextStyle; 4] {
4243 let seen: [Rc<RefCell<Option<TextStyle>>>; 4] =
4244 std::array::from_fn(|_| Rc::new(RefCell::new(None)));
4245 pump_app(tester, theme, |_app| {
4246 Material::new()
4247 .child(
4248 Center::new().child(
4249 ListTile::new()
4250 .leading(test_text("leading", &seen[0]))
4251 .title(test_text("title", &seen[1]))
4252 .subtitle(test_text("subtitle", &seen[2]))
4253 .trailing(test_text("trailing", &seen[3])),
4254 ),
4255 )
4256 .into_widget()
4257 });
4258 seen.map(|seen| seen.borrow_mut().take().expect("built"))
4259 }
4260
4261 #[test]
4264 fn list_tile_font_size() {
4265 test_widgets(|tester| {
4266 let [leading, title, subtitle, trailing] = slot_text_styles(tester, m3);
4267 assert_eq!(leading.font_size, Some(11.0));
4269 assert_eq!(title.font_size, Some(16.0));
4270 assert_eq!(subtitle.font_size, Some(14.0));
4271 assert_eq!(trailing.font_size, Some(11.0));
4272 });
4273 }
4274
4275 #[test]
4277 fn list_tile_text_color() {
4278 test_widgets(|tester| {
4279 let theme = m3(&tester.cell.borrow());
4280 let colors =
4281 slot_text_styles(tester, m3).map(|style| style.color.as_ref().map(AnyColor::color));
4282 assert_eq!(
4284 colors,
4285 [
4286 Some(theme.color_scheme.on_surface_variant()),
4287 Some(theme.color_scheme.on_surface()),
4288 Some(theme.color_scheme.on_surface_variant()),
4289 Some(theme.color_scheme.on_surface_variant()),
4290 ]
4291 );
4292 });
4293 }
4294
4295 fn state_color() -> WidgetStateColor {
4297 WidgetStateColor::resolve_with(|states| {
4298 if states.contains(&WidgetState::Disabled) {
4299 return Colors::RED.primary();
4300 }
4301 if states.contains(&WidgetState::Selected) {
4302 return Colors::GREEN.primary();
4303 }
4304 Colors::BLUE.primary()
4305 })
4306 }
4307
4308 #[test]
4310 fn list_tile_text_color_respects_widget_state_color() {
4311 test_widgets(|tester| {
4312 let title_color = |tester: &mut WidgetTester, enabled: bool, selected: bool| {
4313 pump_app(tester, m3, |_app| {
4314 Material::new()
4315 .child(
4316 Center::new().child(
4317 ListTile::new()
4318 .enabled(enabled)
4319 .selected(selected)
4320 .text_color(state_color())
4321 .title(Text::new("title"))
4322 .subtitle(Text::new("subtitle")),
4323 ),
4324 )
4325 .into_widget()
4326 });
4327 tester.pump_and_settle();
4328 tester.text_color("title")
4329 };
4330 assert_eq!(title_color(tester, false, false), Colors::RED.primary());
4332 assert_eq!(title_color(tester, true, false), Colors::BLUE.primary());
4334 assert_eq!(title_color(tester, true, true), Colors::GREEN.primary());
4336 });
4337 }
4338
4339 #[test]
4341 fn list_tile_icon_color_respects_widget_state_color() {
4342 test_widgets(|tester| {
4343 let icon_color = |tester: &mut WidgetTester, enabled: bool, selected: bool| {
4344 let seen = Rc::new(Cell::new(None));
4345 pump_app(tester, m3, |_app| {
4346 Material::new()
4347 .child(
4348 Center::new().child(
4349 ListTile::new()
4350 .enabled(enabled)
4351 .selected(selected)
4352 .icon_color(state_color())
4353 .leading(test_icon(&seen)),
4354 ),
4355 )
4356 .into_widget()
4357 });
4358 tester.pump_and_settle();
4359 seen.get()
4360 };
4361 assert_eq!(
4363 icon_color(tester, false, false),
4364 Some(Colors::RED.primary())
4365 );
4366 assert_eq!(
4368 icon_color(tester, true, false),
4369 Some(Colors::BLUE.primary())
4370 );
4371 assert_eq!(
4373 icon_color(tester, true, true),
4374 Some(Colors::GREEN.primary())
4375 );
4376 });
4377 }
4378
4379 fn glyph_color(tester: &WidgetTester, icon: &waft_widgets::IconData) -> Option<Color> {
4383 let icon_element = find::by_type::<waft_widgets::Icon>()
4384 .evaluate(tester)
4385 .into_iter()
4386 .find(|element| {
4387 tester.widget(*element, |widget: &waft_widgets::Icon| {
4388 widget.icon.as_ref() == Some(icon)
4389 })
4390 })
4391 .expect("the icon");
4392 let rich_text = find::by_type::<waft_widgets::RichText>()
4393 .evaluate(tester)
4394 .into_iter()
4395 .find(|rich_text| tester.ancestors(*rich_text).contains(&icon_element))
4396 .expect("the icon's RichText");
4397 tester.widget(rich_text, |rich_text: &waft_widgets::RichText| {
4398 rich_text
4399 .text
4400 .style()
4401 .and_then(|style| style.color.as_ref().map(AnyColor::color))
4402 })
4403 }
4404
4405 fn icon_buttons_tile(
4408 tester: &mut WidgetTester,
4409 icon_button_theme: IconButtonThemeData,
4410 icon_color: Option<Color>,
4411 home: fn(ListTile) -> WidgetRef,
4412 ) {
4413 pump_app(
4414 tester,
4415 move |app| {
4416 ThemeData::builder()
4417 .icon_button_theme(icon_button_theme)
4418 .build(app)
4419 },
4420 move |_app| {
4421 let mut tile = ListTile::new()
4422 .leading(IconButton::new(
4423 Some(noop()),
4424 waft_widgets::Icon::new(Some(crate::icons::Icons::favorite())),
4425 ))
4426 .trailing(IconButton::new(
4427 Some(noop()),
4428 waft_widgets::Icon::new(Some(crate::icons::Icons::close())),
4429 ));
4430 if let Some(icon_color) = icon_color {
4431 tile = tile.icon_color(icon_color);
4432 }
4433 home(tile)
4434 },
4435 )
4436 }
4437
4438 fn centered_in_material(tile: ListTile) -> WidgetRef {
4440 Material::new()
4441 .child(Center::new().child(tile))
4442 .into_widget()
4443 }
4444
4445 fn centered_in_scaffold(tile: ListTile) -> WidgetRef {
4447 Scaffold::new()
4448 .body(Center::new().child(tile))
4449 .into_widget()
4450 }
4451
4452 #[test]
4455 fn list_tile_icon_color_respects_icon_color_with_icon_buttons_over_an_icon_button_theme() {
4456 test_widgets(|tester| {
4457 icon_buttons_tile(
4458 tester,
4459 IconButtonThemeData::new().style(
4460 IconButton::style_from()
4461 .foreground_color(Colors::BLUE.primary())
4462 .build(),
4463 ),
4464 Some(Colors::GREEN.primary()),
4465 centered_in_material,
4466 );
4467 assert_eq!(
4468 glyph_color(tester, &crate::icons::Icons::favorite()),
4469 Some(Colors::GREEN.primary())
4470 );
4471 assert_eq!(
4472 glyph_color(tester, &crate::icons::Icons::close()),
4473 Some(Colors::GREEN.primary())
4474 );
4475 });
4476 }
4477
4478 #[test]
4481 fn icon_button_theme_foreground_color_is_preserved_in_list_tile_when_not_overridden() {
4482 test_widgets(|tester| {
4483 icon_buttons_tile(
4484 tester,
4485 IconButtonThemeData::new().style(
4486 IconButton::style_from()
4487 .foreground_color(Colors::BLUE.primary())
4488 .build(),
4489 ),
4490 None,
4491 centered_in_material,
4492 );
4493 assert_eq!(
4494 glyph_color(tester, &crate::icons::Icons::favorite()),
4495 Some(Colors::BLUE.primary())
4496 );
4497 assert_eq!(
4498 glyph_color(tester, &crate::icons::Icons::close()),
4499 Some(Colors::BLUE.primary())
4500 );
4501 });
4502 }
4503
4504 #[test]
4507 fn list_tile_respects_and_combines_parent_icon_button_theme_data_style() {
4508 test_widgets(|tester| {
4509 let custom_shape: WidgetStatePropertyRef<Option<OutlinedBorderRef>> = Rc::new(
4510 WidgetStatePropertyAll::new(Some(OutlinedBorderRef::new(RoundedRectangleBorder {
4511 side: waft_painting::BorderSide::default(),
4512 ..RoundedRectangleBorder::default()
4513 }))),
4514 );
4515 let overlay_color: WidgetStatePropertyRef<Option<Color>> =
4517 Rc::new(WidgetStatePropertyAll::new(Some(Colors::PURPLE.primary())));
4518 let mouse_cursor: WidgetStatePropertyRef<Option<MouseCursorRef>> =
4519 Rc::new(WidgetStatePropertyAll::new(Some(
4520 SystemMouseCursors::RESIZE_UP_RIGHT_DOWN_LEFT.into(),
4521 )));
4522 icon_buttons_tile(
4523 tester,
4524 IconButtonThemeData::new().style(
4525 crate::button_style::ButtonStyle::new()
4526 .overlay_color(Rc::clone(&overlay_color))
4527 .mouse_cursor(Rc::clone(&mouse_cursor))
4528 .shape(Rc::clone(&custom_shape)),
4529 ),
4530 Some(Colors::GREEN.primary()),
4531 centered_in_scaffold,
4532 );
4533
4534 for icon_button in find::by_type::<IconButton>().evaluate(tester) {
4536 let merged_theme = IconButtonTheme::of(&mut tester.cell.borrow_mut(), icon_button);
4537 let style = merged_theme.style.expect("a style");
4538 assert!(Rc::ptr_eq(
4539 style.shape.as_ref().expect("a shape"),
4540 &custom_shape
4541 ));
4542 assert!(Rc::ptr_eq(
4543 style.overlay_color.as_ref().expect("an overlay color"),
4544 &overlay_color
4545 ));
4546 assert!(Rc::ptr_eq(
4547 style.mouse_cursor.as_ref().expect("a mouse cursor"),
4548 &mouse_cursor
4549 ));
4550 }
4551
4552 assert_eq!(
4554 glyph_color(tester, &crate::icons::Icons::favorite()),
4555 Some(Colors::GREEN.primary())
4556 );
4557 assert_eq!(
4558 glyph_color(tester, &crate::icons::Icons::close()),
4559 Some(Colors::GREEN.primary())
4560 );
4561 });
4562 }
4563
4564 #[test]
4566 fn list_tile_dense_does_not_throw_assertion() {
4567 test_widgets(|tester| {
4568 for theme in [m2, m3] {
4570 let theme = theme(&tester.cell.borrow());
4571 tester.pump_widget(material_app(
4572 theme,
4573 Material::new()
4574 .child(Center::new().child(StatefulBuilder::new(
4575 |_app, _context, _set_state| {
4576 ListTile::new()
4577 .dense(true)
4578 .title(Text::new("Title"))
4579 .into_widget()
4580 },
4581 )))
4582 .into_widget(),
4583 ));
4584 }
4585 });
4586 }
4587
4588 struct AlignmentCase<'a> {
4590 tester: &'a mut WidgetTester,
4591 theme: fn(&App) -> ThemeData,
4592 leading_key: GlobalKey,
4593 trailing_key: GlobalKey,
4594 }
4595
4596 impl<'a> AlignmentCase<'a> {
4597 fn new(tester: &'a mut WidgetTester, theme: fn(&App) -> ThemeData) -> AlignmentCase<'a> {
4598 AlignmentCase {
4599 tester,
4600 theme,
4601 leading_key: GlobalKey::new(),
4602 trailing_key: GlobalKey::new(),
4603 }
4604 }
4605
4606 fn offsets(
4609 &mut self,
4610 title_alignment: Option<ListTileTitleAlignment>,
4611 with_subtitle: bool,
4612 is_three_line: bool,
4613 ) -> (f64, f64) {
4614 let mut tile = ListTile::new()
4615 .min_vertical_padding(10.0)
4616 .leading(sized(&self.leading_key, 24.0, 24.0))
4617 .title(SizedBox::new().width(20.0).height(50.0))
4618 .trailing(sized(&self.trailing_key, 24.0, 24.0));
4619 tile.title_alignment = title_alignment;
4620 if with_subtitle {
4621 tile = tile
4622 .subtitle(SizedBox::new().width(20.0).height(50.0))
4623 .is_three_line(is_three_line);
4624 }
4625 let theme = (self.theme)(&self.tester.cell.borrow());
4626 self.tester.pump_widget(material_app(
4627 theme,
4628 Material::new()
4629 .child(Center::new().child(tile))
4630 .into_widget(),
4631 ));
4632 let tile = tile_rect(self.tester, 0).top;
4633 (
4634 key_rect(self.tester, &self.leading_key).top - tile,
4635 key_rect(self.tester, &self.trailing_key).top - tile,
4636 )
4637 }
4638 }
4639
4640 const LEADING_HEIGHT: f64 = 24.0;
4641 const TITLE_HEIGHT: f64 = 50.0;
4642 const MIN_VERTICAL_PADDING: f64 = 10.0;
4643
4644 #[test]
4646 fn title_alignment_position_with_title_widget() {
4647 test_widgets(|tester| {
4648 let mut case = AlignmentCase::new(tester, m3);
4649 const TILE_HEIGHT: f64 = MIN_VERTICAL_PADDING * 2.0 + TITLE_HEIGHT;
4650 const CENTER_POSITION: f64 = (TILE_HEIGHT / 2.0) - (LEADING_HEIGHT / 2.0);
4651 assert_eq!(
4656 case.offsets(None, false, false),
4657 (CENTER_POSITION, CENTER_POSITION)
4658 );
4659 assert_eq!(
4660 case.offsets(Some(ListTileTitleAlignment::ThreeLine), false, false),
4661 (CENTER_POSITION, CENTER_POSITION)
4662 );
4663 assert_eq!(
4667 case.offsets(Some(ListTileTitleAlignment::TitleHeight), false, false),
4668 (16.0, CENTER_POSITION)
4669 );
4670 assert_eq!(
4671 case.offsets(Some(ListTileTitleAlignment::Top), false, false),
4672 (MIN_VERTICAL_PADDING, MIN_VERTICAL_PADDING)
4673 );
4674 assert_eq!(
4675 case.offsets(Some(ListTileTitleAlignment::Center), false, false),
4676 (CENTER_POSITION, CENTER_POSITION)
4677 );
4678 const BOTTOM_POSITION: f64 = TILE_HEIGHT - MIN_VERTICAL_PADDING - LEADING_HEIGHT;
4679 assert_eq!(
4680 case.offsets(Some(ListTileTitleAlignment::Bottom), false, false),
4681 (BOTTOM_POSITION, BOTTOM_POSITION)
4682 );
4683 });
4684 }
4685
4686 #[test]
4688 fn title_alignment_position_with_title_and_subtitle_widgets() {
4689 test_widgets(|tester| {
4690 let mut case = AlignmentCase::new(tester, m3);
4691 const TILE_HEIGHT: f64 = MIN_VERTICAL_PADDING * 2.0 + TITLE_HEIGHT + 50.0;
4692 const CENTER_POSITION: f64 = (TILE_HEIGHT / 2.0) - (LEADING_HEIGHT / 2.0);
4693 assert_eq!(
4694 case.offsets(None, true, false),
4695 (CENTER_POSITION, CENTER_POSITION)
4696 );
4697 assert_eq!(
4698 case.offsets(Some(ListTileTitleAlignment::ThreeLine), true, false),
4699 (CENTER_POSITION, CENTER_POSITION)
4700 );
4701 assert_eq!(
4704 case.offsets(Some(ListTileTitleAlignment::TitleHeight), true, false),
4705 (16.0, 16.0)
4706 );
4707 assert_eq!(
4708 case.offsets(Some(ListTileTitleAlignment::Top), true, false),
4709 (MIN_VERTICAL_PADDING, MIN_VERTICAL_PADDING)
4710 );
4711 assert_eq!(
4712 case.offsets(Some(ListTileTitleAlignment::Center), true, false),
4713 (CENTER_POSITION, CENTER_POSITION)
4714 );
4715 const BOTTOM_POSITION: f64 = TILE_HEIGHT - MIN_VERTICAL_PADDING - LEADING_HEIGHT;
4716 assert_eq!(
4717 case.offsets(Some(ListTileTitleAlignment::Bottom), true, false),
4718 (BOTTOM_POSITION, BOTTOM_POSITION)
4719 );
4720 });
4721 }
4722
4723 #[test]
4725 fn list_tile_is_three_line_updates_the_three_line_alignment() {
4726 test_widgets(|tester| {
4727 let mut case = AlignmentCase::new(tester, m3);
4728 const TILE_HEIGHT: f64 = MIN_VERTICAL_PADDING * 2.0 + TITLE_HEIGHT + 50.0;
4729 const CENTER_POSITION: f64 = (TILE_HEIGHT / 2.0) - (LEADING_HEIGHT / 2.0);
4730 assert_eq!(
4733 case.offsets(None, true, false),
4734 (CENTER_POSITION, CENTER_POSITION)
4735 );
4736 assert_eq!(
4739 case.offsets(None, true, true),
4740 (MIN_VERTICAL_PADDING, MIN_VERTICAL_PADDING)
4741 );
4742 });
4743 }
4744
4745 fn exceeding_width(leading: bool) {
4747 test_widgets(|tester| {
4748 pump_app(tester, m3, |_app| {
4751 let wide = SizedBox::new().width(61.0);
4752 let tile = if leading {
4753 ListTile::new().leading(wide)
4754 } else {
4755 ListTile::new().trailing(wide)
4756 };
4757 Material::new()
4758 .child(Center::new().child(SizedBox::new().width(100.0).child(tile)))
4759 .into_widget()
4760 });
4761 });
4762 }
4763
4764 #[test]
4766 #[should_panic(
4767 expected = "Leading widget consumes the entire tile width (including ListTile.contentPadding).\nEither resize the tile width so that the leading widget plus any content padding do not exceed the tile width, or use a sized widget, or consider replacing ListTile with a custom widget.\nSee also: https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.4"
4768 )]
4769 fn leading_exceeding_list_tile_width_panics() {
4770 exceeding_width(true);
4771 }
4772
4773 #[test]
4775 #[should_panic(
4776 expected = "Trailing widget consumes the entire tile width (including ListTile.contentPadding).\nEither resize the tile width so that the trailing widget plus any content padding do not exceed the tile width, or use a sized widget, or consider replacing ListTile with a custom widget.\nSee also: https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.4"
4777 )]
4778 fn trailing_exceeding_list_tile_width_panics() {
4779 exceeding_width(false);
4780 }
4781
4782 fn is_three_line_rects(
4784 tester: &mut WidgetTester,
4785 theme_data_is_three_line: Option<bool>,
4786 theme_is_three_line: Option<bool>,
4787 is_three_line: Option<bool>,
4788 ) -> [Rect; 6] {
4789 let keys: [GlobalKey; 4] = std::array::from_fn(|_| GlobalKey::new());
4790 pump_unique_app(
4791 tester,
4792 |app| {
4793 let mut theme = ThemeData::builder();
4794 if let Some(is_three_line) = theme_data_is_three_line {
4795 theme = theme
4796 .list_tile_theme(ListTileThemeData::new().is_three_line(is_three_line));
4797 }
4798 theme.build(app)
4799 },
4800 |_app| {
4801 let tile = |avatar_key: &GlobalKey, placeholder_key: &GlobalKey, subtitle: &str| {
4802 let mut tile = ListTile::new()
4803 .leading(avatar(avatar_key))
4804 .trailing(placeholder(placeholder_key, 24.0, 24.0))
4805 .title(Text::new("A"))
4806 .subtitle(Text::new(subtitle));
4807 tile.is_three_line = is_three_line;
4808 tile.into_widget()
4809 };
4810 let list = ListView::new().children([
4811 tile(&keys[0], &keys[1], THIRTEEN_LINES),
4812 tile(&keys[2], &keys[3], "A"),
4813 ]);
4814 let mut theme = ListTileTheme::new(list);
4815 if let Some(is_three_line) = theme_is_three_line {
4816 theme = theme.data(ListTileThemeData::new().is_three_line(is_three_line));
4817 }
4818 Material::new().child(theme).into_widget()
4819 },
4820 );
4821 [
4822 tile_rect(tester, 0),
4823 key_rect(tester, &keys[0]),
4824 key_rect(tester, &keys[1]),
4825 tile_rect(tester, 1),
4826 key_rect(tester, &keys[2]),
4827 key_rect(tester, &keys[3]),
4828 ]
4829 }
4830
4831 #[test]
4833 fn list_tile_is_three_line() {
4834 test_widgets(|tester| {
4835 const HEIGHT: f64 = 300.0;
4836 const AVATAR_TOP: f64 = 130.0;
4837 const PLACEHOLDER_TOP: f64 = 138.0;
4838 let two_line = [
4839 Rect::from_ltwh(0.0, 0.0, 800.0, HEIGHT),
4840 Rect::from_ltwh(16.0, AVATAR_TOP, 40.0, 40.0),
4841 Rect::from_ltwh(800.0 - 24.0 - 24.0, PLACEHOLDER_TOP, 24.0, 24.0),
4842 Rect::from_ltwh(0.0, HEIGHT, 800.0, 72.0),
4843 Rect::from_ltwh(16.0, HEIGHT + 16.0, 40.0, 40.0),
4844 Rect::from_ltwh(800.0 - 24.0 - 24.0, HEIGHT + 24.0, 24.0, 24.0),
4845 ];
4846 let three_line = [
4847 Rect::from_ltwh(0.0, 0.0, 800.0, HEIGHT),
4848 Rect::from_ltwh(16.0, 8.0, 40.0, 40.0),
4849 Rect::from_ltwh(800.0 - 24.0 - 24.0, 8.0, 24.0, 24.0),
4850 Rect::from_ltwh(0.0, HEIGHT, 800.0, 88.0),
4851 Rect::from_ltwh(16.0, HEIGHT + 8.0, 40.0, 40.0),
4852 Rect::from_ltwh(800.0 - 24.0 - 24.0, HEIGHT + 8.0, 24.0, 24.0),
4853 ];
4854 let frames = [
4855 ((None, None, None), two_line),
4856 ((Some(true), None, None), three_line),
4857 ((Some(false), Some(true), None), three_line),
4858 ((Some(true), Some(false), None), two_line),
4859 ((None, None, Some(true)), three_line),
4860 ((None, Some(true), Some(false)), two_line),
4861 ((Some(true), None, Some(false)), two_line),
4862 ((Some(true), Some(true), Some(false)), two_line),
4863 ((None, Some(false), Some(true)), three_line),
4864 ((Some(false), None, Some(true)), three_line),
4865 ((Some(false), Some(false), Some(true)), three_line),
4866 ];
4867 for ((theme_data, theme, tile), expected) in frames {
4868 assert_eq!(
4869 is_three_line_rects(tester, theme_data, theme, tile),
4870 expected
4871 );
4872 }
4873 });
4874 }
4875
4876 #[test]
4879 fn a_three_line_tile_may_be_given_its_subtitle_after_is_three_line() {
4880 test_widgets(|tester| {
4881 pump_app(tester, m3, |_app| {
4882 Material::new()
4883 .child(
4884 ListTile::new()
4885 .is_three_line(true)
4886 .title(Text::new("title"))
4887 .subtitle(Text::new("subtitle")),
4888 )
4889 .into_widget()
4890 });
4891 expect(tester, &find::by_type::<ListTile>(), finds_one_widget());
4892 });
4893 }
4894
4895 #[test]
4897 #[should_panic(expected = "self.is_three_line != Some(true) || self.subtitle.is_some()")]
4898 fn a_three_line_tile_without_a_subtitle_panics_when_made_a_widget() {
4899 ListTile::new()
4900 .is_three_line(true)
4901 .title(Text::new("title"))
4902 .into_widget();
4903 }
4904
4905 #[test]
4907 fn list_tile_states_controller() {
4908 test_widgets(|tester| {
4909 let count = Rc::new(Cell::new(0));
4910 let controller = MaterialStatesController::new(&mut tester.cell.borrow_mut(), None);
4911 controller.add_listener(&mut tester.cell.borrow_mut(), {
4912 let count = Rc::clone(&count);
4913 Listener::new(move |_app| count.set(count.get() + 1))
4914 });
4915 let frame = |tester: &WidgetTester, enabled: bool| {
4916 let mut tile = ListTile::new()
4917 .states_controller(controller.clone())
4918 .title(Text::new("title"));
4919 if enabled {
4920 tile = tile.on_tap(noop());
4921 }
4922 material_app(m3(&tester.cell.borrow()), centered_in_scaffold(tile))
4923 };
4924 let value = |tester: &WidgetTester| -> WidgetStates {
4925 tester.cell.borrow().get(&controller).value().clone()
4926 };
4927 let states =
4928 |states: &[WidgetState]| -> WidgetStates { states.iter().copied().collect() };
4929
4930 let first = frame(tester, true);
4931 tester.pump_widget(first);
4932
4933 assert_eq!(value(tester), states(&[]));
4934 assert_eq!(count.get(), 0);
4935
4936 let center = tester
4937 .get_rect(find::text("title").evaluate(tester)[0])
4938 .center();
4939 let mut gesture = TestPointer::mouse(1, waft_gestures::K_PRIMARY_BUTTON);
4940 tester.add_pointer(&mut gesture, Offset::ZERO);
4941 tester.move_to(&mut gesture, center, false);
4942 tester.pump_and_settle();
4943
4944 assert_eq!(value(tester), states(&[WidgetState::Hovered]));
4945 assert_eq!(count.get(), 1);
4946
4947 tester.move_to(&mut gesture, Offset::ZERO, false);
4948 tester.pump_and_settle();
4949
4950 assert_eq!(value(tester), states(&[]));
4951 assert_eq!(count.get(), 2);
4952
4953 tester.move_to(&mut gesture, center, false);
4954 tester.pump_and_settle();
4955
4956 assert_eq!(value(tester), states(&[WidgetState::Hovered]));
4957 assert_eq!(count.get(), 3);
4958
4959 tester.send(&gesture, waft_embedder::PointerChange::Down);
4960 tester.pump_and_settle();
4961
4962 assert_eq!(
4963 value(tester),
4964 states(&[WidgetState::Hovered, WidgetState::Pressed])
4965 );
4966 assert_eq!(count.get(), 4);
4967
4968 tester.up(&gesture);
4969 tester.pump_and_settle();
4970
4971 assert_eq!(value(tester), states(&[WidgetState::Hovered]));
4972 assert_eq!(count.get(), 5);
4973
4974 tester.move_to(&mut gesture, Offset::ZERO, false);
4975 tester.pump_and_settle();
4976
4977 assert_eq!(value(tester), states(&[]));
4978 assert_eq!(count.get(), 6);
4979
4980 tester.down(&mut gesture, center);
4981 tester.pump_and_settle();
4982 assert_eq!(
4983 value(tester),
4984 states(&[WidgetState::Hovered, WidgetState::Pressed])
4985 );
4986 assert_eq!(count.get(), 8); let disabled = frame(tester, false);
4991 tester.pump_widget(disabled);
4992 tester.pump_and_settle();
4993 assert_eq!(
4994 value(tester),
4995 states(&[WidgetState::Hovered, WidgetState::Disabled])
4996 );
4997 assert_eq!(count.get(), 10); tester.move_to(&mut gesture, Offset::ZERO, true);
4999 tester.pump_and_settle();
5000 assert_eq!(value(tester), states(&[WidgetState::Disabled]));
5001 assert_eq!(count.get(), 11);
5002 });
5003 }
5004
5005 #[test]
5007 fn list_tile_does_not_crash_at_zero_area() {
5008 test_widgets(|tester| {
5009 pump_app(tester, m3, |_app| {
5010 Scaffold::new()
5011 .body(
5012 Center::new().child(
5013 SizedBox::shrink().child(
5014 ListTile::new()
5015 .title(Text::new("title"))
5016 .leading(waft_widgets::Icon::new(Some(
5017 crate::icons::Icons::add(),
5018 )))
5019 .trailing(waft_widgets::Icon::new(Some(
5020 crate::icons::Icons::remove(),
5021 )))
5022 .subtitle(Text::new("subTitle")),
5023 ),
5024 ),
5025 )
5026 .into_widget()
5027 });
5028 assert_eq!(tile_rect(tester, 0).size(), Size::ZERO);
5029 });
5030 }
5031
5032 fn background_check_message(around: fn(ListTile) -> WidgetRef) -> Option<String> {
5037 let mut tester = WidgetTester::new();
5038 let built = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
5039 pump_app(&mut tester, m3, |_app| {
5040 Material::new()
5041 .child(
5042 Center::new().child(around(
5043 ListTile::new()
5044 .tile_color(Colors::RED.primary())
5045 .title(Text::new("ListTile")),
5046 )),
5047 )
5048 .into_widget()
5049 });
5050 }));
5051 match built {
5052 Ok(()) => None,
5053 Err(panic) => Some(
5054 panic
5055 .downcast_ref::<String>()
5056 .cloned()
5057 .unwrap_or_else(|| "a panic without a message".to_owned()),
5058 ),
5059 }
5060 }
5061
5062 #[test]
5065 fn list_tile_shows_warning_when_a_container_with_color_wraps_it() {
5066 test_widgets(|_| {
5067 let message = background_check_message(|tile| {
5068 Container::new()
5069 .color(Colors::AMBER.primary())
5070 .height(200.0)
5071 .child(tile)
5072 .into_widget()
5073 })
5074 .expect("a reported error");
5075 assert!(message.contains("ListTile background color or ink splashes may be invisible"));
5076 assert!(message.contains("The ListTile is wrapped in a ColoredBox"));
5077 assert!(message.contains("ListTile:"));
5078 assert!(message.contains("tile_color:"));
5079 assert!(message.contains("ColoredBox:"));
5080 assert!(message.contains("color:"));
5081 });
5082 }
5083
5084 #[test]
5086 fn list_tile_panics_when_wrapped_in_a_colored_box_with_a_non_transparent_color() {
5087 test_widgets(|_| {
5088 let message = background_check_message(|tile| {
5089 ColoredBox::new(Colors::AMBER.primary())
5090 .child(tile)
5091 .into_widget()
5092 })
5093 .expect("a reported error");
5094 assert!(message.contains("ListTile background color or ink splashes may be invisible"));
5095 assert!(message.contains("The ListTile is wrapped in a ColoredBox"));
5096 assert!(message.contains("ListTile:"));
5097 assert!(message.contains("tile_color:"));
5098 assert!(message.contains("ColoredBox:"));
5099 assert!(message.contains("color:"));
5100 });
5101 }
5102
5103 #[test]
5105 fn list_tile_panics_when_wrapped_in_a_decorated_box_with_a_non_transparent_color() {
5106 test_widgets(|_| {
5107 let message = background_check_message(|tile| {
5108 DecoratedBox::new(BoxDecoration::new().color(Colors::AMBER.primary()))
5109 .child(tile)
5110 .into_widget()
5111 })
5112 .expect("a reported error");
5113 assert!(message.contains("ListTile background color or ink splashes may be invisible"));
5114 assert!(message.contains("The ListTile is wrapped in a DecoratedBox"));
5115 assert!(message.contains("ListTile:"));
5116 assert!(message.contains("tile_color:"));
5117 assert!(message.contains("DecoratedBox:"));
5118 assert!(message.contains("color"));
5119 });
5120 }
5121
5122 #[test]
5124 fn list_tile_does_not_panic_when_the_parent_has_no_or_a_transparent_color() {
5125 test_widgets(|_| {
5126 let message = background_check_message(|tile| {
5127 ColoredBox::new(Colors::TRANSPARENT)
5128 .child(tile)
5129 .into_widget()
5130 });
5131 assert_eq!(message, None);
5132 });
5133 }
5134
5135 #[test]
5141 fn material2_list_tile_geometry_ltr() {
5142 test_widgets(|tester| {
5143 let plain = GeometryFrame::PLAIN;
5144 let frames = [
5145 (plain, 56.0),
5146 (
5147 GeometryFrame {
5148 dense: true,
5149 ..plain
5150 },
5151 48.0,
5152 ),
5153 (
5154 GeometryFrame {
5155 is_two_line: true,
5156 ..plain
5157 },
5158 72.0,
5159 ),
5160 (
5161 GeometryFrame {
5162 is_two_line: true,
5163 dense: true,
5164 ..plain
5165 },
5166 64.0,
5167 ),
5168 (
5169 GeometryFrame {
5170 is_three_line: true,
5171 ..plain
5172 },
5173 88.0,
5174 ),
5175 (
5176 GeometryFrame {
5177 is_three_line: true,
5178 dense: true,
5179 ..plain
5180 },
5181 76.0,
5182 ),
5183 (
5184 GeometryFrame {
5185 text_scale: 4.0,
5186 ..plain
5187 },
5188 72.0,
5189 ),
5190 (
5191 GeometryFrame {
5192 dense: true,
5193 text_scale: 4.0,
5194 ..plain
5195 },
5196 72.0,
5197 ),
5198 (
5199 GeometryFrame {
5200 is_two_line: true,
5201 text_scale: 4.0,
5202 ..plain
5203 },
5204 128.0,
5205 ),
5206 (
5208 GeometryFrame {
5209 is_two_line: true,
5210 text_scale: 0.5,
5211 subtitle_scale: Some(4.0),
5212 ..plain
5213 },
5214 72.0,
5215 ),
5216 (
5217 GeometryFrame {
5218 is_two_line: true,
5219 dense: true,
5220 text_scale: 4.0,
5221 ..plain
5222 },
5223 128.0,
5224 ),
5225 (
5226 GeometryFrame {
5227 is_three_line: true,
5228 text_scale: 4.0,
5229 ..plain
5230 },
5231 128.0,
5232 ),
5233 (
5234 GeometryFrame {
5235 is_three_line: true,
5236 dense: true,
5237 text_scale: 4.0,
5238 ..plain
5239 },
5240 128.0,
5241 ),
5242 ];
5243 let keys = [GlobalKey::new(), GlobalKey::new()];
5244 for (frame, expected_height) in frames {
5245 check_ltr_geometry(tester, &keys, m2, frame, expected_height, 16.0, 56.0);
5247 }
5248 });
5249 }
5250
5251 #[test]
5253 fn material2_list_tile_geometry_rtl() {
5254 test_widgets(|tester| {
5255 pump_app(tester, m2, |_app| {
5256 MediaQuery::new(
5257 MediaQueryData::new().padding(EdgeInsets::only(
5258 LEFT_PADDING,
5259 0.0,
5260 RIGHT_PADDING,
5261 0.0,
5262 )),
5263 Directionality::new(
5264 TextDirection::Rtl,
5265 Material::new().child(
5266 Center::new().child(
5267 ListTile::new()
5268 .leading(Text::new("L"))
5269 .title(Text::new("title"))
5270 .trailing(Text::new("T")),
5271 ),
5272 ),
5273 ),
5274 )
5275 .into_widget()
5276 });
5277 assert_eq!(
5278 tester.get_rect(find::text("L")).right,
5279 800.0 - 16.0_f64.max(RIGHT_PADDING)
5280 );
5281 assert_eq!(
5282 tester.get_rect(find::text("title")).right,
5283 800.0 - 56.0 - 16.0_f64.max(RIGHT_PADDING)
5284 );
5285 assert_eq!(
5286 tester.get_rect(find::text("T")).left,
5287 16.0_f64.max(LEFT_PADDING)
5288 );
5289 });
5290 }
5291
5292 #[test]
5295 fn material2_list_tile_leading_and_trailing_positions() {
5296 test_widgets(|tester| {
5297 let keys: [GlobalKey; 4] = std::array::from_fn(|_| GlobalKey::new());
5300 let [
5301 first_leading,
5302 first_trailing,
5303 second_leading,
5304 second_trailing,
5305 ] = &keys;
5306 let frame = |tester: &mut WidgetTester,
5307 small_leading: bool,
5308 title: &str,
5309 subtitle: Option<&str>,
5310 dense: bool,
5311 is_three_line: bool| {
5312 let tile = |leading_key: &GlobalKey, trailing_key: &GlobalKey, lines: &str| {
5313 let leading = if small_leading {
5314 placeholder(leading_key, 24.0, 12.0)
5315 } else {
5316 avatar(leading_key)
5317 };
5318 let (title, subtitle) = match subtitle {
5319 None => (lines, None),
5320 Some(_) => (title, Some(lines)),
5321 };
5322 let mut tile = ListTile::new()
5323 .dense(dense)
5324 .leading(leading)
5325 .trailing(placeholder(trailing_key, 24.0, 24.0))
5326 .title(Text::new(title));
5327 if let Some(subtitle) = subtitle {
5328 tile = tile.subtitle(Text::new(subtitle));
5329 }
5330 tile.is_three_line(is_three_line).into_widget()
5331 };
5332 let second_lines = subtitle.unwrap_or(title);
5333 let tree = bare(
5334 m2(&tester.cell.borrow()),
5335 Material::new().child(ListView::new().children([
5336 tile(first_leading, first_trailing, THIRTEEN_LINES),
5337 tile(second_leading, second_trailing, second_lines),
5338 ])),
5339 );
5340 tester.pump_widget(tree);
5341 };
5342 let check = |tester: &WidgetTester, expected: [Rect; 6]| {
5343 assert_eq!(
5344 [
5345 tile_rect(tester, 0),
5346 key_rect(tester, first_leading),
5347 key_rect(tester, first_trailing),
5348 tile_rect(tester, 1),
5349 key_rect(tester, second_leading),
5350 key_rect(tester, second_trailing),
5351 ],
5352 expected
5353 );
5354 };
5355 let trailing_left = 800.0 - 24.0 - 16.0;
5356
5357 frame(tester, false, "A", None, true, false);
5359 check(
5360 tester,
5361 [
5362 Rect::from_ltwh(0.0, 0.0, 800.0, 177.0),
5363 Rect::from_ltwh(16.0, 16.0, 40.0, 40.0),
5364 Rect::from_ltwh(trailing_left, 16.0, 24.0, 24.0),
5365 Rect::from_ltwh(0.0, 177.0, 800.0, 48.0),
5366 Rect::from_ltwh(16.0, 177.0 + 4.0, 40.0, 40.0),
5367 Rect::from_ltwh(trailing_left, 177.0 + 12.0, 24.0, 24.0),
5368 ],
5369 );
5370
5371 frame(tester, false, "A", None, false, false);
5373 tester.pump_by(Duration::from_secs(2)); check(
5375 tester,
5376 [
5377 Rect::from_ltwh(0.0, 0.0, 800.0, 216.0),
5378 Rect::from_ltwh(16.0, 16.0, 40.0, 40.0),
5379 Rect::from_ltwh(trailing_left, 16.0, 24.0, 24.0),
5380 Rect::from_ltwh(0.0, 216.0, 800.0, 56.0),
5381 Rect::from_ltwh(16.0, 216.0 + 8.0, 40.0, 40.0),
5382 Rect::from_ltwh(trailing_left, 216.0 + 16.0, 24.0, 24.0),
5383 ],
5384 );
5385
5386 frame(tester, false, "A", Some("A"), true, false);
5388 check(
5389 tester,
5390 [
5391 Rect::from_ltwh(0.0, 0.0, 800.0, 180.0),
5392 Rect::from_ltwh(16.0, 16.0, 40.0, 40.0),
5393 Rect::from_ltwh(trailing_left, 16.0, 24.0, 24.0),
5394 Rect::from_ltwh(0.0, 180.0, 800.0, 64.0),
5395 Rect::from_ltwh(16.0, 180.0 + 12.0, 40.0, 40.0),
5396 Rect::from_ltwh(trailing_left, 180.0 + 20.0, 24.0, 24.0),
5397 ],
5398 );
5399
5400 frame(tester, false, "A", Some("A"), false, false);
5402 check(
5403 tester,
5404 [
5405 Rect::from_ltwh(0.0, 0.0, 800.0, 180.0),
5406 Rect::from_ltwh(16.0, 16.0, 40.0, 40.0),
5407 Rect::from_ltwh(trailing_left, 16.0, 24.0, 24.0),
5408 Rect::from_ltwh(0.0, 180.0, 800.0, 72.0),
5409 Rect::from_ltwh(16.0, 180.0 + 16.0, 40.0, 40.0),
5410 Rect::from_ltwh(trailing_left, 180.0 + 24.0, 24.0, 24.0),
5411 ],
5412 );
5413
5414 frame(tester, false, "A", Some("A"), true, true);
5416 check(
5417 tester,
5418 [
5419 Rect::from_ltwh(0.0, 0.0, 800.0, 180.0),
5420 Rect::from_ltwh(16.0, 16.0, 40.0, 40.0),
5421 Rect::from_ltwh(trailing_left, 16.0, 24.0, 24.0),
5422 Rect::from_ltwh(0.0, 180.0, 800.0, 76.0),
5423 Rect::from_ltwh(16.0, 180.0 + 16.0, 40.0, 40.0),
5424 Rect::from_ltwh(trailing_left, 180.0 + 16.0, 24.0, 24.0),
5425 ],
5426 );
5427
5428 frame(tester, false, "A", Some("A"), false, true);
5430 check(
5431 tester,
5432 [
5433 Rect::from_ltwh(0.0, 0.0, 800.0, 180.0),
5434 Rect::from_ltwh(16.0, 16.0, 40.0, 40.0),
5435 Rect::from_ltwh(trailing_left, 16.0, 24.0, 24.0),
5436 Rect::from_ltwh(0.0, 180.0, 800.0, 88.0),
5437 Rect::from_ltwh(16.0, 180.0 + 16.0, 40.0, 40.0),
5438 Rect::from_ltwh(trailing_left, 180.0 + 16.0, 24.0, 24.0),
5439 ],
5440 );
5441
5442 frame(tester, true, "A", None, false, false);
5444 tester.pump_by(Duration::from_secs(2)); check(
5446 tester,
5447 [
5448 Rect::from_ltwh(0.0, 0.0, 800.0, 216.0),
5449 Rect::from_ltwh(16.0, 16.0, 24.0, 12.0),
5450 Rect::from_ltwh(trailing_left, 16.0, 24.0, 24.0),
5451 Rect::from_ltwh(0.0, 216.0, 800.0, 56.0),
5452 Rect::from_ltwh(16.0, 216.0 + 16.0, 24.0, 12.0),
5453 Rect::from_ltwh(trailing_left, 216.0 + 16.0, 24.0, 24.0),
5454 ],
5455 );
5456 });
5457 }
5458
5459 #[test]
5461 fn material2_list_tile_leading_icon_height_does_not_exceed_list_tile_height() {
5462 test_widgets(|tester| {
5463 let cases = [
5464 ((false, false, true), [(0.0, 48.0), (48.0, 48.0)]),
5466 ((false, false, false), [(0.0, 56.0), (56.0, 56.0)]),
5468 ((true, false, true), [(8.0, 48.0), (64.0 + 8.0, 48.0)]),
5470 ((true, false, false), [(8.0, 56.0), (72.0 + 8.0, 56.0)]),
5472 ((true, true, true), [(16.0, 48.0), (76.0 + 16.0, 48.0)]),
5474 ((true, true, false), [(16.0, 56.0), (88.0 + 16.0, 56.0)]),
5476 ];
5477 for ((subtitle, is_three_line, dense), expected) in cases {
5478 assert_eq!(
5479 oversized_rects_dense(tester, m2, true, subtitle, is_three_line, dense),
5480 expected.map(|(top, height)| Rect::from_ltwh(16.0, top, 24.0, height))
5481 );
5482 }
5483 });
5484 }
5485
5486 #[test]
5488 fn material2_list_tile_trailing_icon_height_does_not_exceed_list_tile_height() {
5489 test_widgets(|tester| {
5490 let cases = [
5491 ((false, false, true), [(0.0, 48.0), (48.0, 48.0)]),
5493 ((false, false, false), [(0.0, 56.0), (56.0, 56.0)]),
5495 ((true, false, true), [(8.0, 48.0), (64.0 + 8.0, 48.0)]),
5497 ((true, false, false), [(8.0, 56.0), (72.0 + 8.0, 56.0)]),
5499 ((true, true, true), [(16.0, 48.0), (76.0 + 16.0, 48.0)]),
5501 ((true, true, false), [(16.0, 56.0), (88.0 + 16.0, 56.0)]),
5503 ];
5504 for ((subtitle, is_three_line, dense), expected) in cases {
5505 assert_eq!(
5506 oversized_rects_dense(tester, m2, false, subtitle, is_three_line, dense),
5507 expected.map(|(top, height)| Rect::from_ltwh(
5508 800.0 - 16.0 - 24.0,
5509 top,
5510 24.0,
5511 height
5512 ))
5513 );
5514 }
5515 });
5516 }
5517
5518 #[test]
5520 fn material2_list_tile_wide_leading_widget() {
5521 test_widgets(|tester| check_wide_leading(tester, m2, 16.0, 56.0));
5522 }
5523
5524 #[test]
5526 fn material2_list_tile_horizontal_title_gap_0() {
5527 test_widgets(|tester| {
5528 for (text_direction, edge) in [(TextDirection::Ltr, 56.0), (TextDirection::Rtl, 744.0)]
5529 {
5530 let frames = [
5531 (None, Some(0.0)),
5532 (Some(0.0), None),
5533 (Some(10.0), Some(0.0)),
5534 ];
5535 for (theme_gap, widget_gap) in frames {
5536 let mut theme_data = ListTileThemeData::new();
5537 theme_data.horizontal_title_gap = theme_gap;
5538 let mut tile = l_title_t();
5539 tile.horizontal_title_gap = widget_gap;
5540 spacing_frame_in(tester, m2, theme_data, text_direction, tile);
5541 assert_eq!(
5542 title_edge(tester, text_direction),
5543 (edge, Size::new(800.0, 56.0))
5544 );
5545 }
5546 }
5547 });
5548 }
5549
5550 #[test]
5553 fn material2_list_tile_default_horizontal_title_gap_and_min_leading_width() {
5554 test_widgets(|tester| {
5555 spacing_frame_in(
5556 tester,
5557 m2,
5558 ListTileThemeData::new(),
5559 TextDirection::Ltr,
5560 l_title_t(),
5561 );
5562 assert_eq!(
5564 title_edge(tester, TextDirection::Ltr),
5565 (72.0, Size::new(800.0, 56.0))
5566 );
5567
5568 spacing_frame_in(
5569 tester,
5570 m2,
5571 ListTileThemeData::new(),
5572 TextDirection::Rtl,
5573 l_title_t(),
5574 );
5575 assert_eq!(
5577 title_edge(tester, TextDirection::Rtl),
5578 (728.0, Size::new(800.0, 56.0))
5579 );
5580 });
5581 }
5582
5583 #[test]
5585 fn material2_list_tile_horizontal_title_gap_with_visual_density() {
5586 test_widgets(|tester| {
5587 spacing_frame_in(
5588 tester,
5589 m2,
5590 ListTileThemeData::new(),
5591 TextDirection::Ltr,
5592 l_title_t()
5593 .visual_density(VisualDensity::new(VisualDensity::MINIMUM_DENSITY, 0.0))
5594 .horizontal_title_gap(10.0),
5595 );
5596 assert_eq!(
5597 title_edge(tester, TextDirection::Ltr),
5598 (58.0, Size::new(800.0, 56.0))
5599 );
5600 });
5601 }
5602
5603 #[test]
5605 fn material2_list_tile_min_vertical_padding_80() {
5606 test_widgets(|tester| {
5607 for text_direction in [TextDirection::Ltr, TextDirection::Rtl] {
5608 let frames = [
5609 (None, Some(80.0)),
5610 (Some(80.0), None),
5611 (Some(0.0), Some(80.0)),
5612 ];
5613 for (theme_padding, widget_padding) in frames {
5614 let mut theme_data = ListTileThemeData::new();
5615 theme_data.min_vertical_padding = theme_padding;
5616 let mut tile = l_title_t();
5617 tile.min_vertical_padding = widget_padding;
5618 spacing_frame_in(tester, m2, theme_data, text_direction, tile);
5619 assert_eq!(tile_rect(tester, 0).size(), Size::new(800.0, 176.0));
5621 }
5622 }
5623 });
5624 }
5625
5626 fn slot_text_styles_of(
5629 tester: &mut WidgetTester,
5630 theme: fn(&App) -> ThemeData,
5631 dense: bool,
5632 style: Option<ListTileStyle>,
5633 settle: bool,
5634 ) -> [TextStyle; 4] {
5635 let seen: [Rc<RefCell<Option<TextStyle>>>; 4] =
5636 std::array::from_fn(|_| Rc::new(RefCell::new(None)));
5637 pump_app(tester, theme, |_app| {
5638 let mut tile = ListTile::new()
5639 .dense(dense)
5640 .leading(test_text("leading", &seen[0]))
5641 .title(test_text("title", &seen[1]))
5642 .subtitle(test_text("subtitle", &seen[2]))
5643 .trailing(test_text("trailing", &seen[3]));
5644 tile.style = style;
5645 Material::new()
5646 .child(Center::new().child(tile))
5647 .into_widget()
5648 });
5649 if settle {
5650 tester.pump_and_settle();
5651 }
5652 seen.map(|seen| seen.borrow_mut().take().expect("built"))
5653 }
5654
5655 #[test]
5657 fn material2_list_tile_font_size() {
5658 test_widgets(|tester| {
5659 let sizes = |tester: &mut WidgetTester,
5661 dense: bool,
5662 style: Option<ListTileStyle>,
5663 settle: bool| {
5664 slot_text_styles_of(tester, m2, dense, style, settle).map(|style| style.font_size)
5665 };
5666 assert_eq!(
5668 sizes(tester, false, None, false),
5669 [Some(14.0), Some(16.0), Some(14.0), Some(14.0)]
5670 );
5671 assert_eq!(
5673 sizes(tester, true, None, true),
5674 [Some(14.0), Some(13.0), Some(12.0), Some(14.0)]
5675 );
5676 assert_eq!(
5678 sizes(tester, false, Some(ListTileStyle::Drawer), true),
5679 [Some(14.0), Some(14.0), Some(14.0), Some(14.0)]
5680 );
5681 assert_eq!(
5683 sizes(tester, true, Some(ListTileStyle::Drawer), true),
5684 [Some(14.0), Some(13.0), Some(12.0), Some(14.0)]
5685 );
5686 });
5687 }
5688
5689 #[test]
5691 fn material2_list_tile_text_color() {
5692 test_widgets(|tester| {
5693 let theme = m2(&tester.cell.borrow());
5694 let color_of = |style: &Option<TextStyle>| {
5695 style
5696 .as_ref()
5697 .and_then(|style| style.color.as_ref().map(AnyColor::color))
5698 };
5699 let expected = [
5700 color_of(&theme.text_theme.body_medium),
5701 color_of(&theme.text_theme.title_medium),
5702 color_of(&theme.text_theme.body_small),
5703 color_of(&theme.text_theme.body_medium),
5704 ];
5705 for style in [None, Some(ListTileStyle::Drawer)] {
5706 let colors = slot_text_styles_of(tester, m2, false, style, false)
5707 .map(|style| style.color.as_ref().map(AnyColor::color));
5708 assert_eq!(colors, expected);
5709 }
5710 });
5711 }
5712
5713 #[test]
5715 fn material2_selected_enabled_list_tile_default_icon_color_light_and_dark_themes() {
5716 test_widgets(|_| {
5717 fn light(app: &App) -> ThemeData {
5719 ThemeData::from(crate::color_scheme::ColorScheme::light())
5720 .use_material3(false)
5721 .build(app)
5722 }
5723 fn dark(app: &App) -> ThemeData {
5724 ThemeData::from(crate::color_scheme::ColorScheme::dark())
5725 .use_material3(false)
5726 .build(app)
5727 }
5728 let slot_icon_colors = |theme: fn(&App) -> ThemeData, selected: bool| {
5731 slot_icon_colors(&mut WidgetTester::new(), theme, selected)
5732 };
5733 let light_color_scheme = crate::color_scheme::ColorScheme::light();
5734 let dark_color_scheme = crate::color_scheme::ColorScheme::dark();
5735
5736 assert_eq!(
5737 slot_icon_colors(light, true),
5738 [Some(light_color_scheme.primary()); 4]
5739 );
5740 assert_eq!(slot_icon_colors(light, false), [Some(Colors::BLACK45); 4]);
5741 assert_eq!(
5742 slot_icon_colors(dark, true),
5743 [Some(dark_color_scheme.primary()); 4]
5744 );
5745 assert_eq!(slot_icon_colors(dark, false), [Some(Colors::WHITE); 4]);
5748 });
5749 }
5750
5751 #[test]
5754 fn material2_list_tile_default_tile_color() {
5755 test_widgets(|tester| {
5756 toggling_tile(tester, m2, ListTile::new);
5757 assert_eq!(ink_tile_color(tester), Some(Colors::TRANSPARENT));
5758
5759 let center = tile_rect(tester, 0).center();
5761 tester.tap_at(center, 1);
5762 tester.pump_and_settle();
5763
5764 assert_eq!(ink_tile_color(tester), Some(Colors::TRANSPARENT));
5765 });
5766 }
5767
5768 #[test]
5770 fn material2_title_alignment_position_with_title_widget() {
5771 test_widgets(|tester| {
5772 let mut case = AlignmentCase::new(tester, m2);
5773 const TILE_HEIGHT: f64 = MIN_VERTICAL_PADDING * 2.0 + TITLE_HEIGHT;
5774 const CENTER_POSITION: f64 = (TILE_HEIGHT / 2.0) - (LEADING_HEIGHT / 2.0);
5775 const TITLE_POSITION: f64 = 16.0;
5776 assert_eq!(
5781 case.offsets(None, false, false),
5782 (TITLE_POSITION, CENTER_POSITION)
5783 );
5784 assert_eq!(
5785 case.offsets(Some(ListTileTitleAlignment::ThreeLine), false, false),
5786 (CENTER_POSITION, CENTER_POSITION)
5787 );
5788 assert_eq!(
5789 case.offsets(Some(ListTileTitleAlignment::TitleHeight), false, false),
5790 (TITLE_POSITION, CENTER_POSITION)
5791 );
5792 assert_eq!(
5793 case.offsets(Some(ListTileTitleAlignment::Top), false, false),
5794 (MIN_VERTICAL_PADDING, MIN_VERTICAL_PADDING)
5795 );
5796 assert_eq!(
5797 case.offsets(Some(ListTileTitleAlignment::Center), false, false),
5798 (CENTER_POSITION, CENTER_POSITION)
5799 );
5800 const BOTTOM_POSITION: f64 = TILE_HEIGHT - MIN_VERTICAL_PADDING - LEADING_HEIGHT;
5801 assert_eq!(
5802 case.offsets(Some(ListTileTitleAlignment::Bottom), false, false),
5803 (BOTTOM_POSITION, BOTTOM_POSITION)
5804 );
5805 });
5806 }
5807
5808 #[test]
5810 fn material2_title_alignment_position_with_title_and_subtitle_widgets() {
5811 test_widgets(|tester| {
5812 let mut case = AlignmentCase::new(tester, m2);
5813 const TILE_HEIGHT: f64 = MIN_VERTICAL_PADDING * 2.0 + TITLE_HEIGHT + 50.0;
5814 const CENTER_POSITION: f64 = (TILE_HEIGHT / 2.0) - (LEADING_HEIGHT / 2.0);
5815 const TITLE_POSITION: f64 = 16.0;
5816 assert_eq!(
5817 case.offsets(None, true, false),
5818 (TITLE_POSITION, TITLE_POSITION)
5819 );
5820 assert_eq!(
5821 case.offsets(Some(ListTileTitleAlignment::ThreeLine), true, false),
5822 (CENTER_POSITION, CENTER_POSITION)
5823 );
5824 assert_eq!(
5825 case.offsets(Some(ListTileTitleAlignment::TitleHeight), true, false),
5826 (TITLE_POSITION, TITLE_POSITION)
5827 );
5828 assert_eq!(
5829 case.offsets(Some(ListTileTitleAlignment::Top), true, false),
5830 (MIN_VERTICAL_PADDING, MIN_VERTICAL_PADDING)
5831 );
5832 assert_eq!(
5833 case.offsets(Some(ListTileTitleAlignment::Center), true, false),
5834 (CENTER_POSITION, CENTER_POSITION)
5835 );
5836 const BOTTOM_POSITION: f64 = TILE_HEIGHT - MIN_VERTICAL_PADDING - LEADING_HEIGHT;
5837 assert_eq!(
5838 case.offsets(Some(ListTileTitleAlignment::Bottom), true, false),
5839 (BOTTOM_POSITION, BOTTOM_POSITION)
5840 );
5841 });
5842 }
5843
5844 #[test]
5847 fn material2_list_tile_is_three_line_updates_the_three_line_alignment() {
5848 test_widgets(|tester| {
5849 let mut case = AlignmentCase::new(tester, m2);
5850 const TILE_HEIGHT: f64 = MIN_VERTICAL_PADDING * 2.0 + TITLE_HEIGHT + 50.0;
5851 const LEADING_TRAILING_POSITION: f64 = (TILE_HEIGHT / 2.0) - (LEADING_HEIGHT / 2.0);
5852 assert_eq!(
5855 case.offsets(Some(ListTileTitleAlignment::ThreeLine), true, false),
5856 (LEADING_TRAILING_POSITION, LEADING_TRAILING_POSITION)
5857 );
5858 assert_eq!(
5860 case.offsets(Some(ListTileTitleAlignment::ThreeLine), true, true),
5861 (MIN_VERTICAL_PADDING, MIN_VERTICAL_PADDING)
5862 );
5863 });
5864 }
5865
5866 #[test]
5868 fn list_tile_can_be_triggered_by_keyboard_shortcuts() {
5869 test_widgets(|tester| {
5870 let tapped = Rc::new(Cell::new(false));
5871 pump_app(tester, m3, |_app| {
5872 let tapped = Rc::clone(&tapped);
5873 Material::new()
5874 .child(Center::new().child(StatefulBuilder::new(
5875 move |_app, _context, set_state| {
5876 let tapped = Rc::clone(&tapped);
5877 SizedBox::new()
5878 .width(200.0)
5879 .height(100.0)
5880 .child(
5881 ListTile::new()
5882 .on_tap(Listener::new(move |app| {
5883 let tapped = Rc::clone(&tapped);
5884 set_state(app, &mut |_app| tapped.set(true));
5885 }))
5886 .hover_color(Colors::ORANGE.shade500())
5887 .autofocus(true),
5888 )
5889 .into_widget()
5890 },
5891 )))
5892 .into_widget()
5893 });
5894 tester.set_highlight_strategy(FocusHighlightStrategy::AlwaysTraditional);
5895 tester.pump_and_settle();
5896
5897 tester.send_key_event(
5898 waft_services::PhysicalKeyboardKey::SPACE,
5899 waft_services::LogicalKeyboardKey::SPACE,
5900 );
5901 tester.pump_and_settle();
5902
5903 assert!(tapped.get());
5904 });
5905 }
5906
5907 fn ink_well_enable_feedback(
5910 tester: &mut WidgetTester,
5911 tile: ListTile,
5912 theme_feedback: Option<bool>,
5913 ) -> bool {
5914 pump_app(tester, m3, |_app| {
5915 let tile = tile.title(Text::new("Title")).on_tap(noop());
5916 let child = match theme_feedback {
5917 Some(enable_feedback) => ListTileTheme::new(tile)
5918 .data(ListTileThemeData::new().enable_feedback(enable_feedback))
5919 .into_widget(),
5920 None => tile.into_widget(),
5921 };
5922 Material::new().child(child).into_widget()
5923 });
5924 let ink_well = tester.element(find::by_type::<InkWell>().at(0));
5925 tester.widget(ink_well, |ink_well: &InkWell| {
5926 ink_well.ink_response.enable_feedback
5927 })
5928 }
5929
5930 #[test]
5932 fn list_tile_with_disabled_feedback() {
5933 test_widgets(|tester| {
5934 assert!(!ink_well_enable_feedback(
5935 tester,
5936 ListTile::new().enable_feedback(false),
5937 None
5938 ));
5939 });
5940 }
5941
5942 #[test]
5944 fn list_tile_with_enabled_feedback() {
5945 test_widgets(|tester| {
5946 assert!(ink_well_enable_feedback(
5947 tester,
5948 ListTile::new().enable_feedback(true),
5949 None
5950 ));
5951 });
5952 }
5953
5954 #[test]
5956 fn list_tile_with_enabled_feedback_by_default() {
5957 test_widgets(|tester| {
5958 assert!(ink_well_enable_feedback(tester, ListTile::new(), None));
5959 });
5960 }
5961
5962 #[test]
5964 fn list_tile_with_disabled_feedback_using_list_tile_theme() {
5965 test_widgets(|tester| {
5966 assert!(!ink_well_enable_feedback(
5967 tester,
5968 ListTile::new(),
5969 Some(false)
5970 ));
5971 });
5972 }
5973
5974 #[test]
5976 fn list_tile_enable_feedback_overrides_list_tile_theme_enable_feedback() {
5977 test_widgets(|tester| {
5978 assert!(ink_well_enable_feedback(
5979 tester,
5980 ListTile::new().enable_feedback(true),
5981 Some(false)
5982 ));
5983 });
5984 }
5985
5986 #[test]
5989 fn a_list_tile_describes_its_children_by_slot() {
5990 use waft_foundation::{AppCell, DiagnosticsNode};
5991 use waft_rendering::{BoxConstraints, RenderConstrainedBox};
5992
5993 let cell = AppCell::new();
5994 let mut app = cell.borrow_mut();
5995 let tile = RenderListTile::new(
5996 &mut app,
5997 false,
5998 VisualDensity::STANDARD,
5999 false,
6000 TextDirection::Ltr,
6001 TextBaseline::Alphabetic,
6002 None,
6003 16.0,
6004 4.0,
6005 40.0,
6006 None,
6007 ListTileTitleAlignment::ThreeLine,
6008 );
6009 let child = |app: &mut App| {
6010 RenderConstrainedBox::new(app, BoxConstraints::tight(Size::new(10.0, 10.0)), None)
6011 .as_box()
6012 };
6013 let leading = child(&mut app);
6014 let title = child(&mut app);
6015 let subtitle = child(&mut app);
6016 let trailing = child(&mut app);
6017 SlottedContainerRenderObjectMixin::set_child(
6018 tile,
6019 &mut app,
6020 Some(leading),
6021 &ListTileSlot::Leading,
6022 );
6023 SlottedContainerRenderObjectMixin::set_child(
6024 tile,
6025 &mut app,
6026 Some(title),
6027 &ListTileSlot::Title,
6028 );
6029 SlottedContainerRenderObjectMixin::set_child(
6030 tile,
6031 &mut app,
6032 Some(subtitle),
6033 &ListTileSlot::Subtitle,
6034 );
6035 SlottedContainerRenderObjectMixin::set_child(
6036 tile,
6037 &mut app,
6038 Some(trailing),
6039 &ListTileSlot::Trailing,
6040 );
6041
6042 let node = tile.as_object().to_diagnostics_node(&app, None);
6043 let names: Vec<&str> = node
6044 .children()
6045 .iter()
6046 .filter_map(|child| child.name())
6047 .collect();
6048 assert_eq!(names, ["leading", "title", "subtitle", "trailing"]);
6049 }
6050 #[test]
6052 fn a_default_list_tile_describes_no_properties() {
6053 use waft_foundation::DiagnosticPropertiesBuilder;
6054
6055 let list_tile = ListTile::new();
6056 let cell = waft_foundation::AppCell::new();
6057 let app = cell.borrow();
6058 let description: Vec<String> = waft_widgets::testing::describe_properties(
6059 |properties: &mut DiagnosticPropertiesBuilder| {
6060 list_tile.debug_fill_properties(&app, properties)
6061 },
6062 );
6063 assert_eq!(description, Vec::<String>::new());
6064 }
6065
6066 #[test]
6068 fn a_list_tile_describes_its_properties() {
6069 use waft_embedder::Color;
6070 use waft_foundation::DiagnosticPropertiesBuilder;
6071 use waft_painting::{BorderRadiusGeometry, BorderSide, EdgeInsets, RoundedRectangleBorder};
6072 use waft_rendering::testing::ignoring_hash_codes;
6073
6074 let blue = Color::new(0xff0000ff);
6075 let green = Color::new(0xff00ff00);
6076 let red = Color::new(0xffff0000);
6077 let cyan = Color::new(0xff00ffff);
6078 let yellow = Color::new(0xffffff00);
6079 let selected_tile = Color::new(0xff123456);
6080 let list_tile = ListTile::new()
6081 .leading(Text::new("leading"))
6082 .title(Text::new("title"))
6083 .subtitle(Text::new("subtitle"))
6084 .trailing(Text::new("trailing"))
6085 .is_three_line(true)
6086 .dense(true)
6087 .visual_density(crate::theme_data::VisualDensity::STANDARD)
6088 .shape(RoundedRectangleBorder::new(
6089 BorderSide::NONE,
6090 BorderRadiusGeometry::ZERO,
6091 ))
6092 .style(ListTileStyle::List)
6093 .selected_color(blue)
6094 .icon_color(green)
6095 .text_color(red)
6096 .title_text_style(TextStyle::new().font_size(22.0))
6097 .subtitle_text_style(TextStyle::new().font_size(18.0))
6098 .leading_and_trailing_text_style(TextStyle::new().font_size(16.0))
6099 .content_padding(EdgeInsets::ZERO)
6100 .enabled(false)
6101 .selected(true)
6102 .focus_color(cyan)
6103 .hover_color(blue)
6104 .autofocus(true)
6105 .tile_color(yellow)
6106 .selected_tile_color(selected_tile)
6107 .enable_feedback(false)
6108 .horizontal_title_gap(4.0)
6109 .min_vertical_padding(2.0)
6110 .min_leading_width(6.0)
6111 .title_alignment(ListTileTitleAlignment::Bottom);
6112 let cell = waft_foundation::AppCell::new();
6113 let app = cell.borrow();
6114 let description: Vec<String> = waft_widgets::testing::describe_properties(
6115 |properties: &mut DiagnosticPropertiesBuilder| {
6116 list_tile.debug_fill_properties(&app, properties)
6117 },
6118 )
6119 .into_iter()
6120 .map(|line| ignoring_hash_codes(&line))
6121 .collect();
6122 assert_eq!(
6123 description,
6124 [
6125 "isThreeLine: THREE_LINE".to_string(),
6126 "dense: true".to_string(),
6127 "visualDensity: VisualDensity#00000(h: 0.0, v: 0.0)".to_string(),
6128 "shape: RoundedRectangleBorder(BorderSide(width: 0.0, style: none), BorderRadius.zero)".to_string(),
6129 "style: ListTileStyle.list".to_string(),
6130 format!("selectedColor: {blue}"),
6131 format!("iconColor: {green}"),
6132 format!("textColor: {red}"),
6133 "titleTextStyle: TextStyle(inherit: true, size: 22.0)".to_string(),
6134 "subtitleTextStyle: TextStyle(inherit: true, size: 18.0)".to_string(),
6135 "leadingAndTrailingTextStyle: TextStyle(inherit: true, size: 16.0)".to_string(),
6136 "contentPadding: EdgeInsets.zero".to_string(),
6137 "enabled: false".to_string(),
6138 "selected: true".to_string(),
6139 format!("focusColor: {cyan}"),
6140 format!("hoverColor: {blue}"),
6141 "autofocus: true".to_string(),
6142 format!("tileColor: {yellow}"),
6143 format!("selectedTileColor: {selected_tile}"),
6144 "enableFeedback: false".to_string(),
6145 "horizontalTitleGap: 4.0".to_string(),
6146 "minVerticalPadding: 2.0".to_string(),
6147 "minLeadingWidth: 6.0".to_string(),
6148 "titleAlignment: ListTileTitleAlignment.bottom".to_string(),
6149 ]
6150 );
6151 }
6152}