bhoo/systems/material/
loading.rs1use crate::*;
5use crate::shapes::loading_shapes as shapes;
6
7#[derive(Clone, Debug, Default)]
8pub struct LoadingState {
9 elapsed: f64,
10 last: Option<f64>,
11 generation: u64,
12}
13impl LoadingState {
14 pub fn pause(&mut self) {
16 self.last = None;
17 }
18 pub fn reset(&mut self) {
19 *self = Self::default();
20 }
21 fn advance(&mut self, time: f64) {
22 if let Some(last) = self.last {
23 self.elapsed += (time - last).max(0.0);
24 }
25 self.last = Some(time);
26 }
27}
28#[derive(Clone, Copy, Debug, PartialEq)]
29pub struct LoadingSample {
30 pub shape: u8,
31 pub progress: f32,
32 pub rotation: f32,
33}
34pub fn loading_sample(elapsed: f64) -> LoadingSample {
37 let elapsed = if elapsed.is_finite() {
38 elapsed.max(0.0)
39 } else {
40 0.0
41 };
42 let cycle = (elapsed / 0.650).floor() as u64;
43 let t = elapsed % 0.650;
44 let (shape, progress, quarter) = if t >= shapes::MORPH_SECONDS {
45 (((cycle + 1) % 7) as u8, 0.0, cycle + 2)
46 } else {
47 ((cycle % 7) as u8, spring_progress(t), cycle + 1)
48 };
49 let rotation =
50 (quarter % 4) as f32 * 90.0 + progress * 90.0 + ((elapsed % 4.666) / 4.666 * 360.0) as f32;
51 LoadingSample {
52 shape,
53 progress,
54 rotation,
55 }
56}
57fn spring_progress(time: f64) -> f32 {
58 let omega = 200.0_f64.sqrt();
59 let decay = 0.6 * omega;
60 let w = omega * 0.8;
61 (1.0 - (-decay * time).exp() * ((w * time).cos() + 0.75 * (w * time).sin())) as f32
62}
63fn lerp(pair: &[[f32; 8]; 2], p: f32) -> [f32; 8] {
64 std::array::from_fn(|i| pair[0][i] + (pair[1][i] - pair[0][i]) * p)
65}
66fn at(c: &[f32; 8], t: f32, axis: usize) -> f32 {
67 let u = 1.0 - t;
68 u * u * u * c[axis]
69 + 3.0 * u * u * t * c[axis + 2]
70 + 3.0 * u * t * t * c[axis + 4]
71 + t * t * t * c[axis + 6]
72}
73fn extrema(c: &[f32; 8], axis: usize) -> [f32; 2] {
75 let mut min = c[axis].min(c[axis + 6]);
76 let mut max = c[axis].max(c[axis + 6]);
77 let (p0, p1, p2, p3) = (c[axis], c[axis + 2], c[axis + 4], c[axis + 6]);
78 let a = -p0 + 3.0 * p1 - 3.0 * p2 + p3;
79 let b = 2.0 * (p0 - 2.0 * p1 + p2);
80 let d = p1 - p0;
81 let roots = if a.abs() < 1e-7 {
82 if b.abs() > 1e-7 {
83 [-d / b, -1.0]
84 } else {
85 [-1.0; 2]
86 }
87 } else {
88 let disc = b * b - 4.0 * a * d;
89 if disc >= 0.0 {
90 [
91 (-b + disc.sqrt()) / (2.0 * a),
92 (-b - disc.sqrt()) / (2.0 * a),
93 ]
94 } else {
95 [-1.0; 2]
96 }
97 };
98 for t in roots {
99 if t > 0.0 && t < 1.0 {
100 let v = at(c, t, axis);
101 min = min.min(v);
102 max = max.max(v);
103 }
104 }
105 [min, max]
106}
107pub fn loading_curve_count(shape: u8) -> usize {
108 shapes::SHAPES[shape.min(7) as usize].len()
109}
110pub fn loading_cubics(bounds: Rect, sample: LoadingSample, mut emit: impl FnMut([f32; 8])) {
112 let pairs = shapes::SHAPES[sample.shape.min(7) as usize];
113 let (mut min, mut max) = ([f32::INFINITY; 2], [f32::NEG_INFINITY; 2]);
114 for pair in pairs {
115 let c = lerp(pair, sample.progress);
116 for axis in 0..2 {
117 let e = extrema(&c, axis);
118 min[axis] = min[axis].min(e[0]);
119 max[axis] = max[axis].max(e[1]);
120 }
121 }
122 let center = [(min[0] + max[0]) * 0.5, (min[1] + max[1]) * 0.5];
123 let scale = bounds.width.min(bounds.height) * shapes::SCALE * 38.0 / 48.0;
124 let angle = sample.rotation.to_radians();
125 let (sin, cos) = angle.sin_cos();
126 for pair in pairs {
127 let c = lerp(pair, sample.progress);
128 let out = std::array::from_fn(|i| {
129 let j = i / 2 * 2;
130 let x = (c[j] - center[0]) * scale;
131 let y = (c[j + 1] - center[1]) * scale;
132 if i % 2 == 0 {
133 bounds.x + bounds.width * 0.5 + x * cos - y * sin
134 } else {
135 bounds.y + bounds.height * 0.5 + x * sin + y * cos
136 }
137 });
138 emit(out);
139 }
140}
141impl Frame<'_> {
142 pub fn loading_indicator(
143 &mut self,
144 id: u64,
145 label: &str,
146 state: &mut LoadingState,
147 progress: Option<f32>,
148 contained: bool,
149 ) {
150 if state.generation != self.ui.pause_generation {
151 state.pause();
152 state.generation = self.ui.pause_generation;
153 }
154 let mut b = self.allocate(48.0);
155 b.width = b.width.min(48.0);
156 let visible = b.intersect(self.clip);
157 if visible.width <= 0.0 || visible.height <= 0.0 {
158 state.pause();
159 return;
160 }
161 let value = progress
162 .filter(|p| p.is_finite())
163 .map(|p| p.clamp(0.0, 1.0));
164 let sample = if let Some(p) = value {
165 let p = self.animate(id, 30, p, p);
166 LoadingSample {
167 shape: 7,
168 progress: p,
169 rotation: p * 180.0,
170 }
171 } else if self.ui.motion_preference == MotionPreference::Reduced {
172 state.pause();
173 LoadingSample {
174 shape: 0,
175 progress: 0.0,
176 rotation: 90.0,
177 }
178 } else {
179 state.advance(self.ui.time);
180 self.ui.continuous = true;
181 loading_sample(state.elapsed)
182 };
183 let t = self.ui.theme;
184 if contained {
185 self.rect(b, 24.0, t.primary_container);
186 }
187 self.commands.push(DrawCommand::Loading {
188 bounds: b,
189 sample,
190 color: if contained {
191 t.on_primary_container
192 } else {
193 t.primary
194 },
195 });
196 self.semantic(
197 Response {
198 id,
199 bounds: b,
200 enabled: self.enabled,
201 ..Response::default()
202 },
203 Role::Progress,
204 label,
205 false,
206 None,
207 value,
208 value.map(|_| [0.0, 1.0]),
209 );
210 }
211}
212#[cfg(test)]
213#[path = "loading.spec.rs"]
214mod tests;