1use std::time::Duration;
12
13use arc_motion::{arc_spring, arc_transition, is_reduced_motion};
14use arc_tokens::{ActiveArcTheme as _, Radii, Typography, oklab_mix};
15use gpui_kit::{
16 App, ElementId, Hsla, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
17 TestSupportExt as _, Window, assets::IconName, component::Icon, div, prelude::*, px, relative,
18};
19
20pub fn progress_percentage(value: f32, max: f32) -> f32 {
22 let max = if max > 0.0 { max } else { 100.0 };
23 ((value.clamp(0.0, max) / max) * 100.0).round()
24}
25
26#[derive(IntoElement)]
28pub struct Progress {
29 id: ElementId,
30 value: f32,
31 max: f32,
32 label: Option<SharedString>,
33 show_value: bool,
34}
35
36impl Progress {
37 pub fn new(id: impl Into<ElementId>) -> Self {
38 Self {
39 id: id.into(),
40 value: 0.0,
41 max: 100.0,
42 label: None,
43 show_value: false,
44 }
45 }
46
47 pub fn value(mut self, value: f32) -> Self {
48 self.value = value;
49 self
50 }
51
52 pub fn max(mut self, max: f32) -> Self {
53 self.max = max;
54 self
55 }
56
57 pub fn label(mut self, label: impl Into<SharedString>) -> Self {
58 self.label = Some(label.into());
59 self
60 }
61
62 pub fn show_value(mut self, show: bool) -> Self {
63 self.show_value = show;
64 self
65 }
66}
67
68impl RenderOnce for Progress {
69 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
70 let theme = cx.arc_theme().clone();
71 let colors = theme.colors;
72 let motion = theme.motion;
73 let reduced = is_reduced_motion(cx);
74
75 let percentage = progress_percentage(self.value, self.max);
76 let complete = percentage >= 100.0;
77 let shown = if reduced {
78 percentage
79 } else {
80 arc_spring(
81 (self.id.clone(), "progress-fill"),
82 percentage,
83 motion.spring.smooth,
84 window,
85 cx,
86 )
87 }
88 .clamp(0.0, 100.0);
89
90 let done_mix = arc_transition(
91 (self.id.clone(), "progress-done"),
92 if complete { 1.0 } else { 0.0 },
93 if reduced {
94 Duration::ZERO
95 } else {
96 motion.duration.standard
97 },
98 motion.ease.standard,
99 window,
100 cx,
101 );
102 let fill_color: Hsla = oklab_mix(colors.success, colors.accent, done_mix);
103 let frac = (shown / 100.0).clamp(0.0, 1.0);
104
105 let fill = div()
109 .id(ElementId::NamedChild(self.id.clone().into(), "fill".into()))
110 .test_support()
111 .h_full()
112 .w(relative(frac))
113 .min_w(px(0.0))
114 .rounded(Radii::PILL)
115 .bg(fill_color);
116
117 let track = div()
118 .id(ElementId::NamedChild(
119 self.id.clone().into(),
120 "track".into(),
121 ))
122 .test_support()
123 .relative()
124 .w_full()
125 .h(px(7.0))
126 .overflow_hidden()
127 .rounded(px(99.0))
128 .border_1()
129 .border_color(colors.border)
130 .bg(colors.surface_muted)
131 .child(fill);
132
133 let has_meta = self.label.is_some() || self.show_value;
134 let meta = has_meta.then(|| {
135 div()
136 .w_full()
137 .mb(px(8.0))
138 .flex()
139 .items_center()
140 .justify_between()
141 .gap(px(12.0))
142 .child(div().min_w_0().children(self.label.clone()))
143 .when(self.show_value, |this| {
144 this.child(
145 div()
146 .flex_none()
147 .flex()
148 .items_center()
149 .gap(px(4.0))
150 .text_color(colors.text_muted)
151 .when(complete && done_mix > 0.5, |t| {
152 t.child(
153 Icon::new(IconName::Check)
154 .text_size(px(14.0))
155 .text_color(colors.success),
156 )
157 })
158 .child(format!("{}%", shown.round() as i32)),
159 )
160 })
161 });
162
163 div()
164 .id(self.id)
165 .w_full()
166 .text_size(Typography::TEXT_SM)
167 .text_color(colors.text_secondary)
168 .children(meta)
169 .child(track)
170 }
171}
172
173#[cfg(test)]
174mod tests {
175 use arc_tokens::{Accent, Spacing, ThemeMode, install_arc_theme};
176 use gpui_kit::{Context, Render, TestAppContext, div, px, test::TestWindowExt as _};
177
178 use super::*;
179
180 struct ProgressHost {
181 value: f32,
182 }
183
184 impl Render for ProgressHost {
185 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
186 div().w(px(360.0)).p(Spacing::S4).child(
187 Progress::new("upload")
188 .label("Uploading")
189 .show_value(true)
190 .value(self.value),
191 )
192 }
193 }
194
195 #[test]
196 fn percentage_is_clamped_and_rounded() {
197 assert_eq!(progress_percentage(-5.0, 100.0), 0.0);
198 assert_eq!(progress_percentage(33.4, 100.0), 33.0);
199 assert_eq!(progress_percentage(250.0, 100.0), 100.0);
200 assert_eq!(progress_percentage(5.0, 10.0), 50.0);
201 assert_eq!(progress_percentage(50.0, 0.0), 50.0);
202 }
203
204 #[gpui_kit::test]
205 fn progress_fill_covers_the_requested_fraction(cx: &mut TestAppContext) {
206 cx.update(|cx| {
207 gpui_kit::init(cx);
208 install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
209 });
210 let (_view, cx) = cx.add_window_view(|_, _| ProgressHost { value: 50.0 });
211 cx.update(|window, cx| {
212 window.render_frame(cx);
213 window.render_frame(cx);
214 let root: std::sync::Arc<ElementId> = ElementId::from("upload").into();
215 let track = window.find(ElementId::NamedChild(root.clone(), "track".into()));
216 let fill = window.find(ElementId::NamedChild(root, "fill".into()));
217 let track_w = f32::from(track.bounds().size.width) - 2.0;
218 let fill_w = f32::from(fill.bounds().size.width);
219 assert!(
220 (fill_w - track_w * 0.5).abs() < 1.5,
221 "fill {fill_w} should be half of track {track_w}"
222 );
223 });
224 }
225
226 #[gpui_kit::test]
227 fn progress_renders_through_completion(cx: &mut TestAppContext) {
228 cx.update(|cx| {
229 gpui_kit::init(cx);
230 install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
231 });
232 let (view, cx) = cx.add_window_view(|_, _| ProgressHost { value: 20.0 });
233 cx.update(|window, cx| {
234 window.render_frame(cx);
235 for v in [60.0, 100.0, 40.0] {
236 view.update(cx, |h, cx| {
237 h.value = v;
238 cx.notify();
239 });
240 window.render_frame(cx);
241 }
242 });
243 }
244}