Skip to main content

waft_material/
app_bar_theme.rs

1//! Flutter counterpart: material_ui's `app_bar_theme.dart`.
2
3use waft_embedder::{Color, SystemUiOverlayStyle, lerp_double};
4use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
5use waft_painting::{ColorProperty, EdgeInsetsGeometry, ShapeBorderRef, TextStyle};
6use waft_widgets::{
7    BuildContext, IconThemeData, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, SizedBox,
8    WidgetRef,
9};
10
11use crate::theme::Theme;
12
13/// Defines default property values for descendant `AppBar` widgets.
14///
15/// Descendant widgets obtain the current [`AppBarThemeData`] object with
16/// [`AppBarTheme::of`]. Instances of [`AppBarThemeData`] can be customized
17/// with [`AppBarThemeData::copy_with`].
18///
19/// Typically an [`AppBarThemeData`] is specified as part of the overall `Theme` with
20/// `ThemeData::app_bar_theme`.
21///
22/// All [`AppBarTheme`] properties are `null` by default. When null, the
23/// `AppBar` constructor provides defaults.
24///
25/// Dart's `AppBarTheme` constructor takes either the properties or a [`data`](Self::data);
26/// here the properties are the `with_*` setters and [`with_data`](Self::with_data) the other,
27/// and each setter that breaks Dart's constructor assert (a `data` together with any property)
28/// fails in a debug build. The getters are methods, as Dart's, hence the `with_` prefix.
29///
30/// See also:
31///
32///  * `ThemeData`, which describes the overall theme information for the
33///    application.
34#[derive(Clone, Debug)]
35pub struct AppBarTheme {
36    pub key: Option<KeyRef>,
37    data: Option<AppBarThemeData>,
38    background_color: Option<Color>,
39    foreground_color: Option<Color>,
40    elevation: Option<f64>,
41    scrolled_under_elevation: Option<f64>,
42    shadow_color: Option<Color>,
43    surface_tint_color: Option<Color>,
44    shape: Option<ShapeBorderRef>,
45    icon_theme: Option<IconThemeData>,
46    actions_icon_theme: Option<IconThemeData>,
47    center_title: Option<bool>,
48    title_spacing: Option<f64>,
49    leading_width: Option<f64>,
50    toolbar_height: Option<f64>,
51    toolbar_text_style: Option<TextStyle>,
52    title_text_style: Option<TextStyle>,
53    system_overlay_style: Option<SystemUiOverlayStyle>,
54    actions_padding: Option<EdgeInsetsGeometry>,
55    child: WidgetRef,
56}
57
58#[allow(deprecated)]
59impl AppBarTheme {
60    /// Creates a theme that can be used for `ThemeData::app_bar_theme`.
61    pub fn new() -> AppBarTheme {
62        AppBarTheme {
63            key: None,
64            data: None,
65            background_color: None,
66            foreground_color: None,
67            elevation: None,
68            scrolled_under_elevation: None,
69            shadow_color: None,
70            surface_tint_color: None,
71            shape: None,
72            icon_theme: None,
73            actions_icon_theme: None,
74            center_title: None,
75            title_spacing: None,
76            leading_width: None,
77            toolbar_height: None,
78            toolbar_text_style: None,
79            title_text_style: None,
80            system_overlay_style: None,
81            actions_padding: None,
82            child: SizedBox::new().into_widget(),
83        }
84    }
85
86    /// Dart `AppBarTheme(key:)`.
87    pub fn key(mut self, key: KeyRef) -> AppBarTheme {
88        self.key = Some(key);
89        self
90    }
91
92    /// Dart `AppBarTheme(child:)`; the default is an empty `SizedBox`.
93    #[cfg_attr(debug_assertions, track_caller)]
94    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> AppBarTheme {
95        self.child = child.into_widget();
96        self
97    }
98
99    /// Dart `AppBarTheme(data:)`.
100    pub fn with_data(mut self, data: AppBarThemeData) -> AppBarTheme {
101        debug_assert!(self.has_no_property());
102        self.data = Some(data);
103        self
104    }
105
106    /// Dart `AppBarTheme(color:)`: the same as [`with_background_color`](Self::with_background_color).
107    #[deprecated(
108        note = "Use backgroundColor instead. This feature was deprecated after v3.33.0-0.2.pre."
109    )]
110    pub fn with_color(self, color: Color) -> AppBarTheme {
111        self.with_background_color(color)
112    }
113
114    /// Dart `AppBarTheme(backgroundColor:)`.
115    pub fn with_background_color(mut self, background_color: Color) -> AppBarTheme {
116        debug_assert!(self.data.is_none());
117        self.background_color = Some(background_color);
118        self
119    }
120
121    /// Dart `AppBarTheme(foregroundColor:)`.
122    pub fn with_foreground_color(mut self, foreground_color: Color) -> AppBarTheme {
123        debug_assert!(self.data.is_none());
124        self.foreground_color = Some(foreground_color);
125        self
126    }
127
128    /// Dart `AppBarTheme(elevation:)`.
129    pub fn with_elevation(mut self, elevation: f64) -> AppBarTheme {
130        debug_assert!(self.data.is_none());
131        self.elevation = Some(elevation);
132        self
133    }
134
135    /// Dart `AppBarTheme(scrolledUnderElevation:)`.
136    pub fn with_scrolled_under_elevation(mut self, scrolled_under_elevation: f64) -> AppBarTheme {
137        debug_assert!(self.data.is_none());
138        self.scrolled_under_elevation = Some(scrolled_under_elevation);
139        self
140    }
141
142    /// Dart `AppBarTheme(shadowColor:)`.
143    pub fn with_shadow_color(mut self, shadow_color: Color) -> AppBarTheme {
144        debug_assert!(self.data.is_none());
145        self.shadow_color = Some(shadow_color);
146        self
147    }
148
149    /// Dart `AppBarTheme(surfaceTintColor:)`.
150    pub fn with_surface_tint_color(mut self, surface_tint_color: Color) -> AppBarTheme {
151        debug_assert!(self.data.is_none());
152        self.surface_tint_color = Some(surface_tint_color);
153        self
154    }
155
156    /// Dart `AppBarTheme(shape:)`.
157    pub fn with_shape(mut self, shape: impl Into<ShapeBorderRef>) -> AppBarTheme {
158        debug_assert!(self.data.is_none());
159        self.shape = Some(shape.into());
160        self
161    }
162
163    /// Dart `AppBarTheme(iconTheme:)`.
164    pub fn with_icon_theme(mut self, icon_theme: IconThemeData) -> AppBarTheme {
165        debug_assert!(self.data.is_none());
166        self.icon_theme = Some(icon_theme);
167        self
168    }
169
170    /// Dart `AppBarTheme(actionsIconTheme:)`.
171    pub fn with_actions_icon_theme(mut self, actions_icon_theme: IconThemeData) -> AppBarTheme {
172        debug_assert!(self.data.is_none());
173        self.actions_icon_theme = Some(actions_icon_theme);
174        self
175    }
176
177    /// Dart `AppBarTheme(centerTitle:)`.
178    pub fn with_center_title(mut self, center_title: bool) -> AppBarTheme {
179        debug_assert!(self.data.is_none());
180        self.center_title = Some(center_title);
181        self
182    }
183
184    /// Dart `AppBarTheme(titleSpacing:)`.
185    pub fn with_title_spacing(mut self, title_spacing: f64) -> AppBarTheme {
186        debug_assert!(self.data.is_none());
187        self.title_spacing = Some(title_spacing);
188        self
189    }
190
191    /// Dart `AppBarTheme(leadingWidth:)`.
192    pub fn with_leading_width(mut self, leading_width: f64) -> AppBarTheme {
193        debug_assert!(self.data.is_none());
194        self.leading_width = Some(leading_width);
195        self
196    }
197
198    /// Dart `AppBarTheme(toolbarHeight:)`.
199    pub fn with_toolbar_height(mut self, toolbar_height: f64) -> AppBarTheme {
200        debug_assert!(self.data.is_none());
201        self.toolbar_height = Some(toolbar_height);
202        self
203    }
204
205    /// Dart `AppBarTheme(toolbarTextStyle:)`.
206    pub fn with_toolbar_text_style(mut self, toolbar_text_style: TextStyle) -> AppBarTheme {
207        debug_assert!(self.data.is_none());
208        self.toolbar_text_style = Some(toolbar_text_style);
209        self
210    }
211
212    /// Dart `AppBarTheme(titleTextStyle:)`.
213    pub fn with_title_text_style(mut self, title_text_style: TextStyle) -> AppBarTheme {
214        debug_assert!(self.data.is_none());
215        self.title_text_style = Some(title_text_style);
216        self
217    }
218
219    /// Dart `AppBarTheme(systemOverlayStyle:)`.
220    pub fn with_system_overlay_style(
221        mut self,
222        system_overlay_style: SystemUiOverlayStyle,
223    ) -> AppBarTheme {
224        debug_assert!(self.data.is_none());
225        self.system_overlay_style = Some(system_overlay_style);
226        self
227    }
228
229    /// Dart `AppBarTheme(actionsPadding:)`.
230    pub fn with_actions_padding(
231        mut self,
232        actions_padding: impl Into<EdgeInsetsGeometry>,
233    ) -> AppBarTheme {
234        let actions_padding = actions_padding.into();
235        debug_assert!(self.data.is_none());
236        self.actions_padding = Some(actions_padding);
237        self
238    }
239
240    /// Overrides the default value of `AppBar::background_color` in all
241    /// descendant `AppBar` widgets.
242    ///
243    /// See also:
244    ///
245    ///  * [`foreground_color`](Self::foreground_color), which overrides the default value of
246    ///    `AppBar::foreground_color` in all descendant `AppBar` widgets.
247    pub fn background_color(&self) -> Option<Color> {
248        match &self.data {
249            Some(data) => data.background_color,
250            None => self.background_color,
251        }
252    }
253
254    /// Overrides the default value of `AppBar::foreground_color` in all
255    /// descendant `AppBar` widgets.
256    ///
257    /// See also:
258    ///
259    ///  * [`background_color`](Self::background_color), which overrides the default value of
260    ///    `AppBar::background_color` in all descendant `AppBar` widgets.
261    pub fn foreground_color(&self) -> Option<Color> {
262        match &self.data {
263            Some(data) => data.foreground_color,
264            None => self.foreground_color,
265        }
266    }
267
268    /// Overrides the default value of `AppBar::elevation` in all
269    /// descendant `AppBar` widgets.
270    pub fn elevation(&self) -> Option<f64> {
271        match &self.data {
272            Some(data) => data.elevation,
273            None => self.elevation,
274        }
275    }
276
277    /// Overrides the default value of `AppBar::scrolled_under_elevation` in all
278    /// descendant `AppBar` widgets.
279    pub fn scrolled_under_elevation(&self) -> Option<f64> {
280        match &self.data {
281            Some(data) => data.scrolled_under_elevation,
282            None => self.scrolled_under_elevation,
283        }
284    }
285
286    /// Overrides the default value of `AppBar::shadow_color` in all
287    /// descendant `AppBar` widgets.
288    pub fn shadow_color(&self) -> Option<Color> {
289        match &self.data {
290            Some(data) => data.shadow_color,
291            None => self.shadow_color,
292        }
293    }
294
295    /// Overrides the default value of `AppBar::surface_tint_color` in all
296    /// descendant `AppBar` widgets.
297    pub fn surface_tint_color(&self) -> Option<Color> {
298        match &self.data {
299            Some(data) => data.surface_tint_color,
300            None => self.surface_tint_color,
301        }
302    }
303
304    /// Overrides the default value of `AppBar::shape` in all
305    /// descendant `AppBar` widgets.
306    pub fn shape(&self) -> Option<&ShapeBorderRef> {
307        match &self.data {
308            Some(data) => data.shape.as_ref(),
309            None => self.shape.as_ref(),
310        }
311    }
312
313    /// Overrides the default value of `AppBar::icon_theme` in all
314    /// descendant `AppBar` widgets.
315    ///
316    /// See also:
317    ///
318    ///  * [`actions_icon_theme`](Self::actions_icon_theme), which overrides the default value of
319    ///    `AppBar::actions_icon_theme` in all descendant `AppBar` widgets.
320    ///  * [`foreground_color`](Self::foreground_color), which overrides the default value
321    ///    `AppBar::foreground_color` in all descendant `AppBar` widgets.
322    pub fn icon_theme(&self) -> Option<&IconThemeData> {
323        match &self.data {
324            Some(data) => data.icon_theme.as_ref(),
325            None => self.icon_theme.as_ref(),
326        }
327    }
328
329    /// Overrides the default value of `AppBar::actions_icon_theme` in all
330    /// descendant `AppBar` widgets.
331    ///
332    /// See also:
333    ///
334    ///  * [`icon_theme`](Self::icon_theme), which overrides the default value of
335    ///    `AppBar::icon_theme` in all descendant `AppBar` widgets.
336    ///  * [`foreground_color`](Self::foreground_color), which overrides the default value
337    ///    `AppBar::foreground_color` in all descendant `AppBar` widgets.
338    pub fn actions_icon_theme(&self) -> Option<&IconThemeData> {
339        match &self.data {
340            Some(data) => data.actions_icon_theme.as_ref(),
341            None => self.actions_icon_theme.as_ref(),
342        }
343    }
344
345    /// Overrides the default value of `AppBar::center_title`
346    /// property in all descendant `AppBar` widgets.
347    pub fn center_title(&self) -> Option<bool> {
348        match &self.data {
349            Some(data) => data.center_title,
350            None => self.center_title,
351        }
352    }
353
354    /// Overrides the default value of the obsolete `AppBar::title_spacing`
355    /// property in all descendant `AppBar` widgets.
356    ///
357    /// If null, `AppBar` uses default value of `NavigationToolbar::K_MIDDLE_SPACING`.
358    pub fn title_spacing(&self) -> Option<f64> {
359        match &self.data {
360            Some(data) => data.title_spacing,
361            None => self.title_spacing,
362        }
363    }
364
365    /// Overrides the default value of the `AppBar::leading_width`
366    /// property in all descendant `AppBar` widgets.
367    pub fn leading_width(&self) -> Option<f64> {
368        match &self.data {
369            Some(data) => data.leading_width,
370            None => self.leading_width,
371        }
372    }
373
374    /// Overrides the default value of the `AppBar::toolbar_height`
375    /// property in all descendant `AppBar` widgets.
376    ///
377    /// See also:
378    ///
379    ///  * `AppBar::preferred_height_for`, which computes the overall
380    ///    height of an AppBar widget, taking this value into account.
381    pub fn toolbar_height(&self) -> Option<f64> {
382        match &self.data {
383            Some(data) => data.toolbar_height,
384            None => self.toolbar_height,
385        }
386    }
387
388    /// Overrides the default value of the obsolete `AppBar::toolbar_text_style`
389    /// property in all descendant `AppBar` widgets.
390    ///
391    /// See also:
392    ///
393    ///  * [`title_text_style`](Self::title_text_style), which overrides the default of `AppBar::title_text_style`
394    ///    in all descendant `AppBar` widgets.
395    pub fn toolbar_text_style(&self) -> Option<&TextStyle> {
396        match &self.data {
397            Some(data) => data.toolbar_text_style.as_ref(),
398            None => self.toolbar_text_style.as_ref(),
399        }
400    }
401
402    /// Overrides the default value of `AppBar::title_text_style`
403    /// property in all descendant `AppBar` widgets.
404    ///
405    /// See also:
406    ///
407    ///  * [`toolbar_text_style`](Self::toolbar_text_style), which overrides the default of `AppBar::toolbar_text_style`
408    ///    in all descendant `AppBar` widgets.
409    pub fn title_text_style(&self) -> Option<&TextStyle> {
410        match &self.data {
411            Some(data) => data.title_text_style.as_ref(),
412            None => self.title_text_style.as_ref(),
413        }
414    }
415
416    /// Overrides the default value of `AppBar::system_overlay_style`
417    /// property in all descendant `AppBar` widgets.
418    pub fn system_overlay_style(&self) -> Option<SystemUiOverlayStyle> {
419        match &self.data {
420            Some(data) => data.system_overlay_style,
421            None => self.system_overlay_style,
422        }
423    }
424
425    /// Overrides the default value of `AppBar::actions_padding`
426    /// property in all descendant `AppBar` widgets.
427    pub fn actions_padding(&self) -> Option<EdgeInsetsGeometry> {
428        match &self.data {
429            Some(data) => data.actions_padding,
430            None => self.actions_padding,
431        }
432    }
433
434    /// The properties used for all descendant `AppBar` widgets.
435    pub fn data(&self) -> AppBarThemeData {
436        self.data.clone().unwrap_or_else(|| AppBarThemeData {
437            background_color: self.background_color,
438            foreground_color: self.foreground_color,
439            elevation: self.elevation,
440            scrolled_under_elevation: self.scrolled_under_elevation,
441            shadow_color: self.shadow_color,
442            surface_tint_color: self.surface_tint_color,
443            shape: self.shape.clone(),
444            icon_theme: self.icon_theme.clone(),
445            actions_icon_theme: self.actions_icon_theme.clone(),
446            center_title: self.center_title,
447            title_spacing: self.title_spacing,
448            leading_width: self.leading_width,
449            toolbar_height: self.toolbar_height,
450            toolbar_text_style: self.toolbar_text_style.clone(),
451            title_text_style: self.title_text_style.clone(),
452            system_overlay_style: self.system_overlay_style,
453            actions_padding: self.actions_padding,
454        })
455    }
456
457    /// Creates a copy of this object with the given fields replaced with the
458    /// new values.
459    ///
460    /// This method is obsolete and will be deprecated in a future release:
461    /// please use the [`AppBarThemeData::copy_with`] method instead.
462    ///
463    /// Dart's named arguments are the fluent setters on the copy
464    /// (`theme.copy_with().with_elevation(2.0)`). As in Dart, the copy carries neither the
465    /// key nor the child, and holds the properties, never a [`data`](Self::data).
466    pub fn copy_with(&self) -> AppBarTheme {
467        AppBarTheme {
468            key: None,
469            data: None,
470            background_color: self.background_color(),
471            foreground_color: self.foreground_color(),
472            elevation: self.elevation(),
473            scrolled_under_elevation: self.scrolled_under_elevation(),
474            shadow_color: self.shadow_color(),
475            surface_tint_color: self.surface_tint_color(),
476            shape: self.shape().cloned(),
477            icon_theme: self.icon_theme().cloned(),
478            actions_icon_theme: self.actions_icon_theme().cloned(),
479            center_title: self.center_title(),
480            title_spacing: self.title_spacing(),
481            leading_width: self.leading_width(),
482            toolbar_height: self.toolbar_height(),
483            toolbar_text_style: self.toolbar_text_style().cloned(),
484            title_text_style: self.title_text_style().cloned(),
485            system_overlay_style: self.system_overlay_style(),
486            actions_padding: self.actions_padding(),
487            child: SizedBox::new().into_widget(),
488        }
489    }
490
491    /// Retrieves the [`AppBarThemeData`] from the closest ancestor [`AppBarTheme`].
492    ///
493    /// If there is no enclosing [`AppBarTheme`] widget, then
494    /// `ThemeData::app_bar_theme` is used.
495    ///
496    /// Typical usage is as follows:
497    ///
498    /// ```text
499    /// let theme = AppBarTheme::of(app, context);
500    /// ```
501    pub fn of(app: &mut App, context: BuildContext) -> AppBarThemeData {
502        match context.depend_on_inherited_widget_of_exact_type::<AppBarTheme>(app) {
503            Some(app_bar_theme) => app_bar_theme.data(),
504            None => Theme::of(app, context).app_bar_theme.clone(),
505        }
506    }
507
508    /// Linearly interpolate between two AppBar themes.
509    ///
510    /// {@macro dart.ui.shadow.lerp}
511    pub fn lerp(a: Option<&AppBarTheme>, b: Option<&AppBarTheme>, t: f64) -> AppBarTheme {
512        if let (Some(a), Some(b)) = (a, b)
513            && std::ptr::eq(a, b)
514        {
515            return a.clone();
516        }
517        AppBarTheme {
518            key: None,
519            data: None,
520            background_color: Color::lerp(
521                a.and_then(|a| a.background_color()),
522                b.and_then(|b| b.background_color()),
523                t,
524            ),
525            foreground_color: Color::lerp(
526                a.and_then(|a| a.foreground_color()),
527                b.and_then(|b| b.foreground_color()),
528                t,
529            ),
530            elevation: lerp_double(
531                a.and_then(|a| a.elevation()),
532                b.and_then(|b| b.elevation()),
533                t,
534            ),
535            scrolled_under_elevation: lerp_double(
536                a.and_then(|a| a.scrolled_under_elevation()),
537                b.and_then(|b| b.scrolled_under_elevation()),
538                t,
539            ),
540            shadow_color: Color::lerp(
541                a.and_then(|a| a.shadow_color()),
542                b.and_then(|b| b.shadow_color()),
543                t,
544            ),
545            surface_tint_color: Color::lerp(
546                a.and_then(|a| a.surface_tint_color()),
547                b.and_then(|b| b.surface_tint_color()),
548                t,
549            ),
550            shape: ShapeBorderRef::lerp(a.and_then(|a| a.shape()), b.and_then(|b| b.shape()), t),
551            icon_theme: Some(IconThemeData::lerp(
552                a.and_then(|a| a.icon_theme()),
553                b.and_then(|b| b.icon_theme()),
554                t,
555            )),
556            actions_icon_theme: Some(IconThemeData::lerp(
557                a.and_then(|a| a.actions_icon_theme()),
558                b.and_then(|b| b.actions_icon_theme()),
559                t,
560            )),
561            center_title: if t < 0.5 {
562                a.and_then(|a| a.center_title())
563            } else {
564                b.and_then(|b| b.center_title())
565            },
566            title_spacing: lerp_double(
567                a.and_then(|a| a.title_spacing()),
568                b.and_then(|b| b.title_spacing()),
569                t,
570            ),
571            leading_width: lerp_double(
572                a.and_then(|a| a.leading_width()),
573                b.and_then(|b| b.leading_width()),
574                t,
575            ),
576            toolbar_height: lerp_double(
577                a.and_then(|a| a.toolbar_height()),
578                b.and_then(|b| b.toolbar_height()),
579                t,
580            ),
581            toolbar_text_style: TextStyle::lerp(
582                a.and_then(|a| a.toolbar_text_style()),
583                b.and_then(|b| b.toolbar_text_style()),
584                t,
585            ),
586            title_text_style: TextStyle::lerp(
587                a.and_then(|a| a.title_text_style()),
588                b.and_then(|b| b.title_text_style()),
589                t,
590            ),
591            system_overlay_style: if t < 0.5 {
592                a.and_then(|a| a.system_overlay_style())
593            } else {
594                b.and_then(|b| b.system_overlay_style())
595            },
596            actions_padding: EdgeInsetsGeometry::lerp(
597                a.and_then(|a| a.actions_padding()),
598                b.and_then(|b| b.actions_padding()),
599                t,
600            ),
601            child: SizedBox::new().into_widget(),
602        }
603    }
604
605    /// Dart's constructor assert: a `data` takes the place of every property.
606    fn has_no_property(&self) -> bool {
607        self.background_color.is_none()
608            && self.foreground_color.is_none()
609            && self.elevation.is_none()
610            && self.scrolled_under_elevation.is_none()
611            && self.shadow_color.is_none()
612            && self.surface_tint_color.is_none()
613            && self.shape.is_none()
614            && self.icon_theme.is_none()
615            && self.actions_icon_theme.is_none()
616            && self.center_title.is_none()
617            && self.title_spacing.is_none()
618            && self.leading_width.is_none()
619            && self.toolbar_height.is_none()
620            && self.toolbar_text_style.is_none()
621            && self.title_text_style.is_none()
622            && self.system_overlay_style.is_none()
623            && self.actions_padding.is_none()
624    }
625}
626
627impl Default for AppBarTheme {
628    fn default() -> AppBarTheme {
629        AppBarTheme::new()
630    }
631}
632
633impl InheritedTheme for AppBarTheme {
634    fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
635        AppBarTheme::new()
636            .with_data(self.data())
637            .child(child)
638            .into_widget()
639    }
640}
641
642impl InheritedWidget for AppBarTheme {
643    fn key(&self) -> Option<&KeyRef> {
644        self.key.as_ref()
645    }
646
647    fn child(&self) -> &WidgetRef {
648        &self.child
649    }
650
651    fn update_should_notify(&self, old_widget: &AppBarTheme) -> bool {
652        self.data() != old_widget.data()
653    }
654
655    fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
656        Some(self)
657    }
658}
659
660/// `ThemeData(appBarTheme:)` takes Dart's `Object?`, either an [`AppBarThemeData`] or an
661/// [`AppBarTheme`] whose [`data`](AppBarTheme::data) is used.
662impl From<AppBarTheme> for AppBarThemeData {
663    fn from(app_bar_theme: AppBarTheme) -> AppBarThemeData {
664        app_bar_theme.data()
665    }
666}
667
668/// Defines default property values for descendant `AppBar` widgets.
669///
670/// Descendant widgets obtain the current [`AppBarThemeData`] object using
671/// [`AppBarTheme::of`]. Instances of [`AppBarThemeData`] can be
672/// customized with [`AppBarThemeData::copy_with`].
673///
674/// Typically an [`AppBarThemeData`] is specified as part of the overall `Theme`
675/// with `ThemeData::app_bar_theme`.
676///
677/// All [`AppBarThemeData`] properties are `null` by default. When null, the `AppBar`
678/// will use the values from `ThemeData` if they exist, otherwise it will
679/// provide its own defaults. See the individual `AppBar` properties for details.
680///
681/// See also:
682///
683///  * `AppBar`, which is the widget that this theme configures.
684///  * `ThemeData`, which describes the overall theme information for the
685///    application.
686#[derive(Clone, Debug, Default, PartialEq)]
687pub struct AppBarThemeData {
688    /// Overrides the default value of `AppBar::background_color`.
689    pub background_color: Option<Color>,
690
691    /// Overrides the default value of `AppBar::foreground_color`.
692    pub foreground_color: Option<Color>,
693
694    /// Overrides the default value of `AppBar::elevation`.
695    pub elevation: Option<f64>,
696
697    /// Overrides the default value of `AppBar::scrolled_under_elevation`.
698    pub scrolled_under_elevation: Option<f64>,
699
700    /// Overrides the default value of `AppBar::shadow_color`.
701    pub shadow_color: Option<Color>,
702
703    /// Overrides the default value of `AppBar::surface_tint_color`.
704    pub surface_tint_color: Option<Color>,
705
706    /// Overrides the default value of `AppBar::shape`.
707    pub shape: Option<ShapeBorderRef>,
708
709    /// Overrides the default value of `AppBar::icon_theme`.
710    pub icon_theme: Option<IconThemeData>,
711
712    /// Overrides the default value of `AppBar::actions_icon_theme`.
713    pub actions_icon_theme: Option<IconThemeData>,
714
715    /// Overrides the default value of `AppBar::center_title`.
716    pub center_title: Option<bool>,
717
718    /// Overrides the default value of `AppBar::title_spacing`.
719    pub title_spacing: Option<f64>,
720
721    /// Overrides the default value of `AppBar::leading_width`.
722    pub leading_width: Option<f64>,
723
724    /// Overrides the default value of `AppBar::toolbar_height`.
725    pub toolbar_height: Option<f64>,
726
727    /// Overrides the default value of `AppBar::toolbar_text_style`.
728    pub toolbar_text_style: Option<TextStyle>,
729
730    /// Overrides the default value of `AppBar::title_text_style`.
731    pub title_text_style: Option<TextStyle>,
732
733    /// Overrides the default value of `AppBar::system_overlay_style`.
734    pub system_overlay_style: Option<SystemUiOverlayStyle>,
735
736    /// Overrides the default value of `AppBar::actions_padding`.
737    pub actions_padding: Option<EdgeInsetsGeometry>,
738}
739
740crate::theme_data_getters::theme_data_getters! {
741    /// Dart's getters of `AppBarThemeData`: the theme data answers with its fields, and a defaults
742    /// subclass (Dart's `_...Defaults...(context)`) computes the ones it overrides when they
743    /// are read.
744    pub trait AppBarThemeDataGetters for AppBarThemeData, fields: app_bar_theme_data {
745        background_color: Option<Color>,
746        foreground_color: Option<Color>,
747        elevation: Option<f64>,
748        scrolled_under_elevation: Option<f64>,
749        shadow_color: Option<Color>,
750        surface_tint_color: Option<Color>,
751        shape: Option<ShapeBorderRef>,
752        icon_theme: Option<IconThemeData>,
753        actions_icon_theme: Option<IconThemeData>,
754        center_title: Option<bool>,
755        title_spacing: Option<f64>,
756        leading_width: Option<f64>,
757        toolbar_height: Option<f64>,
758        toolbar_text_style: Option<TextStyle>,
759        title_text_style: Option<TextStyle>,
760        system_overlay_style: Option<SystemUiOverlayStyle>,
761        actions_padding: Option<EdgeInsetsGeometry>,
762    }
763}
764
765#[allow(deprecated)]
766impl AppBarThemeData {
767    /// Creates an app bar theme that can be used with `ThemeData::app_bar_theme`; Dart's optional
768    /// named arguments are the fluent setters.
769    pub fn new() -> AppBarThemeData {
770        AppBarThemeData::default()
771    }
772
773    /// Dart `AppBarThemeData(color:)` and `AppBarThemeData.copyWith(color:)`: the same as
774    /// [`background_color`](Self::background_color). Dart's constructor ignores it, which
775    /// the one fluent setter of both cannot tell apart.
776    #[deprecated(
777        note = "Use backgroundColor instead. This feature was deprecated after v3.33.0-0.2.pre."
778    )]
779    pub fn color(self, color: Color) -> AppBarThemeData {
780        self.background_color(color)
781    }
782
783    /// Dart `AppBarThemeData(backgroundColor:)`.
784    pub fn background_color(mut self, background_color: Color) -> AppBarThemeData {
785        self.background_color = Some(background_color);
786        self
787    }
788
789    /// Dart `AppBarThemeData(foregroundColor:)`.
790    pub fn foreground_color(mut self, foreground_color: Color) -> AppBarThemeData {
791        self.foreground_color = Some(foreground_color);
792        self
793    }
794
795    /// Dart `AppBarThemeData(elevation:)`.
796    pub fn elevation(mut self, elevation: f64) -> AppBarThemeData {
797        self.elevation = Some(elevation);
798        self
799    }
800
801    /// Dart `AppBarThemeData(scrolledUnderElevation:)`.
802    pub fn scrolled_under_elevation(mut self, scrolled_under_elevation: f64) -> AppBarThemeData {
803        self.scrolled_under_elevation = Some(scrolled_under_elevation);
804        self
805    }
806
807    /// Dart `AppBarThemeData(shadowColor:)`.
808    pub fn shadow_color(mut self, shadow_color: Color) -> AppBarThemeData {
809        self.shadow_color = Some(shadow_color);
810        self
811    }
812
813    /// Dart `AppBarThemeData(surfaceTintColor:)`.
814    pub fn surface_tint_color(mut self, surface_tint_color: Color) -> AppBarThemeData {
815        self.surface_tint_color = Some(surface_tint_color);
816        self
817    }
818
819    /// Dart `AppBarThemeData(shape:)`.
820    pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> AppBarThemeData {
821        self.shape = Some(shape.into());
822        self
823    }
824
825    /// Dart `AppBarThemeData(iconTheme:)`.
826    pub fn icon_theme(mut self, icon_theme: IconThemeData) -> AppBarThemeData {
827        self.icon_theme = Some(icon_theme);
828        self
829    }
830
831    /// Dart `AppBarThemeData(actionsIconTheme:)`.
832    pub fn actions_icon_theme(mut self, actions_icon_theme: IconThemeData) -> AppBarThemeData {
833        self.actions_icon_theme = Some(actions_icon_theme);
834        self
835    }
836
837    /// Dart `AppBarThemeData(centerTitle:)`.
838    pub fn center_title(mut self, center_title: bool) -> AppBarThemeData {
839        self.center_title = Some(center_title);
840        self
841    }
842
843    /// Dart `AppBarThemeData(titleSpacing:)`.
844    pub fn title_spacing(mut self, title_spacing: f64) -> AppBarThemeData {
845        self.title_spacing = Some(title_spacing);
846        self
847    }
848
849    /// Dart `AppBarThemeData(leadingWidth:)`.
850    pub fn leading_width(mut self, leading_width: f64) -> AppBarThemeData {
851        self.leading_width = Some(leading_width);
852        self
853    }
854
855    /// Dart `AppBarThemeData(toolbarHeight:)`.
856    pub fn toolbar_height(mut self, toolbar_height: f64) -> AppBarThemeData {
857        self.toolbar_height = Some(toolbar_height);
858        self
859    }
860
861    /// Dart `AppBarThemeData(toolbarTextStyle:)`.
862    pub fn toolbar_text_style(mut self, toolbar_text_style: TextStyle) -> AppBarThemeData {
863        self.toolbar_text_style = Some(toolbar_text_style);
864        self
865    }
866
867    /// Dart `AppBarThemeData(titleTextStyle:)`.
868    pub fn title_text_style(mut self, title_text_style: TextStyle) -> AppBarThemeData {
869        self.title_text_style = Some(title_text_style);
870        self
871    }
872
873    /// Dart `AppBarThemeData(systemOverlayStyle:)`.
874    pub fn system_overlay_style(
875        mut self,
876        system_overlay_style: SystemUiOverlayStyle,
877    ) -> AppBarThemeData {
878        self.system_overlay_style = Some(system_overlay_style);
879        self
880    }
881
882    /// Dart `AppBarThemeData(actionsPadding:)`.
883    pub fn actions_padding(
884        mut self,
885        actions_padding: impl Into<EdgeInsetsGeometry>,
886    ) -> AppBarThemeData {
887        let actions_padding = actions_padding.into();
888        self.actions_padding = Some(actions_padding);
889        self
890    }
891
892    /// Creates a copy of this object but with the given fields replaced with the
893    /// new values.
894    ///
895    /// Dart's named arguments are the fluent setters on the copy
896    /// (`data.copy_with().elevation(2.0)`).
897    pub fn copy_with(&self) -> AppBarThemeData {
898        self.clone()
899    }
900
901    /// Linearly interpolate between two app bar themes.
902    ///
903    /// {@macro dart.ui.shadow.lerp}
904    pub fn lerp(a: &AppBarThemeData, b: &AppBarThemeData, t: f64) -> AppBarThemeData {
905        if std::ptr::eq(a, b) {
906            return a.clone();
907        }
908        AppBarThemeData {
909            background_color: Color::lerp(a.background_color, b.background_color, t),
910            foreground_color: Color::lerp(a.foreground_color, b.foreground_color, t),
911            elevation: lerp_double(a.elevation, b.elevation, t),
912            scrolled_under_elevation: lerp_double(
913                a.scrolled_under_elevation,
914                b.scrolled_under_elevation,
915                t,
916            ),
917            shadow_color: Color::lerp(a.shadow_color, b.shadow_color, t),
918            surface_tint_color: Color::lerp(a.surface_tint_color, b.surface_tint_color, t),
919            shape: ShapeBorderRef::lerp(a.shape.as_ref(), b.shape.as_ref(), t),
920            icon_theme: Some(IconThemeData::lerp(
921                a.icon_theme.as_ref(),
922                b.icon_theme.as_ref(),
923                t,
924            )),
925            actions_icon_theme: Some(IconThemeData::lerp(
926                a.actions_icon_theme.as_ref(),
927                b.actions_icon_theme.as_ref(),
928                t,
929            )),
930            center_title: if t < 0.5 {
931                a.center_title
932            } else {
933                b.center_title
934            },
935            title_spacing: lerp_double(a.title_spacing, b.title_spacing, t),
936            leading_width: lerp_double(a.leading_width, b.leading_width, t),
937            toolbar_height: lerp_double(a.toolbar_height, b.toolbar_height, t),
938            toolbar_text_style: TextStyle::lerp(
939                a.toolbar_text_style.as_ref(),
940                b.toolbar_text_style.as_ref(),
941                t,
942            ),
943            title_text_style: TextStyle::lerp(
944                a.title_text_style.as_ref(),
945                b.title_text_style.as_ref(),
946                t,
947            ),
948            system_overlay_style: if t < 0.5 {
949                a.system_overlay_style
950            } else {
951                b.system_overlay_style
952            },
953            actions_padding: EdgeInsetsGeometry::lerp(a.actions_padding, b.actions_padding, t),
954        }
955    }
956}
957
958/// Dart's `SystemUiOverlayStyle` description in `AppBarThemeData.debugFillProperties`.
959fn system_overlay_style_description(style: &SystemUiOverlayStyle) -> String {
960    let mut properties = Vec::new();
961    if let Some(color) = style.system_navigation_bar_color {
962        properties.push(format!("systemNavigationBarColor: {color}"));
963    }
964    if let Some(color) = style.system_navigation_bar_divider_color {
965        properties.push(format!("systemNavigationBarDividerColor: {color}"));
966    }
967    if let Some(brightness) = style.system_navigation_bar_icon_brightness {
968        properties.push(format!(
969            "systemNavigationBarIconBrightness: {}",
970            waft_foundation::DartEnum(brightness)
971        ));
972    }
973    if let Some(color) = style.status_bar_color {
974        properties.push(format!("statusBarColor: {color}"));
975    }
976    if let Some(brightness) = style.status_bar_brightness {
977        properties.push(format!(
978            "statusBarBrightness: {}",
979            waft_foundation::DartEnum(brightness)
980        ));
981    }
982    if let Some(brightness) = style.status_bar_icon_brightness {
983        properties.push(format!(
984            "statusBarIconBrightness: {}",
985            waft_foundation::DartEnum(brightness)
986        ));
987    }
988    if let Some(enforced) = style.system_status_bar_contrast_enforced {
989        properties.push(format!("systemStatusBarContrastEnforced: {enforced}"));
990    }
991    if let Some(enforced) = style.system_navigation_bar_contrast_enforced {
992        properties.push(format!("systemNavigationBarContrastEnforced: {enforced}"));
993    }
994    format!("SystemUiOverlayStyle({})", properties.join(", "))
995}
996
997impl waft_foundation::Diagnosticable for AppBarThemeData {
998    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
999        properties
1000            .add(ColorProperty::new("backgroundColor", self.background_color).default_value(None));
1001        properties
1002            .add(ColorProperty::new("foregroundColor", self.foreground_color).default_value(None));
1003        properties.add(DoubleProperty::new("elevation", self.elevation).default_value(None));
1004        properties.add(
1005            DoubleProperty::new("scrolledUnderElevation", self.scrolled_under_elevation)
1006                .default_value(None),
1007        );
1008        properties.add(ColorProperty::new("shadowColor", self.shadow_color).default_value(None));
1009        properties.add(
1010            ColorProperty::new("surfaceTintColor", self.surface_tint_color).default_value(None),
1011        );
1012        properties.add(
1013            DiagnosticsProperty::<ShapeBorderRef>::new("shape", self.shape.clone())
1014                .default_value(None),
1015        );
1016        properties.add(
1017            DiagnosticsProperty::<waft_widgets::IconThemeData>::new(
1018                "iconTheme",
1019                self.icon_theme.clone(),
1020            )
1021            .default_value(None),
1022        );
1023        properties.add(
1024            DiagnosticsProperty::<waft_widgets::IconThemeData>::new(
1025                "actionsIconTheme",
1026                self.actions_icon_theme.clone(),
1027            )
1028            .default_value(None),
1029        );
1030        properties.add(
1031            DiagnosticsProperty::<bool>::new("centerTitle", self.center_title).default_value(None),
1032        );
1033        properties.add(DoubleProperty::new("titleSpacing", self.title_spacing).default_value(None));
1034        properties.add(DoubleProperty::new("leadingWidth", self.leading_width).default_value(None));
1035        properties
1036            .add(DoubleProperty::new("toolbarHeight", self.toolbar_height).default_value(None));
1037        properties.add(
1038            DiagnosticsProperty::<TextStyle>::new(
1039                "toolbarTextStyle",
1040                self.toolbar_text_style.clone(),
1041            )
1042            .default_value(None),
1043        );
1044        properties.add(
1045            DiagnosticsProperty::<TextStyle>::new("titleTextStyle", self.title_text_style.clone())
1046                .default_value(None),
1047        );
1048        properties.add(
1049            DiagnosticsProperty::<String>::new(
1050                "systemOverlayStyle",
1051                self.system_overlay_style
1052                    .as_ref()
1053                    .map(system_overlay_style_description),
1054            )
1055            .default_value(None),
1056        );
1057        properties.add(
1058            DiagnosticsProperty::<EdgeInsetsGeometry>::new("actionsPadding", self.actions_padding)
1059                .default_value(None),
1060        );
1061    }
1062}
1063
1064impl std::fmt::Display for AppBarThemeData {
1065    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1066        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
1067    }
1068}
1069
1070#[cfg(test)]
1071mod tests {
1072    use waft_painting::ShapeBorder;
1073    use waft_painting::{
1074        BorderRadiusGeometry, BorderSide, FontStyle, RoundedRectangleBorder, StadiumBorder,
1075    };
1076
1077    use waft_widgets::testing::test_widgets;
1078
1079    use super::*;
1080    use crate::colors::Colors;
1081    use crate::drawer_theme::tests::theme_seen_through_capture;
1082    use crate::test_support::probe;
1083    use crate::theme_data::ThemeData;
1084
1085    fn rounded(radius: f64) -> RoundedRectangleBorder {
1086        RoundedRectangleBorder::new(BorderSide::NONE, BorderRadiusGeometry::circular(radius))
1087    }
1088
1089    /// Dart's `appBarTheme` constant at the top of `app_bar_theme_test.dart`.
1090    fn app_bar_theme_data() -> AppBarThemeData {
1091        AppBarThemeData::new()
1092            .background_color(Color::new(0xff00ff00))
1093            .foreground_color(Color::new(0xff00ffff))
1094            .elevation(4.0)
1095            .scrolled_under_elevation(6.0)
1096            .shadow_color(Color::new(0xff1212ff))
1097            .shape(rounded(14.0))
1098            .icon_theme(IconThemeData::new().color(Color::new(0xffff0000)))
1099            .actions_icon_theme(IconThemeData::new().color(Color::new(0xff0000ff)))
1100            .center_title(false)
1101            .title_spacing(10.0)
1102            .title_text_style(
1103                TextStyle::new()
1104                    .font_size(22.0)
1105                    .font_style(FontStyle::Italic),
1106            )
1107    }
1108
1109    /// `AppBarTheme` with the same properties as [`app_bar_theme_data`], by the `with_*` setters.
1110    fn app_bar_theme_of_properties() -> AppBarTheme {
1111        AppBarTheme::new()
1112            .with_background_color(Color::new(0xff00ff00))
1113            .with_foreground_color(Color::new(0xff00ffff))
1114            .with_elevation(4.0)
1115            .with_scrolled_under_elevation(6.0)
1116            .with_shadow_color(Color::new(0xff1212ff))
1117            .with_shape(rounded(14.0))
1118            .with_icon_theme(IconThemeData::new().color(Color::new(0xffff0000)))
1119            .with_actions_icon_theme(IconThemeData::new().color(Color::new(0xff0000ff)))
1120            .with_center_title(false)
1121            .with_title_spacing(10.0)
1122            .with_title_text_style(
1123                TextStyle::new()
1124                    .font_size(22.0)
1125                    .font_style(FontStyle::Italic),
1126            )
1127    }
1128
1129    #[test]
1130    fn app_bar_theme_data_copy_with_equality_basics() {
1131        assert_eq!(AppBarThemeData::new(), AppBarThemeData::new().copy_with());
1132
1133        let theme = app_bar_theme_data();
1134        assert_eq!(theme.copy_with(), theme);
1135        assert_eq!(
1136            theme.copy_with().leading_width(40.0).leading_width,
1137            Some(40.0)
1138        );
1139        assert_eq!(theme.copy_with().leading_width(40.0).elevation, Some(4.0));
1140        assert_ne!(theme.copy_with().elevation(5.0), theme);
1141        assert_ne!(theme.copy_with().shape(rounded(9.0)), theme);
1142    }
1143
1144    #[test]
1145    fn app_bar_theme_data_null_fields_by_default() {
1146        let theme = AppBarThemeData::new();
1147        assert_eq!(theme.background_color, None);
1148        assert_eq!(theme.foreground_color, None);
1149        assert_eq!(theme.elevation, None);
1150        assert_eq!(theme.scrolled_under_elevation, None);
1151        assert_eq!(theme.shadow_color, None);
1152        assert_eq!(theme.surface_tint_color, None);
1153        assert!(theme.shape.is_none());
1154        assert_eq!(theme.icon_theme, None);
1155        assert_eq!(theme.actions_icon_theme, None);
1156        assert_eq!(theme.center_title, None);
1157        assert_eq!(theme.title_spacing, None);
1158        assert_eq!(theme.leading_width, None);
1159        assert_eq!(theme.toolbar_height, None);
1160        assert_eq!(theme.toolbar_text_style, None);
1161        assert_eq!(theme.title_text_style, None);
1162        assert_eq!(theme.system_overlay_style, None);
1163        assert_eq!(theme.actions_padding, None);
1164    }
1165
1166    #[test]
1167    #[allow(deprecated)]
1168    fn app_bar_theme_data_color_is_the_background_color() {
1169        let theme = AppBarThemeData::new().color(Colors::RED.primary());
1170
1171        assert_eq!(theme.background_color, Some(Colors::RED.primary()));
1172        assert_eq!(
1173            app_bar_theme_data()
1174                .copy_with()
1175                .color(Colors::RED.primary())
1176                .background_color,
1177            Some(Colors::RED.primary())
1178        );
1179    }
1180
1181    #[test]
1182    fn app_bar_theme_lerp_special_cases() {
1183        let data = AppBarTheme::new();
1184        assert_eq!(
1185            AppBarTheme::lerp(Some(&data), Some(&data), 0.5).data(),
1186            data.data()
1187        );
1188
1189        let data = app_bar_theme_data();
1190        assert_eq!(AppBarThemeData::lerp(&data, &data, 0.5), data);
1191    }
1192
1193    #[test]
1194    fn app_bar_theme_data_lerps_each_field() {
1195        let a = AppBarThemeData::new()
1196            .background_color(Colors::BLACK)
1197            .elevation(2.0)
1198            .shape(rounded(4.0))
1199            .icon_theme(IconThemeData::new().size(10.0))
1200            .center_title(true)
1201            .title_spacing(10.0)
1202            .toolbar_height(40.0)
1203            .title_text_style(TextStyle::new().font_size(10.0))
1204            .system_overlay_style(SystemUiOverlayStyle::LIGHT)
1205            .actions_padding(EdgeInsetsGeometry::all(10.0));
1206        let b = AppBarThemeData::new()
1207            .background_color(Colors::WHITE)
1208            .foreground_color(Colors::RED.primary())
1209            .elevation(6.0)
1210            .shape(rounded(8.0))
1211            .icon_theme(IconThemeData::new().size(30.0))
1212            .center_title(false)
1213            .title_spacing(30.0)
1214            .toolbar_height(80.0)
1215            .title_text_style(TextStyle::new().font_size(20.0))
1216            .system_overlay_style(SystemUiOverlayStyle::DARK)
1217            .actions_padding(EdgeInsetsGeometry::all(30.0));
1218
1219        let halfway = AppBarThemeData::lerp(&a, &b, 0.5);
1220
1221        assert_eq!(
1222            halfway.background_color,
1223            Color::lerp(Some(Colors::BLACK), Some(Colors::WHITE), 0.5)
1224        );
1225        assert_eq!(
1226            halfway.foreground_color,
1227            Color::lerp(None, Some(Colors::RED.primary()), 0.5)
1228        );
1229        assert_eq!(halfway.elevation, Some(4.0));
1230        assert_eq!(
1231            halfway.shape.as_deref(),
1232            Some(&rounded(6.0) as &dyn ShapeBorder)
1233        );
1234        assert_eq!(halfway.icon_theme, Some(IconThemeData::new().size(20.0)));
1235        assert_eq!(halfway.center_title, Some(false));
1236        assert_eq!(halfway.title_spacing, Some(20.0));
1237        assert_eq!(halfway.toolbar_height, Some(60.0));
1238        assert_eq!(
1239            halfway.title_text_style.and_then(|style| style.font_size),
1240            Some(15.0)
1241        );
1242        assert_eq!(
1243            halfway.system_overlay_style,
1244            Some(SystemUiOverlayStyle::DARK)
1245        );
1246        assert_eq!(halfway.actions_padding, Some(EdgeInsetsGeometry::all(20.0)));
1247
1248        let early = AppBarThemeData::lerp(&a, &b, 0.25);
1249        assert_eq!(early.center_title, Some(true));
1250        assert_eq!(
1251            early.system_overlay_style,
1252            Some(SystemUiOverlayStyle::LIGHT)
1253        );
1254    }
1255
1256    #[test]
1257    fn app_bar_theme_data_lerp_gives_an_icon_theme_even_when_neither_side_has_one() {
1258        let lerped = AppBarThemeData::lerp(&AppBarThemeData::new(), &AppBarThemeData::new(), 0.5);
1259
1260        assert_eq!(lerped.icon_theme, Some(IconThemeData::new()));
1261        assert_eq!(lerped.actions_icon_theme, Some(IconThemeData::new()));
1262    }
1263
1264    #[test]
1265    fn app_bar_theme_getters_read_the_properties_it_was_given() {
1266        let theme = app_bar_theme_of_properties();
1267
1268        assert_eq!(theme.background_color(), Some(Color::new(0xff00ff00)));
1269        assert_eq!(theme.foreground_color(), Some(Color::new(0xff00ffff)));
1270        assert_eq!(theme.elevation(), Some(4.0));
1271        assert_eq!(theme.scrolled_under_elevation(), Some(6.0));
1272        assert_eq!(theme.shadow_color(), Some(Color::new(0xff1212ff)));
1273        assert_eq!(theme.surface_tint_color(), None);
1274        assert_eq!(theme.shape(), Some(&ShapeBorderRef::new(rounded(14.0))));
1275        assert_eq!(
1276            theme.icon_theme(),
1277            Some(&IconThemeData::new().color(Color::new(0xffff0000)))
1278        );
1279        assert_eq!(theme.center_title(), Some(false));
1280        assert_eq!(theme.title_spacing(), Some(10.0));
1281        assert_eq!(theme.system_overlay_style(), None);
1282        assert_eq!(theme.data(), app_bar_theme_data());
1283    }
1284
1285    #[test]
1286    fn app_bar_theme_getters_read_its_data_when_it_has_one() {
1287        let theme = AppBarTheme::new().with_data(app_bar_theme_data());
1288
1289        assert_eq!(theme.background_color(), Some(Color::new(0xff00ff00)));
1290        assert_eq!(theme.elevation(), Some(4.0));
1291        assert_eq!(theme.shape(), Some(&ShapeBorderRef::new(rounded(14.0))));
1292        assert_eq!(theme.center_title(), Some(false));
1293        assert_eq!(theme.data(), app_bar_theme_data());
1294    }
1295
1296    #[test]
1297    #[allow(deprecated)]
1298    fn app_bar_theme_color_is_the_background_color() {
1299        assert_eq!(
1300            AppBarTheme::new()
1301                .with_color(Colors::RED.primary())
1302                .background_color(),
1303            Some(Colors::RED.primary())
1304        );
1305    }
1306
1307    #[test]
1308    fn app_bar_theme_copy_with_keeps_the_properties_it_is_not_given() {
1309        let theme = app_bar_theme_of_properties();
1310
1311        let copy = theme.copy_with().with_elevation(8.0);
1312
1313        assert_eq!(copy.elevation(), Some(8.0));
1314        assert_eq!(copy.background_color(), Some(Color::new(0xff00ff00)));
1315        assert_eq!(copy.data(), app_bar_theme_data().copy_with().elevation(8.0));
1316        assert_eq!(
1317            AppBarTheme::new()
1318                .with_data(app_bar_theme_data())
1319                .copy_with()
1320                .with_actions_padding(EdgeInsetsGeometry::all(2.0))
1321                .data(),
1322            app_bar_theme_data().actions_padding(EdgeInsetsGeometry::all(2.0))
1323        );
1324    }
1325
1326    #[test]
1327    #[should_panic]
1328    fn app_bar_theme_asserts_when_data_is_given_after_a_property() {
1329        AppBarTheme::new()
1330            .with_elevation(1.0)
1331            .with_data(AppBarThemeData::new());
1332    }
1333
1334    #[test]
1335    #[should_panic]
1336    fn app_bar_theme_asserts_when_a_property_is_given_after_data() {
1337        AppBarTheme::new()
1338            .with_data(AppBarThemeData::new())
1339            .with_elevation(1.0);
1340    }
1341
1342    #[test]
1343    fn of_returns_the_theme_data_app_bar_theme_without_an_ancestor() {
1344        test_widgets(|tester| {
1345            let expected = app_bar_theme_data();
1346            let theme_data_app_bar = expected.clone();
1347
1348            let seen = probe(
1349                tester,
1350                move |app, child| {
1351                    let data = ThemeData::builder()
1352                        .app_bar_theme(theme_data_app_bar.clone())
1353                        .build(app);
1354                    Theme::new(data, child).into_widget()
1355                },
1356                AppBarTheme::of,
1357            );
1358
1359            assert_eq!(seen, expected);
1360        });
1361    }
1362
1363    #[test]
1364    fn of_returns_the_closest_app_bar_theme_over_theme_data() {
1365        test_widgets(|tester| {
1366            let expected = app_bar_theme_data();
1367
1368            let seen = probe(
1369                tester,
1370                move |app, child| {
1371                    let data = ThemeData::builder()
1372                        .app_bar_theme(AppBarThemeData::new().elevation(3.0))
1373                        .build(app);
1374                    Theme::new(data, app_bar_theme_of_properties().child(child)).into_widget()
1375                },
1376                AppBarTheme::of,
1377            );
1378
1379            assert_eq!(seen, expected);
1380        });
1381    }
1382
1383    #[test]
1384    fn a_captured_app_bar_theme_is_kept_under_another_app_bar_theme() {
1385        test_widgets(|tester| {
1386            let seen = theme_seen_through_capture(
1387                tester,
1388                |probe| {
1389                    AppBarTheme::new()
1390                        .with_elevation(1.0)
1391                        .child(probe)
1392                        .into_widget()
1393                },
1394                |wrapped| {
1395                    AppBarTheme::new()
1396                        .with_data(AppBarThemeData::new().elevation(2.0))
1397                        .child(wrapped)
1398                        .into_widget()
1399                },
1400                AppBarTheme::of,
1401            );
1402
1403            assert_eq!(seen.elevation, Some(1.0));
1404        });
1405    }
1406
1407    #[test]
1408    fn update_should_notify_only_when_the_data_differs() {
1409        let old = AppBarTheme::new().with_elevation(1.0);
1410
1411        assert!(
1412            !AppBarTheme::new()
1413                .with_data(AppBarThemeData::new().elevation(1.0))
1414                .update_should_notify(&old)
1415        );
1416        assert!(
1417            AppBarTheme::new()
1418                .with_elevation(2.0)
1419                .update_should_notify(&old)
1420        );
1421    }
1422
1423    #[test]
1424    fn theme_data_holds_the_app_bar_theme() {
1425        test_widgets(|tester| {
1426            let app = tester.cell.borrow();
1427            let theme = ThemeData::new(&app);
1428            assert_eq!(theme.app_bar_theme, AppBarThemeData::new());
1429
1430            let raised = ThemeData::builder()
1431                .app_bar_theme(AppBarThemeData::new().elevation(8.0))
1432                .build(&app);
1433            assert_eq!(raised.app_bar_theme.elevation, Some(8.0));
1434            assert_ne!(theme, raised);
1435            assert_eq!(
1436                theme
1437                    .copy_with()
1438                    .app_bar_theme(raised.app_bar_theme.clone())
1439                    .build(),
1440                raised
1441            );
1442
1443            let flat = ThemeData::builder()
1444                .app_bar_theme(AppBarThemeData::new().elevation(0.0))
1445                .build(&app);
1446            assert_eq!(
1447                ThemeData::lerp(&flat, &raised, 0.5).app_bar_theme.elevation,
1448                Some(4.0)
1449            );
1450        });
1451    }
1452
1453    #[test]
1454    fn theme_data_takes_the_data_of_an_app_bar_theme() {
1455        test_widgets(|tester| {
1456            let app = tester.cell.borrow();
1457
1458            let built = ThemeData::builder()
1459                .app_bar_theme(app_bar_theme_of_properties())
1460                .build(&app);
1461            let copied = ThemeData::new(&app)
1462                .copy_with()
1463                .app_bar_theme(app_bar_theme_of_properties())
1464                .build();
1465
1466            assert_eq!(built.app_bar_theme, app_bar_theme_data());
1467            assert_eq!(copied.app_bar_theme, app_bar_theme_data());
1468        });
1469    }
1470    // Flutter's "Default AppBarThemeData debugFillProperties".
1471    #[test]
1472    fn a_app_bar_theme_data_describes_nothing_by_default() {
1473        assert_eq!(
1474            waft_widgets::testing::describe_properties(|properties| {
1475                waft_foundation::Diagnosticable::debug_fill_properties(
1476                    &AppBarThemeData::new(),
1477                    properties,
1478                )
1479            }),
1480            Vec::<String>::new()
1481        );
1482    }
1483
1484    // app_bar_theme_test.dart 'AppBarThemeData implements debugFillProperties'.
1485    #[test]
1486    fn an_assigned_app_bar_theme_describes_every_property() {
1487        let theme = AppBarThemeData::new()
1488            .background_color(Color::new(0xff000000))
1489            .foreground_color(Color::new(0xff000001))
1490            .elevation(8.0)
1491            .scrolled_under_elevation(3.0)
1492            .shadow_color(Color::new(0xff000002))
1493            .surface_tint_color(Color::new(0xff000003))
1494            .shape(StadiumBorder::default())
1495            .icon_theme(IconThemeData::new().color(Color::new(0xff000004)))
1496            .actions_icon_theme(IconThemeData::new().color(Color::new(0xff000004)))
1497            .center_title(true)
1498            .title_spacing(40.0)
1499            .leading_width(96.0)
1500            .toolbar_height(96.0)
1501            .toolbar_text_style(TextStyle::new().color(Color::new(0xff000005)))
1502            .title_text_style(TextStyle::new().color(Color::new(0xff000006)))
1503            .system_overlay_style(
1504                SystemUiOverlayStyle::default().system_navigation_bar_color(Color::new(0xff000007)),
1505            )
1506            .actions_padding(EdgeInsetsGeometry::symmetric(0.0, 8.0));
1507        let description = waft_widgets::testing::describe_properties(|properties| {
1508            waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
1509        });
1510        let description: Vec<String> = description
1511            .iter()
1512            .map(|line| waft_rendering::testing::ignoring_hash_codes(line))
1513            .collect();
1514        assert_eq!(
1515            description,
1516            [
1517                format!("backgroundColor: {}", Color::new(0xff000000)),
1518                format!("foregroundColor: {}", Color::new(0xff000001)),
1519                "elevation: 8.0".to_string(),
1520                "scrolledUnderElevation: 3.0".to_string(),
1521                format!("shadowColor: {}", Color::new(0xff000002)),
1522                format!("surfaceTintColor: {}", Color::new(0xff000003)),
1523                format!("shape: {}", ShapeBorderRef::from(StadiumBorder::default())),
1524                format!(
1525                    "iconTheme: IconThemeData#00000(color: {})",
1526                    Color::new(0xff000004)
1527                ),
1528                format!(
1529                    "actionsIconTheme: IconThemeData#00000(color: {})",
1530                    Color::new(0xff000004)
1531                ),
1532                "centerTitle: true".to_string(),
1533                "titleSpacing: 40.0".to_string(),
1534                "leadingWidth: 96.0".to_string(),
1535                "toolbarHeight: 96.0".to_string(),
1536                format!(
1537                    "toolbarTextStyle: {}",
1538                    TextStyle::new().color(Color::new(0xff000005))
1539                ),
1540                format!(
1541                    "titleTextStyle: {}",
1542                    TextStyle::new().color(Color::new(0xff000006))
1543                ),
1544                format!(
1545                    "systemOverlayStyle: SystemUiOverlayStyle(systemNavigationBarColor: {})",
1546                    Color::new(0xff000007)
1547                ),
1548                "actionsPadding: EdgeInsets(8.0, 0.0, 8.0, 0.0)".to_string(),
1549            ]
1550        );
1551    }
1552}