1use waft_foundation::{App, DiagnosticsProperty};
4use waft_widgets::{
5 AnyElement, BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
6 downcast_widget,
7};
8
9use crate::button_style::ButtonStyle;
10use crate::theme::Theme;
11use crate::theme_data::StyleVariant;
12
13#[derive(Clone, Debug, Default)]
34pub struct IconButtonThemeData {
35 pub style: Option<ButtonStyle>,
43
44 pub variant: Option<StyleVariant>,
67}
68
69impl IconButtonThemeData {
70 pub fn new() -> IconButtonThemeData {
74 IconButtonThemeData::default()
75 }
76
77 pub fn style(mut self, style: ButtonStyle) -> IconButtonThemeData {
79 self.style = Some(style);
80 self
81 }
82
83 pub fn variant(mut self, variant: StyleVariant) -> IconButtonThemeData {
85 self.variant = Some(variant);
86 self
87 }
88
89 pub fn lerp(
91 a: Option<&IconButtonThemeData>,
92 b: Option<&IconButtonThemeData>,
93 t: f64,
94 ) -> Option<IconButtonThemeData> {
95 match (a, b) {
96 (None, None) => return None,
97 (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
98 _ => {}
99 }
100 Some(IconButtonThemeData {
101 style: ButtonStyle::lerp(
102 a.and_then(|a| a.style.as_ref()),
103 b.and_then(|b| b.style.as_ref()),
104 t,
105 ),
106 variant: if t < 0.5 {
107 a.and_then(|a| a.variant)
108 } else {
109 b.and_then(|b| b.variant)
110 },
111 })
112 }
113}
114
115impl PartialEq for IconButtonThemeData {
116 fn eq(&self, other: &IconButtonThemeData) -> bool {
117 if std::ptr::eq(self, other) {
118 return true;
119 }
120 other.style == self.style && other.variant == self.variant
121 }
122}
123
124#[derive(Debug)]
134pub struct IconButtonTheme {
135 pub key: Option<KeyRef>,
136 pub data: IconButtonThemeData,
138 pub child: WidgetRef,
139}
140
141impl IconButtonTheme {
142 #[cfg_attr(debug_assertions, track_caller)]
144 pub fn new<K>(data: IconButtonThemeData, child: impl IntoWidget<K>) -> IconButtonTheme {
145 IconButtonTheme {
146 key: None,
147 data,
148 child: child.into_widget(),
149 }
150 }
151
152 pub fn key(mut self, key: KeyRef) -> IconButtonTheme {
154 self.key = Some(key);
155 self
156 }
157
158 pub fn of(app: &mut App, context: BuildContext) -> IconButtonThemeData {
174 match context.depend_on_inherited_widget_of_exact_type::<IconButtonTheme>(app) {
175 Some(button_theme) => button_theme.data.clone(),
176 None => Theme::of(app, context).icon_button_theme.clone(),
177 }
178 }
179
180 pub fn variant_of(app: &mut App, context: BuildContext) -> StyleVariant {
195 let mut element =
196 context.get_element_for_inherited_widget_of_exact_type::<IconButtonTheme>(app);
197 while let Some(found) = element {
198 context.depend_on_inherited_element(app, found, None);
201 let variant = downcast_widget::<IconButtonTheme>(&**found.widget(app))
202 .expect("found as an IconButtonTheme")
203 .data
204 .variant;
205 if let Some(variant) = variant {
206 return variant;
207 }
208 let mut parent: Option<AnyElement> = None;
211 found.visit_ancestor_elements(app, &mut |ancestor| {
212 parent = Some(ancestor);
213 false
214 });
215 element = parent.and_then(|parent| {
216 parent.get_element_for_inherited_widget_of_exact_type::<IconButtonTheme>(app)
217 });
218 }
219 Theme::of(app, context)
220 .icon_button_theme
221 .variant
222 .unwrap_or(StyleVariant::Material3)
223 }
224}
225
226impl InheritedTheme for IconButtonTheme {
227 fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
228 IconButtonTheme::new(self.data.clone(), child).into_widget()
229 }
230}
231
232impl InheritedWidget for IconButtonTheme {
233 fn key(&self) -> Option<&KeyRef> {
234 self.key.as_ref()
235 }
236
237 fn child(&self) -> &WidgetRef {
238 &self.child
239 }
240
241 fn update_should_notify(&self, old_widget: &IconButtonTheme) -> bool {
242 self.data != old_widget.data
243 }
244
245 fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
246 Some(self)
247 }
248}
249
250impl waft_foundation::Diagnosticable for IconButtonThemeData {
251 fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
252 properties.add(
253 DiagnosticsProperty::<crate::button_style::ButtonStyle>::new(
254 "style",
255 self.style.clone(),
256 )
257 .default_value(None),
258 );
259 }
260}
261
262impl std::fmt::Display for IconButtonThemeData {
263 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
264 f.write_str(&waft_foundation::Diagnosticable::to_string(self))
265 }
266}
267
268#[cfg(test)]
269#[allow(deprecated)]
270mod tests {
271 use std::rc::Rc;
272 use std::time::Duration;
273
274 use waft_embedder::{Color, Offset, Radius, Size};
275 use waft_foundation::{App, Listener, ValueNotifier};
276 use waft_painting::{
277 Alignment, AlignmentGeometry, BorderRadius, BorderSide, BorderStyle, EdgeInsets,
278 OutlinedBorderRef, RoundedRectangleBorder, ShapeBorder, StadiumBorder,
279 };
280 use waft_services::{
281 LogicalKeyboardKey, MouseCursorRef, PhysicalKeyboardKey, SystemMouseCursors,
282 };
283 use waft_widgets::{
284 Align, AnyElement, Builder, Center, Icon, KeyRef, ValueListenableBuilder, WidgetState,
285 WidgetStateProperty, WidgetStatePropertyAll, WidgetStates,
286 };
287
288 use waft_widgets::testing::{TestPointer, WidgetTester, find, test_widgets};
289
290 use super::*;
291 use crate::button_style::{ButtonShapeVariant, ButtonSizeVariant, IconButtonWidthVariant};
292 use crate::color_scheme::ColorScheme;
293 use crate::colors::Colors;
294 use crate::icon_button::IconButton;
295 use crate::icons::Icons;
296 use crate::ink_well::InkWell;
297 use crate::material::{Material, MaterialType};
298 use crate::test_support::{PaintInspection, material_app};
299 use crate::theme_data::{MaterialTapTargetSize, ThemeData};
300 use waft_rendering::testing::RecordedCall;
301
302 fn noop() -> Option<Listener> {
303 Some(Listener::new(|_app| {}))
304 }
305
306 fn m3_theme(tester: &WidgetTester) -> ThemeData {
307 ThemeData::from(ColorScheme::light()).build(&tester.cell.borrow())
308 }
309
310 fn icon_button_element(tester: &WidgetTester) -> AnyElement {
311 find::by_type::<IconButton>().evaluate(tester)[0]
312 }
313
314 fn below_button<W: 'static, R>(tester: &WidgetTester, read: impl FnOnce(&W) -> R) -> R {
317 let button = icon_button_element(tester);
318 let found = find::by_type::<W>()
319 .evaluate(tester)
320 .into_iter()
321 .find(|element| tester.ancestors(*element).contains(&button))
322 .expect("a widget of that type below the IconButton");
323 tester.widget(found, read)
324 }
325
326 fn button_size(tester: &WidgetTester) -> Size {
327 tester.get_size(icon_button_element(tester))
328 }
329
330 fn icon_alignment(tester: &WidgetTester) -> AlignmentGeometry {
331 let align = tester
332 .element(find::ancestor(find::by_type::<Icon>(), find::by_type::<Align>()).first());
333 tester.widget(align, |align: &Align| align.alignment)
334 }
335
336 fn states(states: &[WidgetState]) -> WidgetStates {
337 states.iter().copied().collect()
338 }
339
340 #[test]
343 fn lerp_of_nothing_is_nothing_and_of_one_theme_is_that_theme() {
344 assert!(IconButtonThemeData::lerp(None, None, 0.0).is_none());
345 let data = IconButtonThemeData::new();
346 assert_eq!(
347 IconButtonThemeData::lerp(Some(&data), Some(&data), 0.5),
348 Some(data)
349 );
350 }
351
352 #[test]
354 fn the_theme_data_keeps_the_expressive_variant() {
355 let data = IconButtonThemeData::new().variant(StyleVariant::Material3Expressive);
356 assert_eq!(data.variant, Some(StyleVariant::Material3Expressive));
357 }
358
359 #[test]
361 fn the_variant_defaults_to_none() {
362 assert!(IconButtonThemeData::new().variant.is_none());
363 }
364
365 #[test]
367 fn theme_data_with_the_same_variants_are_equal() {
368 let style = |size, width, shape| {
369 IconButtonThemeData::new().style(
370 ButtonStyle::new()
371 .size_variant(size)
372 .icon_button_width(width)
373 .shape_variant(shape),
374 )
375 };
376 let a = style(
377 ButtonSizeVariant::Small,
378 IconButtonWidthVariant::Standard,
379 ButtonShapeVariant::Round,
380 );
381 let b = style(
382 ButtonSizeVariant::Small,
383 IconButtonWidthVariant::Standard,
384 ButtonShapeVariant::Round,
385 );
386 let c = style(
387 ButtonSizeVariant::Large,
388 IconButtonWidthVariant::Wide,
389 ButtonShapeVariant::Square,
390 );
391 assert_eq!(a, b);
392 assert_ne!(a, c);
393 }
394
395 #[test]
397 fn lerp_switches_the_variants_halfway() {
398 let a = IconButtonThemeData::new().style(
399 ButtonStyle::new()
400 .size_variant(ButtonSizeVariant::Small)
401 .icon_button_width(IconButtonWidthVariant::Narrow)
402 .shape_variant(ButtonShapeVariant::Round),
403 );
404 let b = IconButtonThemeData::new().style(
405 ButtonStyle::new()
406 .size_variant(ButtonSizeVariant::Large)
407 .icon_button_width(IconButtonWidthVariant::Wide)
408 .shape_variant(ButtonShapeVariant::Square),
409 );
410 let at = |t: f64| {
411 IconButtonThemeData::lerp(Some(&a), Some(&b), t)
412 .and_then(|data| data.style)
413 .expect("a style")
414 };
415 assert_eq!(at(0.0).size_variant, Some(ButtonSizeVariant::Small));
416 assert_eq!(at(0.4).size_variant, Some(ButtonSizeVariant::Small));
417 assert_eq!(at(0.5).size_variant, Some(ButtonSizeVariant::Large));
418 assert_eq!(at(1.0).size_variant, Some(ButtonSizeVariant::Large));
419 assert_eq!(
420 at(0.4).icon_button_width,
421 Some(IconButtonWidthVariant::Narrow)
422 );
423 assert_eq!(
424 at(0.5).icon_button_width,
425 Some(IconButtonWidthVariant::Wide)
426 );
427 assert_eq!(at(0.4).shape_variant, Some(ButtonShapeVariant::Round));
428 assert_eq!(at(0.5).shape_variant, Some(ButtonShapeVariant::Square));
429 }
430
431 #[test]
434 fn a_disabled_button_builds_under_an_expressive_theme() {
435 test_widgets(|tester| {
436 let theme = ThemeData::builder()
437 .use_material3(true)
438 .build(&tester.cell.borrow());
439 let widget = material_app(
440 theme,
441 IconButtonTheme::new(
442 IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
443 IconButton::new(None, Icon::new(Some(Icons::ac_unit()))),
444 )
445 .into_widget(),
446 );
447 tester.pump_widget(widget);
448 });
449 }
450
451 #[test]
453 fn without_a_theme_the_button_has_its_defaults() {
454 test_widgets(|tester| {
455 let widget = material_app(
456 m3_theme(tester),
457 Center::new()
458 .child(IconButton::new(noop(), Icon::new(Some(Icons::ac_unit()))))
459 .into_widget(),
460 );
461 tester.pump_widget(widget);
462
463 below_button(tester, |material: &Material| {
464 assert_eq!(material.animation_duration, Duration::from_millis(200));
465 assert!(material.border_radius.is_none());
466 assert_eq!(material.color, Some(Colors::TRANSPARENT));
467 assert_eq!(material.elevation, 0.0);
468 assert_eq!(material.shadow_color, Some(Colors::TRANSPARENT));
469 assert!(
470 material.shape.as_deref()
471 == Some(&StadiumBorder::default() as &dyn ShapeBorder)
472 );
473 assert!(material.text_style.is_none());
474 assert_eq!(material.r#type, MaterialType::Button);
475 });
476 assert!(icon_alignment(tester) == AlignmentGeometry::CENTER);
477 });
478 }
479
480 const FOREGROUND_COLOR: Color = Color::new(0xff000001);
481 const DISABLED_FOREGROUND_COLOR: Color = Color::new(0xff000002);
482 const BACKGROUND_COLOR: Color = Color::new(0xff000003);
483 const SHADOW_COLOR: Color = Color::new(0xff000004);
484 const ELEVATION: f64 = 3.0;
485 const MINIMUM_SIZE: Size = Size::new(200.0, 200.0);
486 const ANIMATION_DURATION: Duration = Duration::from_millis(25);
487 const ENABLE_FEEDBACK: bool = false;
488
489 fn side() -> BorderSide {
490 BorderSide::new(
491 Colors::GREEN.primary(),
492 2.0,
493 BorderStyle::Solid,
494 BorderSide::STROKE_ALIGN_INSIDE,
495 )
496 }
497
498 fn shape() -> RoundedRectangleBorder {
499 RoundedRectangleBorder::new(side(), BorderRadius::all(Radius::circular(2.0)).into())
500 }
501
502 fn override_style() -> ButtonStyle {
504 IconButton::style_from()
505 .foreground_color(FOREGROUND_COLOR)
506 .disabled_foreground_color(DISABLED_FOREGROUND_COLOR)
507 .background_color(BACKGROUND_COLOR)
508 .shadow_color(SHADOW_COLOR)
509 .elevation(ELEVATION)
510 .padding(EdgeInsets::all(3.0).into())
511 .minimum_size(MINIMUM_SIZE)
512 .side(side())
513 .shape(OutlinedBorderRef::new(shape()))
514 .enabled_mouse_cursor(SystemMouseCursors::TEXT.into())
515 .disabled_mouse_cursor(SystemMouseCursors::GRAB.into())
516 .tap_target_size(MaterialTapTargetSize::ShrinkWrap)
517 .animation_duration(ANIMATION_DURATION)
518 .enable_feedback(ENABLE_FEEDBACK)
519 .alignment(Alignment::CENTER_LEFT.into())
520 .build()
521 }
522
523 fn override_frame(
525 tester: &WidgetTester,
526 button_style: Option<ButtonStyle>,
527 theme_style: Option<ButtonStyle>,
528 overall_style: Option<ButtonStyle>,
529 ) -> WidgetRef {
530 let child = Builder::new(move |_app, _context| {
531 let button = IconButton::new(noop(), Icon::new(Some(Icons::ac_unit())));
532 match &button_style {
533 Some(style) => button.style(style.clone()),
534 None => button,
535 }
536 .into_widget()
537 })
538 .into_widget();
539 let mut overall = IconButtonThemeData::new();
540 overall.style = overall_style;
541 let theme = m3_theme(tester)
542 .copy_with()
543 .icon_button_theme(overall)
544 .build();
545 let body = match theme_style {
548 None => child,
549 Some(theme_style) => {
550 IconButtonTheme::new(IconButtonThemeData::new().style(theme_style), child)
551 .into_widget()
552 }
553 };
554 material_app(theme, Center::new().child(body).into_widget())
555 }
556
557 fn check_button(tester: &WidgetTester) {
559 below_button(tester, |material: &Material| {
560 assert!(material.text_style.is_none());
561 assert_eq!(material.color, Some(BACKGROUND_COLOR));
562 assert_eq!(material.shadow_color, Some(SHADOW_COLOR));
563 assert_eq!(material.elevation, ELEVATION);
564 assert!(material.border_radius.is_none());
565 assert!(material.shape.as_deref() == Some(&shape() as &dyn ShapeBorder));
566 assert_eq!(material.animation_duration, ANIMATION_DURATION);
567 });
568 below_button(tester, |ink_well: &InkWell| {
569 let response = &ink_well.ink_response;
570 let cursor = response.mouse_cursor.clone().expect("a mouse cursor");
571 let text: MouseCursorRef = SystemMouseCursors::TEXT.into();
572 let grab: MouseCursorRef = SystemMouseCursors::GRAB.into();
573 assert!(
574 *<dyn WidgetStateProperty<MouseCursorRef>>::resolve_as(&cursor, &states(&[]))
575 == *text
576 );
577 assert!(
578 *<dyn WidgetStateProperty<MouseCursorRef>>::resolve_as(
579 &cursor,
580 &states(&[WidgetState::Disabled])
581 ) == *grab
582 );
583 let overlay = response.overlay_color.as_ref().expect("an overlay color");
584 assert_eq!(
585 overlay.resolve(&states(&[WidgetState::Hovered])),
586 Some(FOREGROUND_COLOR.with_opacity(0.08))
587 );
588 assert_eq!(
589 overlay.resolve(&states(&[WidgetState::Focused])),
590 Some(FOREGROUND_COLOR.with_opacity(0.1))
591 );
592 assert_eq!(
593 overlay.resolve(&states(&[WidgetState::Pressed])),
594 Some(FOREGROUND_COLOR.with_opacity(0.1))
595 );
596 assert_eq!(response.enable_feedback, ENABLE_FEEDBACK);
597 });
598 assert_eq!(button_size(tester), Size::new(200.0, 200.0));
599 assert!(icon_alignment(tester) == Alignment::CENTER_LEFT.into());
600 }
601
602 fn check_override(
603 tester: &mut WidgetTester,
604 button_style: Option<ButtonStyle>,
605 theme_style: Option<ButtonStyle>,
606 overall_style: Option<ButtonStyle>,
607 ) {
608 let widget = override_frame(tester, button_style, theme_style, overall_style);
609 tester.pump_widget(widget);
610 tester.pump_and_settle(); check_button(tester);
612 }
613
614 #[test]
616 fn the_button_style_overrides_the_defaults() {
617 test_widgets(|tester| check_override(tester, Some(override_style()), None, None));
618 }
619
620 #[test]
622 fn the_button_theme_style_overrides_the_defaults() {
623 test_widgets(|tester| check_override(tester, None, Some(override_style()), None));
624 }
625
626 #[test]
628 fn the_overall_theme_style_overrides_the_defaults() {
629 test_widgets(|tester| check_override(tester, None, None, Some(override_style())));
630 }
631
632 #[test]
634 fn the_button_style_overrides_empty_theme_and_overall_styles() {
635 test_widgets(|tester| {
636 check_override(
637 tester,
638 Some(override_style()),
639 Some(ButtonStyle::new()),
640 Some(ButtonStyle::new()),
641 )
642 });
643 }
644
645 #[test]
647 fn the_button_theme_style_overrides_empty_button_and_overall_styles() {
648 test_widgets(|tester| {
649 check_override(
650 tester,
651 Some(ButtonStyle::new()),
652 Some(override_style()),
653 Some(ButtonStyle::new()),
654 )
655 });
656 }
657
658 #[test]
661 fn the_overall_theme_style_overrides_an_empty_button_style() {
662 test_widgets(|tester| {
663 check_override(
664 tester,
665 Some(ButtonStyle::new()),
666 None,
667 Some(override_style()),
668 )
669 });
670 }
671
672 #[test]
674 fn the_shadow_color_comes_from_the_button_then_its_theme_not_the_overall_theme() {
675 test_widgets(|tester| {
676 let shadow_color = Color::new(0xff000001);
677 let overridden_color = Color::new(0xff000002);
678 let build = |tester: &mut WidgetTester,
679 overall_shadow_color: Option<Color>,
680 theme_shadow_color: Option<Color>,
681 button_shadow_color: Option<Color>| {
682 let mut theme = m3_theme(tester).copy_with();
683 if let Some(color) = overall_shadow_color {
684 theme = theme.shadow_color(color);
685 }
686 let mut theme_style = IconButton::style_from();
687 if let Some(color) = theme_shadow_color {
688 theme_style = theme_style.shadow_color(color);
689 }
690 let theme_style = theme_style.build();
691 let widget = material_app(
692 theme.build(),
693 Center::new()
694 .child(IconButtonTheme::new(
695 IconButtonThemeData::new().style(theme_style),
696 Builder::new(move |_app, _context| {
697 let mut style = IconButton::style_from();
698 if let Some(color) = button_shadow_color {
699 style = style.shadow_color(color);
700 }
701 IconButton::new(noop(), Icon::new(Some(Icons::add())))
702 .style(style.build())
703 .into_widget()
704 }),
705 ))
706 .into_widget(),
707 );
708 tester.pump_widget(widget);
709 tester.pump_and_settle(); below_button(tester, |material: &Material| material.shadow_color)
711 };
712
713 assert_eq!(build(tester, None, None, None), Some(Colors::TRANSPARENT)); assert_eq!(
715 build(tester, Some(shadow_color), None, None),
716 Some(Colors::TRANSPARENT)
717 );
718 assert_eq!(
719 build(tester, None, Some(shadow_color), None),
720 Some(shadow_color)
721 );
722 assert_eq!(
723 build(tester, None, None, Some(shadow_color)),
724 Some(shadow_color)
725 );
726 assert_eq!(
727 build(tester, Some(overridden_color), Some(shadow_color), None),
728 Some(shadow_color)
729 );
730 assert_eq!(
731 build(tester, None, Some(overridden_color), Some(shadow_color)),
732 Some(shadow_color)
733 );
734 });
735 }
736
737 #[test]
739 fn the_theme_style_from_overlay_color_overrides_the_default_overlay() {
740 test_widgets(|tester| {
741 let overlay_color = Color::new(0xffff0000);
742 let widget = material_app(
743 ThemeData::builder().build(&tester.cell.borrow()),
744 Center::new()
745 .child(IconButtonTheme::new(
746 IconButtonThemeData::new().style(
747 IconButton::style_from()
748 .overlay_color(overlay_color)
749 .build(),
750 ),
751 IconButton::new(noop(), Icon::new(Some(Icons::add()))),
752 ))
753 .into_widget(),
754 );
755 tester.pump_widget(widget);
756 let rects = |tester: &WidgetTester| -> Vec<u32> {
757 tester
758 .ink_calls()
759 .into_iter()
760 .filter_map(|drawn| match drawn {
761 RecordedCall::Rect { color, .. } => Some(color.value()),
762 _ => None,
763 })
764 .collect()
765 };
766
767 let center = tester.get_center(icon_button_element(tester));
769 let mut mouse = TestPointer::mouse(1, waft_gestures::K_PRIMARY_BUTTON);
770 tester.add_pointer(&mut mouse, Offset::ZERO);
771 tester.move_to(&mut mouse, center, false);
772 tester.pump_and_settle();
773 assert_eq!(
774 rects(tester),
775 vec![overlay_color.with_opacity(0.08).value()]
776 );
777
778 tester.send(&mouse, waft_embedder::PointerChange::Down);
780 tester.pump_and_settle();
781 assert_eq!(
782 rects(tester),
783 vec![
784 overlay_color.with_opacity(0.08).value(),
785 overlay_color.with_opacity(0.1).value()
786 ]
787 );
788 tester.up(&mouse);
790 tester.pump_and_settle();
791 tester.move_to(&mut mouse, Offset::new(0.0, 50.0), false);
792 tester.pump_and_settle();
793
794 tester.send_key_event(PhysicalKeyboardKey::TAB, LogicalKeyboardKey::TAB);
796 tester.pump_and_settle();
797 assert_eq!(rects(tester), vec![overlay_color.with_opacity(0.1).value()]);
798 });
799 }
800
801 const BUTTON_KEY: &str = "button";
802
803 fn square_button(key: &KeyRef) -> IconButton {
805 IconButton::new(noop(), Icon::new(Some(Icons::add())))
806 .key(key.clone())
807 .style(ButtonStyle::new().shape_variant(ButtonShapeVariant::Square))
808 }
809
810 fn custom_style() -> ButtonStyle {
811 ButtonStyle::new().foreground_color(Rc::new(WidgetStatePropertyAll::new(Some(Color::new(
812 0xffff0000,
813 )))))
814 }
815
816 fn is_stadium(tester: &WidgetTester) -> bool {
817 below_button(tester, |material: &Material| {
818 material
819 .shape
820 .as_ref()
821 .is_some_and(|shape| shape.as_any().is::<StadiumBorder>())
822 })
823 }
824
825 fn is_rounded_rectangle(tester: &WidgetTester) -> bool {
826 below_button(tester, |material: &Material| {
827 material
828 .shape
829 .as_ref()
830 .is_some_and(|shape| shape.as_any().is::<RoundedRectangleBorder>())
831 })
832 }
833
834 fn button_with_variant(
836 key: &KeyRef,
837 variant: &Rc<std::cell::Cell<Option<StyleVariant>>>,
838 ) -> WidgetRef {
839 let key = key.clone();
840 let variant = Rc::clone(variant);
841 Builder::new(move |app: &mut App, context| {
842 variant.set(Some(IconButtonTheme::variant_of(app, context)));
843 square_button(&key).into_widget()
844 })
845 .into_widget()
846 }
847
848 fn key() -> KeyRef {
849 waft_foundation::ValueKey::new(BUTTON_KEY.to_owned()).into()
850 }
851
852 fn expressive_theme(tester: &WidgetTester) -> ThemeData {
853 ThemeData::builder()
854 .icon_button_theme(
855 IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
856 )
857 .build(&tester.cell.borrow())
858 }
859
860 #[test]
862 fn variant_of_is_material3_when_no_theme_specifies_one() {
863 test_widgets(|tester| {
864 let variant = Rc::new(std::cell::Cell::new(None));
865 let widget = material_app(
866 ThemeData::builder().build(&tester.cell.borrow()),
867 Center::new()
868 .child(IconButtonTheme::new(
869 IconButtonThemeData::new().style(custom_style()),
870 button_with_variant(&key(), &variant),
871 ))
872 .into_widget(),
873 );
874 tester.pump_widget(widget);
875 assert_eq!(variant.get(), Some(StyleVariant::Material3));
876 assert!(is_stadium(tester));
877 });
878 }
879
880 #[test]
882 fn variant_of_reads_the_theme_data_icon_button_theme() {
883 test_widgets(|tester| {
884 let variant = Rc::new(std::cell::Cell::new(None));
885 let widget = material_app(
886 expressive_theme(tester),
887 Center::new()
888 .child(button_with_variant(&key(), &variant))
889 .into_widget(),
890 );
891 tester.pump_widget(widget);
892 assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
893 assert!(is_rounded_rectangle(tester));
894 });
895 }
896
897 #[test]
899 fn a_nested_theme_without_a_variant_inherits_the_outer_one() {
900 test_widgets(|tester| {
901 let variant = Rc::new(std::cell::Cell::new(None));
902 let widget = material_app(
903 ThemeData::builder().build(&tester.cell.borrow()),
904 Center::new()
905 .child(IconButtonTheme::new(
906 IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
907 IconButtonTheme::new(
908 IconButtonThemeData::new().style(custom_style()),
909 button_with_variant(&key(), &variant),
910 ),
911 ))
912 .into_widget(),
913 );
914 tester.pump_widget(widget);
915 assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
916 assert!(is_rounded_rectangle(tester));
917 });
918 }
919
920 #[test]
922 fn a_theme_without_a_variant_inherits_the_theme_data_one() {
923 test_widgets(|tester| {
924 let variant = Rc::new(std::cell::Cell::new(None));
925 let widget = material_app(
926 expressive_theme(tester),
927 Center::new()
928 .child(IconButtonTheme::new(
929 IconButtonThemeData::new().style(custom_style()),
930 button_with_variant(&key(), &variant),
931 ))
932 .into_widget(),
933 );
934 tester.pump_widget(widget);
935 assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
936 assert!(is_rounded_rectangle(tester));
937 });
938 }
939
940 #[test]
942 fn an_explicit_variant_overrides_the_ancestors() {
943 test_widgets(|tester| {
944 let variant = Rc::new(std::cell::Cell::new(None));
945 let widget = material_app(
946 ThemeData::builder().build(&tester.cell.borrow()),
947 Center::new()
948 .child(IconButtonTheme::new(
949 IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
950 IconButtonTheme::new(
951 IconButtonThemeData::new()
952 .style(custom_style())
953 .variant(StyleVariant::Material3),
954 button_with_variant(&key(), &variant),
955 ),
956 ))
957 .into_widget(),
958 );
959 tester.pump_widget(widget);
960 assert_eq!(variant.get(), Some(StyleVariant::Material3));
961 assert!(is_stadium(tester));
962 });
963 }
964
965 #[test]
967 fn an_enclosing_icon_button_theme_precedes_a_closer_theme() {
968 test_widgets(|tester| {
969 let variant = Rc::new(std::cell::Cell::new(None));
970 let closer = ThemeData::builder()
971 .icon_button_theme(IconButtonThemeData::new().variant(StyleVariant::Material3))
972 .build(&tester.cell.borrow());
973 let widget = material_app(
974 ThemeData::builder().build(&tester.cell.borrow()),
975 Center::new()
976 .child(IconButtonTheme::new(
977 IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
978 Theme::new(closer, button_with_variant(&key(), &variant)),
979 ))
980 .into_widget(),
981 );
982 tester.pump_widget(widget);
983 assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
984 assert!(is_rounded_rectangle(tester));
985 });
986 }
987
988 #[test]
990 fn the_button_rebuilds_when_a_farther_theme_changes() {
991 test_widgets(|tester| {
992 let variant = tester
993 .cell
994 .borrow_mut()
995 .create(ValueNotifier::new(Some(StyleVariant::Material3Expressive)));
996 let child = IconButtonTheme::new(
999 IconButtonThemeData::new().style(custom_style()),
1000 square_button(&key()),
1001 )
1002 .into_widget();
1003 let widget = material_app(
1004 ThemeData::builder().build(&tester.cell.borrow()),
1005 Center::new()
1006 .child(
1007 ValueListenableBuilder::new(
1008 Rc::new(variant.clone()),
1009 |_app, _context, value: &Option<StyleVariant>, child| {
1010 let mut data = IconButtonThemeData::new();
1011 data.variant = *value;
1012 IconButtonTheme::new(data, child.expect("a child").clone())
1013 .into_widget()
1014 },
1015 )
1016 .child(child),
1017 )
1018 .into_widget(),
1019 );
1020 tester.pump_widget(widget);
1021 assert!(is_rounded_rectangle(tester));
1022
1023 variant.set_value(&mut tester.cell.borrow_mut(), Some(StyleVariant::Material3));
1024 tester.pump_and_settle();
1025 assert!(is_stadium(tester));
1026 });
1027 }
1028
1029 #[test]
1031 fn an_icon_button_in_a_list_tile_uses_the_inherited_variant() {
1032 test_widgets(|tester| {
1033 let widget = material_app(
1034 expressive_theme(tester),
1035 Material::new()
1036 .child(
1037 crate::list_tile::ListTile::new()
1038 .title(waft_widgets::Text::new("Title"))
1039 .trailing(square_button(&key())),
1040 )
1041 .into_widget(),
1042 );
1043 tester.pump_widget(widget);
1044 assert!(is_rounded_rectangle(tester));
1045 });
1046 }
1047
1048 #[test]
1050 fn an_icon_button_in_an_app_bar_with_a_foreground_color_uses_the_inherited_variant() {
1051 test_widgets(|tester| {
1052 let widget = material_app(
1053 expressive_theme(tester),
1054 crate::scaffold::Scaffold::new()
1055 .app_bar(waft_widgets::PreferredSizeWidgetRef::new(
1056 crate::app_bar::AppBar::new()
1057 .foreground_color(Color::new(0xffff0000))
1058 .actions(vec![square_button(&key()).into_widget()]),
1059 ))
1060 .into_widget(),
1061 );
1062 tester.pump_widget(widget);
1063 assert!(is_rounded_rectangle(tester));
1064 });
1065 }
1066 #[test]
1068 fn a_icon_button_theme_data_describes_nothing_by_default() {
1069 assert_eq!(
1070 waft_widgets::testing::describe_properties(|properties| {
1071 waft_foundation::Diagnosticable::debug_fill_properties(
1072 &IconButtonThemeData::new(),
1073 properties,
1074 )
1075 }),
1076 Vec::<String>::new()
1077 );
1078 }
1079}