use crate::*;
use std::{any::Any, time::Duration};
use waft_animation::{Cubic, Curve};
use waft_embedder::{Color, Rect, Size};
use waft_foundation::{App, Handle, OptionRetained, Ref};
use waft_painting::EdgeInsetsGeometry;
use waft_rendering::CustomClipper;
use waft_scheduler::{FrameCallback, Ticker};
use waft_widgets::*;
#[derive(Clone, Debug)]
pub struct ProgressBar {
pub minimum: f64,
pub maximum: f64,
pub value: f64,
pub is_indeterminate: bool,
pub show_paused: bool,
pub show_error: bool,
pub is_visible: bool,
pub min_height: Option<f64>,
pub padding: [f64; 4],
pub border_thickness: Option<[f64; 4]>,
pub corner_radius: Option<f64>,
pub foreground: Option<Color>,
pub background: Option<Color>,
pub border_brush: Option<Color>,
pub key: Option<KeyRef>,
}
impl ProgressBar {
pub fn new() -> Self {
Self {
minimum: 0.0,
maximum: 100.0,
value: 0.0,
is_indeterminate: false,
show_paused: false,
show_error: false,
is_visible: true,
min_height: None,
padding: [0.0; 4],
border_thickness: None,
corner_radius: None,
foreground: None,
background: None,
border_brush: None,
key: None,
}
}
pub fn minimum(mut self, value: f64) -> Self {
self.minimum = value;
self
}
pub fn maximum(mut self, value: f64) -> Self {
self.maximum = value;
self
}
pub fn value(mut self, value: f64) -> Self {
self.value = value;
self
}
pub fn is_indeterminate(mut self, value: bool) -> Self {
self.is_indeterminate = value;
self
}
pub fn show_paused(mut self, value: bool) -> Self {
self.show_paused = value;
self
}
pub fn show_error(mut self, value: bool) -> Self {
self.show_error = value;
self
}
pub fn is_visible(mut self, value: bool) -> Self {
self.is_visible = value;
self
}
pub fn min_height(mut self, value: f64) -> Self {
self.min_height = Some(value);
self
}
pub fn padding(mut self, value: [f64; 4]) -> Self {
self.padding = value;
self
}
pub fn border_thickness(mut self, value: [f64; 4]) -> Self {
self.border_thickness = Some(value);
self
}
pub fn corner_radius(mut self, value: f64) -> Self {
self.corner_radius = Some(value);
self
}
pub fn foreground(mut self, value: Color) -> Self {
self.foreground = Some(value);
self
}
pub fn background(mut self, value: Color) -> Self {
self.background = Some(value);
self
}
pub fn border_brush(mut self, value: Color) -> Self {
self.border_brush = Some(value);
self
}
pub fn key(mut self, value: KeyRef) -> Self {
self.key = Some(value);
self
}
fn visual_state(&self) -> ProgressState {
match (
self.is_indeterminate && self.is_visible,
self.show_error,
self.show_paused,
) {
(true, true, _) => ProgressState::IndeterminateError,
(true, false, true) => ProgressState::IndeterminatePaused,
(true, false, false) => ProgressState::Indeterminate,
(false, true, _) => ProgressState::Error,
(false, false, true) => ProgressState::Paused,
_ => ProgressState::Determinate,
}
}
fn indicator_color(&self, resources: &ThemeResources) -> Color {
if self.show_error {
resources.progress_bar_error_foreground_color
} else if self.show_paused {
resources.progress_bar_paused_foreground_color
} else {
self.foreground.unwrap_or(resources.progress_bar_foreground)
}
}
}
impl Default for ProgressBar {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ProgressState {
Determinate,
Updating,
UpdatingError,
Paused,
Error,
Indeterminate,
IndeterminatePaused,
IndeterminateError,
}
impl ProgressState {
fn suspended(self) -> bool {
matches!(self, Self::IndeterminatePaused | Self::IndeterminateError)
}
}
#[derive(Clone, Copy, Debug)]
struct ProgressVisuals {
track_opacity: f64,
track_translation: f64,
first: Option<f64>,
second: Option<f64>,
}
fn progress_visuals(
state: ProgressState,
from: ProgressState,
elapsed: f64,
initial_second: f64,
) -> ProgressVisuals {
let mut result = ProgressVisuals {
track_opacity: 1.0,
track_translation: 0.0,
first: None,
second: None,
};
if state == ProgressState::Indeterminate {
if from.suspended() && elapsed < 0.5 {
let curve = Cubic::new(1.0, 0.0, 1.0, 1.0);
result.second = Some(
initial_second
+ (1.2 - initial_second) * curve.transform((elapsed / 0.333).min(1.0)),
);
result.track_opacity = 0.0;
result.track_translation = 1.2 * curve.transform((elapsed / 0.5).min(1.0));
return result;
}
let time = (elapsed - if from.suspended() { 0.5 } else { 0.0 }).max(0.0) % 2.0;
let curve = Cubic::new(0.4, 0.0, 0.6, 1.0);
result.track_opacity = 0.0;
result.first = Some(-0.4 + 1.6 * curve.transform((time / 1.5).min(1.0)));
result.second =
Some(-0.9 + 1.896 * curve.transform(((time - 0.75) / 1.25).clamp(0.0, 1.0)));
} else if state.suspended() {
result.track_opacity = 0.0;
result.second = Some(if elapsed < 0.167 {
initial_second
+ (0.996 - initial_second)
* Cubic::new(1.0, 1.0, 0.0, 1.0).transform(elapsed / 0.167)
} else {
-0.9 + 0.9
* Cubic::new(0.0, 0.0, 0.0, 1.0).transform(((elapsed - 0.167) / 0.583).min(1.0))
});
result.track_translation =
-0.9 + 0.9 * Cubic::new(0.0, 0.0, 0.0, 1.0).transform((elapsed / 0.75).min(1.0));
}
result
}
pub struct ProgressBarState {
state: StateData<ProgressBar>,
single_ticker_provider: SingleTickerProviderStateMixinData,
ticker: Option<Ref<Ticker>>,
from: ProgressState,
elapsed: Duration,
initial_second: f64,
from_color: Option<Color>,
layout_size: Option<Size>,
}
impl StatefulWidget for ProgressBar {
type State = ProgressBarState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> ProgressBarState {
ProgressBarState {
state: StateData::new(),
single_ticker_provider: Default::default(),
ticker: None,
from: self.visual_state(),
elapsed: Duration::ZERO,
initial_second: 0.0,
from_color: None,
layout_size: None,
}
}
}
impl SingleTickerProviderStateMixin for ProgressBarState {
fn single_ticker_provider_data(
self: Handle<Self>,
app: &App,
) -> &SingleTickerProviderStateMixinData {
&app.get(self).single_ticker_provider
}
fn single_ticker_provider_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut SingleTickerProviderStateMixinData {
&mut app.get_mut(self).single_ticker_provider
}
}
impl ProgressBarState {
fn update_indicator_width(self: Handle<Self>, app: &mut App) {
let resources = ThemeResources::of(app, self.context(app));
let widget = self.widget(app);
let from = if widget.show_error {
ProgressState::UpdatingError
} else {
ProgressState::Updating
};
let color = if widget.show_error && !widget.is_indeterminate {
resources.progress_bar_error_foreground_color
} else {
widget
.foreground
.unwrap_or(resources.progress_bar_foreground)
};
self.start_state(app, from, color, 0.0);
}
fn start_state(
self: Handle<Self>,
app: &mut App,
from: ProgressState,
color: Color,
second: f64,
) {
let ticker = app.get(self).ticker.clone().unwrap();
ticker.stop(app, false);
let state = app.get_mut(self);
state.from = from;
state.from_color = Some(color);
state.initial_second = second;
state.elapsed = Duration::ZERO;
if self.widget(app).is_visible {
ticker.start(app);
}
}
fn tick(self: Handle<Self>, app: &mut App, elapsed: Duration) {
self.set_state(app, |state| state.elapsed = elapsed);
let target = self.widget(app).visual_state();
if target != ProgressState::Indeterminate && elapsed >= Duration::from_millis(750) {
app.get(self).ticker.handle().unwrap().stop(app, false);
}
}
fn color(
self: Handle<Self>,
app: &App,
widget: &ProgressBar,
resources: &ThemeResources,
) -> Color {
let target = widget.indicator_color(resources);
let state = app.get(self);
Color::lerp(
state.from_color,
Some(target),
(state.elapsed.as_secs_f64() / 0.167).min(1.0),
)
.unwrap_or(target)
}
}
impl State for ProgressBarState {
type Widget = ProgressBar;
waft_widgets::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
let ticker = SingleTickerProviderStateMixin::create_ticker(
self,
app,
FrameCallback::new(move |app, elapsed| self.tick(app, elapsed)),
);
app.get_mut(self).ticker = Some(ticker.clone());
if self.widget(app).is_indeterminate && self.widget(app).is_visible {
ticker.start(app);
}
}
fn did_update_widget(self: Handle<Self>, app: &mut App, old: &ProgressBar) {
let widget = self.widget(app);
let width_component_changed = old.value != widget.value
|| old.minimum != widget.minimum
|| old.maximum != widget.maximum
|| old.padding != widget.padding
|| old.is_indeterminate != widget.is_indeterminate
|| old.show_paused != widget.show_paused
|| old.show_error != widget.show_error;
if width_component_changed {
self.update_indicator_width(app);
} else if old.visual_state() != widget.visual_state() {
let resources = ThemeResources::of(app, self.context(app));
let color = self.color(app, old, &resources);
let previous = progress_visuals(
old.visual_state(),
app.get(self).from,
app.get(self).elapsed.as_secs_f64(),
app.get(self).initial_second,
);
self.start_state(
app,
old.visual_state(),
color,
previous.second.unwrap_or(0.0),
);
}
}
fn activate(self: Handle<Self>, app: &mut App) {
SingleTickerProviderStateMixin::activate(self, app);
}
fn dispose(self: Handle<Self>, app: &mut App) {
app.get(self).ticker.handle().unwrap().dispose(app);
SingleTickerProviderStateMixin::dispose(self, app);
}
fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
let widget = self.widget(app).clone();
let resources = ThemeResources::of(app, context);
let state = widget.visual_state();
let scale = MediaQuery::device_pixel_ratio_of(app, context);
let border = widget
.border_thickness
.unwrap_or(resources.progress_bar_border_theme_thickness)
.map(|side| (side * scale).round() / scale);
let range = RangeBase::new(widget.minimum, widget.maximum, widget.value).coerced();
let body = LayoutBuilder::new(move |app, _, constraints| {
let width = if constraints.max_width.is_finite() {
constraints.max_width
} else {
constraints.min_width
};
let inset = widget.padding[0] + widget.padding[2] + border[0] + border[2];
let available = (width - inset).max(0.0);
let height = widget
.min_height
.unwrap_or(resources.progress_bar_min_height);
let root_size = constraints.constrain(Size::new(
width,
height + widget.padding[1] + widget.padding[3] + border[1] + border[3],
));
if app.get(self).layout_size != Some(root_size) {
app.get_mut(self).layout_size = Some(root_size);
self.update_indicator_width(app);
}
let foreground = self.color(app, &widget, &resources);
let visuals = progress_visuals(
state,
app.get(self).from,
app.get(self).elapsed.as_secs_f64(),
app.get(self).initial_second,
);
let mut parts = Vec::new();
if visuals.track_opacity > 0.0 {
parts.push(
Positioned::new(progress_rectangle(
available,
resources.progress_bar_track_height,
resources.progress_bar_track_corner_radius[0],
widget
.background
.unwrap_or(resources.progress_bar_background),
))
.left(visuals.track_translation * width)
.top((height - resources.progress_bar_track_height) / 2.0)
.into_widget(),
);
}
if !widget.is_indeterminate {
parts.push(
Positioned::new(progress_rectangle(
if (range.maximum - range.minimum).abs() > f64::EPSILON {
available * range.fraction_of(range.value)
} else {
0.0
},
height,
widget
.corner_radius
.unwrap_or(resources.progress_bar_corner_radius[0]),
foreground,
))
.left(0.0)
.top(0.0)
.into_widget(),
);
}
if let Some(position) = visuals.first {
parts.push(
Positioned::new(progress_rectangle(
available * 0.4,
height,
widget
.corner_radius
.unwrap_or(resources.progress_bar_corner_radius[0]),
foreground,
))
.left(position * width)
.top(0.0)
.into_widget(),
);
}
if let Some(position) = visuals.second {
parts.push(
Positioned::new(progress_rectangle(
available * if state.suspended() { 1.0 } else { 0.6 },
height,
widget
.corner_radius
.unwrap_or(resources.progress_bar_corner_radius[0]),
foreground,
))
.left(position * width)
.top(0.0)
.into_widget(),
);
}
SizedBox::new()
.width(width)
.child(
ControlBorder::new(
Brush::Solid(Color::from_argb(0, 0, 0, 0)),
Brush::Solid(
widget
.border_brush
.unwrap_or(resources.progress_bar_border_brush),
),
)
.border_thickness_ltrb(border)
.corner_radius(
widget
.corner_radius
.unwrap_or(resources.progress_bar_corner_radius[0]),
)
.child(
Padding::new(EdgeInsetsGeometry::from_ltrb(
widget.padding[0],
widget.padding[1],
widget.padding[2],
widget.padding[3],
))
.child(
ClipRect::new()
.clipper(ProgressClipper(Rect::from_ltwh(
widget.padding[0],
widget.padding[1],
(width - widget.padding[0] - widget.padding[2]).max(0.0),
(root_size.height() - widget.padding[1] - widget.padding[3])
.max(0.0),
)))
.child(
Align::new().height_factor(1.0).child(
SizedBox::new().height(height).child(
Stack::new()
.clip_behavior(waft_embedder::Clip::None)
.children(parts),
),
),
),
),
),
)
.into_widget()
});
Offstage::new()
.child(body.into_widget())
.offstage(!self.widget(app).is_visible)
.into_widget()
}
}
fn progress_rectangle(width: f64, height: f64, radius: f64, color: Color) -> WidgetRef {
SizedBox::new()
.width(width)
.height(height)
.child(
ControlBorder::new(
Brush::Solid(color),
Brush::Solid(Color::from_argb(0, 0, 0, 0)),
)
.border_thickness(0.0)
.corner_radius(radius),
)
.into_widget()
}
#[derive(Debug)]
struct ProgressClipper(Rect);
impl CustomClipper<Rect> for ProgressClipper {
fn get_clip(&self, _app: &App, _: Size) -> Rect {
self.0
}
fn should_reclip(&self, _app: &App, old: &dyn CustomClipper<Rect>) -> bool {
old.as_any()
.downcast_ref::<Self>()
.is_none_or(|old| old.0 != self.0)
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_precedes_pause_and_hidden_indeterminate_uses_determinate_states() {
let bar = ProgressBar::new()
.is_indeterminate(true)
.show_paused(true)
.show_error(true);
assert_eq!(bar.visual_state(), ProgressState::IndeterminateError);
assert_eq!(bar.is_visible(false).visual_state(), ProgressState::Error);
}
#[test]
fn indeterminate_segments_follow_the_staggered_two_second_loop() {
let sample = |time| {
progress_visuals(
ProgressState::Indeterminate,
ProgressState::Determinate,
time,
0.0,
)
};
assert_eq!(sample(0.0).first, Some(-0.4));
assert_eq!(sample(0.75).second, Some(-0.9));
assert!((sample(1.5).first.unwrap() - 1.2).abs() < 1e-12);
assert_eq!(sample(2.0).first, Some(-0.4));
assert_eq!(sample(2.0).second, Some(-0.9));
}
#[test]
fn suspension_fills_the_track_and_resume_finishes_before_the_loop() {
let pause = progress_visuals(
ProgressState::IndeterminatePaused,
ProgressState::Indeterminate,
0.75,
0.2,
);
assert_eq!(pause.first, None);
assert_eq!(pause.second, Some(0.0));
let resume = |time| {
progress_visuals(
ProgressState::Indeterminate,
ProgressState::IndeterminatePaused,
time,
0.0,
)
};
assert_eq!(resume(0.333).first, None);
assert!((resume(0.333).second.unwrap() - 1.2).abs() < 1e-12);
assert_eq!(resume(0.5).first, Some(-0.4));
}
}