use waft_animation::AnimationController;
use waft_embedder::StrokeCap;
use waft_embedder::{Color, lerp_double};
use waft_foundation::{App, DiagnosticsProperty, DoubleProperty, Ref};
use waft_painting::{BorderRadiusGeometry, ColorProperty, EdgeInsetsGeometry};
use waft_rendering::BoxConstraints;
use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
use crate::theme::Theme;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ProgressIndicatorThemeData {
pub color: Option<Color>,
pub linear_track_color: Option<Color>,
pub linear_min_height: Option<f64>,
pub circular_track_color: Option<Color>,
pub refresh_background_color: Option<Color>,
pub border_radius: Option<BorderRadiusGeometry>,
pub stop_indicator_color: Option<Color>,
pub stop_indicator_radius: Option<f64>,
pub stroke_width: Option<f64>,
pub stroke_align: Option<f64>,
pub stroke_cap: Option<StrokeCap>,
pub constraints: Option<BoxConstraints>,
pub track_gap: Option<f64>,
pub circular_track_padding: Option<EdgeInsetsGeometry>,
#[deprecated(
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."
)]
pub year2023: Option<bool>,
pub controller: Option<Ref<AnimationController>>,
}
crate::theme_data_getters::theme_data_getters! {
pub trait ProgressIndicatorThemeDataGetters for ProgressIndicatorThemeData, fields: progress_indicator_theme_data {
color: Option<Color>,
linear_track_color: Option<Color>,
linear_min_height: Option<f64>,
circular_track_color: Option<Color>,
refresh_background_color: Option<Color>,
border_radius: Option<BorderRadiusGeometry>,
stop_indicator_color: Option<Color>,
stop_indicator_radius: Option<f64>,
stroke_width: Option<f64>,
stroke_align: Option<f64>,
stroke_cap: Option<StrokeCap>,
constraints: Option<BoxConstraints>,
track_gap: Option<f64>,
circular_track_padding: Option<EdgeInsetsGeometry>,
year2023: Option<bool>,
controller: Option<Ref<AnimationController>>,
}
}
#[allow(deprecated)]
impl ProgressIndicatorThemeData {
pub fn new() -> ProgressIndicatorThemeData {
ProgressIndicatorThemeData::default()
}
pub fn color(mut self, color: Color) -> ProgressIndicatorThemeData {
self.color = Some(color);
self
}
pub fn linear_track_color(mut self, linear_track_color: Color) -> ProgressIndicatorThemeData {
self.linear_track_color = Some(linear_track_color);
self
}
pub fn linear_min_height(mut self, linear_min_height: f64) -> ProgressIndicatorThemeData {
self.linear_min_height = Some(linear_min_height);
self
}
pub fn circular_track_color(
mut self,
circular_track_color: Color,
) -> ProgressIndicatorThemeData {
self.circular_track_color = Some(circular_track_color);
self
}
pub fn refresh_background_color(
mut self,
refresh_background_color: Color,
) -> ProgressIndicatorThemeData {
self.refresh_background_color = Some(refresh_background_color);
self
}
pub fn border_radius(
mut self,
border_radius: impl Into<BorderRadiusGeometry>,
) -> ProgressIndicatorThemeData {
self.border_radius = Some(border_radius.into());
self
}
pub fn stop_indicator_color(
mut self,
stop_indicator_color: Color,
) -> ProgressIndicatorThemeData {
self.stop_indicator_color = Some(stop_indicator_color);
self
}
pub fn stop_indicator_radius(
mut self,
stop_indicator_radius: f64,
) -> ProgressIndicatorThemeData {
self.stop_indicator_radius = Some(stop_indicator_radius);
self
}
pub fn stroke_width(mut self, stroke_width: f64) -> ProgressIndicatorThemeData {
self.stroke_width = Some(stroke_width);
self
}
pub fn stroke_align(mut self, stroke_align: f64) -> ProgressIndicatorThemeData {
self.stroke_align = Some(stroke_align);
self
}
pub fn stroke_cap(mut self, stroke_cap: StrokeCap) -> ProgressIndicatorThemeData {
self.stroke_cap = Some(stroke_cap);
self
}
pub fn constraints(mut self, constraints: BoxConstraints) -> ProgressIndicatorThemeData {
self.constraints = Some(constraints);
self
}
pub fn track_gap(mut self, track_gap: f64) -> ProgressIndicatorThemeData {
self.track_gap = Some(track_gap);
self
}
pub fn circular_track_padding(
mut self,
circular_track_padding: impl Into<EdgeInsetsGeometry>,
) -> ProgressIndicatorThemeData {
self.circular_track_padding = Some(circular_track_padding.into());
self
}
#[deprecated(
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."
)]
pub fn year2023(mut self, year2023: bool) -> ProgressIndicatorThemeData {
self.year2023 = Some(year2023);
self
}
pub fn controller(
mut self,
controller: Ref<AnimationController>,
) -> ProgressIndicatorThemeData {
self.controller = Some(controller);
self
}
pub fn copy_with(&self) -> ProgressIndicatorThemeData {
self.clone()
}
pub fn lerp(
a: Option<&ProgressIndicatorThemeData>,
b: Option<&ProgressIndicatorThemeData>,
t: f64,
) -> Option<ProgressIndicatorThemeData> {
match (a, b) {
(None, None) => return None,
(Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
_ => {}
}
Some(ProgressIndicatorThemeData {
color: Color::lerp(a.and_then(|a| a.color), b.and_then(|b| b.color), t),
linear_track_color: Color::lerp(
a.and_then(|a| a.linear_track_color),
b.and_then(|b| b.linear_track_color),
t,
),
linear_min_height: lerp_double(
a.and_then(|a| a.linear_min_height),
b.and_then(|b| b.linear_min_height),
t,
),
circular_track_color: Color::lerp(
a.and_then(|a| a.circular_track_color),
b.and_then(|b| b.circular_track_color),
t,
),
refresh_background_color: Color::lerp(
a.and_then(|a| a.refresh_background_color),
b.and_then(|b| b.refresh_background_color),
t,
),
border_radius: BorderRadiusGeometry::lerp(
a.and_then(|a| a.border_radius),
b.and_then(|b| b.border_radius),
t,
),
stop_indicator_color: Color::lerp(
a.and_then(|a| a.stop_indicator_color),
b.and_then(|b| b.stop_indicator_color),
t,
),
stop_indicator_radius: lerp_double(
a.and_then(|a| a.stop_indicator_radius),
b.and_then(|b| b.stop_indicator_radius),
t,
),
stroke_width: lerp_double(
a.and_then(|a| a.stroke_width),
b.and_then(|b| b.stroke_width),
t,
),
stroke_align: lerp_double(
a.and_then(|a| a.stroke_align),
b.and_then(|b| b.stroke_align),
t,
),
stroke_cap: pick(a, b, t, |theme| theme.stroke_cap),
constraints: BoxConstraints::lerp(
a.and_then(|a| a.constraints),
b.and_then(|b| b.constraints),
t,
),
track_gap: lerp_double(a.and_then(|a| a.track_gap), b.and_then(|b| b.track_gap), t),
circular_track_padding: EdgeInsetsGeometry::lerp(
a.and_then(|a| a.circular_track_padding),
b.and_then(|b| b.circular_track_padding),
t,
),
year2023: pick(a, b, t, |theme| theme.year2023),
controller: pick(a, b, t, |theme| theme.controller.clone()),
})
}
}
fn pick<T>(
a: Option<&ProgressIndicatorThemeData>,
b: Option<&ProgressIndicatorThemeData>,
t: f64,
field: impl Fn(&ProgressIndicatorThemeData) -> Option<T>,
) -> Option<T> {
if t < 0.5 {
a.and_then(&field)
} else {
b.and_then(&field)
}
}
#[derive(Debug)]
pub struct ProgressIndicatorTheme {
pub key: Option<KeyRef>,
pub data: ProgressIndicatorThemeData,
pub child: WidgetRef,
}
impl ProgressIndicatorTheme {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(
data: ProgressIndicatorThemeData,
child: impl IntoWidget<K>,
) -> ProgressIndicatorTheme {
ProgressIndicatorTheme {
key: None,
data,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> ProgressIndicatorTheme {
self.key = Some(key);
self
}
pub fn of(app: &mut App, context: BuildContext) -> ProgressIndicatorThemeData {
match context.depend_on_inherited_widget_of_exact_type::<ProgressIndicatorTheme>(app) {
Some(progress_indicator_theme) => progress_indicator_theme.data.clone(),
None => Theme::of(app, context).progress_indicator_theme.clone(),
}
}
}
impl InheritedTheme for ProgressIndicatorTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
ProgressIndicatorTheme::new(self.data.clone(), child).into_widget()
}
}
impl InheritedWidget for ProgressIndicatorTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &ProgressIndicatorTheme) -> bool {
self.data != old_widget.data
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
#[allow(deprecated)]
impl waft_foundation::Diagnosticable for ProgressIndicatorThemeData {
fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
properties.add(ColorProperty::new("color", self.color).default_value(None));
properties.add(
ColorProperty::new("linearTrackColor", self.linear_track_color).default_value(None),
);
properties.add(
DoubleProperty::new("linearMinHeight", self.linear_min_height).default_value(None),
);
properties.add(
ColorProperty::new("circularTrackColor", self.circular_track_color).default_value(None),
);
properties.add(
ColorProperty::new("refreshBackgroundColor", self.refresh_background_color)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<BorderRadiusGeometry>::new("borderRadius", self.border_radius)
.default_value(None),
);
properties.add(
ColorProperty::new("stopIndicatorColor", self.stop_indicator_color).default_value(None),
);
properties.add(
DoubleProperty::new("stopIndicatorRadius", self.stop_indicator_radius)
.default_value(None),
);
properties.add(DoubleProperty::new("strokeWidth", self.stroke_width).default_value(None));
properties.add(DoubleProperty::new("strokeAlign", self.stroke_align).default_value(None));
properties.add(
DiagnosticsProperty::<waft_foundation::DartEnum<StrokeCap>>::new(
"strokeCap",
self.stroke_cap.map(waft_foundation::DartEnum),
)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<BoxConstraints>::new("constraints", self.constraints)
.default_value(None),
);
properties.add(DoubleProperty::new("trackGap", self.track_gap).default_value(None));
properties.add(
DiagnosticsProperty::<EdgeInsetsGeometry>::new(
"circularTrackPadding",
self.circular_track_padding,
)
.default_value(None),
);
properties
.add(DiagnosticsProperty::<bool>::new("year2023", self.year2023).default_value(None));
properties.add(
DiagnosticsProperty::<String>::new(
"controller",
self.controller.as_ref().map(|controller| {
waft_foundation::describe_identity(
"AnimationController",
controller.handle().id().identity_hash(),
)
}),
)
.default_value(None),
);
}
}
impl std::fmt::Display for ProgressIndicatorThemeData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&waft_foundation::Diagnosticable::to_string(self))
}
}
#[cfg(test)]
mod tests {
#![allow(deprecated)]
use std::time::Duration;
use waft_embedder::{RRect, Radius, Rect, Size};
use waft_painting::{BorderRadius, BorderSide, EdgeInsets};
use waft_widgets::{Center, SizedBox};
use waft_widgets::testing::{WidgetTester, test_widgets};
use super::*;
use crate::colors::Colors;
use crate::material::Material;
use crate::progress_indicator::tests::{
active_arc_of, arc, centered_width, circle, circular_sizes, controller_at, padded,
painted_by, paints, pump_with, rect, rrect, scaffold_app, theme_with, themed,
};
use crate::progress_indicator::{CircularProgressIndicator, LinearProgressIndicator};
use crate::theme_data::ThemeData;
#[test]
fn progress_indicator_theme_data_copy_with_equality_basics() {
assert_eq!(
ProgressIndicatorThemeData::new(),
ProgressIndicatorThemeData::new().copy_with()
);
}
#[test]
fn progress_indicator_theme_data_lerp_special_cases() {
assert_eq!(ProgressIndicatorThemeData::lerp(None, None, 0.0), None);
let data = ProgressIndicatorThemeData::new();
assert_eq!(
ProgressIndicatorThemeData::lerp(Some(&data), Some(&data), 0.5),
Some(data)
);
}
#[test]
fn can_theme_linear_progress_indicator_using_progress_indicator_theme() {
test_widgets(|tester| {
let color = Color::new(0xFF00FF00);
let linear_track_color = Color::new(0xFFFF0000);
let linear_min_height = 25.0;
let border_radius = 8.0;
pump_with(tester, |app| {
let theme = theme_with(
app,
ProgressIndicatorThemeData::new()
.color(color)
.linear_track_color(linear_track_color)
.linear_min_height(linear_min_height)
.border_radius(BorderRadius::all(Radius::circular(border_radius))),
);
scaffold_app(
Some(theme),
centered_width(200.0, LinearProgressIndicator::new().value(0.5)),
)
});
let radius = Radius::circular(border_radius);
assert!(paints(
&painted_by::<LinearProgressIndicator>(tester),
&[
rrect(RRect::from_ltrbr(
0.0,
0.0,
200.0,
linear_min_height,
radius
))
.color(linear_track_color),
rrect(RRect::from_ltrbr(
0.0,
0.0,
100.0,
linear_min_height,
radius
))
.color(color),
]
));
});
}
#[test]
fn can_theme_linear_progress_indicator_when_year2023_to_false() {
test_widgets(|tester| {
let color = Color::new(0xFF00FF00);
let linear_track_color = Color::new(0xFFFF0000);
let linear_min_height = 25.0;
let border_radius = 8.0;
let stop_indicator_color = Color::new(0xFF0000FF);
let stop_indicator_radius = 10.0;
let track_gap = 16.0;
pump_with(tester, |app| {
let theme = theme_with(
app,
ProgressIndicatorThemeData::new()
.color(color)
.linear_track_color(linear_track_color)
.linear_min_height(linear_min_height)
.border_radius(BorderRadius::all(Radius::circular(border_radius)))
.stop_indicator_color(stop_indicator_color)
.stop_indicator_radius(stop_indicator_radius)
.track_gap(track_gap),
);
scaffold_app(
Some(theme),
centered_width(
200.0,
LinearProgressIndicator::new().year2023(false).value(0.5),
),
)
});
let radius = Radius::circular(border_radius);
assert!(paints(
&painted_by::<LinearProgressIndicator>(tester),
&[
rrect(RRect::from_ltrbr(
100.0 + track_gap,
0.0,
200.0,
linear_min_height,
radius
))
.color(linear_track_color),
circle()
.at(187.5, 12.5)
.radius(stop_indicator_radius)
.color(stop_indicator_color),
rrect(RRect::from_ltrbr(
0.0,
0.0,
100.0,
linear_min_height,
radius
))
.color(color),
]
));
});
}
#[test]
fn local_progress_indicator_theme_takes_precedence_over_inherited_progress_indicator_theme() {
test_widgets(|tester| {
let color = Color::new(0xFFFF00FF);
let linear_track_color = Color::new(0xFF00FFFF);
let linear_min_height = 20.0;
let border_radius = 6.0;
let stop_indicator_color = Color::new(0xFFFFFF00);
let stop_indicator_radius = 8.0;
let track_gap = 12.0;
pump_with(tester, |app| {
let theme = theme_with(
app,
ProgressIndicatorThemeData::new()
.color(Color::new(0xFF00FF00))
.linear_track_color(Color::new(0xFFFF0000))
.linear_min_height(25.0)
.border_radius(BorderRadius::all(Radius::circular(8.0)))
.stop_indicator_color(Color::new(0xFF0000FF))
.stop_indicator_radius(10.0)
.track_gap(16.0),
);
scaffold_app(
Some(theme),
Center::new().child(ProgressIndicatorTheme::new(
ProgressIndicatorThemeData::new()
.color(color)
.linear_track_color(linear_track_color)
.linear_min_height(linear_min_height)
.border_radius(BorderRadius::all(Radius::circular(border_radius)))
.stop_indicator_color(stop_indicator_color)
.stop_indicator_radius(stop_indicator_radius)
.track_gap(track_gap),
SizedBox::new()
.width(200.0)
.child(LinearProgressIndicator::new().value(0.5)),
)),
)
});
let radius = Radius::circular(border_radius);
assert!(paints(
&painted_by::<LinearProgressIndicator>(tester),
&[
rrect(RRect::from_ltrbr(
0.0,
0.0,
200.0,
linear_min_height,
radius
))
.color(linear_track_color),
rrect(RRect::from_ltrbr(
0.0,
0.0,
100.0,
linear_min_height,
radius
))
.color(color),
]
));
});
}
#[test]
fn can_theme_circular_progress_indicator_using_progress_indicator_theme() {
test_widgets(|tester| {
let color = Color::new(0xFFFF0000);
let circular_track_color = Color::new(0xFF0000FF);
let stroke_width = 8.0;
let stroke_align = BorderSide::STROKE_ALIGN_OUTSIDE;
let stroke_cap = StrokeCap::Butt;
let constraints = BoxConstraints::tight_for(Some(80.0), Some(80.0));
let padding: EdgeInsetsGeometry = EdgeInsets::all(14.0).into();
pump_with(tester, |app| {
let theme = theme_with(
app,
ProgressIndicatorThemeData::new()
.color(color)
.circular_track_color(circular_track_color)
.stroke_width(stroke_width)
.stroke_align(stroke_align)
.stroke_cap(stroke_cap)
.constraints(constraints)
.circular_track_padding(padding),
);
scaffold_app(
Some(theme),
Center::new().child(CircularProgressIndicator::new().value(0.5)),
)
});
let (indicator, _) = circular_sizes(tester);
assert_eq!(
indicator,
Size::new(
constraints.max_width + padding.horizontal(),
constraints.max_height + padding.vertical(),
)
);
assert!(paints(
&painted_by::<CircularProgressIndicator>(tester),
&[
arc()
.color(circular_track_color)
.stroke_width(stroke_width)
.stroke_cap(stroke_cap),
arc()
.color(color)
.stroke_width(stroke_width)
.stroke_cap(stroke_cap),
]
));
});
}
#[test]
fn can_theme_circular_progress_indicator_when_year2023_to_false() {
test_widgets(|tester| {
let color = Color::new(0xFFFF0000);
let circular_track_color = Color::new(0xFF0000FF);
let stroke_width = 8.0;
let stroke_align = BorderSide::STROKE_ALIGN_OUTSIDE;
let stroke_cap = StrokeCap::Butt;
let constraints = BoxConstraints::tight_for(Some(80.0), Some(80.0));
let track_gap = 12.0;
let padding: EdgeInsetsGeometry = EdgeInsets::all(18.0).into();
pump_with(tester, |app| {
let theme = theme_with(
app,
ProgressIndicatorThemeData::new()
.color(color)
.circular_track_color(circular_track_color)
.stroke_width(stroke_width)
.stroke_align(stroke_align)
.stroke_cap(stroke_cap)
.constraints(constraints)
.track_gap(track_gap)
.circular_track_padding(padding),
);
scaffold_app(
Some(theme),
Center::new()
.child(CircularProgressIndicator::new().year2023(false).value(0.5)),
)
});
let (indicator, indicator_box) = circular_sizes(tester);
assert_eq!(indicator_box, constraints.biggest());
assert_eq!(indicator, padded(indicator_box, padding));
assert!(paints(
&painted_by::<CircularProgressIndicator>(tester),
&[
arc()
.color(circular_track_color)
.stroke_width(stroke_width)
.stroke_cap(stroke_cap),
arc()
.color(color)
.stroke_width(stroke_width)
.stroke_cap(stroke_cap),
]
));
});
}
#[test]
fn circular_progress_indicator_year2023_set_to_false_and_provided_circular_track_color_does_not_throw_exception()
{
test_widgets(|tester| {
pump_with(tester, |app| {
themed(
theme_with(
app,
ProgressIndicatorThemeData::new()
.circular_track_color(Color::new(0xFF0000FF))
.year2023(false),
),
Center::new().child(CircularProgressIndicator::new()),
)
});
});
}
fn year2023_false_theme(app: &App) -> ThemeData {
theme_with(app, ProgressIndicatorThemeData::new().year2023(false))
}
#[test]
fn opt_into_2024_circular_progress_indicator_appearance_with_progress_indicator_theme_data_year2023()
{
test_widgets(|tester| {
let padding: EdgeInsetsGeometry = EdgeInsets::all(4.0).into();
pump_with(tester, |app| {
scaffold_app(
Some(year2023_false_theme(app)),
Center::new().child(CircularProgressIndicator::new().value(0.5)),
)
});
let colors = year2023_false_theme(&tester.cell.borrow()).color_scheme;
let (indicator, indicator_box) = circular_sizes(tester);
assert_eq!(indicator, padded(indicator_box, padding));
let arc_rect = Rect::from_ltrb(2.0, 2.0, 38.0, 38.0);
assert!(paints(
&painted_by::<CircularProgressIndicator>(tester),
&[
arc()
.in_rect(arc_rect)
.color(colors.secondary_container())
.stroke_width(4.0)
.stroke_cap(StrokeCap::Round),
arc()
.in_rect(arc_rect)
.color(colors.primary())
.stroke_width(4.0)
.stroke_cap(StrokeCap::Round),
]
));
});
}
#[test]
fn circular_progress_indicator_year2023_overrides_progress_indicator_theme_data_year2023() {
test_widgets(|tester| {
pump_with(tester, |app| {
scaffold_app(
Some(year2023_false_theme(app)),
Center::new().child(CircularProgressIndicator::new().year2023(true).value(0.5)),
)
});
let colors = year2023_false_theme(&tester.cell.borrow()).color_scheme;
let (indicator, indicator_box) = circular_sizes(tester);
assert_eq!(indicator, indicator_box);
assert!(paints(
&painted_by::<CircularProgressIndicator>(tester),
&[
arc()
.in_rect(Rect::from_ltrb(-0.0, -0.0, 36.0, 36.0))
.color(colors.primary())
.stroke_width(4.0),
]
));
});
}
#[test]
fn opt_into_2024_linear_progress_indicator_appearance_with_progress_indicator_theme_data_year2023()
{
test_widgets(|tester| {
let default_track_gap = 4.0;
pump_with(tester, |app| {
scaffold_app(
Some(year2023_false_theme(app)),
SizedBox::new()
.width(200.0)
.child(LinearProgressIndicator::new().value(0.5)),
)
});
let colors = year2023_false_theme(&tester.cell.borrow()).color_scheme;
let radius = Radius::circular(2.0);
assert!(paints(
&painted_by::<LinearProgressIndicator>(tester),
&[
rrect(RRect::from_ltrbr(
100.0 + default_track_gap,
0.0,
200.0,
4.0,
radius
))
.color(colors.secondary_container()),
circle().at(198.0, 2.0).radius(2.0).color(colors.primary()),
rrect(RRect::from_ltrbr(0.0, 0.0, 100.0, 4.0, radius)).color(colors.primary()),
]
));
});
}
#[test]
fn linear_progress_indicator_year2023_overrides_progress_indicator_theme_data_year2023() {
test_widgets(|tester| {
pump_with(tester, |app| {
scaffold_app(
Some(year2023_false_theme(app)),
SizedBox::new()
.width(200.0)
.child(LinearProgressIndicator::new().year2023(true).value(0.5)),
)
});
let colors = year2023_false_theme(&tester.cell.borrow()).color_scheme;
assert!(paints(
&painted_by::<LinearProgressIndicator>(tester),
&[
rect(Rect::from_ltrb(0.0, 0.0, 200.0, 4.0)).color(colors.secondary_container()),
rect(Rect::from_ltrb(0.0, 0.0, 100.0, 4.0)).color(colors.primary()),
]
));
});
}
#[derive(Clone, Default)]
struct Controllers {
widget: Option<Ref<AnimationController>>,
indicator_theme: Option<Ref<AnimationController>>,
global_theme: Option<Ref<AnimationController>>,
}
fn build_app(
app: &App,
global_theme: ProgressIndicatorThemeData,
controllers: Controllers,
indicator: WidgetRef,
) -> WidgetRef {
let with = |data: ProgressIndicatorThemeData, controller| match controller {
Some(controller) => data.controller(controller),
None => data,
};
crate::app::MaterialApp::new()
.home(Material::new().child(Center::new().child(themed(
theme_with(app, with(global_theme, controllers.global_theme.clone())),
ProgressIndicatorTheme::new(
with(
ProgressIndicatorThemeData::new(),
controllers.indicator_theme.clone(),
),
SizedBox::new().width(200.0).child(indicator),
),
))))
.into_widget()
}
#[test]
fn linear_progress_indicator_reflects_the_value_of_the_theme_controller() {
test_widgets(|tester| {
let pump = |tester: &mut WidgetTester, controllers: Controllers| {
pump_with(tester, |app| {
let indicator = match controllers.widget.clone() {
Some(controller) => LinearProgressIndicator::new().controller(controller),
None => LinearProgressIndicator::new(),
};
build_app(
app,
ProgressIndicatorThemeData::new(),
controllers.clone(),
indicator.into_widget(),
)
});
};
let expect_progress_at = |tester: &WidgetTester, left: f64, right: f64| {
let mut expected = Vec::new();
if right < 200.0 {
expected.push(rect(Rect::from_ltrb(right, 0.0, 200.0, 4.0)));
}
expected.push(rect(Rect::from_ltrb(left, 0.0, right, 4.0)));
if left > 0.0 {
expected.push(rect(Rect::from_ltrb(0.0, 0.0, left, 4.0)));
}
assert!(
paints(&painted_by::<LinearProgressIndicator>(tester), &expected),
"progress from {left} to {right}"
);
};
let mut controllers = Controllers::default();
pump(tester, controllers.clone());
tester.pump_by(Duration::from_millis(500));
expect_progress_at(tester, 16.028758883476257, 141.07513427734375);
controllers.global_theme = Some(controller_at(tester, 0.1));
pump(tester, controllers.clone());
expect_progress_at(tester, 0.0, 37.14974820613861);
controllers.indicator_theme = Some(controller_at(tester, 0.5));
pump(tester, controllers.clone());
expect_progress_at(tester, 127.79541015625, 200.0);
controllers.widget = Some(controller_at(tester, 0.8));
pump(tester, controllers.clone());
expect_progress_at(tester, 98.24226796627045, 181.18448555469513);
});
}
#[test]
fn circular_progress_indicator_reflects_the_value_of_the_theme_controller() {
test_widgets(|tester| {
let pump = |tester: &mut WidgetTester, controllers: Controllers| {
pump_with(tester, |app| {
let indicator = match controllers.widget.clone() {
Some(controller) => CircularProgressIndicator::new().controller(controller),
None => CircularProgressIndicator::new(),
};
build_app(
app,
ProgressIndicatorThemeData::new()
.color(Colors::BLACK)
.linear_track_color(Colors::GREEN.primary()),
controllers.clone(),
indicator.into_widget(),
)
});
};
let expect_progress_at = |tester: &WidgetTester, start: f64, sweep: f64| {
assert_eq!(
active_arc_of::<CircularProgressIndicator>(tester),
(start, sweep)
);
};
let mut controllers = Controllers::default();
pump(tester, controllers.clone());
tester.pump_by(Duration::from_millis(500));
expect_progress_at(tester, 0.43225767465697107, 4.52182126629162);
controllers.global_theme = Some(controller_at(tester, 0.1));
pump(tester, controllers.clone());
expect_progress_at(tester, 0.628318530718057, 2.8904563625380906);
controllers.indicator_theme = Some(controller_at(tester, 0.5));
pump(tester, controllers.clone());
expect_progress_at(tester, std::f64::consts::FRAC_PI_2, 0.001);
controllers.widget = Some(controller_at(tester, 0.8));
pump(tester, controllers.clone());
expect_progress_at(tester, 2.520489337828999, 4.076855234710353);
});
}
#[test]
fn a_progress_indicator_theme_data_describes_nothing_by_default() {
assert_eq!(
waft_widgets::testing::describe_properties(|properties| {
waft_foundation::Diagnosticable::debug_fill_properties(
&ProgressIndicatorThemeData::new(),
properties,
)
}),
Vec::<String>::new()
);
}
}