Skip to main content

waft_material/
progress_indicator_theme.rs

1//! Flutter counterpart: material_ui's `progress_indicator_theme.dart`.
2
3use waft_animation::AnimationController;
4use waft_embedder::StrokeCap;
5use waft_embedder::{Color, lerp_double};
6use waft_foundation::{App, DiagnosticsProperty, DoubleProperty, Ref};
7use waft_painting::{BorderRadiusGeometry, ColorProperty, EdgeInsetsGeometry};
8use waft_rendering::BoxConstraints;
9use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
10
11use crate::theme::Theme;
12
13/// Overrides the default values of visual properties for descendant
14/// `ProgressIndicator` widgets.
15///
16/// Descendant widgets obtain the current [`ProgressIndicatorThemeData`] object
17/// with [`ProgressIndicatorTheme::of`]. Instances of [`ProgressIndicatorThemeData`]
18/// can be customized with [`ProgressIndicatorThemeData::copy_with`].
19///
20/// Typically a [`ProgressIndicatorThemeData`] is specified as part of the overall
21/// `Theme` with `ThemeData::progress_indicator_theme`.
22///
23/// See also:
24///
25///  * [`ProgressIndicatorTheme`], an `InheritedWidget` that propagates the
26///    theme down its subtree.
27///  * `ThemeData::progress_indicator_theme`, which describes the defaults for
28///    any progress indicators as part of the application's `ThemeData`.
29#[derive(Clone, Debug, Default, PartialEq)]
30pub struct ProgressIndicatorThemeData {
31    /// The color of the `ProgressIndicator`'s indicator.
32    ///
33    /// If null, then it will use `ColorScheme::primary` of the ambient
34    /// `ThemeData::color_scheme`.
35    ///
36    /// See also:
37    ///
38    ///  * `ProgressIndicator.color`, which specifies the indicator color for a
39    ///    specific progress indicator.
40    ///  * `ProgressIndicator.valueColor`, which specifies the indicator color
41    ///    a an animated color.
42    pub color: Option<Color>,
43
44    /// Color of the track being filled by the linear indicator.
45    ///
46    /// If `LinearProgressIndicator.backgroundColor` is null then the
47    /// ambient [`linear_track_color`](Self::linear_track_color) will be used.
48    /// If that is null, then the ambient theme's `ColorScheme::background`
49    /// will be used to draw the track.
50    pub linear_track_color: Option<Color>,
51
52    /// The minimum height of the line used to draw the linear indicator.
53    ///
54    /// If `LinearProgressIndicator.minHeight` is null then it will use the
55    /// ambient [`linear_min_height`](Self::linear_min_height). If that is null
56    /// it will use 4dp.
57    pub linear_min_height: Option<f64>,
58
59    /// Color of the circular track being filled by the circular indicator.
60    ///
61    /// If `CircularProgressIndicator.backgroundColor` is null then the
62    /// ambient [`circular_track_color`](Self::circular_track_color) will be used.
63    /// If that is null, then the track will not be painted.
64    pub circular_track_color: Option<Color>,
65
66    /// Background color of that fills the circle under the refresh indicator.
67    ///
68    /// If `RefreshIndicator.backgroundColor` is null then the
69    /// ambient [`refresh_background_color`](Self::refresh_background_color) will be used.
70    /// If that is null, then the ambient theme's `ThemeData::canvas_color`
71    /// will be used.
72    pub refresh_background_color: Option<Color>,
73
74    /// Overrides the border radius of the `ProgressIndicator`.
75    pub border_radius: Option<BorderRadiusGeometry>,
76
77    /// Overrides the stop indicator color of the `LinearProgressIndicator`.
78    ///
79    /// If `LinearProgressIndicator.year2023` is true or `ThemeData::use_material3`
80    /// is false, then no stop indicator will be drawn.
81    pub stop_indicator_color: Option<Color>,
82
83    /// Overrides the stop indicator radius of the `LinearProgressIndicator`.
84    ///
85    /// If `LinearProgressIndicator.year2023` is true or `ThemeData::use_material3`
86    /// is false, then no stop indicator will be drawn.
87    pub stop_indicator_radius: Option<f64>,
88
89    /// Overrides the stroke width of the `CircularProgressIndicator`.
90    pub stroke_width: Option<f64>,
91
92    /// Overrides the stroke align of the `CircularProgressIndicator`.
93    pub stroke_align: Option<f64>,
94
95    /// Overrides the stroke cap of the `CircularProgressIndicator`.
96    pub stroke_cap: Option<StrokeCap>,
97
98    /// Overrides the constraints of the `CircularProgressIndicator`.
99    pub constraints: Option<BoxConstraints>,
100
101    /// Overrides the active indicator and the background track.
102    ///
103    /// If `CircularProgressIndicator.year2023` is true or `ThemeData::use_material3`
104    /// is false, then no track gap will be drawn.
105    ///
106    /// If `LinearProgressIndicator.year2023` is true or `ThemeData::use_material3`
107    /// is false, then no track gap will be drawn.
108    pub track_gap: Option<f64>,
109
110    /// Overrides the padding of the `CircularProgressIndicator`.
111    pub circular_track_padding: Option<EdgeInsetsGeometry>,
112
113    /// Overrides the `CircularProgressIndicator.year2023` and
114    /// `LinearProgressIndicator.year2023` properties.
115    ///
116    /// When true, the `CircularProgressIndicator` and `LinearProgressIndicator`
117    /// will use the 2023 Material Design 3 appearance. Defaults to true.
118    ///
119    /// If this is set to false, the `CircularProgressIndicator` and
120    /// `LinearProgressIndicator` will use the latest Material Design 3 appearance,
121    /// which was introduced in December 2023.
122    ///
123    /// If `ThemeData::use_material3` is false, then this property is ignored.
124    #[deprecated(
125        note = "Set this flag to false to opt into the 2024 progress indicator appearance. Defaults to true. In the future, this flag will default to false. Use ProgressIndicatorThemeData to customize individual properties. This feature was deprecated after v3.27.0-0.2.pre."
126    )]
127    pub year2023: Option<bool>,
128
129    /// Defines a default [`AnimationController`] for descendant
130    /// `CircularProgressIndicator` and `LinearProgressIndicator` widgets.
131    ///
132    /// If a descendant progress indicator's `controller` property is null, this
133    /// controller will be used to drive its indeterminate animation. This allows
134    /// a single controller to synchronize the animations of multiple indicators.
135    ///
136    /// If this property is also null, the progress indicator will create and
137    /// manage its own internal [`AnimationController`].
138    pub controller: Option<Ref<AnimationController>>,
139}
140
141crate::theme_data_getters::theme_data_getters! {
142    /// Dart's getters of `ProgressIndicatorThemeData`: the theme data answers with its fields, and a defaults
143    /// subclass (Dart's `_...Defaults...(context)`) computes the ones it overrides when they
144    /// are read.
145    pub trait ProgressIndicatorThemeDataGetters for ProgressIndicatorThemeData, fields: progress_indicator_theme_data {
146        color: Option<Color>,
147        linear_track_color: Option<Color>,
148        linear_min_height: Option<f64>,
149        circular_track_color: Option<Color>,
150        refresh_background_color: Option<Color>,
151        border_radius: Option<BorderRadiusGeometry>,
152        stop_indicator_color: Option<Color>,
153        stop_indicator_radius: Option<f64>,
154        stroke_width: Option<f64>,
155        stroke_align: Option<f64>,
156        stroke_cap: Option<StrokeCap>,
157        constraints: Option<BoxConstraints>,
158        track_gap: Option<f64>,
159        circular_track_padding: Option<EdgeInsetsGeometry>,
160        year2023: Option<bool>,
161        controller: Option<Ref<AnimationController>>,
162    }
163}
164
165#[allow(deprecated)]
166impl ProgressIndicatorThemeData {
167    /// Creates the set of properties used to configure `ProgressIndicator` widgets.
168    pub fn new() -> ProgressIndicatorThemeData {
169        ProgressIndicatorThemeData::default()
170    }
171
172    /// Dart `ProgressIndicatorThemeData(color:)`.
173    pub fn color(mut self, color: Color) -> ProgressIndicatorThemeData {
174        self.color = Some(color);
175        self
176    }
177
178    /// Dart `ProgressIndicatorThemeData(linearTrackColor:)`.
179    pub fn linear_track_color(mut self, linear_track_color: Color) -> ProgressIndicatorThemeData {
180        self.linear_track_color = Some(linear_track_color);
181        self
182    }
183
184    /// Dart `ProgressIndicatorThemeData(linearMinHeight:)`.
185    pub fn linear_min_height(mut self, linear_min_height: f64) -> ProgressIndicatorThemeData {
186        self.linear_min_height = Some(linear_min_height);
187        self
188    }
189
190    /// Dart `ProgressIndicatorThemeData(circularTrackColor:)`.
191    pub fn circular_track_color(
192        mut self,
193        circular_track_color: Color,
194    ) -> ProgressIndicatorThemeData {
195        self.circular_track_color = Some(circular_track_color);
196        self
197    }
198
199    /// Dart `ProgressIndicatorThemeData(refreshBackgroundColor:)`.
200    pub fn refresh_background_color(
201        mut self,
202        refresh_background_color: Color,
203    ) -> ProgressIndicatorThemeData {
204        self.refresh_background_color = Some(refresh_background_color);
205        self
206    }
207
208    /// Dart `ProgressIndicatorThemeData(borderRadius:)`.
209    pub fn border_radius(
210        mut self,
211        border_radius: impl Into<BorderRadiusGeometry>,
212    ) -> ProgressIndicatorThemeData {
213        self.border_radius = Some(border_radius.into());
214        self
215    }
216
217    /// Dart `ProgressIndicatorThemeData(stopIndicatorColor:)`.
218    pub fn stop_indicator_color(
219        mut self,
220        stop_indicator_color: Color,
221    ) -> ProgressIndicatorThemeData {
222        self.stop_indicator_color = Some(stop_indicator_color);
223        self
224    }
225
226    /// Dart `ProgressIndicatorThemeData(stopIndicatorRadius:)`.
227    pub fn stop_indicator_radius(
228        mut self,
229        stop_indicator_radius: f64,
230    ) -> ProgressIndicatorThemeData {
231        self.stop_indicator_radius = Some(stop_indicator_radius);
232        self
233    }
234
235    /// Dart `ProgressIndicatorThemeData(strokeWidth:)`.
236    pub fn stroke_width(mut self, stroke_width: f64) -> ProgressIndicatorThemeData {
237        self.stroke_width = Some(stroke_width);
238        self
239    }
240
241    /// Dart `ProgressIndicatorThemeData(strokeAlign:)`.
242    pub fn stroke_align(mut self, stroke_align: f64) -> ProgressIndicatorThemeData {
243        self.stroke_align = Some(stroke_align);
244        self
245    }
246
247    /// Dart `ProgressIndicatorThemeData(strokeCap:)`.
248    pub fn stroke_cap(mut self, stroke_cap: StrokeCap) -> ProgressIndicatorThemeData {
249        self.stroke_cap = Some(stroke_cap);
250        self
251    }
252
253    /// Dart `ProgressIndicatorThemeData(constraints:)`.
254    pub fn constraints(mut self, constraints: BoxConstraints) -> ProgressIndicatorThemeData {
255        self.constraints = Some(constraints);
256        self
257    }
258
259    /// Dart `ProgressIndicatorThemeData(trackGap:)`.
260    pub fn track_gap(mut self, track_gap: f64) -> ProgressIndicatorThemeData {
261        self.track_gap = Some(track_gap);
262        self
263    }
264
265    /// Dart `ProgressIndicatorThemeData(circularTrackPadding:)`.
266    pub fn circular_track_padding(
267        mut self,
268        circular_track_padding: impl Into<EdgeInsetsGeometry>,
269    ) -> ProgressIndicatorThemeData {
270        self.circular_track_padding = Some(circular_track_padding.into());
271        self
272    }
273
274    /// Dart `ProgressIndicatorThemeData(year2023:)`.
275    #[deprecated(
276        note = "Set this flag to false to opt into the 2024 progress indicator appearance. Defaults to true. In the future, this flag will default to false. Use ProgressIndicatorThemeData to customize individual properties. This feature was deprecated after v3.27.0-0.2.pre."
277    )]
278    pub fn year2023(mut self, year2023: bool) -> ProgressIndicatorThemeData {
279        self.year2023 = Some(year2023);
280        self
281    }
282
283    /// Dart `ProgressIndicatorThemeData(controller:)`.
284    pub fn controller(
285        mut self,
286        controller: Ref<AnimationController>,
287    ) -> ProgressIndicatorThemeData {
288        self.controller = Some(controller);
289        self
290    }
291
292    /// Creates a copy of this object but with the given fields replaced with the
293    /// new values.
294    ///
295    /// Dart's named arguments are the fluent setters on the copy
296    /// (`data.copy_with().stroke_width(8.0)`).
297    pub fn copy_with(&self) -> ProgressIndicatorThemeData {
298        self.clone()
299    }
300
301    /// Linearly interpolate between two progress indicator themes.
302    ///
303    /// If both arguments are null, then null is returned.
304    pub fn lerp(
305        a: Option<&ProgressIndicatorThemeData>,
306        b: Option<&ProgressIndicatorThemeData>,
307        t: f64,
308    ) -> Option<ProgressIndicatorThemeData> {
309        match (a, b) {
310            (None, None) => return None,
311            (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
312            _ => {}
313        }
314        Some(ProgressIndicatorThemeData {
315            color: Color::lerp(a.and_then(|a| a.color), b.and_then(|b| b.color), t),
316            linear_track_color: Color::lerp(
317                a.and_then(|a| a.linear_track_color),
318                b.and_then(|b| b.linear_track_color),
319                t,
320            ),
321            linear_min_height: lerp_double(
322                a.and_then(|a| a.linear_min_height),
323                b.and_then(|b| b.linear_min_height),
324                t,
325            ),
326            circular_track_color: Color::lerp(
327                a.and_then(|a| a.circular_track_color),
328                b.and_then(|b| b.circular_track_color),
329                t,
330            ),
331            refresh_background_color: Color::lerp(
332                a.and_then(|a| a.refresh_background_color),
333                b.and_then(|b| b.refresh_background_color),
334                t,
335            ),
336            border_radius: BorderRadiusGeometry::lerp(
337                a.and_then(|a| a.border_radius),
338                b.and_then(|b| b.border_radius),
339                t,
340            ),
341            stop_indicator_color: Color::lerp(
342                a.and_then(|a| a.stop_indicator_color),
343                b.and_then(|b| b.stop_indicator_color),
344                t,
345            ),
346            stop_indicator_radius: lerp_double(
347                a.and_then(|a| a.stop_indicator_radius),
348                b.and_then(|b| b.stop_indicator_radius),
349                t,
350            ),
351            stroke_width: lerp_double(
352                a.and_then(|a| a.stroke_width),
353                b.and_then(|b| b.stroke_width),
354                t,
355            ),
356            stroke_align: lerp_double(
357                a.and_then(|a| a.stroke_align),
358                b.and_then(|b| b.stroke_align),
359                t,
360            ),
361            stroke_cap: pick(a, b, t, |theme| theme.stroke_cap),
362            constraints: BoxConstraints::lerp(
363                a.and_then(|a| a.constraints),
364                b.and_then(|b| b.constraints),
365                t,
366            ),
367            track_gap: lerp_double(a.and_then(|a| a.track_gap), b.and_then(|b| b.track_gap), t),
368            circular_track_padding: EdgeInsetsGeometry::lerp(
369                a.and_then(|a| a.circular_track_padding),
370                b.and_then(|b| b.circular_track_padding),
371                t,
372            ),
373            year2023: pick(a, b, t, |theme| theme.year2023),
374            controller: pick(a, b, t, |theme| theme.controller.clone()),
375        })
376    }
377}
378
379/// Dart's `t < 0.5 ? a?.field : b?.field`.
380fn pick<T>(
381    a: Option<&ProgressIndicatorThemeData>,
382    b: Option<&ProgressIndicatorThemeData>,
383    t: f64,
384    field: impl Fn(&ProgressIndicatorThemeData) -> Option<T>,
385) -> Option<T> {
386    if t < 0.5 {
387        a.and_then(&field)
388    } else {
389        b.and_then(&field)
390    }
391}
392
393/// An inherited widget that defines the configuration for
394/// `ProgressIndicator`s in this widget's subtree.
395///
396/// Values specified here are used for `ProgressIndicator` properties that are not
397/// given an explicit non-null value.
398///
399/// Here is an example of a progress indicator theme that applies a red indicator
400/// color.
401///
402/// ```text
403/// ProgressIndicatorTheme::new(
404///     ProgressIndicatorThemeData::new().color(Colors::RED.primary()),
405///     LinearProgressIndicator::new(),
406/// )
407/// ```
408#[derive(Debug)]
409pub struct ProgressIndicatorTheme {
410    pub key: Option<KeyRef>,
411    /// The properties for descendant `ProgressIndicator` widgets.
412    pub data: ProgressIndicatorThemeData,
413    pub child: WidgetRef,
414}
415
416impl ProgressIndicatorTheme {
417    /// Creates a theme that controls the configurations for `ProgressIndicator`
418    /// widgets.
419    #[cfg_attr(debug_assertions, track_caller)]
420    pub fn new<K>(
421        data: ProgressIndicatorThemeData,
422        child: impl IntoWidget<K>,
423    ) -> ProgressIndicatorTheme {
424        ProgressIndicatorTheme {
425            key: None,
426            data,
427            child: child.into_widget(),
428        }
429    }
430
431    /// Dart `ProgressIndicatorTheme(key:)`.
432    pub fn key(mut self, key: KeyRef) -> ProgressIndicatorTheme {
433        self.key = Some(key);
434        self
435    }
436
437    /// Returns the [`data`](Self::data) from the closest [`ProgressIndicatorTheme`]
438    /// ancestor. If there is no ancestor, it returns `ThemeData::progress_indicator_theme`.
439    ///
440    /// Typical usage is as follows:
441    ///
442    /// ```text
443    /// let theme = ProgressIndicatorTheme::of(app, context);
444    /// ```
445    pub fn of(app: &mut App, context: BuildContext) -> ProgressIndicatorThemeData {
446        match context.depend_on_inherited_widget_of_exact_type::<ProgressIndicatorTheme>(app) {
447            Some(progress_indicator_theme) => progress_indicator_theme.data.clone(),
448            None => Theme::of(app, context).progress_indicator_theme.clone(),
449        }
450    }
451}
452
453impl InheritedTheme for ProgressIndicatorTheme {
454    fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
455        ProgressIndicatorTheme::new(self.data.clone(), child).into_widget()
456    }
457}
458
459impl InheritedWidget for ProgressIndicatorTheme {
460    fn key(&self) -> Option<&KeyRef> {
461        self.key.as_ref()
462    }
463
464    fn child(&self) -> &WidgetRef {
465        &self.child
466    }
467
468    fn update_should_notify(&self, old_widget: &ProgressIndicatorTheme) -> bool {
469        self.data != old_widget.data
470    }
471
472    fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
473        Some(self)
474    }
475}
476
477#[allow(deprecated)]
478impl waft_foundation::Diagnosticable for ProgressIndicatorThemeData {
479    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
480        properties.add(ColorProperty::new("color", self.color).default_value(None));
481        properties.add(
482            ColorProperty::new("linearTrackColor", self.linear_track_color).default_value(None),
483        );
484        properties.add(
485            DoubleProperty::new("linearMinHeight", self.linear_min_height).default_value(None),
486        );
487        properties.add(
488            ColorProperty::new("circularTrackColor", self.circular_track_color).default_value(None),
489        );
490        properties.add(
491            ColorProperty::new("refreshBackgroundColor", self.refresh_background_color)
492                .default_value(None),
493        );
494        properties.add(
495            DiagnosticsProperty::<BorderRadiusGeometry>::new("borderRadius", self.border_radius)
496                .default_value(None),
497        );
498        properties.add(
499            ColorProperty::new("stopIndicatorColor", self.stop_indicator_color).default_value(None),
500        );
501        properties.add(
502            DoubleProperty::new("stopIndicatorRadius", self.stop_indicator_radius)
503                .default_value(None),
504        );
505        properties.add(DoubleProperty::new("strokeWidth", self.stroke_width).default_value(None));
506        properties.add(DoubleProperty::new("strokeAlign", self.stroke_align).default_value(None));
507        properties.add(
508            DiagnosticsProperty::<waft_foundation::DartEnum<StrokeCap>>::new(
509                "strokeCap",
510                self.stroke_cap.map(waft_foundation::DartEnum),
511            )
512            .default_value(None),
513        );
514        properties.add(
515            DiagnosticsProperty::<BoxConstraints>::new("constraints", self.constraints)
516                .default_value(None),
517        );
518        properties.add(DoubleProperty::new("trackGap", self.track_gap).default_value(None));
519        properties.add(
520            DiagnosticsProperty::<EdgeInsetsGeometry>::new(
521                "circularTrackPadding",
522                self.circular_track_padding,
523            )
524            .default_value(None),
525        );
526        properties
527            .add(DiagnosticsProperty::<bool>::new("year2023", self.year2023).default_value(None));
528        properties.add(
529            DiagnosticsProperty::<String>::new(
530                "controller",
531                self.controller.as_ref().map(|controller| {
532                    waft_foundation::describe_identity(
533                        "AnimationController",
534                        controller.handle().id().identity_hash(),
535                    )
536                }),
537            )
538            .default_value(None),
539        );
540    }
541}
542
543impl std::fmt::Display for ProgressIndicatorThemeData {
544    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
545        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
546    }
547}
548
549#[cfg(test)]
550mod tests {
551    //! Ported from `test/progress_indicator_theme_test.dart`.
552    #![allow(deprecated)]
553
554    use std::time::Duration;
555
556    use waft_embedder::{RRect, Radius, Rect, Size};
557    use waft_painting::{BorderRadius, BorderSide, EdgeInsets};
558    use waft_widgets::{Center, SizedBox};
559
560    use waft_widgets::testing::{WidgetTester, test_widgets};
561
562    use super::*;
563    use crate::colors::Colors;
564    use crate::material::Material;
565    use crate::progress_indicator::tests::{
566        active_arc_of, arc, centered_width, circle, circular_sizes, controller_at, padded,
567        painted_by, paints, pump_with, rect, rrect, scaffold_app, theme_with, themed,
568    };
569    use crate::progress_indicator::{CircularProgressIndicator, LinearProgressIndicator};
570    use crate::theme_data::ThemeData;
571
572    #[test]
573    fn progress_indicator_theme_data_copy_with_equality_basics() {
574        assert_eq!(
575            ProgressIndicatorThemeData::new(),
576            ProgressIndicatorThemeData::new().copy_with()
577        );
578    }
579
580    #[test]
581    fn progress_indicator_theme_data_lerp_special_cases() {
582        assert_eq!(ProgressIndicatorThemeData::lerp(None, None, 0.0), None);
583        let data = ProgressIndicatorThemeData::new();
584        // Dart's `identical`: the value comes back as it was.
585        assert_eq!(
586            ProgressIndicatorThemeData::lerp(Some(&data), Some(&data), 0.5),
587            Some(data)
588        );
589    }
590
591    #[test]
592    fn can_theme_linear_progress_indicator_using_progress_indicator_theme() {
593        test_widgets(|tester| {
594            let color = Color::new(0xFF00FF00);
595            let linear_track_color = Color::new(0xFFFF0000);
596            let linear_min_height = 25.0;
597            let border_radius = 8.0;
598            pump_with(tester, |app| {
599                let theme = theme_with(
600                    app,
601                    ProgressIndicatorThemeData::new()
602                        .color(color)
603                        .linear_track_color(linear_track_color)
604                        .linear_min_height(linear_min_height)
605                        .border_radius(BorderRadius::all(Radius::circular(border_radius))),
606                );
607                scaffold_app(
608                    Some(theme),
609                    centered_width(200.0, LinearProgressIndicator::new().value(0.5)),
610                )
611            });
612            let radius = Radius::circular(border_radius);
613            assert!(paints(
614                &painted_by::<LinearProgressIndicator>(tester),
615                &[
616                    // Track.
617                    rrect(RRect::from_ltrbr(
618                        0.0,
619                        0.0,
620                        200.0,
621                        linear_min_height,
622                        radius
623                    ))
624                    .color(linear_track_color),
625                    // Active indicator.
626                    rrect(RRect::from_ltrbr(
627                        0.0,
628                        0.0,
629                        100.0,
630                        linear_min_height,
631                        radius
632                    ))
633                    .color(color),
634                ]
635            ));
636        });
637    }
638
639    #[test]
640    fn can_theme_linear_progress_indicator_when_year2023_to_false() {
641        test_widgets(|tester| {
642            let color = Color::new(0xFF00FF00);
643            let linear_track_color = Color::new(0xFFFF0000);
644            let linear_min_height = 25.0;
645            let border_radius = 8.0;
646            let stop_indicator_color = Color::new(0xFF0000FF);
647            let stop_indicator_radius = 10.0;
648            let track_gap = 16.0;
649            pump_with(tester, |app| {
650                let theme = theme_with(
651                    app,
652                    ProgressIndicatorThemeData::new()
653                        .color(color)
654                        .linear_track_color(linear_track_color)
655                        .linear_min_height(linear_min_height)
656                        .border_radius(BorderRadius::all(Radius::circular(border_radius)))
657                        .stop_indicator_color(stop_indicator_color)
658                        .stop_indicator_radius(stop_indicator_radius)
659                        .track_gap(track_gap),
660                );
661                scaffold_app(
662                    Some(theme),
663                    centered_width(
664                        200.0,
665                        LinearProgressIndicator::new().year2023(false).value(0.5),
666                    ),
667                )
668            });
669            let radius = Radius::circular(border_radius);
670            assert!(paints(
671                &painted_by::<LinearProgressIndicator>(tester),
672                &[
673                    // Track.
674                    rrect(RRect::from_ltrbr(
675                        100.0 + track_gap,
676                        0.0,
677                        200.0,
678                        linear_min_height,
679                        radius
680                    ))
681                    .color(linear_track_color),
682                    // Stop indicator.
683                    circle()
684                        .at(187.5, 12.5)
685                        .radius(stop_indicator_radius)
686                        .color(stop_indicator_color),
687                    // Active indicator.
688                    rrect(RRect::from_ltrbr(
689                        0.0,
690                        0.0,
691                        100.0,
692                        linear_min_height,
693                        radius
694                    ))
695                    .color(color),
696                ]
697            ));
698        });
699    }
700
701    #[test]
702    fn local_progress_indicator_theme_takes_precedence_over_inherited_progress_indicator_theme() {
703        test_widgets(|tester| {
704            let color = Color::new(0xFFFF00FF);
705            let linear_track_color = Color::new(0xFF00FFFF);
706            let linear_min_height = 20.0;
707            let border_radius = 6.0;
708            let stop_indicator_color = Color::new(0xFFFFFF00);
709            let stop_indicator_radius = 8.0;
710            let track_gap = 12.0;
711            pump_with(tester, |app| {
712                let theme = theme_with(
713                    app,
714                    ProgressIndicatorThemeData::new()
715                        .color(Color::new(0xFF00FF00))
716                        .linear_track_color(Color::new(0xFFFF0000))
717                        .linear_min_height(25.0)
718                        .border_radius(BorderRadius::all(Radius::circular(8.0)))
719                        .stop_indicator_color(Color::new(0xFF0000FF))
720                        .stop_indicator_radius(10.0)
721                        .track_gap(16.0),
722                );
723                scaffold_app(
724                    Some(theme),
725                    Center::new().child(ProgressIndicatorTheme::new(
726                        ProgressIndicatorThemeData::new()
727                            .color(color)
728                            .linear_track_color(linear_track_color)
729                            .linear_min_height(linear_min_height)
730                            .border_radius(BorderRadius::all(Radius::circular(border_radius)))
731                            .stop_indicator_color(stop_indicator_color)
732                            .stop_indicator_radius(stop_indicator_radius)
733                            .track_gap(track_gap),
734                        SizedBox::new()
735                            .width(200.0)
736                            .child(LinearProgressIndicator::new().value(0.5)),
737                    )),
738                )
739            });
740            let radius = Radius::circular(border_radius);
741            assert!(paints(
742                &painted_by::<LinearProgressIndicator>(tester),
743                &[
744                    // Track.
745                    rrect(RRect::from_ltrbr(
746                        0.0,
747                        0.0,
748                        200.0,
749                        linear_min_height,
750                        radius
751                    ))
752                    .color(linear_track_color),
753                    // Active indicator.
754                    rrect(RRect::from_ltrbr(
755                        0.0,
756                        0.0,
757                        100.0,
758                        linear_min_height,
759                        radius
760                    ))
761                    .color(color),
762                ]
763            ));
764        });
765    }
766
767    #[test]
768    fn can_theme_circular_progress_indicator_using_progress_indicator_theme() {
769        test_widgets(|tester| {
770            let color = Color::new(0xFFFF0000);
771            let circular_track_color = Color::new(0xFF0000FF);
772            let stroke_width = 8.0;
773            let stroke_align = BorderSide::STROKE_ALIGN_OUTSIDE;
774            let stroke_cap = StrokeCap::Butt;
775            let constraints = BoxConstraints::tight_for(Some(80.0), Some(80.0));
776            let padding: EdgeInsetsGeometry = EdgeInsets::all(14.0).into();
777            pump_with(tester, |app| {
778                let theme = theme_with(
779                    app,
780                    ProgressIndicatorThemeData::new()
781                        .color(color)
782                        .circular_track_color(circular_track_color)
783                        .stroke_width(stroke_width)
784                        .stroke_align(stroke_align)
785                        .stroke_cap(stroke_cap)
786                        .constraints(constraints)
787                        .circular_track_padding(padding),
788                );
789                scaffold_app(
790                    Some(theme),
791                    Center::new().child(CircularProgressIndicator::new().value(0.5)),
792                )
793            });
794
795            let (indicator, _) = circular_sizes(tester);
796            assert_eq!(
797                indicator,
798                Size::new(
799                    constraints.max_width + padding.horizontal(),
800                    constraints.max_height + padding.vertical(),
801                )
802            );
803            assert!(paints(
804                &painted_by::<CircularProgressIndicator>(tester),
805                &[
806                    // Track.
807                    arc()
808                        .color(circular_track_color)
809                        .stroke_width(stroke_width)
810                        .stroke_cap(stroke_cap),
811                    // Active indicator.
812                    arc()
813                        .color(color)
814                        .stroke_width(stroke_width)
815                        .stroke_cap(stroke_cap),
816                ]
817            ));
818        });
819    }
820
821    #[test]
822    fn can_theme_circular_progress_indicator_when_year2023_to_false() {
823        test_widgets(|tester| {
824            let color = Color::new(0xFFFF0000);
825            let circular_track_color = Color::new(0xFF0000FF);
826            let stroke_width = 8.0;
827            let stroke_align = BorderSide::STROKE_ALIGN_OUTSIDE;
828            let stroke_cap = StrokeCap::Butt;
829            let constraints = BoxConstraints::tight_for(Some(80.0), Some(80.0));
830            let track_gap = 12.0;
831            let padding: EdgeInsetsGeometry = EdgeInsets::all(18.0).into();
832            pump_with(tester, |app| {
833                let theme = theme_with(
834                    app,
835                    ProgressIndicatorThemeData::new()
836                        .color(color)
837                        .circular_track_color(circular_track_color)
838                        .stroke_width(stroke_width)
839                        .stroke_align(stroke_align)
840                        .stroke_cap(stroke_cap)
841                        .constraints(constraints)
842                        .track_gap(track_gap)
843                        .circular_track_padding(padding),
844                );
845                scaffold_app(
846                    Some(theme),
847                    Center::new()
848                        .child(CircularProgressIndicator::new().year2023(false).value(0.5)),
849                )
850            });
851
852            let (indicator, indicator_box) = circular_sizes(tester);
853            assert_eq!(indicator_box, constraints.biggest());
854            assert_eq!(indicator, padded(indicator_box, padding));
855            assert!(paints(
856                &painted_by::<CircularProgressIndicator>(tester),
857                &[
858                    // Track.
859                    arc()
860                        .color(circular_track_color)
861                        .stroke_width(stroke_width)
862                        .stroke_cap(stroke_cap),
863                    // Active indicator.
864                    arc()
865                        .color(color)
866                        .stroke_width(stroke_width)
867                        .stroke_cap(stroke_cap),
868                ]
869            ));
870        });
871    }
872
873    #[test]
874    fn circular_progress_indicator_year2023_set_to_false_and_provided_circular_track_color_does_not_throw_exception()
875     {
876        test_widgets(|tester| {
877            pump_with(tester, |app| {
878                themed(
879                    theme_with(
880                        app,
881                        ProgressIndicatorThemeData::new()
882                            .circular_track_color(Color::new(0xFF0000FF))
883                            .year2023(false),
884                    ),
885                    Center::new().child(CircularProgressIndicator::new()),
886                )
887            });
888        });
889    }
890
891    fn year2023_false_theme(app: &App) -> ThemeData {
892        theme_with(app, ProgressIndicatorThemeData::new().year2023(false))
893    }
894
895    #[test]
896    fn opt_into_2024_circular_progress_indicator_appearance_with_progress_indicator_theme_data_year2023()
897     {
898        test_widgets(|tester| {
899            let padding: EdgeInsetsGeometry = EdgeInsets::all(4.0).into();
900            pump_with(tester, |app| {
901                scaffold_app(
902                    Some(year2023_false_theme(app)),
903                    Center::new().child(CircularProgressIndicator::new().value(0.5)),
904                )
905            });
906            let colors = year2023_false_theme(&tester.cell.borrow()).color_scheme;
907
908            let (indicator, indicator_box) = circular_sizes(tester);
909            assert_eq!(indicator, padded(indicator_box, padding));
910            let arc_rect = Rect::from_ltrb(2.0, 2.0, 38.0, 38.0);
911            assert!(paints(
912                &painted_by::<CircularProgressIndicator>(tester),
913                &[
914                    // Track.
915                    arc()
916                        .in_rect(arc_rect)
917                        .color(colors.secondary_container())
918                        .stroke_width(4.0)
919                        .stroke_cap(StrokeCap::Round),
920                    // Active indicator.
921                    arc()
922                        .in_rect(arc_rect)
923                        .color(colors.primary())
924                        .stroke_width(4.0)
925                        .stroke_cap(StrokeCap::Round),
926                ]
927            ));
928        });
929    }
930
931    #[test]
932    fn circular_progress_indicator_year2023_overrides_progress_indicator_theme_data_year2023() {
933        test_widgets(|tester| {
934            pump_with(tester, |app| {
935                scaffold_app(
936                    Some(year2023_false_theme(app)),
937                    Center::new().child(CircularProgressIndicator::new().year2023(true).value(0.5)),
938                )
939            });
940            let colors = year2023_false_theme(&tester.cell.borrow()).color_scheme;
941
942            let (indicator, indicator_box) = circular_sizes(tester);
943            assert_eq!(indicator, indicator_box);
944            assert!(paints(
945                &painted_by::<CircularProgressIndicator>(tester),
946                &[
947                    // Active indicator.
948                    arc()
949                        .in_rect(Rect::from_ltrb(-0.0, -0.0, 36.0, 36.0))
950                        .color(colors.primary())
951                        .stroke_width(4.0),
952                ]
953            ));
954        });
955    }
956
957    #[test]
958    fn opt_into_2024_linear_progress_indicator_appearance_with_progress_indicator_theme_data_year2023()
959     {
960        test_widgets(|tester| {
961            let default_track_gap = 4.0;
962            pump_with(tester, |app| {
963                scaffold_app(
964                    Some(year2023_false_theme(app)),
965                    SizedBox::new()
966                        .width(200.0)
967                        .child(LinearProgressIndicator::new().value(0.5)),
968                )
969            });
970            let colors = year2023_false_theme(&tester.cell.borrow()).color_scheme;
971            let radius = Radius::circular(2.0);
972            assert!(paints(
973                &painted_by::<LinearProgressIndicator>(tester),
974                &[
975                    // Track.
976                    rrect(RRect::from_ltrbr(
977                        100.0 + default_track_gap,
978                        0.0,
979                        200.0,
980                        4.0,
981                        radius
982                    ))
983                    .color(colors.secondary_container()),
984                    // Stop indicator.
985                    circle().at(198.0, 2.0).radius(2.0).color(colors.primary()),
986                    // Active track.
987                    rrect(RRect::from_ltrbr(0.0, 0.0, 100.0, 4.0, radius)).color(colors.primary()),
988                ]
989            ));
990        });
991    }
992
993    #[test]
994    fn linear_progress_indicator_year2023_overrides_progress_indicator_theme_data_year2023() {
995        test_widgets(|tester| {
996            pump_with(tester, |app| {
997                scaffold_app(
998                    Some(year2023_false_theme(app)),
999                    SizedBox::new()
1000                        .width(200.0)
1001                        .child(LinearProgressIndicator::new().year2023(true).value(0.5)),
1002                )
1003            });
1004            let colors = year2023_false_theme(&tester.cell.borrow()).color_scheme;
1005            assert!(paints(
1006                &painted_by::<LinearProgressIndicator>(tester),
1007                &[
1008                    // Track.
1009                    rect(Rect::from_ltrb(0.0, 0.0, 200.0, 4.0)).color(colors.secondary_container()),
1010                    // Active track.
1011                    rect(Rect::from_ltrb(0.0, 0.0, 100.0, 4.0)).color(colors.primary()),
1012                ]
1013            ));
1014        });
1015    }
1016
1017    /// The controllers of the theme-controller cases: the widget's, the
1018    /// `ProgressIndicatorTheme`'s and the `ThemeData`'s.
1019    #[derive(Clone, Default)]
1020    struct Controllers {
1021        widget: Option<Ref<AnimationController>>,
1022        indicator_theme: Option<Ref<AnimationController>>,
1023        global_theme: Option<Ref<AnimationController>>,
1024    }
1025
1026    /// The theme-controller cases' `buildApp`, around `indicator`.
1027    fn build_app(
1028        app: &App,
1029        global_theme: ProgressIndicatorThemeData,
1030        controllers: Controllers,
1031        indicator: WidgetRef,
1032    ) -> WidgetRef {
1033        let with = |data: ProgressIndicatorThemeData, controller| match controller {
1034            Some(controller) => data.controller(controller),
1035            None => data,
1036        };
1037        crate::app::MaterialApp::new()
1038            .home(Material::new().child(Center::new().child(themed(
1039                theme_with(app, with(global_theme, controllers.global_theme.clone())),
1040                ProgressIndicatorTheme::new(
1041                    with(
1042                        ProgressIndicatorThemeData::new(),
1043                        controllers.indicator_theme.clone(),
1044                    ),
1045                    SizedBox::new().width(200.0).child(indicator),
1046                ),
1047            ))))
1048            .into_widget()
1049    }
1050
1051    #[test]
1052    fn linear_progress_indicator_reflects_the_value_of_the_theme_controller() {
1053        test_widgets(|tester| {
1054            let pump = |tester: &mut WidgetTester, controllers: Controllers| {
1055                pump_with(tester, |app| {
1056                    let indicator = match controllers.widget.clone() {
1057                        Some(controller) => LinearProgressIndicator::new().controller(controller),
1058                        None => LinearProgressIndicator::new(),
1059                    };
1060                    build_app(
1061                        app,
1062                        ProgressIndicatorThemeData::new(),
1063                        controllers.clone(),
1064                        indicator.into_widget(),
1065                    )
1066                });
1067            };
1068            let expect_progress_at = |tester: &WidgetTester, left: f64, right: f64| {
1069                let mut expected = Vec::new();
1070                if right < 200.0 {
1071                    // Right track
1072                    expected.push(rect(Rect::from_ltrb(right, 0.0, 200.0, 4.0)));
1073                }
1074                expected.push(rect(Rect::from_ltrb(left, 0.0, right, 4.0)));
1075                if left > 0.0 {
1076                    // Left track
1077                    expected.push(rect(Rect::from_ltrb(0.0, 0.0, left, 4.0)));
1078                }
1079                assert!(
1080                    paints(&painted_by::<LinearProgressIndicator>(tester), &expected),
1081                    "progress from {left} to {right}"
1082                );
1083            };
1084
1085            let mut controllers = Controllers::default();
1086            pump(tester, controllers.clone());
1087            tester.pump_by(Duration::from_millis(500));
1088            expect_progress_at(tester, 16.028758883476257, 141.07513427734375);
1089
1090            controllers.global_theme = Some(controller_at(tester, 0.1));
1091            pump(tester, controllers.clone());
1092            expect_progress_at(tester, 0.0, 37.14974820613861);
1093
1094            controllers.indicator_theme = Some(controller_at(tester, 0.5));
1095            pump(tester, controllers.clone());
1096            expect_progress_at(tester, 127.79541015625, 200.0);
1097
1098            controllers.widget = Some(controller_at(tester, 0.8));
1099            pump(tester, controllers.clone());
1100            expect_progress_at(tester, 98.24226796627045, 181.18448555469513);
1101        });
1102    }
1103
1104    #[test]
1105    fn circular_progress_indicator_reflects_the_value_of_the_theme_controller() {
1106        test_widgets(|tester| {
1107            let pump = |tester: &mut WidgetTester, controllers: Controllers| {
1108                pump_with(tester, |app| {
1109                    let indicator = match controllers.widget.clone() {
1110                        Some(controller) => CircularProgressIndicator::new().controller(controller),
1111                        None => CircularProgressIndicator::new(),
1112                    };
1113                    build_app(
1114                        app,
1115                        ProgressIndicatorThemeData::new()
1116                            .color(Colors::BLACK)
1117                            .linear_track_color(Colors::GREEN.primary()),
1118                        controllers.clone(),
1119                        indicator.into_widget(),
1120                    )
1121                });
1122            };
1123            let expect_progress_at = |tester: &WidgetTester, start: f64, sweep: f64| {
1124                assert_eq!(
1125                    active_arc_of::<CircularProgressIndicator>(tester),
1126                    (start, sweep)
1127                );
1128            };
1129
1130            let mut controllers = Controllers::default();
1131            pump(tester, controllers.clone());
1132            tester.pump_by(Duration::from_millis(500));
1133            expect_progress_at(tester, 0.43225767465697107, 4.52182126629162);
1134
1135            controllers.global_theme = Some(controller_at(tester, 0.1));
1136            pump(tester, controllers.clone());
1137            expect_progress_at(tester, 0.628318530718057, 2.8904563625380906);
1138
1139            controllers.indicator_theme = Some(controller_at(tester, 0.5));
1140            pump(tester, controllers.clone());
1141            expect_progress_at(tester, std::f64::consts::FRAC_PI_2, 0.001);
1142
1143            controllers.widget = Some(controller_at(tester, 0.8));
1144            pump(tester, controllers.clone());
1145            expect_progress_at(tester, 2.520489337828999, 4.076855234710353);
1146        });
1147    }
1148    // Flutter's "Default ProgressIndicatorThemeData debugFillProperties".
1149    #[test]
1150    fn a_progress_indicator_theme_data_describes_nothing_by_default() {
1151        assert_eq!(
1152            waft_widgets::testing::describe_properties(|properties| {
1153                waft_foundation::Diagnosticable::debug_fill_properties(
1154                    &ProgressIndicatorThemeData::new(),
1155                    properties,
1156                )
1157            }),
1158            Vec::<String>::new()
1159        );
1160    }
1161}