gpui_component/progress/
progress.rs1use crate::{ActiveTheme, Sizable, Size, StyledExt};
2use gpui::{
3 Animation, AnimationExt as _, App, Background, ElementId, Hsla, IntoElement, IsZero as _,
4 ParentElement, RenderOnce, SharedString, StyleRefinement, Styled, Window, ease_in_out,
5 prelude::FluentBuilder, px, relative,
6};
7use gpui_base::{
8 Progress as BaseProgress, ProgressIndicator, ProgressTrack, Transition, transition,
9};
10use instant::Duration;
11
12#[derive(IntoElement)]
14pub struct Progress {
15 id: ElementId,
16 style: StyleRefinement,
17 color: Option<Hsla>,
18 value: f32,
19 accessibility_label: Option<SharedString>,
20 size: Size,
21 loading: bool,
22}
23
24impl Progress {
25 pub fn new(id: impl Into<ElementId>) -> Self {
27 Self {
28 id: id.into(),
29 value: Default::default(),
30 color: None,
31 accessibility_label: None,
32 style: StyleRefinement::default(),
33 size: Size::default(),
34 loading: false,
35 }
36 }
37
38 pub fn loading(mut self, loading: bool) -> Self {
43 self.loading = loading;
44 self
45 }
46
47 pub fn color(mut self, color: impl Into<Hsla>) -> Self {
49 self.color = Some(color.into());
50 self
51 }
52
53 pub fn value(mut self, value: f32) -> Self {
57 self.value = value.clamp(0., 100.);
58 self
59 }
60
61 pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
63 self.accessibility_label = Some(label.into());
64 self
65 }
66}
67
68impl Styled for Progress {
69 fn style(&mut self) -> &mut StyleRefinement {
70 &mut self.style
71 }
72}
73
74impl Sizable for Progress {
75 fn with_size(mut self, size: impl Into<Size>) -> Self {
76 self.size = size.into();
77 self
78 }
79}
80
81impl RenderOnce for Progress {
82 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
83 let bg = self
84 .color
85 .map(Background::from)
86 .unwrap_or(cx.theme().tokens.progress_bar.into());
87 let value = self.value;
88 let loading = self.loading;
89 let accessibility_label = self.accessibility_label;
90 let reduce_motion = cx.reduce_motion();
91
92 let radius = self.style.corner_radii.clone();
93 let mut inner_style = StyleRefinement::default();
94 inner_style.corner_radii = radius;
95
96 let (height, pill_radius) = match self.size {
97 Size::XSmall => (px(4.), px(2.)),
98 Size::Small => (px(6.), px(3.)),
99 Size::Medium => (px(8.), px(4.)),
100 Size::Large => (px(10.), px(5.)),
101 Size::Size(s) => (s, s / 2.),
102 };
103 let radius = if cx.theme().radius.is_zero() {
106 px(0.)
107 } else {
108 pill_radius
109 };
110
111 let animated_value = transition(
112 (self.id.clone(), "indicator"),
113 value,
114 Transition::new(Duration::from_millis(180)),
115 window,
116 cx,
117 );
118
119 BaseProgress::new(self.id)
120 .value(value)
121 .indeterminate(loading)
122 .when_some(accessibility_label, |this, label| {
123 this.accessibility_label(label)
124 })
125 .w_full()
126 .relative()
127 .h(height)
128 .rounded(radius)
129 .refine_style(&self.style)
130 .child(
131 ProgressTrack::new()
132 .absolute()
133 .size_full()
134 .bg(bg.opacity(0.2))
135 .rounded(radius)
136 .refine_style(&inner_style),
137 )
138 .child(
139 ProgressIndicator::new()
140 .absolute()
141 .top_0()
142 .left_0()
143 .h_full()
144 .bg(bg)
145 .rounded(radius)
146 .refine_style(&inner_style)
147 .map(|this| {
148 if loading && !reduce_motion {
149 this.with_animation(
150 "progress-loading",
151 Animation::new(Duration::from_secs(1)).repeat(),
152 move |this, delta| {
153 let start =
154 relative(ease_in_out(((delta - 0.5) / 0.5).clamp(0., 1.)));
155 let end = relative(ease_in_out(1.0 - delta));
156 this.when(delta > 0.5, |this| this.left(start)).right(end)
157 },
158 )
159 .into_any_element()
160 } else if loading {
161 this.left(relative(0.325))
162 .right(relative(0.325))
163 .into_any_element()
164 } else {
165 this.w(relative((animated_value / 100.).clamp(0., 1.)))
166 .into_any_element()
167 }
168 }),
169 )
170 }
171}
172
173#[cfg(test)]
174mod tests {
175 use super::*;
176
177 #[test]
178 fn stores_an_explicit_accessibility_label() {
179 let plain = Progress::new("upload");
180 assert_eq!(plain.accessibility_label, None);
181
182 let named = Progress::new("upload").accessibility_label("Upload progress");
183 assert_eq!(
184 named.accessibility_label.as_deref(),
185 Some("Upload progress")
186 );
187 }
188}