mitsuami_widgets/
progress.rs1use std::rc::Rc;
4
5use mitsuami_core::{Element, ElementBuilder, NodeId, Prop, Tweak, Ui, View, WidgetKind};
6use mitsuami_reactive::{IntoValue, Value};
7
8pub 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 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 pub fn label(mut self, label: impl IntoValue<String>) -> Progress {
53 self.element.prop(label.into_value(), Prop::Label);
54 self
55 }
56
57 pub fn value(mut self, value: impl IntoValue<f64>) -> Progress {
59 self.value = Some(value.into_value());
60 self
61 }
62
63 pub fn indeterminate(mut self, indeterminate: impl IntoValue<bool>) -> Progress {
65 self.indeterminate = indeterminate.into_value();
66 self
67 }
68
69 pub fn native(mut self, tweak: Tweak<Progress>) -> Progress {
73 tweak.apply(&mut self.element);
74 self
75 }
76}
77
78pub 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 pub fn label(mut self, label: impl IntoValue<String>) -> Spinner {
115 self.element.prop(label.into_value(), Prop::Label);
116 self
117 }
118
119 pub fn running(mut self, running: impl IntoValue<bool>) -> Spinner {
121 self.running = running.into_value();
122 self
123 }
124
125 pub fn native(mut self, tweak: Tweak<Spinner>) -> Spinner {
127 tweak.apply(&mut self.element);
128 self
129 }
130}
131
132impl Spinner {
133 #[doc(hidden)]
135 pub fn __tag() -> Spinner {
136 Spinner { element: Element::new(WidgetKind::Spinner), running: Value::Static(true) }
137 }
138}
139
140impl Progress {
141 #[doc(hidden)]
143 pub fn __tag() -> Progress {
144 Progress { element: Element::new(WidgetKind::Progress), value: None, indeterminate: Value::Static(false) }
145 }
146}