use std::time::Duration;
use arc_motion::{arc_spring, arc_transition, is_reduced_motion};
use arc_tokens::{ActiveArcTheme as _, Radii, Typography, oklab_mix};
use gpui_kit::{
App, ElementId, Hsla, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
TestSupportExt as _, Window, assets::IconName, component::Icon, div, prelude::*, px, relative,
};
pub fn progress_percentage(value: f32, max: f32) -> f32 {
let max = if max > 0.0 { max } else { 100.0 };
((value.clamp(0.0, max) / max) * 100.0).round()
}
#[derive(IntoElement)]
pub struct Progress {
id: ElementId,
value: f32,
max: f32,
label: Option<SharedString>,
show_value: bool,
}
impl Progress {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
value: 0.0,
max: 100.0,
label: None,
show_value: false,
}
}
pub fn value(mut self, value: f32) -> Self {
self.value = value;
self
}
pub fn max(mut self, max: f32) -> Self {
self.max = max;
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn show_value(mut self, show: bool) -> Self {
self.show_value = show;
self
}
}
impl RenderOnce for Progress {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.arc_theme().clone();
let colors = theme.colors;
let motion = theme.motion;
let reduced = is_reduced_motion(cx);
let percentage = progress_percentage(self.value, self.max);
let complete = percentage >= 100.0;
let shown = if reduced {
percentage
} else {
arc_spring(
(self.id.clone(), "progress-fill"),
percentage,
motion.spring.smooth,
window,
cx,
)
}
.clamp(0.0, 100.0);
let done_mix = arc_transition(
(self.id.clone(), "progress-done"),
if complete { 1.0 } else { 0.0 },
if reduced {
Duration::ZERO
} else {
motion.duration.standard
},
motion.ease.standard,
window,
cx,
);
let fill_color: Hsla = oklab_mix(colors.success, colors.accent, done_mix);
let frac = (shown / 100.0).clamp(0.0, 1.0);
let fill = div()
.id(ElementId::NamedChild(self.id.clone().into(), "fill".into()))
.test_support()
.h_full()
.w(relative(frac))
.min_w(px(0.0))
.rounded(Radii::PILL)
.bg(fill_color);
let track = div()
.id(ElementId::NamedChild(
self.id.clone().into(),
"track".into(),
))
.test_support()
.relative()
.w_full()
.h(px(7.0))
.overflow_hidden()
.rounded(px(99.0))
.border_1()
.border_color(colors.border)
.bg(colors.surface_muted)
.child(fill);
let has_meta = self.label.is_some() || self.show_value;
let meta = has_meta.then(|| {
div()
.w_full()
.mb(px(8.0))
.flex()
.items_center()
.justify_between()
.gap(px(12.0))
.child(div().min_w_0().children(self.label.clone()))
.when(self.show_value, |this| {
this.child(
div()
.flex_none()
.flex()
.items_center()
.gap(px(4.0))
.text_color(colors.text_muted)
.when(complete && done_mix > 0.5, |t| {
t.child(
Icon::new(IconName::Check)
.text_size(px(14.0))
.text_color(colors.success),
)
})
.child(format!("{}%", shown.round() as i32)),
)
})
});
div()
.id(self.id)
.w_full()
.text_size(Typography::TEXT_SM)
.text_color(colors.text_secondary)
.children(meta)
.child(track)
}
}
#[cfg(test)]
mod tests {
use arc_tokens::{Accent, Spacing, ThemeMode, install_arc_theme};
use gpui_kit::{Context, Render, TestAppContext, div, px, test::TestWindowExt as _};
use super::*;
struct ProgressHost {
value: f32,
}
impl Render for ProgressHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().w(px(360.0)).p(Spacing::S4).child(
Progress::new("upload")
.label("Uploading")
.show_value(true)
.value(self.value),
)
}
}
#[test]
fn percentage_is_clamped_and_rounded() {
assert_eq!(progress_percentage(-5.0, 100.0), 0.0);
assert_eq!(progress_percentage(33.4, 100.0), 33.0);
assert_eq!(progress_percentage(250.0, 100.0), 100.0);
assert_eq!(progress_percentage(5.0, 10.0), 50.0);
assert_eq!(progress_percentage(50.0, 0.0), 50.0);
}
#[gpui_kit::test]
fn progress_fill_covers_the_requested_fraction(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
});
let (_view, cx) = cx.add_window_view(|_, _| ProgressHost { value: 50.0 });
cx.update(|window, cx| {
window.render_frame(cx);
window.render_frame(cx);
let root: std::sync::Arc<ElementId> = ElementId::from("upload").into();
let track = window.find(ElementId::NamedChild(root.clone(), "track".into()));
let fill = window.find(ElementId::NamedChild(root, "fill".into()));
let track_w = f32::from(track.bounds().size.width) - 2.0;
let fill_w = f32::from(fill.bounds().size.width);
assert!(
(fill_w - track_w * 0.5).abs() < 1.5,
"fill {fill_w} should be half of track {track_w}"
);
});
}
#[gpui_kit::test]
fn progress_renders_through_completion(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
});
let (view, cx) = cx.add_window_view(|_, _| ProgressHost { value: 20.0 });
cx.update(|window, cx| {
window.render_frame(cx);
for v in [60.0, 100.0, 40.0] {
view.update(cx, |h, cx| {
h.value = v;
cx.notify();
});
window.render_frame(cx);
}
});
}
}