Skip to main content

waft_material/
search_bar_theme.rs

1//! Flutter counterpart: material_ui's `search_bar_theme.dart`.
2
3use std::fmt::{self, Debug};
4
5use waft_embedder::{Color, TextCapitalization, lerp_double};
6use waft_foundation::{App, DiagnosticsProperty};
7use waft_painting::{BorderSide, EdgeInsetsGeometry, OutlinedBorderRef, TextStyle};
8use waft_rendering::BoxConstraints;
9use waft_widgets::{
10    BuildContext, InheritedWidget, IntoWidget, KeyRef, WidgetRef, WidgetStateBorderSide,
11    WidgetStateProperty, WidgetStatePropertyRef,
12};
13
14use crate::theme::Theme;
15
16/// Defines default property values for descendant `SearchBar` widgets.
17///
18/// Descendant widgets obtain the current [`SearchBarThemeData`] object using
19/// [`SearchBarTheme::of`]. Instances of [`SearchBarThemeData`] can be customized with
20/// [`SearchBarThemeData::copy_with`].
21///
22/// Typically a [`SearchBarThemeData`] is specified as part of the overall `Theme` with
23/// `ThemeData::search_bar_theme`.
24///
25/// All [`SearchBarThemeData`] properties are `None` by default. When null, the `SearchBar`
26/// will use the values from `ThemeData` if they exist, otherwise it will provide its own
27/// defaults based on the overall `Theme`'s colorScheme. See the individual `SearchBar`
28/// properties for details.
29///
30/// See also:
31///
32///  * `ThemeData`, which describes the overall theme information for the application.
33///  * `SearchViewThemeData`, which describes the theme for the search view of a
34///    `SearchAnchor`.
35#[derive(Clone, Default)]
36pub struct SearchBarThemeData {
37    /// Overrides the default value of the `SearchBar::elevation`.
38    pub elevation: Option<WidgetStatePropertyRef<Option<f64>>>,
39
40    /// Overrides the default value of the `SearchBar::background_color`.
41    pub background_color: Option<WidgetStatePropertyRef<Option<Color>>>,
42
43    /// Overrides the default value of the `SearchBar::shadow_color`.
44    pub shadow_color: Option<WidgetStatePropertyRef<Option<Color>>>,
45
46    /// Overrides the default value of the `SearchBar::surface_tint_color`.
47    pub surface_tint_color: Option<WidgetStatePropertyRef<Option<Color>>>,
48
49    /// Overrides the default value of the `SearchBar::overlay_color`.
50    pub overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>,
51
52    /// Overrides the default value of the `SearchBar::side`.
53    pub side: Option<WidgetStatePropertyRef<Option<BorderSide>>>,
54
55    /// Overrides the default value of the `SearchBar::shape`.
56    pub shape: Option<WidgetStatePropertyRef<Option<OutlinedBorderRef>>>,
57
58    /// Overrides the default value for `SearchBar::padding`.
59    pub padding: Option<WidgetStatePropertyRef<Option<EdgeInsetsGeometry>>>,
60
61    /// Overrides the default value for `SearchBar::text_style`.
62    pub text_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
63
64    /// Overrides the default value for `SearchBar::hint_style`.
65    pub hint_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
66
67    /// Overrides the value of size constraints for `SearchBar`.
68    pub constraints: Option<BoxConstraints>,
69
70    /// Overrides the value of `SearchBar::text_capitalization`.
71    pub text_capitalization: Option<TextCapitalization>,
72}
73
74crate::theme_data_getters::theme_data_getters! {
75    /// Dart's getters of `SearchBarThemeData`: the theme data answers with its fields, and
76    /// `_SearchBarDefaultsM3(context)` computes the ones it overrides when they are read.
77    pub trait SearchBarThemeDataGetters for SearchBarThemeData, fields: search_bar_theme_data {
78        elevation: Option<WidgetStatePropertyRef<Option<f64>>>,
79        background_color: Option<WidgetStatePropertyRef<Option<Color>>>,
80        shadow_color: Option<WidgetStatePropertyRef<Option<Color>>>,
81        surface_tint_color: Option<WidgetStatePropertyRef<Option<Color>>>,
82        overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>,
83        side: Option<WidgetStatePropertyRef<Option<BorderSide>>>,
84        shape: Option<WidgetStatePropertyRef<Option<OutlinedBorderRef>>>,
85        padding: Option<WidgetStatePropertyRef<Option<EdgeInsetsGeometry>>>,
86        text_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
87        hint_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
88        constraints: Option<BoxConstraints>,
89        text_capitalization: Option<TextCapitalization>,
90    }
91}
92
93impl SearchBarThemeData {
94    /// Creates a theme that can be used for `ThemeData::search_bar_theme`.
95    pub fn new() -> SearchBarThemeData {
96        SearchBarThemeData::default()
97    }
98
99    /// Dart `SearchBarThemeData(elevation:)`.
100    pub fn elevation(
101        mut self,
102        elevation: WidgetStatePropertyRef<Option<f64>>,
103    ) -> SearchBarThemeData {
104        self.elevation = Some(elevation);
105        self
106    }
107
108    /// Dart `SearchBarThemeData(backgroundColor:)`.
109    pub fn background_color(
110        mut self,
111        background_color: WidgetStatePropertyRef<Option<Color>>,
112    ) -> SearchBarThemeData {
113        self.background_color = Some(background_color);
114        self
115    }
116
117    /// Dart `SearchBarThemeData(shadowColor:)`.
118    pub fn shadow_color(
119        mut self,
120        shadow_color: WidgetStatePropertyRef<Option<Color>>,
121    ) -> SearchBarThemeData {
122        self.shadow_color = Some(shadow_color);
123        self
124    }
125
126    /// Dart `SearchBarThemeData(surfaceTintColor:)`.
127    pub fn surface_tint_color(
128        mut self,
129        surface_tint_color: WidgetStatePropertyRef<Option<Color>>,
130    ) -> SearchBarThemeData {
131        self.surface_tint_color = Some(surface_tint_color);
132        self
133    }
134
135    /// Dart `SearchBarThemeData(overlayColor:)`.
136    pub fn overlay_color(
137        mut self,
138        overlay_color: WidgetStatePropertyRef<Option<Color>>,
139    ) -> SearchBarThemeData {
140        self.overlay_color = Some(overlay_color);
141        self
142    }
143
144    /// Dart `SearchBarThemeData(side:)`.
145    pub fn side(mut self, side: WidgetStatePropertyRef<Option<BorderSide>>) -> SearchBarThemeData {
146        self.side = Some(side);
147        self
148    }
149
150    /// Dart `SearchBarThemeData(shape:)`.
151    pub fn shape(
152        mut self,
153        shape: WidgetStatePropertyRef<Option<OutlinedBorderRef>>,
154    ) -> SearchBarThemeData {
155        self.shape = Some(shape);
156        self
157    }
158
159    /// Dart `SearchBarThemeData(padding:)`.
160    pub fn padding(
161        mut self,
162        padding: WidgetStatePropertyRef<Option<EdgeInsetsGeometry>>,
163    ) -> SearchBarThemeData {
164        self.padding = Some(padding);
165        self
166    }
167
168    /// Dart `SearchBarThemeData(textStyle:)`.
169    pub fn text_style(
170        mut self,
171        text_style: WidgetStatePropertyRef<Option<TextStyle>>,
172    ) -> SearchBarThemeData {
173        self.text_style = Some(text_style);
174        self
175    }
176
177    /// Dart `SearchBarThemeData(hintStyle:)`.
178    pub fn hint_style(
179        mut self,
180        hint_style: WidgetStatePropertyRef<Option<TextStyle>>,
181    ) -> SearchBarThemeData {
182        self.hint_style = Some(hint_style);
183        self
184    }
185
186    /// Dart `SearchBarThemeData(constraints:)`.
187    pub fn constraints(mut self, constraints: BoxConstraints) -> SearchBarThemeData {
188        self.constraints = Some(constraints);
189        self
190    }
191
192    /// Dart `SearchBarThemeData(textCapitalization:)`.
193    pub fn text_capitalization(
194        mut self,
195        text_capitalization: TextCapitalization,
196    ) -> SearchBarThemeData {
197        self.text_capitalization = Some(text_capitalization);
198        self
199    }
200
201    /// Creates a copy of this object but with the given fields replaced with the new values:
202    /// `data.copy_with().elevation(..)`.
203    pub fn copy_with(&self) -> SearchBarThemeData {
204        self.clone()
205    }
206
207    /// Linearly interpolate between two [`SearchBarThemeData`].
208    pub fn lerp(
209        a: Option<&SearchBarThemeData>,
210        b: Option<&SearchBarThemeData>,
211        t: f64,
212    ) -> Option<SearchBarThemeData> {
213        match (a, b) {
214            (None, None) => return None,
215            (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
216            _ => {}
217        }
218        Some(SearchBarThemeData {
219            elevation: <dyn WidgetStateProperty<Option<_>>>::lerp_optional(
220                a.and_then(|a| a.elevation.clone()),
221                b.and_then(|b| b.elevation.clone()),
222                t,
223                lerp_double,
224            ),
225            background_color: <dyn WidgetStateProperty<Option<_>>>::lerp_optional(
226                a.and_then(|a| a.background_color.clone()),
227                b.and_then(|b| b.background_color.clone()),
228                t,
229                Color::lerp,
230            ),
231            shadow_color: <dyn WidgetStateProperty<Option<_>>>::lerp_optional(
232                a.and_then(|a| a.shadow_color.clone()),
233                b.and_then(|b| b.shadow_color.clone()),
234                t,
235                Color::lerp,
236            ),
237            surface_tint_color: <dyn WidgetStateProperty<Option<_>>>::lerp_optional(
238                a.and_then(|a| a.surface_tint_color.clone()),
239                b.and_then(|b| b.surface_tint_color.clone()),
240                t,
241                Color::lerp,
242            ),
243            overlay_color: <dyn WidgetStateProperty<Option<_>>>::lerp_optional(
244                a.and_then(|a| a.overlay_color.clone()),
245                b.and_then(|b| b.overlay_color.clone()),
246                t,
247                Color::lerp,
248            ),
249            side: WidgetStateBorderSide::lerp(
250                a.and_then(|a| a.side.clone()),
251                b.and_then(|b| b.side.clone()),
252                t,
253            ),
254            shape: <dyn WidgetStateProperty<Option<_>>>::lerp_optional(
255                a.and_then(|a| a.shape.clone()),
256                b.and_then(|b| b.shape.clone()),
257                t,
258                |a, b, t| OutlinedBorderRef::lerp(a.as_ref(), b.as_ref(), t),
259            ),
260            padding: <dyn WidgetStateProperty<Option<_>>>::lerp_optional(
261                a.and_then(|a| a.padding.clone()),
262                b.and_then(|b| b.padding.clone()),
263                t,
264                EdgeInsetsGeometry::lerp,
265            ),
266            text_style: <dyn WidgetStateProperty<Option<_>>>::lerp_optional(
267                a.and_then(|a| a.text_style.clone()),
268                b.and_then(|b| b.text_style.clone()),
269                t,
270                |a, b, t| TextStyle::lerp(a.as_ref(), b.as_ref(), t),
271            ),
272            hint_style: <dyn WidgetStateProperty<Option<_>>>::lerp_optional(
273                a.and_then(|a| a.hint_style.clone()),
274                b.and_then(|b| b.hint_style.clone()),
275                t,
276                |a, b, t| TextStyle::lerp(a.as_ref(), b.as_ref(), t),
277            ),
278            constraints: BoxConstraints::lerp(
279                a.and_then(|a| a.constraints),
280                b.and_then(|b| b.constraints),
281                t,
282            ),
283            text_capitalization: if t < 0.5 {
284                a.and_then(|a| a.text_capitalization)
285            } else {
286                b.and_then(|b| b.text_capitalization)
287            },
288        })
289    }
290}
291
292impl PartialEq for SearchBarThemeData {
293    fn eq(&self, other: &SearchBarThemeData) -> bool {
294        if std::ptr::eq(self, other) {
295            return true;
296        }
297        other.elevation == self.elevation
298            && other.background_color == self.background_color
299            && other.shadow_color == self.shadow_color
300            && other.surface_tint_color == self.surface_tint_color
301            && other.overlay_color == self.overlay_color
302            && other.side == self.side
303            && other.shape == self.shape
304            && other.padding == self.padding
305            && other.text_style == self.text_style
306            && other.hint_style == self.hint_style
307            && other.constraints == self.constraints
308            && other.text_capitalization == self.text_capitalization
309    }
310}
311
312impl Debug for SearchBarThemeData {
313    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314        let mut properties = f.debug_struct("SearchBarThemeData");
315        macro_rules! property {
316            ($($field:ident),* $(,)?) => {
317                $(
318                    if let Some(value) = &self.$field {
319                        properties.field(stringify!($field), value);
320                    }
321                )*
322            };
323        }
324        property!(
325            elevation,
326            background_color,
327            shadow_color,
328            surface_tint_color,
329            overlay_color,
330            side,
331            shape,
332            padding,
333            text_style,
334            hint_style,
335            constraints,
336            text_capitalization,
337        );
338        properties.finish()
339    }
340}
341
342/// Applies a search bar theme to descendant `SearchBar` widgets.
343///
344/// Descendant widgets obtain the current theme's [`SearchBarTheme`] object using
345/// [`SearchBarTheme::of`]. When a widget uses [`SearchBarTheme::of`], it is automatically
346/// rebuilt if the theme later changes.
347///
348/// A search bar theme can be specified as part of the overall Material theme using
349/// `ThemeData::search_bar_theme`.
350///
351/// See also:
352///
353///  * [`SearchBarThemeData`], which describes the actual configuration of a search bar
354///    theme.
355#[derive(Debug)]
356pub struct SearchBarTheme {
357    pub key: Option<KeyRef>,
358    /// Specifies the default color, shape, and text style values for descendant `SearchBar`
359    /// widgets.
360    pub data: SearchBarThemeData,
361    pub child: WidgetRef,
362}
363
364impl SearchBarTheme {
365    /// Creates a search bar theme that controls the configurations for descendant
366    /// `SearchBar` widgets.
367    #[cfg_attr(debug_assertions, track_caller)]
368    pub fn new<K>(data: SearchBarThemeData, child: impl IntoWidget<K>) -> SearchBarTheme {
369        SearchBarTheme {
370            key: None,
371            data,
372            child: child.into_widget(),
373        }
374    }
375
376    /// Dart `SearchBarTheme(key:)`.
377    pub fn key(mut self, key: KeyRef) -> SearchBarTheme {
378        self.key = Some(key);
379        self
380    }
381
382    /// Retrieves the [`SearchBarThemeData`] from the closest ancestor [`SearchBarTheme`].
383    ///
384    /// If there is no enclosing [`SearchBarTheme`] widget, then
385    /// `ThemeData::search_bar_theme` is used.
386    pub fn of(app: &mut App, context: BuildContext) -> SearchBarThemeData {
387        match context.depend_on_inherited_widget_of_exact_type::<SearchBarTheme>(app) {
388            Some(search_bar_theme) => search_bar_theme.data.clone(),
389            None => Theme::of(app, context).search_bar_theme.clone(),
390        }
391    }
392}
393
394impl InheritedWidget for SearchBarTheme {
395    fn key(&self) -> Option<&KeyRef> {
396        self.key.as_ref()
397    }
398
399    fn child(&self) -> &WidgetRef {
400        &self.child
401    }
402
403    fn update_should_notify(&self, old_widget: &SearchBarTheme) -> bool {
404        self.data != old_widget.data
405    }
406}
407
408impl waft_foundation::Diagnosticable for SearchBarThemeData {
409    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
410        properties.add(
411            DiagnosticsProperty::<String>::new(
412                "elevation",
413                self.elevation
414                    .as_deref()
415                    .map(|property| property.to_string()),
416            )
417            .default_value(None),
418        );
419        properties.add(
420            DiagnosticsProperty::<String>::new(
421                "backgroundColor",
422                self.background_color
423                    .as_deref()
424                    .map(|property| property.to_string()),
425            )
426            .default_value(None),
427        );
428        properties.add(
429            DiagnosticsProperty::<String>::new(
430                "shadowColor",
431                self.shadow_color
432                    .as_deref()
433                    .map(|property| property.to_string()),
434            )
435            .default_value(None),
436        );
437        properties.add(
438            DiagnosticsProperty::<String>::new(
439                "surfaceTintColor",
440                self.surface_tint_color
441                    .as_deref()
442                    .map(|property| property.to_string()),
443            )
444            .default_value(None),
445        );
446        properties.add(
447            DiagnosticsProperty::<String>::new(
448                "overlayColor",
449                self.overlay_color
450                    .as_deref()
451                    .map(|property| property.to_string()),
452            )
453            .default_value(None),
454        );
455        properties.add(
456            DiagnosticsProperty::<String>::new(
457                "side",
458                self.side.as_deref().map(|property| property.to_string()),
459            )
460            .default_value(None),
461        );
462        properties.add(
463            DiagnosticsProperty::<String>::new(
464                "shape",
465                self.shape.as_deref().map(|property| property.to_string()),
466            )
467            .default_value(None),
468        );
469        properties.add(
470            DiagnosticsProperty::<String>::new(
471                "padding",
472                self.padding.as_deref().map(|property| property.to_string()),
473            )
474            .default_value(None),
475        );
476        properties.add(
477            DiagnosticsProperty::<String>::new(
478                "textStyle",
479                self.text_style
480                    .as_deref()
481                    .map(|property| property.to_string()),
482            )
483            .default_value(None),
484        );
485        properties.add(
486            DiagnosticsProperty::<String>::new(
487                "hintStyle",
488                self.hint_style
489                    .as_deref()
490                    .map(|property| property.to_string()),
491            )
492            .default_value(None),
493        );
494        properties.add(
495            DiagnosticsProperty::<BoxConstraints>::new("constraints", self.constraints)
496                .default_value(None),
497        );
498        properties.add(
499            DiagnosticsProperty::<waft_foundation::DartEnum<TextCapitalization>>::new(
500                "textCapitalization",
501                self.text_capitalization.map(waft_foundation::DartEnum),
502            )
503            .default_value(None),
504        );
505    }
506}
507
508impl std::fmt::Display for SearchBarThemeData {
509    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
510        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
511    }
512}
513
514#[cfg(test)]
515mod tests {
516    use waft_embedder::Size;
517    use waft_painting::{BorderRadiusGeometry, EdgeInsets, RoundedRectangleBorder};
518    use waft_widgets::testing::{WidgetTester, find, test_widgets};
519    use waft_widgets::{
520        Builder, Center, EditableText, Icon, SizedBox, Text, WidgetState, WidgetStatePropertyAll,
521        WidgetStates,
522    };
523
524    use super::*;
525    use crate::color_scheme::ColorScheme;
526    use crate::icons::Icons;
527    use crate::ink_well::InkWell;
528    use crate::material::Material;
529    use crate::scaffold::Scaffold;
530    use crate::search_anchor::SearchBar;
531    use crate::test_support::material_app;
532    use crate::text_field::TextField;
533    use crate::theme_data::ThemeData;
534    use std::rc::Rc;
535
536    // search_bar_theme_test.dart 'SearchBarThemeData copyWith, ==, hashCode basics'.
537    #[test]
538    fn search_bar_theme_data_copy_with_equals_basics() {
539        assert_eq!(
540            SearchBarThemeData::new(),
541            SearchBarThemeData::new().copy_with()
542        );
543    }
544
545    // search_bar_theme_test.dart 'SearchBarThemeData lerp special cases'.
546    #[test]
547    fn search_bar_theme_data_lerp_special_cases() {
548        assert_eq!(SearchBarThemeData::lerp(None, None, 0.0), None);
549        let data = SearchBarThemeData::new();
550        assert_eq!(
551            SearchBarThemeData::lerp(Some(&data), Some(&data), 0.5),
552            Some(data)
553        );
554    }
555
556    // search_bar_theme_test.dart 'SearchBarThemeData defaults'.
557    #[test]
558    fn search_bar_theme_data_defaults_are_all_null() {
559        let theme_data = SearchBarThemeData::new();
560        assert!(theme_data.elevation.is_none());
561        assert!(theme_data.background_color.is_none());
562        assert!(theme_data.shadow_color.is_none());
563        assert!(theme_data.surface_tint_color.is_none());
564        assert!(theme_data.overlay_color.is_none());
565        assert!(theme_data.side.is_none());
566        assert!(theme_data.shape.is_none());
567        assert!(theme_data.padding.is_none());
568        assert!(theme_data.text_style.is_none());
569        assert!(theme_data.hint_style.is_none());
570        assert_eq!(theme_data.constraints, None);
571        assert_eq!(theme_data.text_capitalization, None);
572
573        let theme = SearchBarTheme::new(SearchBarThemeData::new(), SizedBox::new());
574        assert!(theme.data.elevation.is_none());
575        assert!(theme.data.background_color.is_none());
576        assert!(theme.data.shadow_color.is_none());
577        assert!(theme.data.surface_tint_color.is_none());
578        assert!(theme.data.overlay_color.is_none());
579        assert!(theme.data.side.is_none());
580        assert!(theme.data.shape.is_none());
581        assert!(theme.data.padding.is_none());
582        assert!(theme.data.text_style.is_none());
583        assert!(theme.data.hint_style.is_none());
584        assert_eq!(theme.data.constraints, None);
585        assert_eq!(theme.data.text_capitalization, None);
586    }
587
588    const ELEVATION_VALUE: f64 = 5.0;
589    const BACKGROUND_COLOR_VALUE: Color = Color::new(0xff000001);
590    const SHADOW_COLOR_VALUE: Color = Color::new(0xff000001);
591    const SURFACE_TINT_COLOR_VALUE: Color = Color::new(0xff000001);
592    const OVERLAY_COLOR_VALUE: Color = Color::new(0xff000001);
593
594    fn side_value() -> BorderSide {
595        BorderSide {
596            color: Color::new(0xff000004),
597            width: 2.0,
598            ..BorderSide::default()
599        }
600    }
601
602    fn shape_value() -> OutlinedBorderRef {
603        OutlinedBorderRef::new(RoundedRectangleBorder::new(
604            side_value(),
605            BorderRadiusGeometry::circular(2.0),
606        ))
607    }
608
609    fn all<T: Clone + std::fmt::Debug + PartialEq + 'static>(
610        value: T,
611    ) -> WidgetStatePropertyRef<Option<T>> {
612        Rc::new(WidgetStatePropertyAll::new(Some(value)))
613    }
614
615    fn text_style_value() -> TextStyle {
616        TextStyle::new()
617            .color(Color::new(0xff000005))
618            .font_size(20.0)
619    }
620
621    fn hint_style_value() -> TextStyle {
622        TextStyle::new()
623            .color(Color::new(0xff000006))
624            .font_size(18.0)
625    }
626
627    fn constraints_value() -> BoxConstraints {
628        BoxConstraints {
629            min_width: 250.0,
630            max_width: 300.0,
631            min_height: 80.0,
632            ..BoxConstraints::new()
633        }
634    }
635
636    fn search_bar_theme() -> SearchBarThemeData {
637        SearchBarThemeData::new()
638            .elevation(all(ELEVATION_VALUE))
639            .background_color(all(BACKGROUND_COLOR_VALUE))
640            .shadow_color(all(SHADOW_COLOR_VALUE))
641            .surface_tint_color(all(SURFACE_TINT_COLOR_VALUE))
642            .overlay_color(all(OVERLAY_COLOR_VALUE))
643            .side(all(side_value()))
644            .shape(all(shape_value()))
645            .padding(all(EdgeInsetsGeometry::from(EdgeInsets::symmetric(
646                0.0, 16.0,
647            ))))
648            .text_style(all(text_style_value()))
649            .hint_style(all(hint_style_value()))
650            .constraints(constraints_value())
651            .text_capitalization(TextCapitalization::Words)
652    }
653
654    fn themed_bar(use_search_bar_properties: bool) -> WidgetRef {
655        let bar = SearchBar::new()
656            .hint_text("hint")
657            .leading(Icon::new(Some(Icons::search())))
658            .trailing(vec![Icon::new(Some(Icons::menu())).into_widget()]);
659        let bar = if use_search_bar_properties {
660            let theme = search_bar_theme();
661            let mut bar = bar;
662            bar.elevation = theme.elevation;
663            bar.background_color = theme.background_color;
664            bar.shadow_color = theme.shadow_color;
665            bar.surface_tint_color = theme.surface_tint_color;
666            bar.overlay_color = theme.overlay_color;
667            bar.side = theme.side;
668            bar.shape = theme.shape;
669            bar.padding = theme.padding;
670            bar.text_style = theme.text_style;
671            bar.hint_style = theme.hint_style;
672            bar.constraints = theme.constraints;
673            bar.text_capitalization = theme.text_capitalization;
674            bar
675        } else {
676            bar
677        };
678        bar.into_widget()
679    }
680
681    /// Dart's `buildFrame`.
682    fn build_frame(
683        tester: &WidgetTester,
684        use_search_bar_properties: bool,
685        search_bar_theme_data: Option<SearchBarThemeData>,
686        overall_theme: Option<SearchBarThemeData>,
687    ) -> WidgetRef {
688        let child = Builder::new(move |_app, _context| themed_bar(use_search_bar_properties));
689        let theme = {
690            let app = tester.cell.borrow();
691            let theme = ThemeData::from(ColorScheme::light()).build(&app);
692            match overall_theme {
693                Some(overall_theme) => theme.copy_with().search_bar_theme(overall_theme).build(),
694                None => theme,
695            }
696        };
697        material_app(
698            theme,
699            Scaffold::new()
700                .body(Center::new().child(
701                    // If the SearchBarThemeData widget is present, it's used instead of the
702                    // Theme's ThemeData.searchBarTheme.
703                    match search_bar_theme_data {
704                        None => child.into_widget(),
705                        Some(data) => SearchBarTheme::new(data, child).into_widget(),
706                    },
707                ))
708                .into_widget(),
709        )
710    }
711
712    fn check_search_bar(tester: &mut WidgetTester) {
713        let find_material =
714            find::descendant(find::by_type::<SearchBar>(), find::by_type::<Material>());
715        let find_ink_well =
716            find::descendant(find::by_type::<SearchBar>(), find::by_type::<InkWell>());
717        let (elevation, color, shadow_color, surface_tint_color, shape) =
718            tester.widget(find_material, |material: &Material| {
719                (
720                    material.elevation,
721                    material.color,
722                    material.shadow_color,
723                    material.surface_tint_color,
724                    material.shape.clone(),
725                )
726            });
727        assert_eq!(elevation, ELEVATION_VALUE);
728        assert_eq!(color, Some(BACKGROUND_COLOR_VALUE));
729        assert_eq!(shadow_color, Some(SHADOW_COLOR_VALUE));
730        assert_eq!(surface_tint_color, Some(SURFACE_TINT_COLOR_VALUE));
731        assert_eq!(shape, Some(shape_value().into()));
732        let (overlay_color, custom_border) = tester.widget(find_ink_well, |ink_well: &InkWell| {
733            (
734                ink_well.ink_response.overlay_color.clone(),
735                ink_well.ink_response.custom_border.clone(),
736            )
737        });
738        let overlay_color = overlay_color.expect("an overlay color");
739        for state in [
740            WidgetState::Hovered,
741            WidgetState::Focused,
742            WidgetState::Pressed,
743        ] {
744            let states: WidgetStates = [state].into_iter().collect();
745            assert_eq!(overlay_color.resolve(&states), Some(OVERLAY_COLOR_VALUE));
746        }
747        assert_eq!(custom_border, Some(shape_value().into()));
748
749        assert_eq!(
750            tester.get_size(find::by_type::<SearchBar>()),
751            Size::new(300.0, 80.0)
752        );
753
754        let hint_style = tester
755            .widget(find::text("hint"), |text: &Text| text.style.clone())
756            .expect("a hint style");
757        assert_eq!(
758            hint_style.color.as_ref().map(|color| color.color()),
759            hint_style_value().color.as_ref().map(|color| color.color())
760        );
761        assert_eq!(hint_style.font_size, hint_style_value().font_size);
762
763        tester.enter_text(find::by_type::<TextField>(), "input");
764        let (input_style, text_capitalization) = tester
765            .widget(find::text("input"), |text: &EditableText| {
766                (text.style.clone(), text.text_capitalization)
767            });
768        assert_eq!(
769            input_style.color.as_ref().map(|color| color.color()),
770            text_style_value().color.as_ref().map(|color| color.color())
771        );
772        assert_eq!(input_style.font_size, text_style_value().font_size);
773        assert_eq!(text_capitalization, TextCapitalization::Words);
774
775        let bar_rect = tester.get_rect(find::by_type::<SearchBar>());
776        let leading_rect = tester.get_rect(find::by_icon(&Icons::search()));
777        let text_field_rect = tester.get_rect(find::by_type::<TextField>());
778        let trailing_rect = tester.get_rect(find::by_icon(&Icons::menu()));
779
780        assert_eq!(bar_rect.left, leading_rect.left - 16.0);
781        assert_eq!(leading_rect.right, text_field_rect.left - 16.0);
782        assert_eq!(text_field_rect.right, trailing_rect.left - 16.0);
783        assert_eq!(trailing_rect.right, bar_rect.right - 16.0);
784    }
785
786    /// A theme argument of Dart's `buildFrame`: absent, empty or the full theme (built inside
787    /// the case, because a case runs on its own thread).
788    #[derive(Clone, Copy)]
789    enum ThemeArgument {
790        Null,
791        Empty,
792        Full,
793    }
794
795    impl ThemeArgument {
796        fn data(self) -> Option<SearchBarThemeData> {
797            match self {
798                ThemeArgument::Null => None,
799                ThemeArgument::Empty => Some(SearchBarThemeData::new()),
800                ThemeArgument::Full => Some(search_bar_theme()),
801            }
802        }
803    }
804
805    fn pump_and_check(
806        use_search_bar_properties: bool,
807        search_bar_theme_data: ThemeArgument,
808        overall_theme: ThemeArgument,
809    ) {
810        test_widgets(move |tester| {
811            let root = build_frame(
812                tester,
813                use_search_bar_properties,
814                search_bar_theme_data.data(),
815                overall_theme.data(),
816            );
817            tester.pump_widget(root);
818            tester.pump_and_settle(); // allow the animations to finish
819            check_search_bar(tester);
820        });
821    }
822
823    // search_bar_theme_test.dart 'SearchBar properties overrides defaults'.
824    #[test]
825    fn search_bar_properties_override_defaults() {
826        pump_and_check(true, ThemeArgument::Null, ThemeArgument::Null);
827    }
828
829    // search_bar_theme_test.dart 'SearchBar theme data overrides defaults'.
830    #[test]
831    fn search_bar_theme_data_overrides_defaults() {
832        pump_and_check(false, ThemeArgument::Full, ThemeArgument::Null);
833    }
834
835    // search_bar_theme_test.dart 'Overall Theme SearchBar theme overrides defaults'.
836    #[test]
837    fn overall_theme_search_bar_theme_overrides_defaults() {
838        pump_and_check(false, ThemeArgument::Null, ThemeArgument::Full);
839    }
840
841    // search_bar_theme_test.dart 'SearchBar properties overrides defaults, empty theme and
842    // overall theme'.
843    #[test]
844    fn search_bar_properties_override_defaults_with_empty_theme_and_overall_theme() {
845        pump_and_check(true, ThemeArgument::Empty, ThemeArgument::Empty);
846    }
847
848    // search_bar_theme_test.dart 'SearchBar theme overrides defaults and overall theme'.
849    #[test]
850    fn search_bar_theme_overrides_defaults_and_overall_theme() {
851        pump_and_check(false, ThemeArgument::Full, ThemeArgument::Empty);
852    }
853
854    // search_bar_theme_test.dart 'Overall Theme SearchBar theme overrides defaults and null
855    // theme'.
856    #[test]
857    fn overall_theme_search_bar_theme_overrides_defaults_and_null_theme() {
858        pump_and_check(false, ThemeArgument::Null, ThemeArgument::Full);
859    }
860    // Flutter's "Default SearchBarThemeData debugFillProperties".
861    #[test]
862    fn a_search_bar_theme_data_describes_nothing_by_default() {
863        assert_eq!(
864            waft_widgets::testing::describe_properties(|properties| {
865                waft_foundation::Diagnosticable::debug_fill_properties(
866                    &SearchBarThemeData::new(),
867                    properties,
868                )
869            }),
870            Vec::<String>::new()
871        );
872    }
873}