waft-material 0.16.0

Material Design widgets for Waft
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
//! Flutter counterpart: material_ui's `snack_bar_theme.dart`.

use waft_embedder::{Color, lerp_double};
use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
use waft_painting::{
    AnyColor, ColorProperty, EdgeInsets, EdgeInsetsGeometry, ShapeBorderRef, TextStyle,
};
use waft_widgets::{
    BuildContext, DismissDirection, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
    WidgetStateColor,
};

use crate::theme::Theme;

/// Defines where a `SnackBar` should appear within a `Scaffold` and how its
/// location should be adjusted when the scaffold also includes a
/// `FloatingActionButton` or a `BottomNavigationBar`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SnackBarBehavior {
    /// Fixes the `SnackBar` at the bottom of the `Scaffold`.
    ///
    /// The exception is that the `SnackBar` will be shown above a
    /// `BottomNavigationBar` or a `NavigationBar`. Additionally, the `SnackBar`
    /// will cause other non-fixed widgets inside `Scaffold` to be pushed above
    /// (for example, the `FloatingActionButton`).
    Fixed,

    /// This behavior will cause `SnackBar` to be shown above other widgets in the
    /// `Scaffold`. This includes being displayed above a `BottomNavigationBar` or
    /// a `NavigationBar`, and a `FloatingActionButton` when its location is on the
    /// bottom. When the floating action button location is on the top, this behavior
    /// will cause the `SnackBar` to be shown above other widgets in the `Scaffold`
    /// except the floating action button.
    ///
    /// See <https://material.io/design/components/snackbars.html> for more details.
    Floating,
}

/// Customizes default property values for `SnackBar` widgets.
///
/// Descendant widgets obtain the current [`SnackBarThemeData`] object using
/// [`SnackBarTheme::of`]. Instances of [`SnackBarThemeData`] can be
/// customized with [`SnackBarThemeData::copy_with`].
///
/// Typically a [`SnackBarThemeData`] is specified as part of the overall `Theme`
/// with `ThemeData::snack_bar_theme`. The default for `ThemeData::snack_bar_theme`
/// provides all `null` properties.
///
/// All [`SnackBarThemeData`] properties are `null` by default. When null, the
/// `SnackBar` will provide its own defaults.
///
/// Dart's constructor asserts run in the setters that can break them
/// ([`elevation`](Self::elevation), [`width`](Self::width), [`behavior`](Self::behavior),
/// [`action_overflow_threshold`](Self::action_overflow_threshold),
/// [`action_background_color`](Self::action_background_color) and
/// [`disabled_action_background_color`](Self::disabled_action_background_color)), so that
/// they hold in any order. A [`width`](Self::width) given with no
/// [`behavior`](Self::behavior) at all is not caught: a setter cannot know that no
/// [`behavior`](Self::behavior) is coming.
///
/// See also:
///
///  * `ThemeData`, which describes the overall theme information for the
///    application.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SnackBarThemeData {
    /// Overrides the default value for `SnackBar::background_color`.
    ///
    /// If null, `SnackBar` defaults to dark grey: `Color(0xFF323232)`.
    pub background_color: Option<Color>,

    /// Overrides the default value for `SnackBarAction::text_color`.
    ///
    /// If null, `SnackBarAction` defaults to `ColorScheme::secondary` of
    /// `ThemeData::color_scheme`.
    pub action_text_color: Option<AnyColor>,

    /// Overrides the default value for `SnackBarAction::disabled_text_color`.
    ///
    /// If null, `SnackBarAction` defaults to `ColorScheme::on_surface` with its
    /// opacity set to 0.30 if the `Theme`'s brightness is `Brightness::Dark`, 0.38
    /// otherwise.
    pub disabled_action_text_color: Option<Color>,

    /// Used to configure the `DefaultTextStyle` for the `SnackBar::content` widget.
    ///
    /// If null, `SnackBar` defines its default.
    pub content_text_style: Option<TextStyle>,

    /// Overrides the default value for `SnackBar::elevation`.
    ///
    /// If null, `SnackBar` uses a default of 6.0.
    pub elevation: Option<f64>,

    /// Overrides the default value for `SnackBar::shape`.
    ///
    /// If null, `SnackBar` provides different defaults depending on the
    /// [`SnackBarBehavior`]. For [`SnackBarBehavior::Fixed`], no overriding shape is
    /// specified, so the `SnackBar` is rectangular. For
    /// [`SnackBarBehavior::Floating`], it uses a `RoundedRectangleBorder` with a
    /// circular corner radius of 4.0.
    pub shape: Option<ShapeBorderRef>,

    /// Overrides the default value for `SnackBar::behavior`.
    ///
    /// If null, `SnackBar` will default to [`SnackBarBehavior::Fixed`].
    pub behavior: Option<SnackBarBehavior>,

    /// Overrides the default value for `SnackBar::width`.
    ///
    /// If this property is null, then the snack bar will take up the full device
    /// width less the margin. This value is only used when [`behavior`](Self::behavior) is
    /// [`SnackBarBehavior::Floating`].
    pub width: Option<f64>,

    /// Overrides the default value for `SnackBar::margin`.
    ///
    /// This value is only used when [`behavior`](Self::behavior) is [`SnackBarBehavior::Floating`].
    pub inset_padding: Option<EdgeInsets>,

    /// Overrides the default value for `SnackBar::show_close_icon`.
    ///
    /// Whether to show an optional "Close" icon.
    pub show_close_icon: Option<bool>,

    /// Overrides the default value for `SnackBar::close_icon_color`.
    ///
    /// This value is only used if [`show_close_icon`](Self::show_close_icon) is true.
    pub close_icon_color: Option<AnyColor>,

    /// Overrides the default value for `SnackBar::action_overflow_threshold`.
    ///
    /// Must be a value between 0 and 1, if present.
    pub action_overflow_threshold: Option<f64>,

    /// Overrides default value for `SnackBarAction::background_color`.
    ///
    /// If null, `SnackBarAction` falls back to `Colors::TRANSPARENT`.
    pub action_background_color: Option<AnyColor>,

    /// Overrides default value for `SnackBarAction::disabled_background_color`.
    ///
    /// If null, `SnackBarAction` falls back to `Colors::TRANSPARENT`.
    pub disabled_action_background_color: Option<Color>,

    /// Overrides the default value for `SnackBar::dismiss_direction`.
    ///
    /// If null, `SnackBar` will default to [`DismissDirection::Down`].
    pub dismiss_direction: Option<DismissDirection>,
}

crate::theme_data_getters::theme_data_getters! {
    /// Dart's getters of `SnackBarThemeData`: the theme data answers with its fields, and a defaults
    /// subclass (Dart's `_...Defaults...(context)`) computes the ones it overrides when they
    /// are read.
    pub trait SnackBarThemeDataGetters for SnackBarThemeData, fields: snack_bar_theme_data {
        background_color: Option<Color>,
        action_text_color: Option<AnyColor>,
        disabled_action_text_color: Option<Color>,
        content_text_style: Option<TextStyle>,
        elevation: Option<f64>,
        shape: Option<ShapeBorderRef>,
        behavior: Option<SnackBarBehavior>,
        width: Option<f64>,
        inset_padding: Option<EdgeInsets>,
        show_close_icon: Option<bool>,
        close_icon_color: Option<AnyColor>,
        action_overflow_threshold: Option<f64>,
        action_background_color: Option<AnyColor>,
        disabled_action_background_color: Option<Color>,
        dismiss_direction: Option<DismissDirection>,
    }
}

impl SnackBarThemeData {
    /// Creates a theme that can be used for `ThemeData::snack_bar_theme`; Dart's optional
    /// named arguments are the fluent setters.
    ///
    /// The [`elevation`](Self::elevation) must be null or non-negative.
    pub fn new() -> SnackBarThemeData {
        SnackBarThemeData::default()
    }

    /// Dart `SnackBarThemeData(backgroundColor:)`.
    pub fn background_color(mut self, background_color: Color) -> SnackBarThemeData {
        self.background_color = Some(background_color);
        self
    }

    /// Dart `SnackBarThemeData(actionTextColor:)`.
    pub fn action_text_color(
        mut self,
        action_text_color: impl Into<AnyColor>,
    ) -> SnackBarThemeData {
        self.action_text_color = Some(action_text_color.into());
        self
    }

    /// Dart `SnackBarThemeData(disabledActionTextColor:)`.
    pub fn disabled_action_text_color(
        mut self,
        disabled_action_text_color: Color,
    ) -> SnackBarThemeData {
        self.disabled_action_text_color = Some(disabled_action_text_color);
        self
    }

    /// Dart `SnackBarThemeData(contentTextStyle:)`.
    pub fn content_text_style(mut self, content_text_style: TextStyle) -> SnackBarThemeData {
        self.content_text_style = Some(content_text_style);
        self
    }

    /// Dart `SnackBarThemeData(elevation:)`.
    pub fn elevation(mut self, elevation: f64) -> SnackBarThemeData {
        debug_assert!(elevation >= 0.0);
        self.elevation = Some(elevation);
        self
    }

    /// Dart `SnackBarThemeData(shape:)`.
    pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> SnackBarThemeData {
        self.shape = Some(shape.into());
        self
    }

    /// Dart `SnackBarThemeData(behavior:)`.
    pub fn behavior(mut self, behavior: SnackBarBehavior) -> SnackBarThemeData {
        debug_assert!(
            self.width.is_none() || behavior == SnackBarBehavior::Floating,
            "Width can only be set if behaviour is SnackBarBehavior.floating"
        );
        self.behavior = Some(behavior);
        self
    }

    /// Dart `SnackBarThemeData(width:)`.
    pub fn width(mut self, width: f64) -> SnackBarThemeData {
        debug_assert!(
            self.behavior
                .is_none_or(|behavior| behavior == SnackBarBehavior::Floating),
            "Width can only be set if behaviour is SnackBarBehavior.floating"
        );
        self.width = Some(width);
        self
    }

    /// Dart `SnackBarThemeData(insetPadding:)`.
    pub fn inset_padding(mut self, inset_padding: EdgeInsets) -> SnackBarThemeData {
        self.inset_padding = Some(inset_padding);
        self
    }

    /// Dart `SnackBarThemeData(showCloseIcon:)`.
    pub fn show_close_icon(mut self, show_close_icon: bool) -> SnackBarThemeData {
        self.show_close_icon = Some(show_close_icon);
        self
    }

    /// Dart `SnackBarThemeData(closeIconColor:)`.
    pub fn close_icon_color(mut self, close_icon_color: impl Into<AnyColor>) -> SnackBarThemeData {
        self.close_icon_color = Some(close_icon_color.into());
        self
    }

    /// Dart `SnackBarThemeData(actionOverflowThreshold:)`.
    pub fn action_overflow_threshold(
        mut self,
        action_overflow_threshold: f64,
    ) -> SnackBarThemeData {
        debug_assert!(
            (0.0..=1.0).contains(&action_overflow_threshold),
            "Action overflow threshold must be between 0 and 1 inclusive"
        );
        self.action_overflow_threshold = Some(action_overflow_threshold);
        self
    }

    /// Dart `SnackBarThemeData(actionBackgroundColor:)`.
    pub fn action_background_color(
        mut self,
        action_background_color: impl Into<AnyColor>,
    ) -> SnackBarThemeData {
        let action_background_color = action_background_color.into();
        debug_assert!(
            !is_widget_state_color(&action_background_color)
                || self.disabled_action_background_color.is_none(),
            "disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
        );
        self.action_background_color = Some(action_background_color);
        self
    }

    /// Dart `SnackBarThemeData(disabledActionBackgroundColor:)`.
    pub fn disabled_action_background_color(
        mut self,
        disabled_action_background_color: Color,
    ) -> SnackBarThemeData {
        debug_assert!(
            !self
                .action_background_color
                .as_ref()
                .is_some_and(is_widget_state_color),
            "disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
        );
        self.disabled_action_background_color = Some(disabled_action_background_color);
        self
    }

    /// Dart `SnackBarThemeData(dismissDirection:)`.
    pub fn dismiss_direction(mut self, dismiss_direction: DismissDirection) -> SnackBarThemeData {
        self.dismiss_direction = Some(dismiss_direction);
        self
    }

    /// Creates a copy of this object with the given fields replaced with the
    /// new values.
    ///
    /// Dart's named arguments are the fluent setters on the copy
    /// (`data.copy_with().elevation(2.0)`).
    pub fn copy_with(&self) -> SnackBarThemeData {
        self.clone()
    }

    /// Linearly interpolate between two SnackBar themes.
    ///
    /// {@macro dart.ui.shadow.lerp}
    pub fn lerp(
        a: Option<&SnackBarThemeData>,
        b: Option<&SnackBarThemeData>,
        t: f64,
    ) -> SnackBarThemeData {
        if let (Some(a), Some(b)) = (a, b)
            && std::ptr::eq(a, b)
        {
            return a.clone();
        }
        let lerped = SnackBarThemeData {
            background_color: Color::lerp(
                a.and_then(|a| a.background_color),
                b.and_then(|b| b.background_color),
                t,
            ),
            action_text_color: AnyColor::lerp(
                a.and_then(|a| a.action_text_color.as_ref()),
                b.and_then(|b| b.action_text_color.as_ref()),
                t,
            )
            .map(AnyColor::from),
            disabled_action_text_color: Color::lerp(
                a.and_then(|a| a.disabled_action_text_color),
                b.and_then(|b| b.disabled_action_text_color),
                t,
            ),
            content_text_style: TextStyle::lerp(
                a.and_then(|a| a.content_text_style.as_ref()),
                b.and_then(|b| b.content_text_style.as_ref()),
                t,
            ),
            elevation: lerp_double(a.and_then(|a| a.elevation), b.and_then(|b| b.elevation), t),
            shape: ShapeBorderRef::lerp(
                a.and_then(|a| a.shape.as_ref()),
                b.and_then(|b| b.shape.as_ref()),
                t,
            ),
            behavior: if t < 0.5 {
                a.and_then(|a| a.behavior)
            } else {
                b.and_then(|b| b.behavior)
            },
            width: lerp_double(a.and_then(|a| a.width), b.and_then(|b| b.width), t),
            inset_padding: EdgeInsets::lerp(
                a.and_then(|a| a.inset_padding),
                b.and_then(|b| b.inset_padding),
                t,
            ),
            show_close_icon: if t < 0.5 {
                a.and_then(|a| a.show_close_icon)
            } else {
                b.and_then(|b| b.show_close_icon)
            },
            close_icon_color: AnyColor::lerp(
                a.and_then(|a| a.close_icon_color.as_ref()),
                b.and_then(|b| b.close_icon_color.as_ref()),
                t,
            )
            .map(AnyColor::from),
            action_overflow_threshold: lerp_double(
                a.and_then(|a| a.action_overflow_threshold),
                b.and_then(|b| b.action_overflow_threshold),
                t,
            ),
            action_background_color: AnyColor::lerp(
                a.and_then(|a| a.action_background_color.as_ref()),
                b.and_then(|b| b.action_background_color.as_ref()),
                t,
            )
            .map(AnyColor::from),
            disabled_action_background_color: Color::lerp(
                a.and_then(|a| a.disabled_action_background_color),
                b.and_then(|b| b.disabled_action_background_color),
                t,
            ),
            dismiss_direction: if t < 0.5 {
                a.and_then(|a| a.dismiss_direction)
            } else {
                b.and_then(|b| b.dismiss_direction)
            },
        };
        lerped.debug_assert_valid();
        lerped
    }
}

impl SnackBarThemeData {
    /// Dart's constructor asserts, for a value assembled field by field ([`lerp`](Self::lerp)).
    fn debug_assert_valid(&self) {
        debug_assert!(self.elevation.is_none_or(|elevation| elevation >= 0.0));
        debug_assert!(
            self.width.is_none() || self.behavior == Some(SnackBarBehavior::Floating),
            "Width can only be set if behaviour is SnackBarBehavior.floating"
        );
        debug_assert!(
            self.action_overflow_threshold
                .is_none_or(|threshold| (0.0..=1.0).contains(&threshold)),
            "Action overflow threshold must be between 0 and 1 inclusive"
        );
        debug_assert!(
            !self
                .action_background_color
                .as_ref()
                .is_some_and(is_widget_state_color)
                || self.disabled_action_background_color.is_none(),
            "disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
        );
    }
}

/// Dart's `color is WidgetStateColor`.
fn is_widget_state_color(color: &AnyColor) -> bool {
    color.extension::<WidgetStateColor>().is_some()
}

/// An inherited widget that defines the configuration for
/// `SnackBar`s in this widget's subtree.
///
/// Values specified here are used for `SnackBar` properties that are not
/// given an explicit non-null value.
#[derive(Debug)]
pub struct SnackBarTheme {
    pub key: Option<KeyRef>,
    /// The properties for descendant `SnackBar` widgets.
    pub data: SnackBarThemeData,
    pub child: WidgetRef,
}

impl SnackBarTheme {
    /// Creates a snackbar theme that controls the configurations for
    /// `SnackBar`s in its widget subtree.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn new<K>(data: SnackBarThemeData, child: impl IntoWidget<K>) -> SnackBarTheme {
        SnackBarTheme {
            key: None,
            data,
            child: child.into_widget(),
        }
    }

    /// Dart `SnackBarTheme(key:)`.
    pub fn key(mut self, key: KeyRef) -> SnackBarTheme {
        self.key = Some(key);
        self
    }

    /// The closest instance of this class's [`data`](Self::data) value that encloses the given
    /// context.
    ///
    /// If there is no ancestor, it returns `ThemeData::snack_bar_theme`. Applications
    /// can assume that the returned value will not be null.
    ///
    /// Typical usage is as follows:
    ///
    /// ```text
    /// let theme = SnackBarTheme::of(app, context);
    /// ```
    pub fn of(app: &mut App, context: BuildContext) -> SnackBarThemeData {
        match context.depend_on_inherited_widget_of_exact_type::<SnackBarTheme>(app) {
            Some(snack_bar_theme) => snack_bar_theme.data.clone(),
            None => Theme::of(app, context).snack_bar_theme.clone(),
        }
    }
}

impl InheritedTheme for SnackBarTheme {
    fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
        SnackBarTheme::new(self.data.clone(), child).into_widget()
    }
}

impl InheritedWidget for SnackBarTheme {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn child(&self) -> &WidgetRef {
        &self.child
    }

    fn update_should_notify(&self, old_widget: &SnackBarTheme) -> bool {
        self.data != old_widget.data
    }

    fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
        Some(self)
    }
}

impl waft_foundation::Diagnosticable for SnackBarThemeData {
    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
        properties
            .add(ColorProperty::new("backgroundColor", self.background_color).default_value(None));
        properties.add(
            DiagnosticsProperty::<AnyColor>::new("actionTextColor", self.action_text_color.clone())
                .default_value(None),
        );
        properties.add(
            ColorProperty::new("disabledActionTextColor", self.disabled_action_text_color)
                .default_value(None),
        );
        properties.add(
            DiagnosticsProperty::<TextStyle>::new(
                "contentTextStyle",
                self.content_text_style.clone(),
            )
            .default_value(None),
        );
        properties.add(DoubleProperty::new("elevation", self.elevation).default_value(None));
        properties.add(
            DiagnosticsProperty::<ShapeBorderRef>::new("shape", self.shape.clone())
                .default_value(None),
        );
        properties.add(
            DiagnosticsProperty::<waft_foundation::DartEnum<SnackBarBehavior>>::new(
                "behavior",
                self.behavior.map(waft_foundation::DartEnum),
            )
            .default_value(None),
        );
        properties.add(DoubleProperty::new("width", self.width).default_value(None));
        properties.add(
            DiagnosticsProperty::<EdgeInsetsGeometry>::new(
                "insetPadding",
                self.inset_padding.map(EdgeInsetsGeometry::from),
            )
            .default_value(None),
        );
        properties.add(
            DiagnosticsProperty::<bool>::new("showCloseIcon", self.show_close_icon)
                .default_value(None),
        );
        properties.add(
            DiagnosticsProperty::<AnyColor>::new("closeIconColor", self.close_icon_color.clone())
                .default_value(None),
        );
        properties.add(
            DoubleProperty::new("actionOverflowThreshold", self.action_overflow_threshold)
                .default_value(None),
        );
        properties.add(
            DiagnosticsProperty::<AnyColor>::new(
                "actionBackgroundColor",
                self.action_background_color.clone(),
            )
            .default_value(None),
        );
        properties.add(
            ColorProperty::new(
                "disabledActionBackgroundColor",
                self.disabled_action_background_color,
            )
            .default_value(None),
        );
        properties.add(
            DiagnosticsProperty::<waft_foundation::DartEnum<DismissDirection>>::new(
                "dismissDirection",
                self.dismiss_direction.map(waft_foundation::DartEnum),
            )
            .default_value(None),
        );
    }
}

impl std::fmt::Display for SnackBarThemeData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
    }
}

#[cfg(test)]
mod tests {
    use waft_painting::ShapeBorder;
    use waft_painting::{BorderRadiusGeometry, BorderSide, RoundedRectangleBorder};
    use waft_widgets::{SizedBox, WidgetState};

    use waft_widgets::testing::test_widgets;

    use super::*;
    use crate::colors::Colors;
    use crate::drawer_theme::tests::probe;
    use crate::drawer_theme::tests::theme_seen_through_capture;
    use crate::theme_data::ThemeData;

    fn rounded(radius: f64) -> RoundedRectangleBorder {
        RoundedRectangleBorder::new(BorderSide::NONE, BorderRadiusGeometry::circular(radius))
    }

    fn widget_state_color() -> AnyColor {
        WidgetStateColor::resolve_with(|states| {
            if states.contains(&WidgetState::Disabled) {
                Colors::BLUE.primary()
            } else {
                Colors::PURPLE.primary()
            }
        })
        .into_any()
    }

    #[test]
    fn snack_bar_theme_data_copy_with_equality_basics() {
        assert_eq!(
            SnackBarThemeData::new(),
            SnackBarThemeData::new().copy_with()
        );

        let theme = SnackBarThemeData::new()
            .elevation(4.0)
            .shape(rounded(8.0))
            .behavior(SnackBarBehavior::Floating)
            .width(300.0);
        assert_eq!(theme.copy_with(), theme);
        assert_eq!(
            theme
                .copy_with()
                .dismiss_direction(DismissDirection::Up)
                .elevation,
            Some(4.0)
        );
        assert_eq!(
            theme
                .copy_with()
                .dismiss_direction(DismissDirection::Up)
                .dismiss_direction,
            Some(DismissDirection::Up)
        );
        assert_ne!(theme.copy_with().elevation(5.0), theme);
        assert_ne!(theme.copy_with().shape(rounded(9.0)), theme);
    }

    #[test]
    fn snack_bar_theme_data_lerp_special_cases() {
        assert_eq!(
            SnackBarThemeData::lerp(None, None, 0.0),
            SnackBarThemeData::new()
        );
        let data = SnackBarThemeData::new();
        assert_eq!(SnackBarThemeData::lerp(Some(&data), Some(&data), 0.5), data);
    }

    #[test]
    fn snack_bar_theme_data_lerp_keeps_show_close_icon() {
        let a = SnackBarThemeData::new().show_close_icon(true);
        let b = SnackBarThemeData::new().show_close_icon(false);

        assert_eq!(
            SnackBarThemeData::lerp(Some(&a), Some(&b), 0.25).show_close_icon,
            Some(true)
        );
        assert_eq!(
            SnackBarThemeData::lerp(Some(&a), Some(&b), 0.75).show_close_icon,
            Some(false)
        );
        assert_eq!(
            SnackBarThemeData::lerp(Some(&a), None, 0.25).show_close_icon,
            Some(true)
        );
        assert_eq!(
            SnackBarThemeData::lerp(None, Some(&a), 0.75).show_close_icon,
            Some(true)
        );
    }

    #[test]
    fn snack_bar_theme_data_null_fields_by_default() {
        let snack_bar_theme = SnackBarThemeData::new();
        assert_eq!(snack_bar_theme.background_color, None);
        assert_eq!(snack_bar_theme.action_text_color, None);
        assert_eq!(snack_bar_theme.disabled_action_text_color, None);
        assert_eq!(snack_bar_theme.content_text_style, None);
        assert_eq!(snack_bar_theme.elevation, None);
        assert!(snack_bar_theme.shape.is_none());
        assert_eq!(snack_bar_theme.behavior, None);
        assert_eq!(snack_bar_theme.width, None);
        assert_eq!(snack_bar_theme.inset_padding, None);
        assert_eq!(snack_bar_theme.show_close_icon, None);
        assert_eq!(snack_bar_theme.close_icon_color, None);
        assert_eq!(snack_bar_theme.action_overflow_threshold, None);
        assert_eq!(snack_bar_theme.action_background_color, None);
        assert_eq!(snack_bar_theme.disabled_action_background_color, None);
        assert_eq!(snack_bar_theme.dismiss_direction, None);
    }

    #[test]
    fn snack_bar_theme_data_lerps_each_field() {
        let a = SnackBarThemeData::new()
            .background_color(Colors::BLACK)
            .action_text_color(Colors::BLACK)
            .elevation(2.0)
            .shape(rounded(4.0))
            .behavior(SnackBarBehavior::Floating)
            .width(100.0)
            .inset_padding(EdgeInsets::all(10.0))
            .action_overflow_threshold(0.2)
            .dismiss_direction(DismissDirection::Down);
        let b = SnackBarThemeData::new()
            .background_color(Colors::WHITE)
            .action_text_color(Colors::WHITE)
            .close_icon_color(Colors::RED.primary())
            .elevation(6.0)
            .shape(rounded(8.0))
            .behavior(SnackBarBehavior::Floating)
            .width(300.0)
            .inset_padding(EdgeInsets::all(30.0))
            .action_overflow_threshold(0.6)
            .dismiss_direction(DismissDirection::Up);

        let halfway = SnackBarThemeData::lerp(Some(&a), Some(&b), 0.5);

        assert_eq!(
            halfway.background_color,
            Color::lerp(Some(Colors::BLACK), Some(Colors::WHITE), 0.5)
        );
        assert_eq!(
            halfway.action_text_color.map(|color| color.color()),
            Color::lerp(Some(Colors::BLACK), Some(Colors::WHITE), 0.5)
        );
        assert_eq!(
            halfway.close_icon_color.map(|color| color.color()),
            Color::lerp(None, Some(Colors::RED.primary()), 0.5)
        );
        assert_eq!(halfway.elevation, Some(4.0));
        assert_eq!(
            halfway.shape.as_deref(),
            Some(&rounded(6.0) as &dyn ShapeBorder)
        );
        assert_eq!(halfway.behavior, Some(SnackBarBehavior::Floating));
        assert_eq!(halfway.width, Some(200.0));
        assert_eq!(halfway.inset_padding, Some(EdgeInsets::all(20.0)));
        assert_eq!(halfway.action_overflow_threshold, Some(0.4));
        assert_eq!(halfway.dismiss_direction, Some(DismissDirection::Up));
        assert_eq!(
            SnackBarThemeData::lerp(Some(&a), Some(&b), 0.25).dismiss_direction,
            Some(DismissDirection::Down)
        );
    }

    #[test]
    #[should_panic(expected = "Width can only be set if behaviour is SnackBarBehavior.floating")]
    fn snack_bar_theme_data_asserts_if_width_is_provided_with_fixed_behavior() {
        SnackBarThemeData::new()
            .behavior(SnackBarBehavior::Fixed)
            .width(300.0);
    }

    #[test]
    #[should_panic(expected = "Width can only be set if behaviour is SnackBarBehavior.floating")]
    fn snack_bar_theme_data_asserts_if_fixed_behavior_is_provided_with_width() {
        SnackBarThemeData::new()
            .width(300.0)
            .behavior(SnackBarBehavior::Fixed);
    }

    #[test]
    fn snack_bar_theme_data_accepts_width_with_floating_behavior_in_either_order() {
        let width_first = SnackBarThemeData::new()
            .width(300.0)
            .behavior(SnackBarBehavior::Floating);
        let behavior_first = SnackBarThemeData::new()
            .behavior(SnackBarBehavior::Floating)
            .width(300.0);

        assert_eq!(width_first, behavior_first);
    }

    #[test]
    fn snack_bar_theme_data_asserts_for_action_overflow_threshold_outside_of_0_to_1() {
        for overflow_threshold in [-1.0, -0.0001, 1.000001, 5.0] {
            let result = std::panic::catch_unwind(|| {
                SnackBarThemeData::new().action_overflow_threshold(overflow_threshold)
            });
            assert!(result.is_err(), "{overflow_threshold} was accepted");
        }
        for overflow_threshold in [0.0, 0.5, 1.0] {
            assert_eq!(
                SnackBarThemeData::new()
                    .action_overflow_threshold(overflow_threshold)
                    .action_overflow_threshold,
                Some(overflow_threshold)
            );
        }
    }

    #[test]
    #[should_panic]
    fn snack_bar_theme_data_asserts_for_a_negative_elevation() {
        SnackBarThemeData::new().elevation(-1.0);
    }

    #[test]
    #[should_panic(
        expected = "disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
    )]
    fn snack_bar_theme_data_asserts_when_action_background_color_is_a_widget_state_color_and_disabled_action_background_color_is_also_provided()
     {
        SnackBarThemeData::new()
            .action_background_color(widget_state_color())
            .disabled_action_background_color(Colors::AMBER.primary());
    }

    #[test]
    #[should_panic(
        expected = "disabledBackgroundColor must not be provided when background color is a WidgetStateColor"
    )]
    fn snack_bar_theme_data_asserts_when_disabled_action_background_color_is_provided_before_a_widget_state_color()
     {
        SnackBarThemeData::new()
            .disabled_action_background_color(Colors::AMBER.primary())
            .action_background_color(widget_state_color());
    }

    #[test]
    fn snack_bar_theme_data_keeps_a_widget_state_color_as_the_action_background_color() {
        let theme = SnackBarThemeData::new().action_background_color(widget_state_color());

        let color = theme.action_background_color.expect("a color");

        assert!(color.extension::<WidgetStateColor>().is_some());
        assert_eq!(color.color(), Colors::PURPLE.primary());
    }

    #[test]
    fn snack_bar_theme_data_accepts_a_plain_action_background_color_with_a_disabled_one() {
        let theme = SnackBarThemeData::new()
            .action_background_color(Colors::PURPLE.primary())
            .disabled_action_background_color(Colors::AMBER.primary());

        assert_eq!(
            theme.disabled_action_background_color,
            Some(Colors::AMBER.primary())
        );
    }

    #[test]
    fn of_returns_the_theme_data_snack_bar_theme_without_an_ancestor() {
        test_widgets(|tester| {
            let expected = SnackBarThemeData::new()
                .elevation(3.0)
                .background_color(Colors::RED.primary());
            let theme_data_snack_bar = expected.clone();

            let seen = probe(
                tester,
                move |app, child| {
                    let data = ThemeData::builder()
                        .snack_bar_theme(theme_data_snack_bar.clone())
                        .build(app);
                    Theme::new(data, child).into_widget()
                },
                SnackBarTheme::of,
            );

            assert_eq!(seen, expected);
        });
    }

    #[test]
    fn of_returns_the_closest_snack_bar_theme_over_theme_data() {
        test_widgets(|tester| {
            let widget_snack_bar = SnackBarThemeData::new().elevation(7.0);
            let expected = widget_snack_bar.clone();

            let seen = probe(
                tester,
                move |app, child| {
                    let data = ThemeData::builder()
                        .snack_bar_theme(SnackBarThemeData::new().elevation(3.0))
                        .build(app);
                    Theme::new(data, SnackBarTheme::new(widget_snack_bar.clone(), child))
                        .into_widget()
                },
                SnackBarTheme::of,
            );

            assert_eq!(seen, expected);
        });
    }

    #[test]
    fn a_captured_snack_bar_theme_is_kept_under_another_snack_bar_theme() {
        test_widgets(|tester| {
            let captured = SnackBarThemeData::new().elevation(1.0);
            let shadowing = SnackBarThemeData::new().elevation(2.0);

            let seen = theme_seen_through_capture(
                tester,
                |probe| SnackBarTheme::new(captured.clone(), probe).into_widget(),
                |wrapped| SnackBarTheme::new(shadowing.clone(), wrapped).into_widget(),
                SnackBarTheme::of,
            );

            assert_eq!(seen.elevation, Some(1.0));
        });
    }

    #[test]
    fn update_should_notify_only_when_the_data_differs() {
        let theme = |data: SnackBarThemeData| SnackBarTheme::new(data, SizedBox::shrink());
        let old = theme(SnackBarThemeData::new().elevation(1.0));

        assert!(!theme(SnackBarThemeData::new().elevation(1.0)).update_should_notify(&old));
        assert!(theme(SnackBarThemeData::new().elevation(2.0)).update_should_notify(&old));
    }

    #[test]
    fn theme_data_holds_the_snack_bar_theme() {
        test_widgets(|tester| {
            let app = tester.cell.borrow();
            let theme = ThemeData::new(&app);
            assert_eq!(theme.snack_bar_theme, SnackBarThemeData::new());

            let raised = ThemeData::builder()
                .snack_bar_theme(SnackBarThemeData::new().elevation(8.0))
                .build(&app);
            assert_eq!(raised.snack_bar_theme.elevation, Some(8.0));
            assert_ne!(theme, raised);
            assert_eq!(
                theme
                    .copy_with()
                    .snack_bar_theme(raised.snack_bar_theme.clone())
                    .build(),
                raised
            );

            let flat = ThemeData::builder()
                .snack_bar_theme(SnackBarThemeData::new().elevation(0.0))
                .build(&app);
            assert_eq!(
                ThemeData::lerp(&flat, &raised, 0.5)
                    .snack_bar_theme
                    .elevation,
                Some(4.0)
            );
        });
    }
    // Flutter's "Default SnackBarThemeData debugFillProperties".
    #[test]
    fn a_snack_bar_theme_data_describes_nothing_by_default() {
        assert_eq!(
            waft_widgets::testing::describe_properties(|properties| {
                waft_foundation::Diagnosticable::debug_fill_properties(
                    &SnackBarThemeData::new(),
                    properties,
                )
            }),
            Vec::<String>::new()
        );
    }

    // snack_bar_theme_test.dart 'SnackBarThemeData implements debugFillProperties'.
    #[test]
    fn an_assigned_snack_bar_theme_describes_every_property() {
        let theme = SnackBarThemeData::new()
            .background_color(Color::new(0xffffffff))
            .action_text_color(Color::new(0xff0000aa))
            .disabled_action_text_color(Color::new(0xff00aa00))
            .content_text_style(TextStyle::new().color(Color::new(0xff123456)))
            .elevation(2.0)
            .shape(RoundedRectangleBorder::new(
                BorderSide::NONE,
                BorderRadiusGeometry::circular(2.0),
            ))
            .behavior(SnackBarBehavior::Floating)
            .width(400.0)
            .inset_padding(EdgeInsets::all(10.0))
            .show_close_icon(false)
            .close_icon_color(Color::new(0xff0000aa))
            .action_overflow_threshold(0.5)
            .dismiss_direction(DismissDirection::Down);
        let description = waft_widgets::testing::describe_properties(|properties| {
            waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
        });
        let description: Vec<String> = description
            .iter()
            .map(|line| waft_rendering::testing::ignoring_hash_codes(line))
            .collect();
        assert_eq!(
            description,
            [
                format!("backgroundColor: {}", Color::new(0xffffffff)),
                format!("actionTextColor: {}", Color::new(0xff0000aa)),
                format!("disabledActionTextColor: {}", Color::new(0xff00aa00)),
                format!(
                    "contentTextStyle: {}",
                    TextStyle::new().color(Color::new(0xff123456))
                ),
                "elevation: 2.0".to_string(),
                format!(
                    "shape: {}",
                    ShapeBorderRef::from(RoundedRectangleBorder::new(
                        BorderSide::NONE,
                        BorderRadiusGeometry::circular(2.0)
                    ))
                ),
                "behavior: SnackBarBehavior.floating".to_string(),
                "width: 400.0".to_string(),
                "insetPadding: EdgeInsets.all(10.0)".to_string(),
                "showCloseIcon: false".to_string(),
                format!("closeIconColor: {}", Color::new(0xff0000aa)),
                "actionOverflowThreshold: 0.5".to_string(),
                "dismissDirection: DismissDirection.down".to_string(),
            ]
        );
    }
}