1#![allow(
3 deprecated,
4 reason = "Dart deprecates `ButtonBarThemeData` and `ButtonBarTheme`, which this file implements"
5)]
6
7use waft_embedder::lerp_double;
8use waft_foundation::{App, DiagnosticsProperty, DoubleProperty, FlagProperty};
9use waft_painting::EdgeInsetsGeometry;
10use waft_widgets::{
11 BuildContext, InheritedWidget, IntoWidget, KeyRef, MainAxisAlignment, MainAxisSize,
12 VerticalDirection, WidgetRef,
13};
14
15use crate::button_theme::{ButtonBarLayoutBehavior, ButtonTextTheme};
16use crate::theme::Theme;
17
18#[deprecated(note = "Use OverflowBar instead. This feature was deprecated after v3.21.0-10.0.pre.")]
28#[derive(Clone, Debug, Default, PartialEq)]
29pub struct ButtonBarThemeData {
30 pub alignment: Option<MainAxisAlignment>,
32 pub main_axis_size: Option<MainAxisSize>,
34 pub button_text_theme: Option<ButtonTextTheme>,
37 pub button_min_width: Option<f64>,
39 pub button_height: Option<f64>,
41 pub button_padding: Option<EdgeInsetsGeometry>,
43 pub button_aligned_dropdown: Option<bool>,
46 pub layout_behavior: Option<ButtonBarLayoutBehavior>,
49 pub overflow_direction: Option<VerticalDirection>,
51}
52
53impl ButtonBarThemeData {
54 pub fn new() -> ButtonBarThemeData {
57 ButtonBarThemeData::default()
58 }
59
60 pub fn with_alignment(mut self, alignment: MainAxisAlignment) -> ButtonBarThemeData {
62 self.alignment = Some(alignment);
63 self
64 }
65
66 pub fn with_main_axis_size(mut self, main_axis_size: MainAxisSize) -> ButtonBarThemeData {
68 self.main_axis_size = Some(main_axis_size);
69 self
70 }
71
72 pub fn with_button_text_theme(
74 mut self,
75 button_text_theme: ButtonTextTheme,
76 ) -> ButtonBarThemeData {
77 self.button_text_theme = Some(button_text_theme);
78 self
79 }
80
81 pub fn with_button_min_width(mut self, button_min_width: f64) -> ButtonBarThemeData {
83 debug_assert!(button_min_width >= 0.0);
84 self.button_min_width = Some(button_min_width);
85 self
86 }
87
88 pub fn with_button_height(mut self, button_height: f64) -> ButtonBarThemeData {
90 debug_assert!(button_height >= 0.0);
91 self.button_height = Some(button_height);
92 self
93 }
94
95 pub fn with_button_padding(
97 mut self,
98 button_padding: impl Into<EdgeInsetsGeometry>,
99 ) -> ButtonBarThemeData {
100 self.button_padding = Some(button_padding.into());
101 self
102 }
103
104 pub fn with_button_aligned_dropdown(
106 mut self,
107 button_aligned_dropdown: bool,
108 ) -> ButtonBarThemeData {
109 self.button_aligned_dropdown = Some(button_aligned_dropdown);
110 self
111 }
112
113 pub fn with_layout_behavior(
115 mut self,
116 layout_behavior: ButtonBarLayoutBehavior,
117 ) -> ButtonBarThemeData {
118 self.layout_behavior = Some(layout_behavior);
119 self
120 }
121
122 pub fn with_overflow_direction(
124 mut self,
125 overflow_direction: VerticalDirection,
126 ) -> ButtonBarThemeData {
127 self.overflow_direction = Some(overflow_direction);
128 self
129 }
130
131 pub fn copy_with(&self) -> ButtonBarThemeData {
133 self.clone()
134 }
135
136 pub fn lerp(
140 a: Option<&ButtonBarThemeData>,
141 b: Option<&ButtonBarThemeData>,
142 t: f64,
143 ) -> Option<ButtonBarThemeData> {
144 match (a, b) {
145 (None, None) => return None,
146 (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
147 _ => {}
148 }
149 let a = a.cloned().unwrap_or_default();
150 let b = b.cloned().unwrap_or_default();
151 let pick = |t: f64| t < 0.5;
152 Some(ButtonBarThemeData {
153 alignment: if pick(t) { a.alignment } else { b.alignment },
154 main_axis_size: if pick(t) {
155 a.main_axis_size
156 } else {
157 b.main_axis_size
158 },
159 button_text_theme: if pick(t) {
160 a.button_text_theme
161 } else {
162 b.button_text_theme
163 },
164 button_min_width: lerp_double(a.button_min_width, b.button_min_width, t),
165 button_height: lerp_double(a.button_height, b.button_height, t),
166 button_padding: EdgeInsetsGeometry::lerp(a.button_padding, b.button_padding, t),
167 button_aligned_dropdown: if pick(t) {
168 a.button_aligned_dropdown
169 } else {
170 b.button_aligned_dropdown
171 },
172 layout_behavior: if pick(t) {
173 a.layout_behavior
174 } else {
175 b.layout_behavior
176 },
177 overflow_direction: if pick(t) {
178 a.overflow_direction
179 } else {
180 b.overflow_direction
181 },
182 })
183 }
184}
185
186#[deprecated(note = "Use OverflowBar instead. This feature was deprecated after v3.21.0-10.0.pre.")]
191pub struct ButtonBarTheme {
192 pub key: Option<KeyRef>,
193 pub data: ButtonBarThemeData,
195 pub child: WidgetRef,
196}
197
198impl ButtonBarTheme {
199 #[cfg_attr(debug_assertions, track_caller)]
201 pub fn new<K>(data: ButtonBarThemeData, child: impl IntoWidget<K>) -> ButtonBarTheme {
202 ButtonBarTheme {
203 key: None,
204 data,
205 child: child.into_widget(),
206 }
207 }
208
209 pub fn key(mut self, key: KeyRef) -> ButtonBarTheme {
211 self.key = Some(key);
212 self
213 }
214
215 pub fn of(app: &mut App, context: BuildContext) -> ButtonBarThemeData {
218 if let Some(button_bar_theme) =
219 context.depend_on_inherited_widget_of_exact_type::<ButtonBarTheme>(app)
220 {
221 return button_bar_theme.data.clone();
222 }
223 Theme::of(app, context).button_bar_theme.clone()
224 }
225}
226
227impl std::fmt::Debug for ButtonBarTheme {
228 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
229 f.debug_struct("ButtonBarTheme")
230 .field("data", &self.data)
231 .finish_non_exhaustive()
232 }
233}
234
235impl InheritedWidget for ButtonBarTheme {
236 fn key(&self) -> Option<&KeyRef> {
237 self.key.as_ref()
238 }
239
240 fn child(&self) -> &WidgetRef {
241 &self.child
242 }
243
244 fn update_should_notify(&self, old_widget: &ButtonBarTheme) -> bool {
245 self.data != old_widget.data
246 }
247}
248
249impl waft_foundation::Diagnosticable for ButtonBarThemeData {
250 fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
251 properties.add(
252 DiagnosticsProperty::<waft_foundation::DartEnum<MainAxisAlignment>>::new(
253 "alignment",
254 self.alignment.map(waft_foundation::DartEnum),
255 )
256 .default_value(None),
257 );
258 properties.add(
259 DiagnosticsProperty::<waft_foundation::DartEnum<MainAxisSize>>::new(
260 "mainAxisSize",
261 self.main_axis_size.map(waft_foundation::DartEnum),
262 )
263 .default_value(None),
264 );
265 properties.add(
266 DiagnosticsProperty::<waft_foundation::DartEnum<ButtonTextTheme>>::new(
267 "textTheme",
268 self.button_text_theme.map(waft_foundation::DartEnum),
269 )
270 .default_value(None),
271 );
272 properties.add(DoubleProperty::new("minWidth", self.button_min_width).default_value(None));
273 properties.add(DoubleProperty::new("height", self.button_height).default_value(None));
274 properties.add(
275 DiagnosticsProperty::<EdgeInsetsGeometry>::new("padding", self.button_padding)
276 .default_value(None),
277 );
278 properties.add(
279 FlagProperty::new("buttonAlignedDropdown", self.button_aligned_dropdown)
280 .if_true("dropdown width matches button")
281 .default_value(None),
282 );
283 properties.add(
284 DiagnosticsProperty::<waft_foundation::DartEnum<ButtonBarLayoutBehavior>>::new(
285 "layoutBehavior",
286 self.layout_behavior.map(waft_foundation::DartEnum),
287 )
288 .default_value(None),
289 );
290 properties.add(
291 DiagnosticsProperty::<waft_foundation::DartEnum<VerticalDirection>>::new(
292 "overflowDirection",
293 self.overflow_direction.map(waft_foundation::DartEnum),
294 )
295 .default_value(None),
296 );
297 }
298}
299
300impl std::fmt::Display for ButtonBarThemeData {
301 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
302 f.write_str(&waft_foundation::Diagnosticable::to_string(self))
303 }
304}
305
306#[cfg(test)]
307mod tests {
308 use waft_widgets::testing::test_widgets;
309 use waft_widgets::{Builder, Center, MainAxisAlignment, MainAxisSize, Text, VerticalDirection};
310
311 use super::*;
312 use crate::test_support::material_app;
313 use crate::theme_data::ThemeData;
314
315 #[test]
316 fn button_bar_theme_data_lerp_special_cases() {
317 assert_eq!(ButtonBarThemeData::lerp(None, None, 0.0), None);
318 let data = ButtonBarThemeData::new();
319 assert_eq!(
320 ButtonBarThemeData::lerp(Some(&data), Some(&data), 0.5),
321 Some(data)
322 );
323 }
324
325 #[test]
326 fn every_field_is_null_by_default() {
327 let theme = ButtonBarThemeData::new();
328 assert_eq!(theme.alignment, None);
329 assert_eq!(theme.main_axis_size, None);
330 assert_eq!(theme.button_text_theme, None);
331 assert_eq!(theme.button_min_width, None);
332 assert_eq!(theme.button_height, None);
333 assert_eq!(theme.button_padding, None);
334 assert_eq!(theme.button_aligned_dropdown, None);
335 assert_eq!(theme.layout_behavior, None);
336 assert_eq!(theme.overflow_direction, None);
337 }
338
339 #[test]
340 fn theme_data_uses_the_default_button_bar_theme_data() {
341 let cell = waft_foundation::AppCell::new();
342 let theme = ThemeData::builder().build(&cell.borrow());
343 assert_eq!(theme.button_bar_theme, ButtonBarThemeData::default());
344 }
345
346 #[test]
347 fn copy_with_and_equality_basics() {
348 assert_eq!(
349 ButtonBarThemeData::new(),
350 ButtonBarThemeData::new().copy_with()
351 );
352 }
353
354 #[test]
355 fn the_fields_lerp_correctly() {
356 let primary = ButtonBarThemeData::new()
357 .with_alignment(MainAxisAlignment::End)
358 .with_main_axis_size(MainAxisSize::Min)
359 .with_button_text_theme(ButtonTextTheme::Primary)
360 .with_button_min_width(20.0)
361 .with_button_height(20.0)
362 .with_button_padding(waft_painting::EdgeInsets::symmetric(5.0, 0.0))
363 .with_button_aligned_dropdown(false)
364 .with_layout_behavior(ButtonBarLayoutBehavior::Padded)
365 .with_overflow_direction(VerticalDirection::Down);
366 let accent = ButtonBarThemeData::new()
367 .with_alignment(MainAxisAlignment::Center)
368 .with_main_axis_size(MainAxisSize::Max)
369 .with_button_text_theme(ButtonTextTheme::Accent)
370 .with_button_min_width(10.0)
371 .with_button_height(40.0)
372 .with_button_padding(waft_painting::EdgeInsets::symmetric(0.0, 10.0))
373 .with_button_aligned_dropdown(true)
374 .with_layout_behavior(ButtonBarLayoutBehavior::Constrained)
375 .with_overflow_direction(VerticalDirection::Up);
376
377 let lerp = ButtonBarThemeData::lerp(Some(&primary), Some(&accent), 0.5)
378 .expect("two themes lerp to a theme");
379 assert_eq!(lerp.alignment, Some(MainAxisAlignment::Center));
380 assert_eq!(lerp.main_axis_size, Some(MainAxisSize::Max));
381 assert_eq!(lerp.button_text_theme, Some(ButtonTextTheme::Accent));
382 assert_eq!(lerp.button_min_width, Some(15.0));
383 assert_eq!(lerp.button_height, Some(30.0));
384 assert_eq!(
385 lerp.button_padding,
386 Some(waft_painting::EdgeInsets::from_ltrb(5.0, 2.5, 5.0, 2.5).into())
387 );
388 assert_eq!(lerp.button_aligned_dropdown, Some(true));
389 assert_eq!(
390 lerp.layout_behavior,
391 Some(ButtonBarLayoutBehavior::Constrained)
392 );
393 assert_eq!(lerp.overflow_direction, Some(VerticalDirection::Up));
394 }
395
396 #[test]
397 fn of_falls_back_to_the_theme_data_field() {
398 test_widgets(|tester| {
399 let data = ButtonBarThemeData::new().with_button_min_width(42.0);
400 let theme = ThemeData::builder()
401 .button_bar_theme(data.clone())
402 .build(&tester.cell.borrow());
403 let seen = std::rc::Rc::new(std::cell::RefCell::new(None));
404 let captured = std::rc::Rc::clone(&seen);
405 tester.pump_widget(material_app(
406 theme,
407 Builder::new(move |app, context| {
408 *captured.borrow_mut() = Some(ButtonBarTheme::of(app, context));
409 Center::new().child(Text::new("x")).into_widget()
410 })
411 .into_widget(),
412 ));
413 assert_eq!(seen.borrow().clone(), Some(data));
414 assert_eq!(
415 seen.borrow()
416 .as_ref()
417 .and_then(|theme| theme.button_min_width),
418 Some(42.0)
419 );
420 });
421 }
422
423 #[test]
424 fn an_ancestor_button_bar_theme_overrides_the_theme_data_field() {
425 test_widgets(|tester| {
426 let default_bar_theme = ButtonBarThemeData::new().with_button_min_width(42.0);
427 let override_theme = ButtonBarThemeData::new().with_button_min_width(84.0);
428 let theme = ThemeData::builder()
429 .button_bar_theme(default_bar_theme)
430 .build(&tester.cell.borrow());
431 let seen = std::rc::Rc::new(std::cell::RefCell::new(None));
432 let captured = std::rc::Rc::clone(&seen);
433 let override_clone = override_theme.clone();
434 tester.pump_widget(material_app(
435 theme,
436 ButtonBarTheme::new(
437 override_clone,
438 Builder::new(move |app, context| {
439 *captured.borrow_mut() = Some(ButtonBarTheme::of(app, context));
440 Center::new().child(Text::new("x")).into_widget()
441 }),
442 )
443 .into_widget(),
444 ));
445 assert_eq!(seen.borrow().clone(), Some(override_theme));
446 assert_eq!(
447 seen.borrow()
448 .as_ref()
449 .and_then(|theme| theme.button_min_width),
450 Some(84.0)
451 );
452 });
453 }
454 #[test]
456 fn a_button_bar_theme_data_describes_nothing_by_default() {
457 assert_eq!(
458 waft_widgets::testing::describe_properties(|properties| {
459 waft_foundation::Diagnosticable::debug_fill_properties(
460 &ButtonBarThemeData::new(),
461 properties,
462 )
463 }),
464 Vec::<String>::new()
465 );
466 }
467
468 #[test]
470 fn an_assigned_button_bar_theme_describes_every_property() {
471 let theme = ButtonBarThemeData::new()
472 .with_alignment(MainAxisAlignment::Center)
473 .with_main_axis_size(MainAxisSize::Max)
474 .with_button_text_theme(ButtonTextTheme::Accent)
475 .with_button_min_width(10.0)
476 .with_button_height(42.0)
477 .with_button_padding(EdgeInsetsGeometry::symmetric(0.0, 7.3))
478 .with_button_aligned_dropdown(true)
479 .with_layout_behavior(ButtonBarLayoutBehavior::Constrained)
480 .with_overflow_direction(VerticalDirection::Up);
481 let description = waft_widgets::testing::describe_properties(|properties| {
482 waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
483 });
484 assert_eq!(
485 description,
486 [
487 "alignment: MainAxisAlignment.center".to_string(),
488 "mainAxisSize: MainAxisSize.max".to_string(),
489 "textTheme: ButtonTextTheme.accent".to_string(),
490 "minWidth: 10.0".to_string(),
491 "height: 42.0".to_string(),
492 "padding: EdgeInsets(7.3, 0.0, 7.3, 0.0)".to_string(),
493 "dropdown width matches button".to_string(),
494 "layoutBehavior: ButtonBarLayoutBehavior.constrained".to_string(),
495 "overflowDirection: VerticalDirection.up".to_string(),
496 ]
497 );
498 }
499}