1use waft_embedder::{Color, Size, lerp_double};
4use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
5use waft_painting::{ColorProperty, ShapeBorderRef, TextStyle};
6use waft_widgets::{
7 BuildContext, IconThemeData, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
8 WidgetStateProperty, WidgetStatePropertyRef,
9};
10
11use crate::theme::Theme;
12
13#[derive(Clone, Debug, Default)]
35pub struct NavigationDrawerThemeData {
36 pub tile_height: Option<f64>,
38
39 pub background_color: Option<Color>,
41
42 pub elevation: Option<f64>,
44
45 pub shadow_color: Option<Color>,
47
48 pub surface_tint_color: Option<Color>,
50
51 pub indicator_color: Option<Color>,
53
54 pub indicator_shape: Option<ShapeBorderRef>,
56
57 pub indicator_size: Option<Size>,
59
60 pub label_text_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
65
66 pub icon_theme: Option<WidgetStatePropertyRef<Option<IconThemeData>>>,
72}
73
74crate::theme_data_getters::theme_data_getters! {
75 pub trait NavigationDrawerThemeDataGetters for NavigationDrawerThemeData, fields: navigation_drawer_theme_data {
79 tile_height: Option<f64>,
80 background_color: Option<Color>,
81 elevation: Option<f64>,
82 shadow_color: Option<Color>,
83 surface_tint_color: Option<Color>,
84 indicator_color: Option<Color>,
85 indicator_shape: Option<ShapeBorderRef>,
86 indicator_size: Option<Size>,
87 label_text_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
88 icon_theme: Option<WidgetStatePropertyRef<Option<IconThemeData>>>,
89 }
90}
91
92impl NavigationDrawerThemeData {
93 pub fn new() -> NavigationDrawerThemeData {
96 NavigationDrawerThemeData::default()
97 }
98
99 pub fn tile_height(mut self, tile_height: f64) -> NavigationDrawerThemeData {
101 self.tile_height = Some(tile_height);
102 self
103 }
104
105 pub fn background_color(mut self, background_color: Color) -> NavigationDrawerThemeData {
107 self.background_color = Some(background_color);
108 self
109 }
110
111 pub fn elevation(mut self, elevation: f64) -> NavigationDrawerThemeData {
113 self.elevation = Some(elevation);
114 self
115 }
116
117 pub fn shadow_color(mut self, shadow_color: Color) -> NavigationDrawerThemeData {
119 self.shadow_color = Some(shadow_color);
120 self
121 }
122
123 pub fn surface_tint_color(mut self, surface_tint_color: Color) -> NavigationDrawerThemeData {
125 self.surface_tint_color = Some(surface_tint_color);
126 self
127 }
128
129 pub fn indicator_color(mut self, indicator_color: Color) -> NavigationDrawerThemeData {
131 self.indicator_color = Some(indicator_color);
132 self
133 }
134
135 pub fn indicator_shape(
137 mut self,
138 indicator_shape: impl Into<ShapeBorderRef>,
139 ) -> NavigationDrawerThemeData {
140 self.indicator_shape = Some(indicator_shape.into());
141 self
142 }
143
144 pub fn indicator_size(mut self, indicator_size: Size) -> NavigationDrawerThemeData {
146 self.indicator_size = Some(indicator_size);
147 self
148 }
149
150 pub fn label_text_style(
152 mut self,
153 label_text_style: WidgetStatePropertyRef<Option<TextStyle>>,
154 ) -> NavigationDrawerThemeData {
155 self.label_text_style = Some(label_text_style);
156 self
157 }
158
159 pub fn icon_theme(
161 mut self,
162 icon_theme: WidgetStatePropertyRef<Option<IconThemeData>>,
163 ) -> NavigationDrawerThemeData {
164 self.icon_theme = Some(icon_theme);
165 self
166 }
167
168 pub fn copy_with(&self) -> NavigationDrawerThemeData {
174 self.clone()
175 }
176
177 pub fn lerp(
183 a: Option<&NavigationDrawerThemeData>,
184 b: Option<&NavigationDrawerThemeData>,
185 t: f64,
186 ) -> Option<NavigationDrawerThemeData> {
187 match (a, b) {
188 (None, None) => return None,
189 (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
190 _ => {}
191 }
192 Some(NavigationDrawerThemeData {
193 tile_height: lerp_double(
194 a.and_then(|a| a.tile_height),
195 b.and_then(|b| b.tile_height),
196 t,
197 ),
198 background_color: Color::lerp(
199 a.and_then(|a| a.background_color),
200 b.and_then(|b| b.background_color),
201 t,
202 ),
203 elevation: lerp_double(a.and_then(|a| a.elevation), b.and_then(|b| b.elevation), t),
204 shadow_color: Color::lerp(
205 a.and_then(|a| a.shadow_color),
206 b.and_then(|b| b.shadow_color),
207 t,
208 ),
209 surface_tint_color: Color::lerp(
210 a.and_then(|a| a.surface_tint_color),
211 b.and_then(|b| b.surface_tint_color),
212 t,
213 ),
214 indicator_color: Color::lerp(
215 a.and_then(|a| a.indicator_color),
216 b.and_then(|b| b.indicator_color),
217 t,
218 ),
219 indicator_shape: ShapeBorderRef::lerp(
220 a.and_then(|a| a.indicator_shape.as_ref()),
221 b.and_then(|b| b.indicator_shape.as_ref()),
222 t,
223 ),
224 indicator_size: Size::lerp(
226 a.and_then(|a| a.indicator_size),
227 a.and_then(|a| a.indicator_size),
228 t,
229 ),
230 label_text_style: <dyn WidgetStateProperty<Option<TextStyle>>>::lerp_optional(
231 a.and_then(|a| a.label_text_style.clone()),
232 b.and_then(|b| b.label_text_style.clone()),
233 t,
234 |a: Option<TextStyle>, b: Option<TextStyle>, t| {
235 TextStyle::lerp(a.as_ref(), b.as_ref(), t)
236 },
237 ),
238 icon_theme: <dyn WidgetStateProperty<Option<IconThemeData>>>::lerp_optional(
239 a.and_then(|a| a.icon_theme.clone()),
240 b.and_then(|b| b.icon_theme.clone()),
241 t,
242 |a: Option<IconThemeData>, b: Option<IconThemeData>, t| {
243 Some(IconThemeData::lerp(a.as_ref(), b.as_ref(), t))
244 },
245 ),
246 })
247 }
248}
249
250impl PartialEq for NavigationDrawerThemeData {
251 fn eq(&self, other: &NavigationDrawerThemeData) -> bool {
252 if std::ptr::eq(self, other) {
253 return true;
254 }
255 other.tile_height == self.tile_height
256 && other.background_color == self.background_color
257 && other.elevation == self.elevation
258 && other.shadow_color == self.shadow_color
259 && other.surface_tint_color == self.surface_tint_color
260 && other.indicator_color == self.indicator_color
261 && other.indicator_shape == self.indicator_shape
262 && other.indicator_size == self.indicator_size
263 && other.label_text_style == self.label_text_style
264 && other.icon_theme == self.icon_theme
265 }
266}
267
268#[derive(Debug)]
279pub struct NavigationDrawerTheme {
280 pub key: Option<KeyRef>,
281 pub data: NavigationDrawerThemeData,
284 pub child: WidgetRef,
285}
286
287impl NavigationDrawerTheme {
288 #[cfg_attr(debug_assertions, track_caller)]
291 pub fn new<K>(
292 data: NavigationDrawerThemeData,
293 child: impl IntoWidget<K>,
294 ) -> NavigationDrawerTheme {
295 NavigationDrawerTheme {
296 key: None,
297 data,
298 child: child.into_widget(),
299 }
300 }
301
302 pub fn key(mut self, key: KeyRef) -> NavigationDrawerTheme {
304 self.key = Some(key);
305 self
306 }
307
308 pub fn of(app: &mut App, context: BuildContext) -> NavigationDrawerThemeData {
320 match context.depend_on_inherited_widget_of_exact_type::<NavigationDrawerTheme>(app) {
321 Some(navigation_drawer_theme) => navigation_drawer_theme.data.clone(),
322 None => Theme::of(app, context).navigation_drawer_theme.clone(),
323 }
324 }
325}
326
327impl InheritedTheme for NavigationDrawerTheme {
328 fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
329 NavigationDrawerTheme::new(self.data.clone(), child).into_widget()
330 }
331}
332
333impl InheritedWidget for NavigationDrawerTheme {
334 fn key(&self) -> Option<&KeyRef> {
335 self.key.as_ref()
336 }
337
338 fn child(&self) -> &WidgetRef {
339 &self.child
340 }
341
342 fn update_should_notify(&self, old_widget: &NavigationDrawerTheme) -> bool {
343 self.data != old_widget.data
344 }
345
346 fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
347 Some(self)
348 }
349}
350
351impl waft_foundation::Diagnosticable for NavigationDrawerThemeData {
352 fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
353 properties.add(DoubleProperty::new("tileHeight", self.tile_height).default_value(None));
354 properties
355 .add(ColorProperty::new("backgroundColor", self.background_color).default_value(None));
356 properties.add(DoubleProperty::new("elevation", self.elevation).default_value(None));
357 properties.add(ColorProperty::new("shadowColor", self.shadow_color).default_value(None));
358 properties.add(
359 ColorProperty::new("surfaceTintColor", self.surface_tint_color).default_value(None),
360 );
361 properties
362 .add(ColorProperty::new("indicatorColor", self.indicator_color).default_value(None));
363 properties.add(
364 DiagnosticsProperty::<ShapeBorderRef>::new(
365 "indicatorShape",
366 self.indicator_shape.clone(),
367 )
368 .default_value(None),
369 );
370 properties.add(
371 DiagnosticsProperty::<Size>::new("indicatorSize", self.indicator_size)
372 .default_value(None),
373 );
374 properties.add(
375 DiagnosticsProperty::<String>::new(
376 "labelTextStyle",
377 self.label_text_style
378 .as_deref()
379 .map(|property| property.to_string()),
380 )
381 .default_value(None),
382 );
383 properties.add(
384 DiagnosticsProperty::<String>::new(
385 "iconTheme",
386 self.icon_theme
387 .as_deref()
388 .map(|property| property.to_string()),
389 )
390 .default_value(None),
391 );
392 }
393}
394
395impl std::fmt::Display for NavigationDrawerThemeData {
396 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
397 f.write_str(&waft_foundation::Diagnosticable::to_string(self))
398 }
399}
400
401#[cfg(test)]
402mod tests {
403 use std::rc::Rc;
404
405 use waft_embedder::Size;
406 use waft_painting::{
407 BorderRadiusGeometry, BorderSide, RoundedRectangleBorder, ShapeBorderRef, TextStyle,
408 };
409 use waft_widgets::{IconThemeData, WidgetStatePropertyAll};
410
411 use super::*;
412
413 #[test]
415 fn an_assigned_navigation_drawer_theme_describes_every_property() {
416 let theme = NavigationDrawerThemeData::new()
417 .tile_height(50.0)
418 .background_color(Color::new(0x00000099))
419 .elevation(5.0)
420 .shadow_color(Color::new(0x00000098))
421 .surface_tint_color(Color::new(0x00000097))
422 .indicator_color(Color::new(0x00000096))
423 .indicator_shape(RoundedRectangleBorder::new(
424 BorderSide::NONE,
425 BorderRadiusGeometry::circular(2.0),
426 ))
427 .indicator_size(Size::new(10.0, 10.0))
428 .label_text_style(Rc::new(WidgetStatePropertyAll::new(Some(
429 TextStyle::new().font_size(7.0),
430 ))))
431 .icon_theme(Rc::new(WidgetStatePropertyAll::new(Some(
432 IconThemeData::new().color(Color::new(0x00000095)),
433 ))));
434 let description = waft_widgets::testing::describe_properties(|properties| {
435 waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
436 });
437 let description: Vec<String> = description
438 .iter()
439 .map(|line| waft_rendering::testing::ignoring_hash_codes(line))
440 .collect();
441 assert_eq!(
442 description,
443 [
444 "tileHeight: 50.0".to_string(),
445 format!("backgroundColor: {}", Color::new(0x00000099)),
446 "elevation: 5.0".to_string(),
447 format!("shadowColor: {}", Color::new(0x00000098)),
448 format!("surfaceTintColor: {}", Color::new(0x00000097)),
449 format!("indicatorColor: {}", Color::new(0x00000096)),
450 format!(
451 "indicatorShape: {}",
452 ShapeBorderRef::from(RoundedRectangleBorder::new(
453 BorderSide::NONE,
454 BorderRadiusGeometry::circular(2.0)
455 ))
456 ),
457 "indicatorSize: Size(10.0, 10.0)".to_string(),
458 "labelTextStyle: WidgetStatePropertyAll(TextStyle(inherit: true, size: 7.0))"
459 .to_string(),
460 format!(
461 "iconTheme: WidgetStatePropertyAll(IconThemeData#00000(color: {}))",
462 Color::new(0x00000095)
463 ),
464 ]
465 );
466 }
467}