Skip to main content

mitsuami_widgets/
progress.rs

1//! Progress bars and spinners.
2
3use std::rc::Rc;
4
5use mitsuami_core::{Element, ElementBuilder, NodeId, Prop, Tweak, Ui, View, WidgetKind};
6use mitsuami_reactive::{IntoValue, Value};
7
8/// A progress bar, as the platform draws one: how far along a task is, from
9/// 0 to 1, or, until a value is given (or while `indeterminate`), an
10/// animated bar for work of unknown length. Its label is its accessible
11/// name.
12///
13/// ```ignore
14/// Progress::new("Upload").value(move || sent.get() / total)
15/// ```
16pub struct Progress {
17    element: Element,
18    value: Option<Value<f64>>,
19    indeterminate: Value<bool>,
20}
21
22impl ElementBuilder for Progress {
23    fn element(&mut self) -> &mut Element {
24        &mut self.element
25    }
26}
27
28impl View for Progress {
29    fn build(mut self, ui: &Ui) -> NodeId {
30        // A fraction that isn't a number (`sent / total` with nothing to
31        // send) is no progress yet: native progress bars don't take it.
32        let fraction = |v: f64| if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) };
33        let progress = match (self.value, self.indeterminate) {
34            (None, _) => Value::Static(None),
35            (Some(Value::Static(v)), Value::Static(unknown)) => Value::Static((!unknown).then_some(fraction(v))),
36            (Some(value), unknown) => Value::Dynamic(Rc::new(move || (!unknown.get()).then(|| fraction(value.get())))),
37        };
38        self.element.prop(progress, Prop::Progress);
39        self.element.build(ui)
40    }
41}
42
43impl Progress {
44    pub fn new(label: impl IntoValue<String>) -> Progress {
45        let mut element = Element::new(WidgetKind::Progress);
46        element.prop(label.into_value(), Prop::Label);
47        Progress { element, value: None, indeterminate: Value::Static(false) }
48    }
49
50    /// Its label: the accessible name, which `new` takes. In `view!`,
51    /// `<Progress label="…"/>`.
52    pub fn label(mut self, label: impl IntoValue<String>) -> Progress {
53        self.element.prop(label.into_value(), Prop::Label);
54        self
55    }
56
57    /// How far along, from 0 to 1.
58    pub fn value(mut self, value: impl IntoValue<f64>) -> Progress {
59        self.value = Some(value.into_value());
60        self
61    }
62
63    /// Shows work of unknown length instead of the value while true.
64    pub fn indeterminate(mut self, indeterminate: impl IntoValue<bool>) -> Progress {
65        self.indeterminate = indeterminate.into_value();
66        self
67    }
68
69    /// Raw platform settings: see [`Tweak`]. Progress bars have no semantic
70    /// options past the value: what the platforms offer (sizes on AppKit,
71    /// text on GTK, paused and error states on WinUI) is each one's own.
72    pub fn native(mut self, tweak: Tweak<Progress>) -> Progress {
73        tweak.apply(&mut self.element);
74        self
75    }
76}
77
78/// A spinner, as the platform draws one, for work of unknown length: a
79/// spinning `NSProgressIndicator`, `gtk::Spinner`, `ProgressRing`,
80/// `QQC2.BusyIndicator`. It spins while running (from the start, unless
81/// told otherwise), and shows nothing while stopped, keeping its place. Its
82/// label is its accessible name.
83///
84/// ```ignore
85/// Spinner::new("Loading").running(move || loading.get())
86/// ```
87pub struct Spinner {
88    element: Element,
89    running: Value<bool>,
90}
91
92impl ElementBuilder for Spinner {
93    fn element(&mut self) -> &mut Element {
94        &mut self.element
95    }
96}
97
98impl View for Spinner {
99    fn build(mut self, ui: &Ui) -> NodeId {
100        self.element.prop(self.running, Prop::Running);
101        self.element.build(ui)
102    }
103}
104
105impl Spinner {
106    pub fn new(label: impl IntoValue<String>) -> Spinner {
107        let mut element = Element::new(WidgetKind::Spinner);
108        element.prop(label.into_value(), Prop::Label);
109        Spinner { element, running: Value::Static(true) }
110    }
111
112    /// Its label: the accessible name, which `new` takes. In `view!`,
113    /// `<Spinner label="…"/>`.
114    pub fn label(mut self, label: impl IntoValue<String>) -> Spinner {
115        self.element.prop(label.into_value(), Prop::Label);
116        self
117    }
118
119    /// Spins while true; shows nothing while false.
120    pub fn running(mut self, running: impl IntoValue<bool>) -> Spinner {
121        self.running = running.into_value();
122        self
123    }
124
125    /// Raw platform settings: see [`Tweak`].
126    pub fn native(mut self, tweak: Tweak<Spinner>) -> Spinner {
127        tweak.apply(&mut self.element);
128        self
129    }
130}
131
132impl Spinner {
133    /// `<Spinner a11y_label="Loading" running=loading/>`
134    #[doc(hidden)]
135    pub fn __tag() -> Spinner {
136        Spinner { element: Element::new(WidgetKind::Spinner), running: Value::Static(true) }
137    }
138}
139
140impl Progress {
141    /// `<Progress a11y_label="Upload" value=done/>`
142    #[doc(hidden)]
143    pub fn __tag() -> Progress {
144        Progress { element: Element::new(WidgetKind::Progress), value: None, indeterminate: Value::Static(false) }
145    }
146}