use waft_embedder::{Color, Size, lerp_double};
use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
use waft_painting::{ColorProperty, ShapeBorderRef, TextStyle};
use waft_widgets::{
BuildContext, IconThemeData, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
WidgetStateProperty, WidgetStatePropertyRef,
};
use crate::theme::Theme;
#[derive(Clone, Debug, Default)]
pub struct NavigationDrawerThemeData {
pub tile_height: Option<f64>,
pub background_color: Option<Color>,
pub elevation: Option<f64>,
pub shadow_color: Option<Color>,
pub surface_tint_color: Option<Color>,
pub indicator_color: Option<Color>,
pub indicator_shape: Option<ShapeBorderRef>,
pub indicator_size: Option<Size>,
pub label_text_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
pub icon_theme: Option<WidgetStatePropertyRef<Option<IconThemeData>>>,
}
crate::theme_data_getters::theme_data_getters! {
pub trait NavigationDrawerThemeDataGetters for NavigationDrawerThemeData, fields: navigation_drawer_theme_data {
tile_height: Option<f64>,
background_color: Option<Color>,
elevation: Option<f64>,
shadow_color: Option<Color>,
surface_tint_color: Option<Color>,
indicator_color: Option<Color>,
indicator_shape: Option<ShapeBorderRef>,
indicator_size: Option<Size>,
label_text_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
icon_theme: Option<WidgetStatePropertyRef<Option<IconThemeData>>>,
}
}
impl NavigationDrawerThemeData {
pub fn new() -> NavigationDrawerThemeData {
NavigationDrawerThemeData::default()
}
pub fn tile_height(mut self, tile_height: f64) -> NavigationDrawerThemeData {
self.tile_height = Some(tile_height);
self
}
pub fn background_color(mut self, background_color: Color) -> NavigationDrawerThemeData {
self.background_color = Some(background_color);
self
}
pub fn elevation(mut self, elevation: f64) -> NavigationDrawerThemeData {
self.elevation = Some(elevation);
self
}
pub fn shadow_color(mut self, shadow_color: Color) -> NavigationDrawerThemeData {
self.shadow_color = Some(shadow_color);
self
}
pub fn surface_tint_color(mut self, surface_tint_color: Color) -> NavigationDrawerThemeData {
self.surface_tint_color = Some(surface_tint_color);
self
}
pub fn indicator_color(mut self, indicator_color: Color) -> NavigationDrawerThemeData {
self.indicator_color = Some(indicator_color);
self
}
pub fn indicator_shape(
mut self,
indicator_shape: impl Into<ShapeBorderRef>,
) -> NavigationDrawerThemeData {
self.indicator_shape = Some(indicator_shape.into());
self
}
pub fn indicator_size(mut self, indicator_size: Size) -> NavigationDrawerThemeData {
self.indicator_size = Some(indicator_size);
self
}
pub fn label_text_style(
mut self,
label_text_style: WidgetStatePropertyRef<Option<TextStyle>>,
) -> NavigationDrawerThemeData {
self.label_text_style = Some(label_text_style);
self
}
pub fn icon_theme(
mut self,
icon_theme: WidgetStatePropertyRef<Option<IconThemeData>>,
) -> NavigationDrawerThemeData {
self.icon_theme = Some(icon_theme);
self
}
pub fn copy_with(&self) -> NavigationDrawerThemeData {
self.clone()
}
pub fn lerp(
a: Option<&NavigationDrawerThemeData>,
b: Option<&NavigationDrawerThemeData>,
t: f64,
) -> Option<NavigationDrawerThemeData> {
match (a, b) {
(None, None) => return None,
(Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
_ => {}
}
Some(NavigationDrawerThemeData {
tile_height: lerp_double(
a.and_then(|a| a.tile_height),
b.and_then(|b| b.tile_height),
t,
),
background_color: Color::lerp(
a.and_then(|a| a.background_color),
b.and_then(|b| b.background_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,
),
indicator_color: Color::lerp(
a.and_then(|a| a.indicator_color),
b.and_then(|b| b.indicator_color),
t,
),
indicator_shape: ShapeBorderRef::lerp(
a.and_then(|a| a.indicator_shape.as_ref()),
b.and_then(|b| b.indicator_shape.as_ref()),
t,
),
indicator_size: Size::lerp(
a.and_then(|a| a.indicator_size),
a.and_then(|a| a.indicator_size),
t,
),
label_text_style: <dyn WidgetStateProperty<Option<TextStyle>>>::lerp_optional(
a.and_then(|a| a.label_text_style.clone()),
b.and_then(|b| b.label_text_style.clone()),
t,
|a: Option<TextStyle>, b: Option<TextStyle>, t| {
TextStyle::lerp(a.as_ref(), b.as_ref(), t)
},
),
icon_theme: <dyn WidgetStateProperty<Option<IconThemeData>>>::lerp_optional(
a.and_then(|a| a.icon_theme.clone()),
b.and_then(|b| b.icon_theme.clone()),
t,
|a: Option<IconThemeData>, b: Option<IconThemeData>, t| {
Some(IconThemeData::lerp(a.as_ref(), b.as_ref(), t))
},
),
})
}
}
impl PartialEq for NavigationDrawerThemeData {
fn eq(&self, other: &NavigationDrawerThemeData) -> bool {
if std::ptr::eq(self, other) {
return true;
}
other.tile_height == self.tile_height
&& other.background_color == self.background_color
&& other.elevation == self.elevation
&& other.shadow_color == self.shadow_color
&& other.surface_tint_color == self.surface_tint_color
&& other.indicator_color == self.indicator_color
&& other.indicator_shape == self.indicator_shape
&& other.indicator_size == self.indicator_size
&& other.label_text_style == self.label_text_style
&& other.icon_theme == self.icon_theme
}
}
#[derive(Debug)]
pub struct NavigationDrawerTheme {
pub key: Option<KeyRef>,
pub data: NavigationDrawerThemeData,
pub child: WidgetRef,
}
impl NavigationDrawerTheme {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(
data: NavigationDrawerThemeData,
child: impl IntoWidget<K>,
) -> NavigationDrawerTheme {
NavigationDrawerTheme {
key: None,
data,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> NavigationDrawerTheme {
self.key = Some(key);
self
}
pub fn of(app: &mut App, context: BuildContext) -> NavigationDrawerThemeData {
match context.depend_on_inherited_widget_of_exact_type::<NavigationDrawerTheme>(app) {
Some(navigation_drawer_theme) => navigation_drawer_theme.data.clone(),
None => Theme::of(app, context).navigation_drawer_theme.clone(),
}
}
}
impl InheritedTheme for NavigationDrawerTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
NavigationDrawerTheme::new(self.data.clone(), child).into_widget()
}
}
impl InheritedWidget for NavigationDrawerTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &NavigationDrawerTheme) -> bool {
self.data != old_widget.data
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
impl waft_foundation::Diagnosticable for NavigationDrawerThemeData {
fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
properties.add(DoubleProperty::new("tileHeight", self.tile_height).default_value(None));
properties
.add(ColorProperty::new("backgroundColor", self.background_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(ColorProperty::new("indicatorColor", self.indicator_color).default_value(None));
properties.add(
DiagnosticsProperty::<ShapeBorderRef>::new(
"indicatorShape",
self.indicator_shape.clone(),
)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<Size>::new("indicatorSize", self.indicator_size)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<String>::new(
"labelTextStyle",
self.label_text_style
.as_deref()
.map(|property| property.to_string()),
)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<String>::new(
"iconTheme",
self.icon_theme
.as_deref()
.map(|property| property.to_string()),
)
.default_value(None),
);
}
}
impl std::fmt::Display for NavigationDrawerThemeData {
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 std::rc::Rc;
use waft_embedder::Size;
use waft_painting::{
BorderRadiusGeometry, BorderSide, RoundedRectangleBorder, ShapeBorderRef, TextStyle,
};
use waft_widgets::{IconThemeData, WidgetStatePropertyAll};
use super::*;
#[test]
fn an_assigned_navigation_drawer_theme_describes_every_property() {
let theme = NavigationDrawerThemeData::new()
.tile_height(50.0)
.background_color(Color::new(0x00000099))
.elevation(5.0)
.shadow_color(Color::new(0x00000098))
.surface_tint_color(Color::new(0x00000097))
.indicator_color(Color::new(0x00000096))
.indicator_shape(RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::circular(2.0),
))
.indicator_size(Size::new(10.0, 10.0))
.label_text_style(Rc::new(WidgetStatePropertyAll::new(Some(
TextStyle::new().font_size(7.0),
))))
.icon_theme(Rc::new(WidgetStatePropertyAll::new(Some(
IconThemeData::new().color(Color::new(0x00000095)),
))));
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,
[
"tileHeight: 50.0".to_string(),
format!("backgroundColor: {}", Color::new(0x00000099)),
"elevation: 5.0".to_string(),
format!("shadowColor: {}", Color::new(0x00000098)),
format!("surfaceTintColor: {}", Color::new(0x00000097)),
format!("indicatorColor: {}", Color::new(0x00000096)),
format!(
"indicatorShape: {}",
ShapeBorderRef::from(RoundedRectangleBorder::new(
BorderSide::NONE,
BorderRadiusGeometry::circular(2.0)
))
),
"indicatorSize: Size(10.0, 10.0)".to_string(),
"labelTextStyle: WidgetStatePropertyAll(TextStyle(inherit: true, size: 7.0))"
.to_string(),
format!(
"iconTheme: WidgetStatePropertyAll(IconThemeData#00000(color: {}))",
Color::new(0x00000095)
),
]
);
}
}