1use std::rc::Rc;
4
5use waft_embedder::Color;
6use waft_foundation::App;
7use waft_painting::{BoxDecoration, EdgeInsetsDirectional, TextOverflow};
8use waft_widgets::{
9 BuildContext, Column, Container, CrossAxisAlignment, DefaultTextStyle, Expanded, IconTheme,
10 IconThemeData, IntoWidget, KeyRef, MainAxisAlignment, Padding, Row, StatelessWidget, WidgetRef,
11};
12
13use crate::colors::Colors;
14use crate::theme::Theme;
15use crate::theme_data::ThemeData;
16
17#[derive(Debug)]
31pub struct GridTileBar {
32 pub key: Option<KeyRef>,
33 pub background_color: Option<Color>,
37 pub leading: Option<WidgetRef>,
41 pub title: Option<WidgetRef>,
45 pub subtitle: Option<WidgetRef>,
49 pub trailing: Option<WidgetRef>,
53}
54
55impl GridTileBar {
56 pub fn new() -> GridTileBar {
58 GridTileBar {
59 key: None,
60 background_color: None,
61 leading: None,
62 title: None,
63 subtitle: None,
64 trailing: None,
65 }
66 }
67
68 pub fn key(mut self, key: KeyRef) -> GridTileBar {
70 self.key = Some(key);
71 self
72 }
73
74 pub fn background_color(mut self, background_color: Color) -> GridTileBar {
76 self.background_color = Some(background_color);
77 self
78 }
79
80 #[cfg_attr(debug_assertions, track_caller)]
82 pub fn leading<K>(mut self, leading: impl IntoWidget<K>) -> GridTileBar {
83 self.leading = Some(leading.into_widget());
84 self
85 }
86
87 #[cfg_attr(debug_assertions, track_caller)]
89 pub fn title<K>(mut self, title: impl IntoWidget<K>) -> GridTileBar {
90 self.title = Some(title.into_widget());
91 self
92 }
93
94 #[cfg_attr(debug_assertions, track_caller)]
96 pub fn subtitle<K>(mut self, subtitle: impl IntoWidget<K>) -> GridTileBar {
97 self.subtitle = Some(subtitle.into_widget());
98 self
99 }
100
101 #[cfg_attr(debug_assertions, track_caller)]
103 pub fn trailing<K>(mut self, trailing: impl IntoWidget<K>) -> GridTileBar {
104 self.trailing = Some(trailing.into_widget());
105 self
106 }
107}
108
109impl Default for GridTileBar {
110 fn default() -> GridTileBar {
111 GridTileBar::new()
112 }
113}
114
115impl StatelessWidget for GridTileBar {
116 fn key(&self) -> Option<&KeyRef> {
117 self.key.as_ref()
118 }
119
120 fn build(&self, app: &mut App, _context: BuildContext) -> WidgetRef {
121 let dark_theme = Rc::new(ThemeData::dark(app, None));
122 let padding = EdgeInsetsDirectional::only(
123 if self.leading.is_some() { 8.0 } else { 16.0 },
124 0.0,
125 if self.trailing.is_some() { 8.0 } else { 16.0 },
126 0.0,
127 );
128 let height = if self.title.is_some() && self.subtitle.is_some() {
129 68.0
130 } else {
131 48.0
132 };
133
134 let mut children: Vec<WidgetRef> = Vec::new();
135 if let Some(leading) = &self.leading {
136 children.push(
137 Padding::new(EdgeInsetsDirectional::only(0.0, 0.0, 8.0, 0.0))
138 .child(leading.clone())
139 .into_widget(),
140 );
141 }
142
143 if self.title.is_some() && self.subtitle.is_some() {
144 let title = DefaultTextStyle::new(
145 dark_theme
146 .text_theme
147 .title_medium
148 .clone()
149 .expect("a dark theme has a title medium"),
150 self.title.clone().expect("a title"),
151 )
152 .soft_wrap(false)
153 .overflow(TextOverflow::Ellipsis);
154 let subtitle = DefaultTextStyle::new(
155 dark_theme
156 .text_theme
157 .body_small
158 .clone()
159 .expect("a dark theme has a body small"),
160 self.subtitle.clone().expect("a subtitle"),
161 )
162 .soft_wrap(false)
163 .overflow(TextOverflow::Ellipsis);
164 children.push(
165 Expanded::new(
166 Column::new()
167 .main_axis_alignment(MainAxisAlignment::Center)
168 .cross_axis_alignment(CrossAxisAlignment::Start)
169 .children(vec![title.into_widget(), subtitle.into_widget()]),
170 )
171 .into_widget(),
172 );
173 } else if self.title.is_some() || self.subtitle.is_some() {
174 let text = self
175 .title
176 .clone()
177 .or_else(|| self.subtitle.clone())
178 .expect("a title or a subtitle");
179 children.push(
180 Expanded::new(
181 DefaultTextStyle::new(
182 dark_theme
183 .text_theme
184 .title_medium
185 .clone()
186 .expect("a dark theme has a title medium"),
187 text,
188 )
189 .soft_wrap(false)
190 .overflow(TextOverflow::Ellipsis),
191 )
192 .into_widget(),
193 );
194 }
195
196 if let Some(trailing) = &self.trailing {
197 children.push(
198 Padding::new(EdgeInsetsDirectional::only(8.0, 0.0, 0.0, 0.0))
199 .child(trailing.clone())
200 .into_widget(),
201 );
202 }
203
204 let mut container = Container::new().padding(padding).height(height);
205 if let Some(background_color) = self.background_color {
206 container = container.decoration(BoxDecoration::new().color(background_color));
207 }
208 container
209 .child(Theme::new(
210 Rc::clone(&dark_theme),
211 IconTheme::merge(
212 None,
213 IconThemeData::new().color(Colors::WHITE),
214 Row::new().children(children),
215 ),
216 ))
217 .into_widget()
218 }
219}
220
221#[cfg(test)]
222mod tests {
223 use waft_widgets::testing::{find, test_widgets};
224 use waft_widgets::{Center, SizedBox, Text};
225
226 use super::*;
227 use crate::test_support::material_app;
228 use crate::theme_data::ThemeData;
229
230 fn pump(tester: &mut waft_widgets::testing::WidgetTester, bar: GridTileBar) {
231 let theme = ThemeData::builder().build(&tester.cell.borrow());
232 tester.pump_widget(material_app(theme, Center::new().child(bar).into_widget()));
233 }
234
235 fn height(tester: &waft_widgets::testing::WidgetTester) -> f64 {
236 tester.get_size(find::by_type::<GridTileBar>()).height()
237 }
238
239 #[test]
240 fn grid_tile_bar_does_not_crash_at_zero_area() {
241 test_widgets(|tester| {
242 let theme = ThemeData::builder().build(&tester.cell.borrow());
243 tester.pump_widget(material_app(
244 theme,
245 Center::new()
246 .child(SizedBox::shrink().child(GridTileBar::new().title(Text::new("X"))))
247 .into_widget(),
248 ));
249 assert_eq!(
250 tester.get_size(find::by_type::<GridTileBar>()),
251 waft_embedder::Size::ZERO
252 );
253 });
254 }
255
256 #[test]
257 fn a_bar_with_a_title_and_a_subtitle_is_sixty_eight_tall() {
258 test_widgets(|tester| {
259 pump(
260 tester,
261 GridTileBar::new()
262 .title(Text::new("Title"))
263 .subtitle(Text::new("Subtitle")),
264 );
265 assert_eq!(height(tester), 68.0);
266 });
267 }
268
269 #[test]
270 fn a_bar_with_one_line_is_forty_eight_tall() {
271 test_widgets(|tester| {
272 pump(tester, GridTileBar::new().title(Text::new("Title")));
273 assert_eq!(height(tester), 48.0);
274 });
275 }
276
277 #[test]
278 fn a_leading_and_a_trailing_pad_the_bar_by_eight() {
279 test_widgets(|tester| {
280 pump(
281 tester,
282 GridTileBar::new()
283 .leading(SizedBox::square(Some(10.0)))
284 .trailing(SizedBox::square(Some(10.0))),
285 );
286 let bar = tester.get_top_left(find::by_type::<GridTileBar>());
287 let leading = tester.get_top_left(find::by_type::<SizedBox>().first());
288 assert_eq!(leading.dx(), bar.dx() + 8.0);
289 });
290 }
291
292 #[test]
293 fn a_title_without_a_leading_starts_sixteen_in() {
294 test_widgets(|tester| {
295 pump(tester, GridTileBar::new().title(Text::new("Title")));
296 let bar = tester.get_top_left(find::by_type::<GridTileBar>());
297 let title = tester.get_top_left(find::by_type::<Text>());
298 assert_eq!(title.dx(), bar.dx() + 16.0);
299 });
300 }
301}