use waft_foundation::{App, DiagnosticsProperty};
use waft_widgets::{
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, PartialEq)]
pub struct ElevatedButtonThemeData {
pub style: Option<ButtonStyle>,
pub variant: Option<StyleVariant>,
}
impl ElevatedButtonThemeData {
pub fn new() -> ElevatedButtonThemeData {
ElevatedButtonThemeData::default()
}
pub fn style(mut self, style: ButtonStyle) -> ElevatedButtonThemeData {
self.style = Some(style);
self
}
pub fn variant(mut self, variant: StyleVariant) -> ElevatedButtonThemeData {
self.variant = Some(variant);
self
}
pub fn lerp(
a: Option<&ElevatedButtonThemeData>,
b: Option<&ElevatedButtonThemeData>,
t: f64,
) -> Option<ElevatedButtonThemeData> {
match (a, b) {
(None, None) => return None,
(Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
_ => {}
}
Some(ElevatedButtonThemeData {
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)
},
})
}
}
#[derive(Debug)]
pub struct ElevatedButtonTheme {
pub key: Option<KeyRef>,
pub data: ElevatedButtonThemeData,
pub child: WidgetRef,
}
impl ElevatedButtonTheme {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(data: ElevatedButtonThemeData, child: impl IntoWidget<K>) -> ElevatedButtonTheme {
ElevatedButtonTheme {
key: None,
data,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> ElevatedButtonTheme {
self.key = Some(key);
self
}
pub fn of(app: &mut App, context: BuildContext) -> ElevatedButtonThemeData {
match context.depend_on_inherited_widget_of_exact_type::<ElevatedButtonTheme>(app) {
Some(button_theme) => button_theme.data.clone(),
None => Theme::of(app, context).elevated_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::<ElevatedButtonTheme>(app);
while let Some(current) = element {
let widget = context.depend_on_inherited_element(app, current, None);
let variant = downcast_widget::<ElevatedButtonTheme>(&*widget)
.expect("found by type")
.data
.variant;
if let Some(variant) = variant {
return variant;
}
let mut parent = None;
current.visit_ancestor_elements(app, &mut |ancestor| {
parent = Some(ancestor);
false
});
element = parent.and_then(|parent| {
parent.get_element_for_inherited_widget_of_exact_type::<ElevatedButtonTheme>(app)
});
}
Theme::of(app, context)
.elevated_button_theme
.variant
.unwrap_or(StyleVariant::Material3)
}
}
impl InheritedTheme for ElevatedButtonTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
ElevatedButtonTheme::new(self.data.clone(), child).into_widget()
}
}
impl InheritedWidget for ElevatedButtonTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &ElevatedButtonTheme) -> bool {
self.data != old_widget.data
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
impl waft_foundation::Diagnosticable for ElevatedButtonThemeData {
fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
properties.add(
DiagnosticsProperty::<crate::button_style::ButtonStyle>::new(
"style",
self.style.clone(),
)
.default_value(None),
);
}
}
impl std::fmt::Display for ElevatedButtonThemeData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&waft_foundation::Diagnosticable::to_string(self))
}
}
#[cfg(test)]
mod tests {
#![allow(deprecated)]
use waft_widgets::Text;
use waft_widgets::testing::assert_f32_equals;
use std::cell::Cell;
use std::rc::Rc;
use std::time::Duration;
use waft_embedder::{Color, FontWeight, Radius, Size};
use waft_foundation::{Listener, ValueNotifier};
use waft_painting::{
Alignment, AlignmentGeometry, BorderRadius, BorderSide, BorderStyle, EdgeInsetsGeometry,
OutlinedBorderRef, RoundedRectangleBorder, ShapeBorder, StadiumBorder, TextStyle,
};
use waft_services::{MouseCursorRef, SystemMouseCursors};
use waft_widgets::{
Align, AnyElement, Builder, Center, Icon, IconTheme, KeyRef, Row, SizedBox,
ValueListenableBuilder, WidgetState, WidgetStateProperty, WidgetStatePropertyAll,
WidgetStates,
};
use waft_widgets::testing::{WidgetTester, find, test_widgets};
use super::*;
use crate::button_style::{ButtonShapeVariant, ButtonSizeVariant};
use crate::button_style_button::IconAlignment;
use crate::color_scheme::ColorScheme;
use crate::colors::Colors;
use crate::elevated_button::ElevatedButton;
use crate::icons::Icons;
use crate::ink_well::InkWell;
use crate::material::Material;
use crate::test_support::material_app;
use crate::theme_data::{MaterialTapTargetSize, ThemeData};
fn pressed() -> Option<Listener> {
Some(Listener::new(|_app| {}))
}
fn text(text: &str) -> WidgetRef {
Text::new(text).into_widget()
}
fn states(states: &[WidgetState]) -> WidgetStates {
states.iter().copied().collect()
}
fn is_inside<W: 'static>(tester: &WidgetTester, element: AnyElement) -> bool {
let ancestors = tester.ancestors(element);
let app = tester.cell.borrow();
ancestors
.into_iter()
.any(|ancestor| downcast_widget::<W>(&**ancestor.widget(&app)).is_some())
}
fn in_button<W: 'static>(tester: &WidgetTester) -> AnyElement {
find::by_type::<W>()
.evaluate(tester)
.into_iter()
.find(|element| is_inside::<ElevatedButton>(tester, *element))
.expect("a descendant of the button")
}
fn material<R>(tester: &WidgetTester, read: impl FnOnce(&Material) -> R) -> R {
tester.widget::<Material, R>(in_button::<Material>(tester), read)
}
fn text_color(material: &Material) -> Option<Color> {
material
.text_style
.as_ref()
.and_then(|text_style| text_style.color.as_ref())
.map(|color| color.color())
}
fn rounded(radius: f64) -> RoundedRectangleBorder {
RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadius::all(Radius::circular(radius)).into(),
)
}
#[test]
fn the_theme_data_carries_a_variant_that_lerps_at_the_midpoint() {
let material3 = ElevatedButtonThemeData::new().variant(StyleVariant::Material3);
let expressive = ElevatedButtonThemeData::new().variant(StyleVariant::Material3Expressive);
assert_eq!(material3.variant, Some(StyleVariant::Material3));
assert_eq!(expressive.variant, Some(StyleVariant::Material3Expressive));
assert_eq!(
ElevatedButtonThemeData::lerp(Some(&material3), Some(&expressive), 0.49),
Some(material3.clone())
);
assert_eq!(
ElevatedButtonThemeData::lerp(Some(&material3), Some(&expressive), 0.5),
Some(expressive.clone())
);
assert_ne!(material3, expressive);
}
#[test]
fn the_debug_description_and_lerp_include_the_variant() {
let expressive = ElevatedButtonThemeData::new().variant(StyleVariant::Material3Expressive);
assert!(format!("{expressive:?}").contains("Material3Expressive"));
assert_eq!(
ElevatedButtonThemeData::lerp(None, Some(&expressive), 0.4).and_then(|t| t.variant),
None
);
assert_eq!(
ElevatedButtonThemeData::lerp(None, Some(&expressive), 0.6).and_then(|t| t.variant),
Some(StyleVariant::Material3Expressive)
);
}
#[test]
fn lerp_of_nothing_is_nothing_and_of_a_theme_with_itself_is_that_theme() {
assert_eq!(ElevatedButtonThemeData::lerp(None, None, 0.0), None);
let data = ElevatedButtonThemeData::new();
assert_eq!(
ElevatedButtonThemeData::lerp(Some(&data), Some(&data), 0.5),
Some(data)
);
}
#[test]
fn without_a_button_theme_the_button_shows_its_defaults() {
for use_material3 in [true, false] {
test_widgets(|tester| {
let color_scheme = ColorScheme::light();
let theme = ThemeData::from(color_scheme.clone())
.use_material3(use_material3)
.build(&tester.cell.borrow());
tester.pump_widget(material_app(
theme,
Center::new()
.child(ElevatedButton::new(pressed(), Some(text("button"))))
.into_widget(),
));
material(tester, |material| {
assert_eq!(material.animation_duration, Duration::from_millis(200));
assert!(material.border_radius.is_none());
let shape = material.shape.as_deref().expect("a shape");
if use_material3 {
assert_eq!(material.color, Some(color_scheme.surface()));
assert_eq!(material.elevation, 1.0);
assert_eq!(material.shadow_color, Some(color_scheme.shadow()));
assert!(shape.eq_shape(&StadiumBorder::default()));
assert_eq!(text_color(material), Some(color_scheme.primary()));
} else {
assert_eq!(material.color, Some(color_scheme.primary()));
assert_eq!(material.elevation, 2.0);
assert_eq!(material.shadow_color, Some(Color::new(0xff000000)));
assert!(shape.eq_shape(&rounded(4.0)));
assert_eq!(text_color(material), Some(color_scheme.on_primary()));
}
let text_style = material.text_style.as_ref().expect("a text style");
assert_eq!(text_style.font_family.as_deref(), Some("Roboto"));
assert_eq!(text_style.font_size, Some(14.0));
assert_eq!(text_style.font_weight, Some(FontWeight::W500));
});
let align = in_button::<Align>(tester);
tester.widget::<Align, _>(align, |align| {
assert!(align.alignment == AlignmentGeometry::from(Alignment::CENTER));
});
});
}
}
const FOREGROUND_COLOR: Color = Color::new(0xff000001);
const BACKGROUND_COLOR: Color = Color::new(0xff000002);
const DISABLED_COLOR: Color = Color::new(0xff000003);
const SHADOW_COLOR: Color = Color::new(0xff000004);
const ELEVATION: f64 = 1.0;
const MINIMUM_SIZE: Size = Size::new(200.0, 200.0);
const ANIMATION_DURATION: Duration = Duration::from_millis(25);
const ENABLE_FEEDBACK: bool = false;
const ALIGNMENT: AlignmentGeometry = AlignmentGeometry::CENTER_LEFT;
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 {
let text: MouseCursorRef = SystemMouseCursors::TEXT.into();
let grab: MouseCursorRef = SystemMouseCursors::GRAB.into();
ElevatedButton::style_from()
.foreground_color(FOREGROUND_COLOR)
.disabled_foreground_color(DISABLED_COLOR)
.background_color(BACKGROUND_COLOR)
.disabled_background_color(DISABLED_COLOR)
.shadow_color(SHADOW_COLOR)
.elevation(ELEVATION)
.text_style(TextStyle::new().font_size(12.0))
.padding(EdgeInsetsGeometry::all(3.0))
.minimum_size(MINIMUM_SIZE)
.side(side())
.shape(OutlinedBorderRef::new(shape()))
.enabled_mouse_cursor(text)
.disabled_mouse_cursor(grab)
.tap_target_size(MaterialTapTargetSize::ShrinkWrap)
.animation_duration(ANIMATION_DURATION)
.enable_feedback(ENABLE_FEEDBACK)
.alignment(ALIGNMENT)
.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 mut button = ElevatedButton::new(pressed(), Some(text("button")));
if let Some(button_style) = &button_style {
button = button.style(button_style.clone());
}
button.into_widget()
})
.into_widget();
let mut overall = ElevatedButtonThemeData::new();
overall.style = overall_style;
let theme = ThemeData::from(ColorScheme::light())
.use_material3(false)
.build(&tester.cell.borrow())
.copy_with()
.elevated_button_theme(overall)
.build();
material_app(
theme,
Center::new()
.child(match theme_style {
None => child,
Some(theme_style) => ElevatedButtonTheme::new(
ElevatedButtonThemeData::new().style(theme_style),
child,
)
.into_widget(),
})
.into_widget(),
)
}
fn check_button(tester: &WidgetTester) {
material(tester, |material| {
assert_eq!(text_color(material), Some(FOREGROUND_COLOR));
assert_eq!(
material
.text_style
.as_ref()
.and_then(|text_style| text_style.font_size),
Some(12.0)
);
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);
});
tester.widget::<InkWell, _>(in_button::<InkWell>(tester), |ink_well| {
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);
});
let button = find::by_type::<ElevatedButton>().evaluate(tester)[0];
let size = tester.get_size(button);
assert_eq!(size, Size::new(200.0, 200.0));
let align = in_button::<Align>(tester);
tester.widget::<Align, _>(align, |align| assert!(align.alignment == ALIGNMENT));
}
#[test]
fn the_button_style_the_button_theme_and_the_overall_theme_each_override_the_defaults() {
test_widgets(|tester| {
let empty = || Some(ButtonStyle::new());
let cases: [[Option<ButtonStyle>; 3]; 6] = [
[Some(override_style()), None, None],
[None, Some(override_style()), None],
[None, None, Some(override_style())],
[Some(override_style()), empty(), empty()],
[empty(), Some(override_style()), empty()],
[empty(), None, Some(override_style())],
];
for [button_style, theme_style, overall_style] in cases {
let frame = override_frame(tester, button_style, theme_style, overall_style);
tester.pump_widget(frame);
tester.pump_and_settle(); check_button(tester);
}
});
}
#[test]
fn the_shadow_color_comes_from_the_button_then_its_theme_then_the_overall_theme() {
test_widgets(|tester| {
let shadow_color = Color::new(0xff000001);
let overridden_color = Color::new(0xff000002);
for use_material3 in [true, false] {
let build_frame =
|tester: &WidgetTester,
overall_shadow_color: Option<Color>,
theme_shadow_color: Option<Color>,
button_shadow_color: Option<Color>| {
let theme = if use_material3 {
let mut color_scheme = ColorScheme::light();
if let Some(overall) = overall_shadow_color {
color_scheme = color_scheme.copy_with().with_shadow(overall);
}
ThemeData::from(color_scheme).build(&tester.cell.borrow())
} else {
let theme = ThemeData::from(ColorScheme::light())
.use_material3(false)
.build(&tester.cell.borrow());
match overall_shadow_color {
Some(overall) => theme.copy_with().shadow_color(overall).build(),
None => theme,
}
};
let style_from = |color: Option<Color>| {
let mut style = ElevatedButton::style_from();
if let Some(color) = color {
style = style.shadow_color(color);
}
style.build()
};
let button_style = style_from(button_shadow_color);
material_app(
theme,
Center::new()
.child(ElevatedButtonTheme::new(
ElevatedButtonThemeData::new()
.style(style_from(theme_shadow_color)),
Builder::new(move |_app, _context| {
ElevatedButton::new(pressed(), Some(text("button")))
.style(button_style.clone())
.into_widget()
}),
))
.into_widget(),
)
};
let shadow =
|tester: &WidgetTester| material(tester, |material| material.shadow_color);
let frame = build_frame(tester, None, None, None);
tester.pump_widget(frame);
assert_eq!(shadow(tester), Some(Colors::BLACK));
for (overall, theme, button) in [
(Some(shadow_color), None, None),
(None, Some(shadow_color), None),
(None, None, Some(shadow_color)),
(Some(overridden_color), Some(shadow_color), None),
(None, Some(overridden_color), Some(shadow_color)),
] {
let frame = build_frame(tester, overall, theme, button);
tester.pump_widget(frame);
tester.pump_and_settle(); assert_eq!(shadow(tester), Some(shadow_color));
}
}
});
}
fn icon_button_frame(tester: &WidgetTester, theme: ThemeData) -> WidgetRef {
let _ = tester;
material_app(
theme,
Center::new()
.child(
ElevatedButton::icon(pressed(), text("button"))
.icon(Icon::new(Some(Icons::add()))),
)
.into_widget(),
)
}
#[test]
fn an_icon_button_takes_its_icon_alignment_from_the_theme() {
test_widgets(|tester| {
let build = |tester: &WidgetTester, icon_alignment: Option<IconAlignment>| {
let mut style = ButtonStyle::new();
style.icon_alignment = icon_alignment;
let theme = ThemeData::builder()
.elevated_button_theme(ElevatedButtonThemeData::new().style(style))
.build(&tester.cell.borrow());
icon_button_frame(tester, theme)
};
let rects = |tester: &WidgetTester| {
let material = tester.get_rect(in_button::<Material>(tester));
let icon = tester.get_rect(find::by_type::<Icon>().evaluate(tester)[0]);
(material, icon)
};
let frame = build(tester, None);
tester.pump_widget(frame);
let (button, icon) = rects(tester);
assert_eq!(button.left, icon.left - 16.0);
let frame = build(tester, Some(IconAlignment::End));
tester.pump_widget(frame);
tester.pump_and_settle();
let (button, icon) = rects(tester);
assert_f32_equals(button.right, icon.right + 24.0);
});
}
#[test]
fn an_icon_takes_the_theme_foreground_color() {
test_widgets(|tester| {
let foreground_color = Color::new(0xFFFFA500);
let icon_color = Rc::new(Cell::new(None));
let probe = Builder::new({
let icon_color = Rc::clone(&icon_color);
move |app, context| {
icon_color.set(IconTheme::of(app, context).color.map(|color| color.color()));
SizedBox::new().width(18.0).height(18.0).into_widget()
}
});
let theme = ThemeData::builder()
.elevated_button_theme(
ElevatedButtonThemeData::new().style(
ElevatedButton::style_from()
.foreground_color(foreground_color)
.build(),
),
)
.build(&tester.cell.borrow());
tester.pump_widget(material_app(
theme,
Material::new()
.child(
Center::new()
.child(ElevatedButton::icon(pressed(), text("Button")).icon(probe)),
)
.into_widget(),
));
assert_eq!(icon_color.get(), Some(foreground_color));
});
}
fn square_button() -> WidgetRef {
ElevatedButton::new(pressed(), Some(text("Button")))
.key(button_key())
.style(ButtonStyle::new().shape_variant(ButtonShapeVariant::Square))
.into_widget()
}
fn button_key() -> KeyRef {
waft_foundation::ValueKey::new("button".to_owned()).into()
}
fn custom_style() -> ButtonStyle {
ButtonStyle::new().foreground_color(Rc::new(WidgetStatePropertyAll::new(Some(Color::new(
0xffff0000,
)))))
}
fn is_stadium(tester: &WidgetTester) -> bool {
material(tester, |material| {
material
.shape
.as_ref()
.is_some_and(|shape| shape.as_any().downcast_ref::<StadiumBorder>().is_some())
})
}
fn is_rounded_rectangle(tester: &WidgetTester) -> bool {
material(tester, |material| {
material.shape.as_ref().is_some_and(|shape| {
shape
.as_any()
.downcast_ref::<RoundedRectangleBorder>()
.is_some()
})
})
}
fn button_with_variant(variant: &Rc<Cell<Option<StyleVariant>>>) -> WidgetRef {
let variant = Rc::clone(variant);
Builder::new(move |app, context| {
variant.set(Some(ElevatedButtonTheme::variant_of(app, context)));
square_button()
})
.into_widget()
}
fn variant_data(variant: StyleVariant) -> ElevatedButtonThemeData {
ElevatedButtonThemeData::new().variant(variant)
}
fn variant_theme(tester: &WidgetTester, variant: StyleVariant) -> ThemeData {
ThemeData::builder()
.elevated_button_theme(variant_data(variant))
.build(&tester.cell.borrow())
}
fn default_theme(tester: &WidgetTester) -> ThemeData {
ThemeData::new(&tester.cell.borrow())
}
#[test]
fn variant_of_is_material_3_when_no_theme_names_one() {
test_widgets(|tester| {
let variant = Rc::new(Cell::new(None));
let theme = default_theme(tester);
tester.pump_widget(material_app(
theme,
Center::new()
.child(ElevatedButtonTheme::new(
ElevatedButtonThemeData::new().style(custom_style()),
button_with_variant(&variant),
))
.into_widget(),
));
assert_eq!(variant.get(), Some(StyleVariant::Material3));
assert!(is_stadium(tester));
});
}
#[test]
fn variant_of_falls_back_to_the_theme_data_variant() {
test_widgets(|tester| {
let variant = Rc::new(Cell::new(None));
let theme = variant_theme(tester, StyleVariant::Material3Expressive);
tester.pump_widget(material_app(
theme,
Center::new()
.child(button_with_variant(&variant))
.into_widget(),
));
assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
assert!(is_rounded_rectangle(tester));
});
}
#[test]
fn variant_of_takes_the_nearest_theme_that_names_one() {
test_widgets(|tester| {
let variant = Rc::new(Cell::new(None));
let theme = default_theme(tester);
tester.pump_widget(material_app(
theme,
Center::new()
.child(ElevatedButtonTheme::new(
variant_data(StyleVariant::Material3Expressive),
ElevatedButtonTheme::new(
ElevatedButtonThemeData::new().style(custom_style()),
button_with_variant(&variant),
),
))
.into_widget(),
));
tester.pump_and_settle();
assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
assert!(is_rounded_rectangle(tester));
let variant = Rc::new(Cell::new(None));
let theme = variant_theme(tester, StyleVariant::Material3Expressive);
tester.pump_widget(material_app(
theme,
Center::new()
.child(ElevatedButtonTheme::new(
ElevatedButtonThemeData::new().style(custom_style()),
button_with_variant(&variant),
))
.into_widget(),
));
tester.pump_and_settle();
assert_eq!(variant.get(), Some(StyleVariant::Material3Expressive));
assert!(is_rounded_rectangle(tester));
let variant = Rc::new(Cell::new(None));
let theme = default_theme(tester);
tester.pump_widget(material_app(
theme,
Center::new()
.child(ElevatedButtonTheme::new(
variant_data(StyleVariant::Material3Expressive),
ElevatedButtonTheme::new(
ElevatedButtonThemeData::new()
.style(custom_style())
.variant(StyleVariant::Material3),
button_with_variant(&variant),
),
))
.into_widget(),
));
tester.pump_and_settle();
assert_eq!(variant.get(), Some(StyleVariant::Material3));
assert!(is_stadium(tester));
});
}
#[test]
fn an_enclosing_button_theme_wins_over_a_closer_theme() {
test_widgets(|tester| {
let variant = Rc::new(Cell::new(None));
let theme = default_theme(tester);
let closer = variant_theme(tester, StyleVariant::Material3);
tester.pump_widget(material_app(
theme,
Center::new()
.child(ElevatedButtonTheme::new(
variant_data(StyleVariant::Material3Expressive),
Theme::new(closer, button_with_variant(&variant)),
))
.into_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 = ElevatedButtonTheme::new(
ElevatedButtonThemeData::new().style(custom_style()),
square_button(),
)
.into_widget();
let theme = default_theme(tester);
tester.pump_widget(material_app(
theme,
Center::new()
.child(
ValueListenableBuilder::new(
Rc::new(variant.clone()),
|_app, _context, value: &Option<StyleVariant>, child| {
let mut data = ElevatedButtonThemeData::new();
data.variant = *value;
ElevatedButtonTheme::new(data, child.expect("a child").clone())
.into_widget()
},
)
.child(child),
)
.into_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_gap_follows_the_inherited_variant() {
test_widgets(|tester| {
let theme = default_theme(tester);
tester.pump_widget(material_app(
theme,
Center::new()
.child(ElevatedButtonTheme::new(
variant_data(StyleVariant::Material3Expressive),
ElevatedButtonTheme::new(
ElevatedButtonThemeData::new().style(custom_style()),
ElevatedButton::icon(pressed(), text("Button"))
.icon(Icon::new(Some(Icons::add())))
.key(button_key())
.style(ButtonStyle::new().size_variant(ButtonSizeVariant::Large)),
),
))
.into_widget(),
));
let row = in_button::<Row>(tester);
assert_eq!(tester.widget::<Row, _>(row, |row| row.spacing), 12.0);
});
}
#[test]
fn a_elevated_button_theme_data_describes_nothing_by_default() {
assert_eq!(
waft_widgets::testing::describe_properties(|properties| {
waft_foundation::Diagnosticable::debug_fill_properties(
&ElevatedButtonThemeData::new(),
properties,
)
}),
Vec::<String>::new()
);
}
}