use waft_embedder::{Clip, Color, lerp_double};
use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
use waft_painting::{ColorProperty, ShapeBorderRef};
use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
use crate::theme::Theme;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DrawerThemeData {
pub background_color: Option<Color>,
pub scrim_color: Option<Color>,
pub elevation: Option<f64>,
pub shadow_color: Option<Color>,
pub surface_tint_color: Option<Color>,
pub shape: Option<ShapeBorderRef>,
pub end_shape: Option<ShapeBorderRef>,
pub width: Option<f64>,
pub clip_behavior: Option<Clip>,
}
crate::theme_data_getters::theme_data_getters! {
pub trait DrawerThemeDataGetters for DrawerThemeData, fields: drawer_theme_data {
background_color: Option<Color>,
scrim_color: Option<Color>,
elevation: Option<f64>,
shadow_color: Option<Color>,
surface_tint_color: Option<Color>,
shape: Option<ShapeBorderRef>,
end_shape: Option<ShapeBorderRef>,
width: Option<f64>,
clip_behavior: Option<Clip>,
}
}
impl DrawerThemeData {
pub fn new() -> DrawerThemeData {
DrawerThemeData::default()
}
pub fn background_color(mut self, background_color: Color) -> DrawerThemeData {
self.background_color = Some(background_color);
self
}
pub fn scrim_color(mut self, scrim_color: Color) -> DrawerThemeData {
self.scrim_color = Some(scrim_color);
self
}
pub fn elevation(mut self, elevation: f64) -> DrawerThemeData {
self.elevation = Some(elevation);
self
}
pub fn shadow_color(mut self, shadow_color: Color) -> DrawerThemeData {
self.shadow_color = Some(shadow_color);
self
}
pub fn surface_tint_color(mut self, surface_tint_color: Color) -> DrawerThemeData {
self.surface_tint_color = Some(surface_tint_color);
self
}
pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> DrawerThemeData {
self.shape = Some(shape.into());
self
}
pub fn end_shape(mut self, end_shape: impl Into<ShapeBorderRef>) -> DrawerThemeData {
self.end_shape = Some(end_shape.into());
self
}
pub fn width(mut self, width: f64) -> DrawerThemeData {
self.width = Some(width);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> DrawerThemeData {
self.clip_behavior = Some(clip_behavior);
self
}
pub fn copy_with(&self) -> DrawerThemeData {
self.clone()
}
pub fn lerp(
a: Option<&DrawerThemeData>,
b: Option<&DrawerThemeData>,
t: f64,
) -> Option<DrawerThemeData> {
match (a, b) {
(None, None) => return None,
(Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
_ => {}
}
Some(DrawerThemeData {
background_color: Color::lerp(
a.and_then(|a| a.background_color),
b.and_then(|b| b.background_color),
t,
),
scrim_color: Color::lerp(
a.and_then(|a| a.scrim_color),
b.and_then(|b| b.scrim_color),
t,
),
elevation: lerp_double(a.and_then(|a| a.elevation), b.and_then(|b| b.elevation), t),
shadow_color: Color::lerp(
a.and_then(|a| a.shadow_color),
b.and_then(|b| b.shadow_color),
t,
),
surface_tint_color: Color::lerp(
a.and_then(|a| a.surface_tint_color),
b.and_then(|b| b.surface_tint_color),
t,
),
shape: ShapeBorderRef::lerp(
a.and_then(|a| a.shape.as_ref()),
b.and_then(|b| b.shape.as_ref()),
t,
),
end_shape: ShapeBorderRef::lerp(
a.and_then(|a| a.end_shape.as_ref()),
b.and_then(|b| b.end_shape.as_ref()),
t,
),
width: lerp_double(a.and_then(|a| a.width), b.and_then(|b| b.width), t),
clip_behavior: if t < 0.5 {
a.and_then(|a| a.clip_behavior)
} else {
b.and_then(|b| b.clip_behavior)
},
})
}
}
#[derive(Debug)]
pub struct DrawerTheme {
pub key: Option<KeyRef>,
pub data: DrawerThemeData,
pub child: WidgetRef,
}
impl DrawerTheme {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(data: DrawerThemeData, child: impl IntoWidget<K>) -> DrawerTheme {
DrawerTheme {
key: None,
data,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> DrawerTheme {
self.key = Some(key);
self
}
pub fn of(app: &mut App, context: BuildContext) -> DrawerThemeData {
match context.depend_on_inherited_widget_of_exact_type::<DrawerTheme>(app) {
Some(drawer_theme) => drawer_theme.data.clone(),
None => Theme::of(app, context).drawer_theme.clone(),
}
}
}
impl InheritedTheme for DrawerTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
DrawerTheme::new(self.data.clone(), child).into_widget()
}
}
impl InheritedWidget for DrawerTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &DrawerTheme) -> bool {
self.data != old_widget.data
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
impl waft_foundation::Diagnosticable for DrawerThemeData {
fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
properties
.add(ColorProperty::new("backgroundColor", self.background_color).default_value(None));
properties.add(ColorProperty::new("scrimColor", self.scrim_color).default_value(None));
properties.add(DoubleProperty::new("elevation", self.elevation).default_value(None));
properties.add(ColorProperty::new("shadowColor", self.shadow_color).default_value(None));
properties.add(
ColorProperty::new("surfaceTintColor", self.surface_tint_color).default_value(None),
);
properties.add(
DiagnosticsProperty::<ShapeBorderRef>::new("shape", self.shape.clone())
.default_value(None),
);
properties.add(
DiagnosticsProperty::<ShapeBorderRef>::new("endShape", self.end_shape.clone())
.default_value(None),
);
properties.add(DoubleProperty::new("width", self.width).default_value(None));
properties.add(
DiagnosticsProperty::<waft_foundation::DartEnum<Clip>>::new(
"clipBehavior",
self.clip_behavior.map(waft_foundation::DartEnum),
)
.default_value(None),
);
}
}
impl std::fmt::Display for DrawerThemeData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&waft_foundation::Diagnosticable::to_string(self))
}
}
#[cfg(test)]
pub(crate) mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use waft_painting::ShapeBorder;
use waft_painting::{BorderRadiusGeometry, BorderSide, RoundedRectangleBorder};
use waft_widgets::{Builder, SizedBox};
use waft_widgets::testing::{WidgetTester, test_widgets};
use super::*;
use crate::colors::Colors;
use crate::theme_data::ThemeData;
pub(crate) fn probe<T: 'static>(
tester: &mut WidgetTester,
wrap: impl FnOnce(&App, WidgetRef) -> WidgetRef,
read: impl Fn(&mut App, BuildContext) -> T + 'static,
) -> T {
let seen = Rc::new(RefCell::new(None));
let probe = Builder::new({
let seen = Rc::clone(&seen);
move |app, context| {
*seen.borrow_mut() = Some(read(app, context));
SizedBox::shrink().into_widget()
}
})
.into_widget();
let root = wrap(&tester.cell.borrow(), probe);
tester.pump_widget(root);
let value = seen.borrow_mut().take();
value.expect("the probe built")
}
pub(crate) fn theme_seen_through_capture<T: 'static>(
tester: &mut WidgetTester,
captured_under: impl FnOnce(WidgetRef) -> WidgetRef,
shadowed_by: impl FnOnce(WidgetRef) -> WidgetRef,
read: impl Fn(&mut App, BuildContext) -> T + 'static,
) -> T {
let captured = Rc::new(RefCell::new(None));
let capturing_probe = Builder::new({
let captured = Rc::clone(&captured);
move |app, context| {
*captured.borrow_mut() = Some(<dyn waft_widgets::InheritedTheme>::capture(
app, context, None,
));
SizedBox::shrink().into_widget()
}
})
.into_widget();
tester.pump_widget(captured_under(capturing_probe));
let captured = captured.borrow_mut().take().expect("the probe built");
probe(
tester,
|_, reading_probe| shadowed_by(captured.wrap(reading_probe)),
read,
)
}
fn rounded(radius: f64) -> RoundedRectangleBorder {
RoundedRectangleBorder::new(BorderSide::NONE, BorderRadiusGeometry::circular(radius))
}
#[test]
fn copy_with_equality_basics() {
assert_eq!(DrawerThemeData::new(), DrawerThemeData::new().copy_with());
let theme = DrawerThemeData::new().elevation(4.0).shape(rounded(8.0));
assert_eq!(theme.copy_with(), theme);
assert_eq!(theme.copy_with().width(300.0).width, Some(300.0));
assert_eq!(theme.copy_with().width(300.0).elevation, Some(4.0));
assert_ne!(theme.copy_with().elevation(5.0), theme);
assert_ne!(theme.copy_with().shape(rounded(9.0)), theme);
}
#[test]
fn drawer_theme_data_lerp_special_cases() {
assert_eq!(DrawerThemeData::lerp(None, None, 0.0), None);
let data = DrawerThemeData::new();
assert_eq!(
DrawerThemeData::lerp(Some(&data), Some(&data), 0.5),
Some(data)
);
}
#[test]
fn drawer_theme_data_null_fields_by_default() {
let theme = DrawerThemeData::new();
assert_eq!(theme.background_color, None);
assert_eq!(theme.scrim_color, None);
assert_eq!(theme.elevation, None);
assert_eq!(theme.shadow_color, None);
assert_eq!(theme.surface_tint_color, None);
assert!(theme.shape.is_none());
assert!(theme.end_shape.is_none());
assert_eq!(theme.width, None);
assert_eq!(theme.clip_behavior, None);
}
#[test]
fn drawer_theme_data_lerps_each_field() {
let a = DrawerThemeData::new()
.background_color(Colors::BLACK)
.elevation(2.0)
.shape(rounded(4.0))
.width(100.0)
.clip_behavior(Clip::HardEdge);
let b = DrawerThemeData::new()
.background_color(Colors::WHITE)
.scrim_color(Colors::RED.primary())
.elevation(6.0)
.shape(rounded(8.0))
.end_shape(rounded(8.0))
.width(300.0)
.clip_behavior(Clip::AntiAlias);
let halfway = DrawerThemeData::lerp(Some(&a), Some(&b), 0.5).expect("both given");
assert_eq!(
halfway.background_color,
Color::lerp(Some(Colors::BLACK), Some(Colors::WHITE), 0.5)
);
assert_eq!(
halfway.scrim_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.end_shape.as_deref(),
ShapeBorderRef::lerp(None, Some(&ShapeBorderRef::new(rounded(8.0))), 0.5).as_deref()
);
assert_eq!(halfway.width, Some(200.0));
assert_eq!(halfway.clip_behavior, Some(Clip::AntiAlias));
assert_eq!(
DrawerThemeData::lerp(Some(&a), Some(&b), 0.25)
.expect("both given")
.clip_behavior,
Some(Clip::HardEdge)
);
}
#[test]
fn of_returns_the_theme_data_drawer_theme_without_an_ancestor() {
test_widgets(|tester| {
let expected = DrawerThemeData::new().width(280.0).elevation(3.0);
let theme_data_drawer = expected.clone();
let seen = probe(
tester,
move |app, child| {
let data = ThemeData::builder()
.drawer_theme(theme_data_drawer.clone())
.build(app);
crate::theme::Theme::new(data, child).into_widget()
},
DrawerTheme::of,
);
assert_eq!(seen, expected);
});
}
#[test]
fn of_returns_the_closest_drawer_theme_over_theme_data() {
test_widgets(|tester| {
let widget_drawer = DrawerThemeData::new().width(100.0);
let expected = widget_drawer.clone();
let seen = probe(
tester,
move |app, child| {
let data = ThemeData::builder()
.drawer_theme(DrawerThemeData::new().width(280.0))
.build(app);
crate::theme::Theme::new(data, DrawerTheme::new(widget_drawer.clone(), child))
.into_widget()
},
DrawerTheme::of,
);
assert_eq!(seen, expected);
});
}
#[test]
fn a_captured_drawer_theme_is_kept_under_another_drawer_theme() {
test_widgets(|tester| {
let captured = DrawerThemeData::new().width(111.0);
let shadowing = DrawerThemeData::new().width(222.0);
let seen = theme_seen_through_capture(
tester,
|probe| DrawerTheme::new(captured.clone(), probe).into_widget(),
|wrapped| DrawerTheme::new(shadowing.clone(), wrapped).into_widget(),
DrawerTheme::of,
);
assert_eq!(seen.width, Some(111.0));
});
}
#[test]
fn update_should_notify_only_when_the_data_differs() {
let theme = |data: DrawerThemeData| DrawerTheme::new(data, SizedBox::shrink());
let old = theme(DrawerThemeData::new().width(1.0));
assert!(!theme(DrawerThemeData::new().width(1.0)).update_should_notify(&old));
assert!(theme(DrawerThemeData::new().width(2.0)).update_should_notify(&old));
}
#[test]
fn theme_data_holds_the_drawer_theme() {
test_widgets(|tester| {
let app = tester.cell.borrow();
let theme = ThemeData::new(&app);
assert_eq!(theme.drawer_theme, DrawerThemeData::new());
let wide = ThemeData::builder()
.drawer_theme(DrawerThemeData::new().width(300.0))
.build(&app);
assert_eq!(wide.drawer_theme.width, Some(300.0));
assert_ne!(theme, wide);
assert_eq!(
theme
.copy_with()
.drawer_theme(wide.drawer_theme.clone())
.build(),
wide
);
let narrow = ThemeData::builder()
.drawer_theme(DrawerThemeData::new().width(100.0))
.build(&app);
assert_eq!(
ThemeData::lerp(&narrow, &wide, 0.5).drawer_theme.width,
Some(200.0)
);
});
}
#[test]
fn a_drawer_theme_data_describes_nothing_by_default() {
assert_eq!(
waft_widgets::testing::describe_properties(|properties| {
waft_foundation::Diagnosticable::debug_fill_properties(
&DrawerThemeData::new(),
properties,
)
}),
Vec::<String>::new()
);
}
#[test]
fn an_assigned_drawer_theme_describes_every_property() {
let theme = DrawerThemeData::new()
.background_color(Color::new(0x00000099))
.scrim_color(Color::new(0x00000098))
.elevation(5.0)
.shadow_color(Color::new(0x00000097))
.surface_tint_color(Color::new(0x00000096))
.shape(RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::circular(2.0),
))
.width(200.0)
.clip_behavior(Clip::HardEdge);
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(0x00000099)),
format!("scrimColor: {}", Color::new(0x00000098)),
"elevation: 5.0".to_string(),
format!("shadowColor: {}", Color::new(0x00000097)),
format!("surfaceTintColor: {}", Color::new(0x00000096)),
format!(
"shape: {}",
waft_painting::ShapeBorderRef::from(RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::circular(2.0)
))
),
"width: 200.0".to_string(),
"clipBehavior: Clip.hardEdge".to_string(),
]
);
}
}