use waft_foundation::App;
use waft_widgets::{
AnyElement, BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
downcast_widget,
};
use crate::button_style::ButtonStyle;
use crate::theme::Theme;
use crate::theme_data::StyleVariant;
#[derive(Clone, Debug, Default)]
pub struct IconButtonThemeData {
pub style: Option<ButtonStyle>,
pub variant: Option<StyleVariant>,
}
impl IconButtonThemeData {
pub fn new() -> IconButtonThemeData {
IconButtonThemeData::default()
}
pub fn style(mut self, style: ButtonStyle) -> IconButtonThemeData {
self.style = Some(style);
self
}
pub fn variant(mut self, variant: StyleVariant) -> IconButtonThemeData {
self.variant = Some(variant);
self
}
pub fn lerp(
a: Option<&IconButtonThemeData>,
b: Option<&IconButtonThemeData>,
t: f64,
) -> Option<IconButtonThemeData> {
match (a, b) {
(None, None) => return None,
(Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
_ => {}
}
Some(IconButtonThemeData {
style: ButtonStyle::lerp(
a.and_then(|a| a.style.as_ref()),
b.and_then(|b| b.style.as_ref()),
t,
),
variant: if t < 0.5 {
a.and_then(|a| a.variant)
} else {
b.and_then(|b| b.variant)
},
})
}
}
impl PartialEq for IconButtonThemeData {
fn eq(&self, other: &IconButtonThemeData) -> bool {
if std::ptr::eq(self, other) {
return true;
}
other.style == self.style && other.variant == self.variant
}
}
#[derive(Debug)]
pub struct IconButtonTheme {
pub key: Option<KeyRef>,
pub data: IconButtonThemeData,
pub child: WidgetRef,
}
impl IconButtonTheme {
pub fn new<K>(data: IconButtonThemeData, child: impl IntoWidget<K>) -> IconButtonTheme {
IconButtonTheme {
key: None,
data,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> IconButtonTheme {
self.key = Some(key);
self
}
pub fn of(app: &mut App, context: BuildContext) -> IconButtonThemeData {
match context.depend_on_inherited_widget_of_exact_type::<IconButtonTheme>(app) {
Some(button_theme) => button_theme.data.clone(),
None => Theme::of(app, context).icon_button_theme.clone(),
}
}
pub fn variant_of(app: &mut App, context: BuildContext) -> StyleVariant {
let mut element =
context.get_element_for_inherited_widget_of_exact_type::<IconButtonTheme>(app);
while let Some(found) = element {
context.depend_on_inherited_element(app, found, None);
let variant = downcast_widget::<IconButtonTheme>(&**found.widget(app))
.expect("found as an IconButtonTheme")
.data
.variant;
if let Some(variant) = variant {
return variant;
}
let mut parent: Option<AnyElement> = None;
found.visit_ancestor_elements(app, &mut |ancestor| {
parent = Some(ancestor);
false
});
element = parent.and_then(|parent| {
parent.get_element_for_inherited_widget_of_exact_type::<IconButtonTheme>(app)
});
}
Theme::of(app, context)
.icon_button_theme
.variant
.unwrap_or(StyleVariant::Material3)
}
}
impl InheritedTheme for IconButtonTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
IconButtonTheme::new(self.data.clone(), child).into_widget()
}
}
impl InheritedWidget for IconButtonTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &IconButtonTheme) -> bool {
self.data != old_widget.data
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
#[cfg(test)]
#[allow(deprecated)]
mod tests {
use std::rc::Rc;
use std::time::Duration;
use waft_embedder::{Color, Offset, Radius, Size};
use waft_foundation::{App, Listener, ValueNotifier};
use waft_painting::{
Alignment, AlignmentGeometry, BorderRadius, BorderSide, BorderStyle, EdgeInsets,
OutlinedBorderRef, RoundedRectangleBorder, ShapeBorder, StadiumBorder,
};
use waft_services::{
LogicalKeyboardKey, MouseCursorRef, PhysicalKeyboardKey, SystemMouseCursors,
};
use waft_widgets::{
Align, AnyElement, Builder, Center, Icon, KeyRef, ValueListenableBuilder, WidgetState,
WidgetStateProperty, WidgetStatePropertyAll, WidgetStates,
};
use waft_widgets::testing::{TestPointer, WidgetTester, find, test_widgets};
use super::*;
use crate::button_style::{ButtonShapeVariant, ButtonSizeVariant, IconButtonWidthVariant};
use crate::color_scheme::ColorScheme;
use crate::colors::Colors;
use crate::icon_button::IconButton;
use crate::icons::Icons;
use crate::ink_well::InkWell;
use crate::material::{Material, MaterialType};
use crate::test_support::{PaintInspection, material_app};
use crate::theme_data::{MaterialTapTargetSize, ThemeData};
use waft_rendering::testing::RecordedCall;
fn noop() -> Option<Listener> {
Some(Listener::new(|_app| {}))
}
fn m3_theme(tester: &WidgetTester) -> ThemeData {
ThemeData::from(ColorScheme::light()).build(&tester.cell.borrow())
}
fn icon_button_element(tester: &WidgetTester) -> AnyElement {
find::by_type::<IconButton>().evaluate(tester)[0]
}
fn below_button<W: 'static, R>(tester: &WidgetTester, read: impl FnOnce(&W) -> R) -> R {
let button = icon_button_element(tester);
let found = find::by_type::<W>()
.evaluate(tester)
.into_iter()
.find(|element| tester.ancestors(*element).contains(&button))
.expect("a widget of that type below the IconButton");
tester.widget(found, read)
}
fn button_size(tester: &WidgetTester) -> Size {
tester.get_size(icon_button_element(tester))
}
fn icon_alignment(tester: &WidgetTester) -> AlignmentGeometry {
let align = tester
.element(find::ancestor(find::by_type::<Icon>(), find::by_type::<Align>()).first());
tester.widget(align, |align: &Align| align.alignment)
}
fn states(states: &[WidgetState]) -> WidgetStates {
states.iter().copied().collect()
}
#[test]
fn lerp_of_nothing_is_nothing_and_of_one_theme_is_that_theme() {
assert!(IconButtonThemeData::lerp(None, None, 0.0).is_none());
let data = IconButtonThemeData::new();
assert_eq!(
IconButtonThemeData::lerp(Some(&data), Some(&data), 0.5),
Some(data)
);
}
#[test]
fn the_theme_data_keeps_the_expressive_variant() {
let data = IconButtonThemeData::new().variant(StyleVariant::Material3Expressive);
assert_eq!(data.variant, Some(StyleVariant::Material3Expressive));
}
#[test]
fn the_variant_defaults_to_none() {
assert!(IconButtonThemeData::new().variant.is_none());
}
#[test]
fn theme_data_with_the_same_variants_are_equal() {
let style = |size, width, shape| {
IconButtonThemeData::new().style(
ButtonStyle::new()
.size_variant(size)
.icon_button_width(width)
.shape_variant(shape),
)
};
let a = style(
ButtonSizeVariant::Small,
IconButtonWidthVariant::Standard,
ButtonShapeVariant::Round,
);
let b = style(
ButtonSizeVariant::Small,
IconButtonWidthVariant::Standard,
ButtonShapeVariant::Round,
);
let c = style(
ButtonSizeVariant::Large,
IconButtonWidthVariant::Wide,
ButtonShapeVariant::Square,
);
assert_eq!(a, b);
assert_ne!(a, c);
}
#[test]
fn lerp_switches_the_variants_halfway() {
let a = IconButtonThemeData::new().style(
ButtonStyle::new()
.size_variant(ButtonSizeVariant::Small)
.icon_button_width(IconButtonWidthVariant::Narrow)
.shape_variant(ButtonShapeVariant::Round),
);
let b = IconButtonThemeData::new().style(
ButtonStyle::new()
.size_variant(ButtonSizeVariant::Large)
.icon_button_width(IconButtonWidthVariant::Wide)
.shape_variant(ButtonShapeVariant::Square),
);
let at = |t: f64| {
IconButtonThemeData::lerp(Some(&a), Some(&b), t)
.and_then(|data| data.style)
.expect("a style")
};
assert_eq!(at(0.0).size_variant, Some(ButtonSizeVariant::Small));
assert_eq!(at(0.4).size_variant, Some(ButtonSizeVariant::Small));
assert_eq!(at(0.5).size_variant, Some(ButtonSizeVariant::Large));
assert_eq!(at(1.0).size_variant, Some(ButtonSizeVariant::Large));
assert_eq!(
at(0.4).icon_button_width,
Some(IconButtonWidthVariant::Narrow)
);
assert_eq!(
at(0.5).icon_button_width,
Some(IconButtonWidthVariant::Wide)
);
assert_eq!(at(0.4).shape_variant, Some(ButtonShapeVariant::Round));
assert_eq!(at(0.5).shape_variant, Some(ButtonShapeVariant::Square));
}
#[test]
fn a_disabled_button_builds_under_an_expressive_theme() {
test_widgets(|tester| {
let theme = ThemeData::builder()
.use_material3(true)
.build(&tester.cell.borrow());
let widget = material_app(
theme,
IconButtonTheme::new(
IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
IconButton::new(None, Icon::new(Some(Icons::ac_unit()))),
)
.into_widget(),
);
tester.pump_widget(widget);
});
}
#[test]
fn without_a_theme_the_button_has_its_defaults() {
test_widgets(|tester| {
let widget = material_app(
m3_theme(tester),
Center::new()
.child(IconButton::new(noop(), Icon::new(Some(Icons::ac_unit()))))
.into_widget(),
);
tester.pump_widget(widget);
below_button(tester, |material: &Material| {
assert_eq!(material.animation_duration, Duration::from_millis(200));
assert!(material.border_radius.is_none());
assert_eq!(material.color, Some(Colors::TRANSPARENT));
assert_eq!(material.elevation, 0.0);
assert_eq!(material.shadow_color, Some(Colors::TRANSPARENT));
assert!(
material.shape.as_deref()
== Some(&StadiumBorder::default() as &dyn ShapeBorder)
);
assert!(material.text_style.is_none());
assert_eq!(material.r#type, MaterialType::Button);
});
assert!(icon_alignment(tester) == AlignmentGeometry::CENTER);
});
}
const FOREGROUND_COLOR: Color = Color::new(0xff000001);
const DISABLED_FOREGROUND_COLOR: Color = Color::new(0xff000002);
const BACKGROUND_COLOR: Color = Color::new(0xff000003);
const SHADOW_COLOR: Color = Color::new(0xff000004);
const ELEVATION: f64 = 3.0;
const MINIMUM_SIZE: Size = Size::new(200.0, 200.0);
const ANIMATION_DURATION: Duration = Duration::from_millis(25);
const ENABLE_FEEDBACK: bool = false;
fn side() -> BorderSide {
BorderSide::new(
Colors::GREEN.primary(),
2.0,
BorderStyle::Solid,
BorderSide::STROKE_ALIGN_INSIDE,
)
}
fn shape() -> RoundedRectangleBorder {
RoundedRectangleBorder::new(side(), BorderRadius::all(Radius::circular(2.0)).into())
}
fn override_style() -> ButtonStyle {
IconButton::style_from()
.foreground_color(FOREGROUND_COLOR)
.disabled_foreground_color(DISABLED_FOREGROUND_COLOR)
.background_color(BACKGROUND_COLOR)
.shadow_color(SHADOW_COLOR)
.elevation(ELEVATION)
.padding(EdgeInsets::all(3.0).into())
.minimum_size(MINIMUM_SIZE)
.side(side())
.shape(OutlinedBorderRef::new(shape()))
.enabled_mouse_cursor(SystemMouseCursors::TEXT.into())
.disabled_mouse_cursor(SystemMouseCursors::GRAB.into())
.tap_target_size(MaterialTapTargetSize::ShrinkWrap)
.animation_duration(ANIMATION_DURATION)
.enable_feedback(ENABLE_FEEDBACK)
.alignment(Alignment::CENTER_LEFT.into())
.build()
}
fn override_frame(
tester: &WidgetTester,
button_style: Option<ButtonStyle>,
theme_style: Option<ButtonStyle>,
overall_style: Option<ButtonStyle>,
) -> WidgetRef {
let child = Builder::new(move |_app, _context| {
let button = IconButton::new(noop(), Icon::new(Some(Icons::ac_unit())));
match &button_style {
Some(style) => button.style(style.clone()),
None => button,
}
.into_widget()
})
.into_widget();
let mut overall = IconButtonThemeData::new();
overall.style = overall_style;
let theme = m3_theme(tester)
.copy_with()
.icon_button_theme(overall)
.build();
let body = match theme_style {
None => child,
Some(theme_style) => {
IconButtonTheme::new(IconButtonThemeData::new().style(theme_style), child)
.into_widget()
}
};
material_app(theme, Center::new().child(body).into_widget())
}
fn check_button(tester: &WidgetTester) {
below_button(tester, |material: &Material| {
assert!(material.text_style.is_none());
assert_eq!(material.color, Some(BACKGROUND_COLOR));
assert_eq!(material.shadow_color, Some(SHADOW_COLOR));
assert_eq!(material.elevation, ELEVATION);
assert!(material.border_radius.is_none());
assert!(material.shape.as_deref() == Some(&shape() as &dyn ShapeBorder));
assert_eq!(material.animation_duration, ANIMATION_DURATION);
});
below_button(tester, |ink_well: &InkWell| {
let response = &ink_well.ink_response;
let cursor = response.mouse_cursor.clone().expect("a mouse cursor");
let text: MouseCursorRef = SystemMouseCursors::TEXT.into();
let grab: MouseCursorRef = SystemMouseCursors::GRAB.into();
assert!(
*<dyn WidgetStateProperty<MouseCursorRef>>::resolve_as(&cursor, &states(&[]))
== *text
);
assert!(
*<dyn WidgetStateProperty<MouseCursorRef>>::resolve_as(
&cursor,
&states(&[WidgetState::Disabled])
) == *grab
);
let overlay = response.overlay_color.as_ref().expect("an overlay color");
assert_eq!(
overlay.resolve(&states(&[WidgetState::Hovered])),
Some(FOREGROUND_COLOR.with_opacity(0.08))
);
assert_eq!(
overlay.resolve(&states(&[WidgetState::Focused])),
Some(FOREGROUND_COLOR.with_opacity(0.1))
);
assert_eq!(
overlay.resolve(&states(&[WidgetState::Pressed])),
Some(FOREGROUND_COLOR.with_opacity(0.1))
);
assert_eq!(response.enable_feedback, ENABLE_FEEDBACK);
});
assert_eq!(button_size(tester), Size::new(200.0, 200.0));
assert!(icon_alignment(tester) == Alignment::CENTER_LEFT.into());
}
fn check_override(
tester: &mut WidgetTester,
button_style: Option<ButtonStyle>,
theme_style: Option<ButtonStyle>,
overall_style: Option<ButtonStyle>,
) {
let widget = override_frame(tester, button_style, theme_style, overall_style);
tester.pump_widget(widget);
tester.pump_and_settle(); check_button(tester);
}
#[test]
fn the_button_style_overrides_the_defaults() {
test_widgets(|tester| check_override(tester, Some(override_style()), None, None));
}
#[test]
fn the_button_theme_style_overrides_the_defaults() {
test_widgets(|tester| check_override(tester, None, Some(override_style()), None));
}
#[test]
fn the_overall_theme_style_overrides_the_defaults() {
test_widgets(|tester| check_override(tester, None, None, Some(override_style())));
}
#[test]
fn the_button_style_overrides_empty_theme_and_overall_styles() {
test_widgets(|tester| {
check_override(
tester,
Some(override_style()),
Some(ButtonStyle::new()),
Some(ButtonStyle::new()),
)
});
}
#[test]
fn the_button_theme_style_overrides_empty_button_and_overall_styles() {
test_widgets(|tester| {
check_override(
tester,
Some(ButtonStyle::new()),
Some(override_style()),
Some(ButtonStyle::new()),
)
});
}
#[test]
fn the_overall_theme_style_overrides_an_empty_button_style() {
test_widgets(|tester| {
check_override(
tester,
Some(ButtonStyle::new()),
None,
Some(override_style()),
)
});
}
#[test]
fn the_shadow_color_comes_from_the_button_then_its_theme_not_the_overall_theme() {
test_widgets(|tester| {
let shadow_color = Color::new(0xff000001);
let overridden_color = Color::new(0xff000002);
let build = |tester: &mut WidgetTester,
overall_shadow_color: Option<Color>,
theme_shadow_color: Option<Color>,
button_shadow_color: Option<Color>| {
let mut theme = m3_theme(tester).copy_with();
if let Some(color) = overall_shadow_color {
theme = theme.shadow_color(color);
}
let mut theme_style = IconButton::style_from();
if let Some(color) = theme_shadow_color {
theme_style = theme_style.shadow_color(color);
}
let theme_style = theme_style.build();
let widget = material_app(
theme.build(),
Center::new()
.child(IconButtonTheme::new(
IconButtonThemeData::new().style(theme_style),
Builder::new(move |_app, _context| {
let mut style = IconButton::style_from();
if let Some(color) = button_shadow_color {
style = style.shadow_color(color);
}
IconButton::new(noop(), Icon::new(Some(Icons::add())))
.style(style.build())
.into_widget()
}),
))
.into_widget(),
);
tester.pump_widget(widget);
tester.pump_and_settle(); below_button(tester, |material: &Material| material.shadow_color)
};
assert_eq!(build(tester, None, None, None), Some(Colors::TRANSPARENT)); assert_eq!(
build(tester, Some(shadow_color), None, None),
Some(Colors::TRANSPARENT)
);
assert_eq!(
build(tester, None, Some(shadow_color), None),
Some(shadow_color)
);
assert_eq!(
build(tester, None, None, Some(shadow_color)),
Some(shadow_color)
);
assert_eq!(
build(tester, Some(overridden_color), Some(shadow_color), None),
Some(shadow_color)
);
assert_eq!(
build(tester, None, Some(overridden_color), Some(shadow_color)),
Some(shadow_color)
);
});
}
#[test]
fn the_theme_style_from_overlay_color_overrides_the_default_overlay() {
test_widgets(|tester| {
let overlay_color = Color::new(0xffff0000);
let widget = material_app(
ThemeData::builder().build(&tester.cell.borrow()),
Center::new()
.child(IconButtonTheme::new(
IconButtonThemeData::new().style(
IconButton::style_from()
.overlay_color(overlay_color)
.build(),
),
IconButton::new(noop(), Icon::new(Some(Icons::add()))),
))
.into_widget(),
);
tester.pump_widget(widget);
let rects = |tester: &WidgetTester| -> Vec<u32> {
tester
.ink_calls()
.into_iter()
.filter_map(|drawn| match drawn {
RecordedCall::Rect { color, .. } => Some(color.value()),
_ => None,
})
.collect()
};
let center = tester.get_center(icon_button_element(tester));
let mut mouse = TestPointer::mouse(1, waft_gestures::K_PRIMARY_BUTTON);
tester.add_pointer(&mut mouse, Offset::ZERO);
tester.move_to(&mut mouse, center, false);
tester.pump_and_settle();
assert_eq!(
rects(tester),
vec![overlay_color.with_opacity(0.08).value()]
);
tester.send(&mouse, waft_embedder::PointerChange::Down);
tester.pump_and_settle();
assert_eq!(
rects(tester),
vec![
overlay_color.with_opacity(0.08).value(),
overlay_color.with_opacity(0.1).value()
]
);
tester.up(&mouse);
tester.pump_and_settle();
tester.move_to(&mut mouse, Offset::new(0.0, 50.0), false);
tester.pump_and_settle();
tester.send_key_event(PhysicalKeyboardKey::TAB, LogicalKeyboardKey::TAB);
tester.pump_and_settle();
assert_eq!(rects(tester), vec![overlay_color.with_opacity(0.1).value()]);
});
}
const BUTTON_KEY: &str = "button";
fn square_button(key: &KeyRef) -> IconButton {
IconButton::new(noop(), Icon::new(Some(Icons::add())))
.key(key.clone())
.style(ButtonStyle::new().shape_variant(ButtonShapeVariant::Square))
}
fn custom_style() -> ButtonStyle {
ButtonStyle::new().foreground_color(Rc::new(WidgetStatePropertyAll::new(Some(Color::new(
0xffff0000,
)))))
}
fn is_stadium(tester: &WidgetTester) -> bool {
below_button(tester, |material: &Material| {
material
.shape
.as_ref()
.is_some_and(|shape| shape.as_any().is::<StadiumBorder>())
})
}
fn is_rounded_rectangle(tester: &WidgetTester) -> bool {
below_button(tester, |material: &Material| {
material
.shape
.as_ref()
.is_some_and(|shape| shape.as_any().is::<RoundedRectangleBorder>())
})
}
fn button_with_variant(
key: &KeyRef,
variant: &Rc<std::cell::Cell<Option<StyleVariant>>>,
) -> WidgetRef {
let key = key.clone();
let variant = Rc::clone(variant);
Builder::new(move |app: &mut App, context| {
variant.set(Some(IconButtonTheme::variant_of(app, context)));
square_button(&key).into_widget()
})
.into_widget()
}
fn key() -> KeyRef {
waft_foundation::ValueKey::new(BUTTON_KEY.to_owned()).into()
}
fn expressive_theme(tester: &WidgetTester) -> ThemeData {
ThemeData::builder()
.icon_button_theme(
IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
)
.build(&tester.cell.borrow())
}
#[test]
fn variant_of_is_material3_when_no_theme_specifies_one() {
test_widgets(|tester| {
let variant = Rc::new(std::cell::Cell::new(None));
let widget = material_app(
ThemeData::builder().build(&tester.cell.borrow()),
Center::new()
.child(IconButtonTheme::new(
IconButtonThemeData::new().style(custom_style()),
button_with_variant(&key(), &variant),
))
.into_widget(),
);
tester.pump_widget(widget);
assert_eq!(variant.get(), Some(StyleVariant::Material3));
assert!(is_stadium(tester));
});
}
#[test]
fn variant_of_reads_the_theme_data_icon_button_theme() {
test_widgets(|tester| {
let variant = Rc::new(std::cell::Cell::new(None));
let widget = material_app(
expressive_theme(tester),
Center::new()
.child(button_with_variant(&key(), &variant))
.into_widget(),
);
tester.pump_widget(widget);
assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
assert!(is_rounded_rectangle(tester));
});
}
#[test]
fn a_nested_theme_without_a_variant_inherits_the_outer_one() {
test_widgets(|tester| {
let variant = Rc::new(std::cell::Cell::new(None));
let widget = material_app(
ThemeData::builder().build(&tester.cell.borrow()),
Center::new()
.child(IconButtonTheme::new(
IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
IconButtonTheme::new(
IconButtonThemeData::new().style(custom_style()),
button_with_variant(&key(), &variant),
),
))
.into_widget(),
);
tester.pump_widget(widget);
assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
assert!(is_rounded_rectangle(tester));
});
}
#[test]
fn a_theme_without_a_variant_inherits_the_theme_data_one() {
test_widgets(|tester| {
let variant = Rc::new(std::cell::Cell::new(None));
let widget = material_app(
expressive_theme(tester),
Center::new()
.child(IconButtonTheme::new(
IconButtonThemeData::new().style(custom_style()),
button_with_variant(&key(), &variant),
))
.into_widget(),
);
tester.pump_widget(widget);
assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
assert!(is_rounded_rectangle(tester));
});
}
#[test]
fn an_explicit_variant_overrides_the_ancestors() {
test_widgets(|tester| {
let variant = Rc::new(std::cell::Cell::new(None));
let widget = material_app(
ThemeData::builder().build(&tester.cell.borrow()),
Center::new()
.child(IconButtonTheme::new(
IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
IconButtonTheme::new(
IconButtonThemeData::new()
.style(custom_style())
.variant(StyleVariant::Material3),
button_with_variant(&key(), &variant),
),
))
.into_widget(),
);
tester.pump_widget(widget);
assert_eq!(variant.get(), Some(StyleVariant::Material3));
assert!(is_stadium(tester));
});
}
#[test]
fn an_enclosing_icon_button_theme_precedes_a_closer_theme() {
test_widgets(|tester| {
let variant = Rc::new(std::cell::Cell::new(None));
let closer = ThemeData::builder()
.icon_button_theme(IconButtonThemeData::new().variant(StyleVariant::Material3))
.build(&tester.cell.borrow());
let widget = material_app(
ThemeData::builder().build(&tester.cell.borrow()),
Center::new()
.child(IconButtonTheme::new(
IconButtonThemeData::new().variant(StyleVariant::Material3Expressive),
Theme::new(closer, button_with_variant(&key(), &variant)),
))
.into_widget(),
);
tester.pump_widget(widget);
assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
assert!(is_rounded_rectangle(tester));
});
}
#[test]
fn the_button_rebuilds_when_a_farther_theme_changes() {
test_widgets(|tester| {
let variant = tester
.cell
.borrow_mut()
.create(ValueNotifier::new(Some(StyleVariant::Material3Expressive)));
let child = IconButtonTheme::new(
IconButtonThemeData::new().style(custom_style()),
square_button(&key()),
)
.into_widget();
let widget = material_app(
ThemeData::builder().build(&tester.cell.borrow()),
Center::new()
.child(
ValueListenableBuilder::new(
Rc::new(variant.clone()),
|_app, _context, value: &Option<StyleVariant>, child| {
let mut data = IconButtonThemeData::new();
data.variant = *value;
IconButtonTheme::new(data, child.expect("a child").clone())
.into_widget()
},
)
.child(child),
)
.into_widget(),
);
tester.pump_widget(widget);
assert!(is_rounded_rectangle(tester));
variant.set_value(&mut tester.cell.borrow_mut(), Some(StyleVariant::Material3));
tester.pump_and_settle();
assert!(is_stadium(tester));
});
}
#[test]
fn an_icon_button_in_a_list_tile_uses_the_inherited_variant() {
test_widgets(|tester| {
let widget = material_app(
expressive_theme(tester),
Material::new()
.child(
crate::list_tile::ListTile::new()
.title(waft_widgets::Text::new("Title"))
.trailing(square_button(&key())),
)
.into_widget(),
);
tester.pump_widget(widget);
assert!(is_rounded_rectangle(tester));
});
}
#[test]
fn an_icon_button_in_an_app_bar_with_a_foreground_color_uses_the_inherited_variant() {
test_widgets(|tester| {
let widget = material_app(
expressive_theme(tester),
crate::scaffold::Scaffold::new()
.app_bar(waft_widgets::PreferredSizeWidgetRef::new(
crate::app_bar::AppBar::new()
.foreground_color(Color::new(0xffff0000))
.actions(vec![square_button(&key()).into_widget()]),
))
.into_widget(),
);
tester.pump_widget(widget);
assert!(is_rounded_rectangle(tester));
});
}
}