Skip to main content

gpui_deck/
progress.rs

1//! Controlled progress semantics with application-owned presentation.
2// Modified for GPUI Deck from GPUI Kit v0.9.0: retain the percentage root,
3// remove decorative part wrappers, and reject non-finite values.
4
5use gpui::{
6    AnyElement, App, Div, ElementId, InteractiveElement, Interactivity, IntoElement, ParentElement,
7    RenderOnce, Role, SharedString, StatefulInteractiveElement, StyleRefinement, Styled, Window,
8    div, prelude::FluentBuilder as _,
9};
10use smallvec::SmallVec;
11
12use crate::{StyledExt as _, TestSupportExt as _};
13
14/// An unstyled progress indicator with an application-controlled percentage.
15///
16/// The application supplies the track, fill, and any indeterminate animation as
17/// children. This root supplies the accessible role, name, and numeric range;
18/// it has no keyboard focus or Tab stop of its own.
19#[derive(IntoElement)]
20pub struct Progress {
21    base: crate::ObservedElement<gpui::Stateful<Div>>,
22    style: StyleRefinement,
23    value: f32,
24    indeterminate: bool,
25    accessibility_label: Option<SharedString>,
26    children: SmallVec<[AnyElement; 2]>,
27}
28
29impl Progress {
30    /// Creates an indicator with known progress of zero percent.
31    pub fn new(id: impl Into<ElementId>) -> Self {
32        Self {
33            base: div().id(id).test_support(),
34            style: StyleRefinement::default(),
35            value: 0.,
36            indeterminate: false,
37            accessibility_label: None,
38            children: SmallVec::new(),
39        }
40    }
41
42    /// Sets the controlled percentage, clamped to `0..=100`.
43    ///
44    /// # Panics
45    /// Panics if `value` is not finite.
46    pub fn value(mut self, value: f32) -> Self {
47        assert!(value.is_finite(), "Progress value must be finite");
48        self.value = value.clamp(0., 100.);
49        self
50    }
51
52    /// Omits the current numeric value while the amount of progress is unknown.
53    /// This does not choose or animate the visual presentation.
54    pub fn indeterminate(mut self, indeterminate: bool) -> Self {
55        self.indeterminate = indeterminate;
56        self
57    }
58
59    /// Names the operation whose progress is being reported.
60    pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
61        self.accessibility_label = Some(label.into());
62        self
63    }
64}
65
66impl Styled for Progress {
67    fn style(&mut self) -> &mut StyleRefinement {
68        &mut self.style
69    }
70}
71
72impl ParentElement for Progress {
73    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
74        self.children.extend(elements);
75    }
76}
77
78impl InteractiveElement for Progress {
79    fn interactivity(&mut self) -> &mut Interactivity {
80        self.base.interactivity()
81    }
82}
83
84impl StatefulInteractiveElement for Progress {}
85
86impl RenderOnce for Progress {
87    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
88        self.base
89            .role(Role::ProgressIndicator)
90            .when_some(self.accessibility_label, |this, label| {
91                this.aria_label(label)
92            })
93            .aria_min_numeric_value(0.)
94            .aria_max_numeric_value(100.)
95            .when(!self.indeterminate, |this| {
96                this.aria_numeric_value(self.value as f64)
97            })
98            .children(self.children)
99            .refine_style(&self.style)
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use gpui::{Element as _, TestAppContext, accesskit};
106
107    use super::*;
108
109    #[gpui::test]
110    fn known_and_unknown_progress_expose_the_numeric_contract(cx: &mut TestAppContext) {
111        let window = cx.add_empty_window();
112        window.update(|window, cx| {
113            for (value, indeterminate, expected) in [
114                (-10., false, Some(0.)),
115                (42.5, false, Some(42.5)),
116                (120., false, Some(100.)),
117                (42.5, true, None),
118            ] {
119                let element = Progress::new("progress")
120                    .value(value)
121                    .indeterminate(indeterminate)
122                    .accessibility_label("Preview progress")
123                    .render(window, cx)
124                    .into_element();
125                assert_eq!(element.a11y_role(), Some(Role::ProgressIndicator));
126                let mut node = accesskit::Node::new(Role::ProgressIndicator);
127                element.write_a11y_info(&mut node);
128                assert_eq!(node.label(), Some("Preview progress"));
129                assert_eq!(node.min_numeric_value(), Some(0.));
130                assert_eq!(node.max_numeric_value(), Some(100.));
131                assert_eq!(node.numeric_value(), expected);
132            }
133        });
134    }
135}