use waft_embedder::{Color, lerp_double};
use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
use waft_painting::{
AnyColor, ColorProperty, EdgeInsets, EdgeInsetsGeometry, ShapeBorderRef, TextStyle,
};
use waft_widgets::{
BuildContext, DismissDirection, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
WidgetStateColor,
};
use crate::theme::Theme;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SnackBarBehavior {
Fixed,
Floating,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SnackBarThemeData {
pub background_color: Option<Color>,
pub action_text_color: Option<AnyColor>,
pub disabled_action_text_color: Option<Color>,
pub content_text_style: Option<TextStyle>,
pub elevation: Option<f64>,
pub shape: Option<ShapeBorderRef>,
pub behavior: Option<SnackBarBehavior>,
pub width: Option<f64>,
pub inset_padding: Option<EdgeInsets>,
pub show_close_icon: Option<bool>,
pub close_icon_color: Option<AnyColor>,
pub action_overflow_threshold: Option<f64>,
pub action_background_color: Option<AnyColor>,
pub disabled_action_background_color: Option<Color>,
pub dismiss_direction: Option<DismissDirection>,
}
crate::theme_data_getters::theme_data_getters! {
pub trait SnackBarThemeDataGetters for SnackBarThemeData, fields: snack_bar_theme_data {
background_color: Option<Color>,
action_text_color: Option<AnyColor>,
disabled_action_text_color: Option<Color>,
content_text_style: Option<TextStyle>,
elevation: Option<f64>,
shape: Option<ShapeBorderRef>,
behavior: Option<SnackBarBehavior>,
width: Option<f64>,
inset_padding: Option<EdgeInsets>,
show_close_icon: Option<bool>,
close_icon_color: Option<AnyColor>,
action_overflow_threshold: Option<f64>,
action_background_color: Option<AnyColor>,
disabled_action_background_color: Option<Color>,
dismiss_direction: Option<DismissDirection>,
}
}
impl SnackBarThemeData {
pub fn new() -> SnackBarThemeData {
SnackBarThemeData::default()
}
pub fn background_color(mut self, background_color: Color) -> SnackBarThemeData {
self.background_color = Some(background_color);
self
}
pub fn action_text_color(
mut self,
action_text_color: impl Into<AnyColor>,
) -> SnackBarThemeData {
self.action_text_color = Some(action_text_color.into());
self
}
pub fn disabled_action_text_color(
mut self,
disabled_action_text_color: Color,
) -> SnackBarThemeData {
self.disabled_action_text_color = Some(disabled_action_text_color);
self
}
pub fn content_text_style(mut self, content_text_style: TextStyle) -> SnackBarThemeData {
self.content_text_style = Some(content_text_style);
self
}
pub fn elevation(mut self, elevation: f64) -> SnackBarThemeData {
debug_assert!(elevation >= 0.0);
self.elevation = Some(elevation);
self
}
pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> SnackBarThemeData {
self.shape = Some(shape.into());
self
}
pub fn behavior(mut self, behavior: SnackBarBehavior) -> SnackBarThemeData {
debug_assert!(
self.width.is_none() || behavior == SnackBarBehavior::Floating,
"Width can only be set if behaviour is SnackBarBehavior.floating"
);
self.behavior = Some(behavior);
self
}
pub fn width(mut self, width: f64) -> SnackBarThemeData {
debug_assert!(
self.behavior
.is_none_or(|behavior| behavior == SnackBarBehavior::Floating),
"Width can only be set if behaviour is SnackBarBehavior.floating"
);
self.width = Some(width);
self
}
pub fn inset_padding(mut self, inset_padding: EdgeInsets) -> SnackBarThemeData {
self.inset_padding = Some(inset_padding);
self
}
pub fn show_close_icon(mut self, show_close_icon: bool) -> SnackBarThemeData {
self.show_close_icon = Some(show_close_icon);
self
}
pub fn close_icon_color(mut self, close_icon_color: impl Into<AnyColor>) -> SnackBarThemeData {
self.close_icon_color = Some(close_icon_color.into());
self
}
pub fn action_overflow_threshold(
mut self,
action_overflow_threshold: f64,
) -> SnackBarThemeData {
debug_assert!(
(0.0..=1.0).contains(&action_overflow_threshold),
"Action overflow threshold must be between 0 and 1 inclusive"
);
self.action_overflow_threshold = Some(action_overflow_threshold);
self
}
pub fn action_background_color(
mut self,
action_background_color: impl Into<AnyColor>,
) -> SnackBarThemeData {
let action_background_color = action_background_color.into();
debug_assert!(
!is_widget_state_color(&action_background_color)
|| self.disabled_action_background_color.is_none(),
"disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
);
self.action_background_color = Some(action_background_color);
self
}
pub fn disabled_action_background_color(
mut self,
disabled_action_background_color: Color,
) -> SnackBarThemeData {
debug_assert!(
!self
.action_background_color
.as_ref()
.is_some_and(is_widget_state_color),
"disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
);
self.disabled_action_background_color = Some(disabled_action_background_color);
self
}
pub fn dismiss_direction(mut self, dismiss_direction: DismissDirection) -> SnackBarThemeData {
self.dismiss_direction = Some(dismiss_direction);
self
}
pub fn copy_with(&self) -> SnackBarThemeData {
self.clone()
}
pub fn lerp(
a: Option<&SnackBarThemeData>,
b: Option<&SnackBarThemeData>,
t: f64,
) -> SnackBarThemeData {
if let (Some(a), Some(b)) = (a, b)
&& std::ptr::eq(a, b)
{
return a.clone();
}
let lerped = SnackBarThemeData {
background_color: Color::lerp(
a.and_then(|a| a.background_color),
b.and_then(|b| b.background_color),
t,
),
action_text_color: AnyColor::lerp(
a.and_then(|a| a.action_text_color.as_ref()),
b.and_then(|b| b.action_text_color.as_ref()),
t,
)
.map(AnyColor::from),
disabled_action_text_color: Color::lerp(
a.and_then(|a| a.disabled_action_text_color),
b.and_then(|b| b.disabled_action_text_color),
t,
),
content_text_style: TextStyle::lerp(
a.and_then(|a| a.content_text_style.as_ref()),
b.and_then(|b| b.content_text_style.as_ref()),
t,
),
elevation: lerp_double(a.and_then(|a| a.elevation), b.and_then(|b| b.elevation), t),
shape: ShapeBorderRef::lerp(
a.and_then(|a| a.shape.as_ref()),
b.and_then(|b| b.shape.as_ref()),
t,
),
behavior: if t < 0.5 {
a.and_then(|a| a.behavior)
} else {
b.and_then(|b| b.behavior)
},
width: lerp_double(a.and_then(|a| a.width), b.and_then(|b| b.width), t),
inset_padding: EdgeInsets::lerp(
a.and_then(|a| a.inset_padding),
b.and_then(|b| b.inset_padding),
t,
),
show_close_icon: if t < 0.5 {
a.and_then(|a| a.show_close_icon)
} else {
b.and_then(|b| b.show_close_icon)
},
close_icon_color: AnyColor::lerp(
a.and_then(|a| a.close_icon_color.as_ref()),
b.and_then(|b| b.close_icon_color.as_ref()),
t,
)
.map(AnyColor::from),
action_overflow_threshold: lerp_double(
a.and_then(|a| a.action_overflow_threshold),
b.and_then(|b| b.action_overflow_threshold),
t,
),
action_background_color: AnyColor::lerp(
a.and_then(|a| a.action_background_color.as_ref()),
b.and_then(|b| b.action_background_color.as_ref()),
t,
)
.map(AnyColor::from),
disabled_action_background_color: Color::lerp(
a.and_then(|a| a.disabled_action_background_color),
b.and_then(|b| b.disabled_action_background_color),
t,
),
dismiss_direction: if t < 0.5 {
a.and_then(|a| a.dismiss_direction)
} else {
b.and_then(|b| b.dismiss_direction)
},
};
lerped.debug_assert_valid();
lerped
}
}
impl SnackBarThemeData {
fn debug_assert_valid(&self) {
debug_assert!(self.elevation.is_none_or(|elevation| elevation >= 0.0));
debug_assert!(
self.width.is_none() || self.behavior == Some(SnackBarBehavior::Floating),
"Width can only be set if behaviour is SnackBarBehavior.floating"
);
debug_assert!(
self.action_overflow_threshold
.is_none_or(|threshold| (0.0..=1.0).contains(&threshold)),
"Action overflow threshold must be between 0 and 1 inclusive"
);
debug_assert!(
!self
.action_background_color
.as_ref()
.is_some_and(is_widget_state_color)
|| self.disabled_action_background_color.is_none(),
"disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
);
}
}
fn is_widget_state_color(color: &AnyColor) -> bool {
color.extension::<WidgetStateColor>().is_some()
}
#[derive(Debug)]
pub struct SnackBarTheme {
pub key: Option<KeyRef>,
pub data: SnackBarThemeData,
pub child: WidgetRef,
}
impl SnackBarTheme {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(data: SnackBarThemeData, child: impl IntoWidget<K>) -> SnackBarTheme {
SnackBarTheme {
key: None,
data,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> SnackBarTheme {
self.key = Some(key);
self
}
pub fn of(app: &mut App, context: BuildContext) -> SnackBarThemeData {
match context.depend_on_inherited_widget_of_exact_type::<SnackBarTheme>(app) {
Some(snack_bar_theme) => snack_bar_theme.data.clone(),
None => Theme::of(app, context).snack_bar_theme.clone(),
}
}
}
impl InheritedTheme for SnackBarTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
SnackBarTheme::new(self.data.clone(), child).into_widget()
}
}
impl InheritedWidget for SnackBarTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &SnackBarTheme) -> bool {
self.data != old_widget.data
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
impl waft_foundation::Diagnosticable for SnackBarThemeData {
fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
properties
.add(ColorProperty::new("backgroundColor", self.background_color).default_value(None));
properties.add(
DiagnosticsProperty::<AnyColor>::new("actionTextColor", self.action_text_color.clone())
.default_value(None),
);
properties.add(
ColorProperty::new("disabledActionTextColor", self.disabled_action_text_color)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new(
"contentTextStyle",
self.content_text_style.clone(),
)
.default_value(None),
);
properties.add(DoubleProperty::new("elevation", self.elevation).default_value(None));
properties.add(
DiagnosticsProperty::<ShapeBorderRef>::new("shape", self.shape.clone())
.default_value(None),
);
properties.add(
DiagnosticsProperty::<waft_foundation::DartEnum<SnackBarBehavior>>::new(
"behavior",
self.behavior.map(waft_foundation::DartEnum),
)
.default_value(None),
);
properties.add(DoubleProperty::new("width", self.width).default_value(None));
properties.add(
DiagnosticsProperty::<EdgeInsetsGeometry>::new(
"insetPadding",
self.inset_padding.map(EdgeInsetsGeometry::from),
)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<bool>::new("showCloseIcon", self.show_close_icon)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<AnyColor>::new("closeIconColor", self.close_icon_color.clone())
.default_value(None),
);
properties.add(
DoubleProperty::new("actionOverflowThreshold", self.action_overflow_threshold)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<AnyColor>::new(
"actionBackgroundColor",
self.action_background_color.clone(),
)
.default_value(None),
);
properties.add(
ColorProperty::new(
"disabledActionBackgroundColor",
self.disabled_action_background_color,
)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<waft_foundation::DartEnum<DismissDirection>>::new(
"dismissDirection",
self.dismiss_direction.map(waft_foundation::DartEnum),
)
.default_value(None),
);
}
}
impl std::fmt::Display for SnackBarThemeData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&waft_foundation::Diagnosticable::to_string(self))
}
}
#[cfg(test)]
mod tests {
use waft_painting::ShapeBorder;
use waft_painting::{BorderRadiusGeometry, BorderSide, RoundedRectangleBorder};
use waft_widgets::{SizedBox, WidgetState};
use waft_widgets::testing::test_widgets;
use super::*;
use crate::colors::Colors;
use crate::drawer_theme::tests::probe;
use crate::drawer_theme::tests::theme_seen_through_capture;
use crate::theme_data::ThemeData;
fn rounded(radius: f64) -> RoundedRectangleBorder {
RoundedRectangleBorder::new(BorderSide::NONE, BorderRadiusGeometry::circular(radius))
}
fn widget_state_color() -> AnyColor {
WidgetStateColor::resolve_with(|states| {
if states.contains(&WidgetState::Disabled) {
Colors::BLUE.primary()
} else {
Colors::PURPLE.primary()
}
})
.into_any()
}
#[test]
fn snack_bar_theme_data_copy_with_equality_basics() {
assert_eq!(
SnackBarThemeData::new(),
SnackBarThemeData::new().copy_with()
);
let theme = SnackBarThemeData::new()
.elevation(4.0)
.shape(rounded(8.0))
.behavior(SnackBarBehavior::Floating)
.width(300.0);
assert_eq!(theme.copy_with(), theme);
assert_eq!(
theme
.copy_with()
.dismiss_direction(DismissDirection::Up)
.elevation,
Some(4.0)
);
assert_eq!(
theme
.copy_with()
.dismiss_direction(DismissDirection::Up)
.dismiss_direction,
Some(DismissDirection::Up)
);
assert_ne!(theme.copy_with().elevation(5.0), theme);
assert_ne!(theme.copy_with().shape(rounded(9.0)), theme);
}
#[test]
fn snack_bar_theme_data_lerp_special_cases() {
assert_eq!(
SnackBarThemeData::lerp(None, None, 0.0),
SnackBarThemeData::new()
);
let data = SnackBarThemeData::new();
assert_eq!(SnackBarThemeData::lerp(Some(&data), Some(&data), 0.5), data);
}
#[test]
fn snack_bar_theme_data_lerp_keeps_show_close_icon() {
let a = SnackBarThemeData::new().show_close_icon(true);
let b = SnackBarThemeData::new().show_close_icon(false);
assert_eq!(
SnackBarThemeData::lerp(Some(&a), Some(&b), 0.25).show_close_icon,
Some(true)
);
assert_eq!(
SnackBarThemeData::lerp(Some(&a), Some(&b), 0.75).show_close_icon,
Some(false)
);
assert_eq!(
SnackBarThemeData::lerp(Some(&a), None, 0.25).show_close_icon,
Some(true)
);
assert_eq!(
SnackBarThemeData::lerp(None, Some(&a), 0.75).show_close_icon,
Some(true)
);
}
#[test]
fn snack_bar_theme_data_null_fields_by_default() {
let snack_bar_theme = SnackBarThemeData::new();
assert_eq!(snack_bar_theme.background_color, None);
assert_eq!(snack_bar_theme.action_text_color, None);
assert_eq!(snack_bar_theme.disabled_action_text_color, None);
assert_eq!(snack_bar_theme.content_text_style, None);
assert_eq!(snack_bar_theme.elevation, None);
assert!(snack_bar_theme.shape.is_none());
assert_eq!(snack_bar_theme.behavior, None);
assert_eq!(snack_bar_theme.width, None);
assert_eq!(snack_bar_theme.inset_padding, None);
assert_eq!(snack_bar_theme.show_close_icon, None);
assert_eq!(snack_bar_theme.close_icon_color, None);
assert_eq!(snack_bar_theme.action_overflow_threshold, None);
assert_eq!(snack_bar_theme.action_background_color, None);
assert_eq!(snack_bar_theme.disabled_action_background_color, None);
assert_eq!(snack_bar_theme.dismiss_direction, None);
}
#[test]
fn snack_bar_theme_data_lerps_each_field() {
let a = SnackBarThemeData::new()
.background_color(Colors::BLACK)
.action_text_color(Colors::BLACK)
.elevation(2.0)
.shape(rounded(4.0))
.behavior(SnackBarBehavior::Floating)
.width(100.0)
.inset_padding(EdgeInsets::all(10.0))
.action_overflow_threshold(0.2)
.dismiss_direction(DismissDirection::Down);
let b = SnackBarThemeData::new()
.background_color(Colors::WHITE)
.action_text_color(Colors::WHITE)
.close_icon_color(Colors::RED.primary())
.elevation(6.0)
.shape(rounded(8.0))
.behavior(SnackBarBehavior::Floating)
.width(300.0)
.inset_padding(EdgeInsets::all(30.0))
.action_overflow_threshold(0.6)
.dismiss_direction(DismissDirection::Up);
let halfway = SnackBarThemeData::lerp(Some(&a), Some(&b), 0.5);
assert_eq!(
halfway.background_color,
Color::lerp(Some(Colors::BLACK), Some(Colors::WHITE), 0.5)
);
assert_eq!(
halfway.action_text_color.map(|color| color.color()),
Color::lerp(Some(Colors::BLACK), Some(Colors::WHITE), 0.5)
);
assert_eq!(
halfway.close_icon_color.map(|color| color.color()),
Color::lerp(None, Some(Colors::RED.primary()), 0.5)
);
assert_eq!(halfway.elevation, Some(4.0));
assert_eq!(
halfway.shape.as_deref(),
Some(&rounded(6.0) as &dyn ShapeBorder)
);
assert_eq!(halfway.behavior, Some(SnackBarBehavior::Floating));
assert_eq!(halfway.width, Some(200.0));
assert_eq!(halfway.inset_padding, Some(EdgeInsets::all(20.0)));
assert_eq!(halfway.action_overflow_threshold, Some(0.4));
assert_eq!(halfway.dismiss_direction, Some(DismissDirection::Up));
assert_eq!(
SnackBarThemeData::lerp(Some(&a), Some(&b), 0.25).dismiss_direction,
Some(DismissDirection::Down)
);
}
#[test]
#[should_panic(expected = "Width can only be set if behaviour is SnackBarBehavior.floating")]
fn snack_bar_theme_data_asserts_if_width_is_provided_with_fixed_behavior() {
SnackBarThemeData::new()
.behavior(SnackBarBehavior::Fixed)
.width(300.0);
}
#[test]
#[should_panic(expected = "Width can only be set if behaviour is SnackBarBehavior.floating")]
fn snack_bar_theme_data_asserts_if_fixed_behavior_is_provided_with_width() {
SnackBarThemeData::new()
.width(300.0)
.behavior(SnackBarBehavior::Fixed);
}
#[test]
fn snack_bar_theme_data_accepts_width_with_floating_behavior_in_either_order() {
let width_first = SnackBarThemeData::new()
.width(300.0)
.behavior(SnackBarBehavior::Floating);
let behavior_first = SnackBarThemeData::new()
.behavior(SnackBarBehavior::Floating)
.width(300.0);
assert_eq!(width_first, behavior_first);
}
#[test]
fn snack_bar_theme_data_asserts_for_action_overflow_threshold_outside_of_0_to_1() {
for overflow_threshold in [-1.0, -0.0001, 1.000001, 5.0] {
let result = std::panic::catch_unwind(|| {
SnackBarThemeData::new().action_overflow_threshold(overflow_threshold)
});
assert!(result.is_err(), "{overflow_threshold} was accepted");
}
for overflow_threshold in [0.0, 0.5, 1.0] {
assert_eq!(
SnackBarThemeData::new()
.action_overflow_threshold(overflow_threshold)
.action_overflow_threshold,
Some(overflow_threshold)
);
}
}
#[test]
#[should_panic]
fn snack_bar_theme_data_asserts_for_a_negative_elevation() {
SnackBarThemeData::new().elevation(-1.0);
}
#[test]
#[should_panic(
expected = "disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
)]
fn snack_bar_theme_data_asserts_when_action_background_color_is_a_widget_state_color_and_disabled_action_background_color_is_also_provided()
{
SnackBarThemeData::new()
.action_background_color(widget_state_color())
.disabled_action_background_color(Colors::AMBER.primary());
}
#[test]
#[should_panic(
expected = "disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
)]
fn snack_bar_theme_data_asserts_when_disabled_action_background_color_is_provided_before_a_widget_state_color()
{
SnackBarThemeData::new()
.disabled_action_background_color(Colors::AMBER.primary())
.action_background_color(widget_state_color());
}
#[test]
fn snack_bar_theme_data_keeps_a_widget_state_color_as_the_action_background_color() {
let theme = SnackBarThemeData::new().action_background_color(widget_state_color());
let color = theme.action_background_color.expect("a color");
assert!(color.extension::<WidgetStateColor>().is_some());
assert_eq!(color.color(), Colors::PURPLE.primary());
}
#[test]
fn snack_bar_theme_data_accepts_a_plain_action_background_color_with_a_disabled_one() {
let theme = SnackBarThemeData::new()
.action_background_color(Colors::PURPLE.primary())
.disabled_action_background_color(Colors::AMBER.primary());
assert_eq!(
theme.disabled_action_background_color,
Some(Colors::AMBER.primary())
);
}
#[test]
fn of_returns_the_theme_data_snack_bar_theme_without_an_ancestor() {
test_widgets(|tester| {
let expected = SnackBarThemeData::new()
.elevation(3.0)
.background_color(Colors::RED.primary());
let theme_data_snack_bar = expected.clone();
let seen = probe(
tester,
move |app, child| {
let data = ThemeData::builder()
.snack_bar_theme(theme_data_snack_bar.clone())
.build(app);
Theme::new(data, child).into_widget()
},
SnackBarTheme::of,
);
assert_eq!(seen, expected);
});
}
#[test]
fn of_returns_the_closest_snack_bar_theme_over_theme_data() {
test_widgets(|tester| {
let widget_snack_bar = SnackBarThemeData::new().elevation(7.0);
let expected = widget_snack_bar.clone();
let seen = probe(
tester,
move |app, child| {
let data = ThemeData::builder()
.snack_bar_theme(SnackBarThemeData::new().elevation(3.0))
.build(app);
Theme::new(data, SnackBarTheme::new(widget_snack_bar.clone(), child))
.into_widget()
},
SnackBarTheme::of,
);
assert_eq!(seen, expected);
});
}
#[test]
fn a_captured_snack_bar_theme_is_kept_under_another_snack_bar_theme() {
test_widgets(|tester| {
let captured = SnackBarThemeData::new().elevation(1.0);
let shadowing = SnackBarThemeData::new().elevation(2.0);
let seen = theme_seen_through_capture(
tester,
|probe| SnackBarTheme::new(captured.clone(), probe).into_widget(),
|wrapped| SnackBarTheme::new(shadowing.clone(), wrapped).into_widget(),
SnackBarTheme::of,
);
assert_eq!(seen.elevation, Some(1.0));
});
}
#[test]
fn update_should_notify_only_when_the_data_differs() {
let theme = |data: SnackBarThemeData| SnackBarTheme::new(data, SizedBox::shrink());
let old = theme(SnackBarThemeData::new().elevation(1.0));
assert!(!theme(SnackBarThemeData::new().elevation(1.0)).update_should_notify(&old));
assert!(theme(SnackBarThemeData::new().elevation(2.0)).update_should_notify(&old));
}
#[test]
fn theme_data_holds_the_snack_bar_theme() {
test_widgets(|tester| {
let app = tester.cell.borrow();
let theme = ThemeData::new(&app);
assert_eq!(theme.snack_bar_theme, SnackBarThemeData::new());
let raised = ThemeData::builder()
.snack_bar_theme(SnackBarThemeData::new().elevation(8.0))
.build(&app);
assert_eq!(raised.snack_bar_theme.elevation, Some(8.0));
assert_ne!(theme, raised);
assert_eq!(
theme
.copy_with()
.snack_bar_theme(raised.snack_bar_theme.clone())
.build(),
raised
);
let flat = ThemeData::builder()
.snack_bar_theme(SnackBarThemeData::new().elevation(0.0))
.build(&app);
assert_eq!(
ThemeData::lerp(&flat, &raised, 0.5)
.snack_bar_theme
.elevation,
Some(4.0)
);
});
}
#[test]
fn a_snack_bar_theme_data_describes_nothing_by_default() {
assert_eq!(
waft_widgets::testing::describe_properties(|properties| {
waft_foundation::Diagnosticable::debug_fill_properties(
&SnackBarThemeData::new(),
properties,
)
}),
Vec::<String>::new()
);
}
#[test]
fn an_assigned_snack_bar_theme_describes_every_property() {
let theme = SnackBarThemeData::new()
.background_color(Color::new(0xffffffff))
.action_text_color(Color::new(0xff0000aa))
.disabled_action_text_color(Color::new(0xff00aa00))
.content_text_style(TextStyle::new().color(Color::new(0xff123456)))
.elevation(2.0)
.shape(RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::circular(2.0),
))
.behavior(SnackBarBehavior::Floating)
.width(400.0)
.inset_padding(EdgeInsets::all(10.0))
.show_close_icon(false)
.close_icon_color(Color::new(0xff0000aa))
.action_overflow_threshold(0.5)
.dismiss_direction(DismissDirection::Down);
let description = waft_widgets::testing::describe_properties(|properties| {
waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
});
let description: Vec<String> = description
.iter()
.map(|line| waft_rendering::testing::ignoring_hash_codes(line))
.collect();
assert_eq!(
description,
[
format!("backgroundColor: {}", Color::new(0xffffffff)),
format!("actionTextColor: {}", Color::new(0xff0000aa)),
format!("disabledActionTextColor: {}", Color::new(0xff00aa00)),
format!(
"contentTextStyle: {}",
TextStyle::new().color(Color::new(0xff123456))
),
"elevation: 2.0".to_string(),
format!(
"shape: {}",
ShapeBorderRef::from(RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::circular(2.0)
))
),
"behavior: SnackBarBehavior.floating".to_string(),
"width: 400.0".to_string(),
"insetPadding: EdgeInsets.all(10.0)".to_string(),
"showCloseIcon: false".to_string(),
format!("closeIconColor: {}", Color::new(0xff0000aa)),
"actionOverflowThreshold: 0.5".to_string(),
"dismissDirection: DismissDirection.down".to_string(),
]
);
}
}