buoyant 0.6.1

SwiftUI-like UIs in Rust for embedded devices
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use core::time::Duration;

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Animation {
    pub duration: Duration,
    pub curve: Curve,
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Curve {
    Linear,
    /// Quadratic ease in
    EaseIn,
    /// Quadratic ease out
    EaseOut,
    /// Quadratic ease in and out
    EaseInOut,
    /// Cubic ease in
    EaseInCubic,
    /// Cubic ease out
    EaseOutCubic,
    /// Ease out with a bounce effect
    EaseOutBounce,
}

impl Animation {
    /// Constructs a new animation with a linear curve.
    #[must_use]
    pub const fn linear(duration: Duration) -> Self {
        Self {
            duration,
            curve: Curve::Linear,
        }
    }

    /// Constructs a new animation with a quadratic ease-in curve.
    ///
    /// The animation will start slow and speed up.
    #[must_use]
    pub const fn ease_in(duration: Duration) -> Self {
        Self {
            duration,
            curve: Curve::EaseIn,
        }
    }

    /// Constructs a new animation with a quadratic ease-out curve.
    ///
    /// The animation will start fast and slow down.
    #[must_use]
    pub const fn ease_out(duration: Duration) -> Self {
        Self {
            duration,
            curve: Curve::EaseOut,
        }
    }

    /// Constructs a new animation with a quadratic ease-in and ease-out curve.
    ///
    /// The animation will begin and end slowly, with a fast middle section.
    #[must_use]
    pub const fn ease_in_out(duration: Duration) -> Self {
        Self {
            duration,
            curve: Curve::EaseInOut,
        }
    }

    /// Constructs a new animation with a cubic ease-in curve.
    ///
    /// The animation will start slow and speed up.
    #[must_use]
    pub const fn ease_in_cubic(duration: Duration) -> Self {
        Self {
            duration,
            curve: Curve::EaseInCubic,
        }
    }

    /// Constructs a new animation with a cubic ease-out curve.
    ///
    /// The animation will start fast and slow down.
    #[must_use]
    pub const fn ease_out_cubic(duration: Duration) -> Self {
        Self {
            duration,
            curve: Curve::EaseOutCubic,
        }
    }
    /// Constructs a new animation with a bouncy ease-out curve.
    ///
    /// The animation will bounce at the end, staying within the bounds of the start and end points.
    #[must_use]
    pub const fn ease_out_bounce(duration: Duration) -> Self {
        Self {
            duration,
            curve: Curve::EaseOutBounce,
        }
    }

    #[must_use]
    pub const fn with_duration(self, duration: Duration) -> Self {
        Self { duration, ..self }
    }
}

impl Curve {
    /// Computes the animation factor for a given time offset.
    ///
    /// This calculation is expected to occur only once per animation node
    /// in the view rendering, and thus has considerable compute headroom
    #[must_use]
    pub fn factor(&self, time: Duration, duration: Duration) -> u8 {
        match self {
            Self::Linear => (time.as_millis() * 255)
                .checked_div(duration.as_millis())
                .unwrap_or(255)
                .min(255) as u8,
            Self::EaseIn => {
                let x = (time.as_millis() * 256)
                    .checked_div(duration.as_millis())
                    .unwrap_or(255) as u64;
                let x_2 = (x * x) >> 8;
                x_2.min(255) as u8
            }
            Self::EaseOut => {
                let duration_ms = duration.as_millis();
                let x = (duration_ms.saturating_sub(time.as_millis()) * 256)
                    .checked_div(duration_ms)
                    .unwrap_or(255) as u64;
                let x_2 = (x * x) >> 8;
                255u8 - x_2.min(255) as u8
            }
            Self::EaseInOut => {
                let x = (time.as_millis() * 256)
                    .checked_div(duration.as_millis())
                    .unwrap_or(255) as i64;
                if x < 128 {
                    ((x * x) >> 7).min(255).try_into().unwrap_or(255)
                } else {
                    (255 - (((256 - x) * (256 - x)) >> 7))
                        .min(255)
                        .try_into()
                        .unwrap_or(255)
                }
            }
            Self::EaseInCubic => {
                let x = (time.as_millis() * 256)
                    .checked_div(duration.as_millis())
                    .unwrap_or(255) as u64;
                let x_3 = (((x * x) >> 8) * x) >> 8;
                x_3.min(255) as u8
            }
            Self::EaseOutCubic => {
                let duration_ms = duration.as_millis();
                let x = (duration_ms.saturating_sub(time.as_millis()) * 256)
                    .checked_div(duration_ms)
                    .unwrap_or(255) as u64;
                let x_2 = (((x * x) >> 8) * x) >> 8;
                255u8 - x_2.min(255) as u8
            }
            Self::EaseOutBounce => {
                // Use 1024-scale (4x) for better precision
                let x = (time.as_millis() * 1024)
                    .checked_div(duration.as_millis())
                    .unwrap_or(1024)
                    .min(1024) as i32;

                // n1 = 7.5625, d1 = 2.75
                // Boundaries scaled to 1024
                // 1/d1 = 1/2.75 = 0.364 -> 372
                // 2/d1 = 2/2.75 = 0.727 -> 745
                // 2.5/d1 = 2.5/2.75 = 0.909 -> 931

                let result = if x < 372 {
                    // x < 1/d1
                    // n1 * x * x where n1 = 7.5625
                    // n1 in 1024-scale: 7.5625 * 1024 = 7744
                    let x_sq = (x * x) >> 10; // x^2 / 1024 to normalize
                    ((7744 * x_sq) >> 10) >> 2 // Apply n1, then convert from 1024 to 256 scale
                } else if x < 745 {
                    // x < 2/d1
                    // n1 * (x - 1.5/d1)^2 + 0.75
                    let offset = 559; // 1.5/d1 * 1024 = 559
                    let adjusted_x = x - offset;
                    let x_sq = (adjusted_x * adjusted_x) >> 10;
                    192 + (((7744 * x_sq) >> 10) >> 2) // 0.75 * 256 = 192
                } else if x < 931 {
                    // x < 2.5/d1
                    // n1 * (x - 2.25/d1)^2 + 0.9375
                    let offset = 838; // 2.25/d1 * 1024 = 838
                    let adjusted_x = x - offset;
                    let x_sq = (adjusted_x * adjusted_x) >> 10;
                    240 + (((7744 * x_sq) >> 10) >> 2) // 0.9375 * 256 = 240
                } else {
                    // n1 * (x - 2.625/d1)^2 + 0.984375
                    let offset = 977; // 2.625/d1 * 1024 = 977
                    let adjusted_x = x - offset;
                    let x_sq = (adjusted_x * adjusted_x) >> 10;
                    252 + (((7744 * x_sq) >> 10) >> 2) // 0.984375 * 256 = 252
                };

                result.clamp(0, 255) as u8
            }
        }
    }

    /// Computes the animation factor for a given time offset.
    ///
    /// This calculation is expected to occur only once per animation node
    /// in the view rendering, and thus has considerable compute headroom.
    #[must_use]
    #[allow(dead_code)]
    fn factor_f32(self, time: Duration, duration: Duration) -> f32 {
        match self {
            Self::Linear => time.as_secs_f32() / duration.as_secs_f32(),
            Self::EaseIn => {
                let x = time.as_secs_f32() / duration.as_secs_f32();
                x * x
            }
            Self::EaseOut => {
                let x = time.as_secs_f32() / duration.as_secs_f32();
                1.0 - (1.0 - x) * (1.0 - x)
            }
            Self::EaseInOut => {
                let x = time.as_secs_f32() / duration.as_secs_f32();
                if x < 0.5 {
                    2.0 * x * x
                } else {
                    let y = -2.0 * x + 2.0;
                    1.0 - y * y / 2.0
                }
            }
            Self::EaseInCubic => {
                let x = time.as_secs_f32() / duration.as_secs_f32();
                x * x * x
            }
            Self::EaseOutCubic => {
                let x = time.as_secs_f32() / duration.as_secs_f32();
                1.0 - (1.0 - x) * (1.0 - x) * (1.0 - x)
            }
            Self::EaseOutBounce => {
                let x = time.as_secs_f32() / duration.as_secs_f32();
                let n1 = 7.5625;
                let d1 = 2.75;

                if x < 1.0 / d1 {
                    n1 * x * x
                } else if x < 2.0 / d1 {
                    let adjusted_x = x - 1.5 / d1;
                    n1 * adjusted_x * adjusted_x + 0.75
                } else if x < 2.5 / d1 {
                    let adjusted_x = x - 2.25 / d1;
                    n1 * adjusted_x * adjusted_x + 0.9375
                } else {
                    let adjusted_x = x - 2.625 / d1;
                    n1 * adjusted_x * adjusted_x + 0.984_375
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use core::time::Duration;

    use super::*;

    fn factor(animation: &Animation, time: u64) -> u8 {
        animation
            .curve
            .factor(Duration::from_millis(time), animation.duration)
    }

    #[expect(clippy::cast_sign_loss)]
    fn factor_f32(animation: &Animation, time: u64) -> u8 {
        (animation
            .curve
            .factor_f32(Duration::from_millis(time), animation.duration)
            * 255.0)
            .clamp(0.0, 255.0) as u8
    }

    #[test]
    fn linear_factor_approximates_f32() {
        let animation = Animation::linear(Duration::from_millis(500));
        for time in 0..512 {
            let f32_factor = factor_f32(&animation, time);
            let u8_factor = factor(&animation, time);
            assert!(f32_factor.abs_diff(u8_factor) <= 1);
        }
    }

    #[test]
    fn ease_in_factor_approximates_f32() {
        let animation = Animation::ease_in(Duration::from_millis(500));
        for time in 0..512 {
            let f32_factor = factor_f32(&animation, time);
            let u8_factor = factor(&animation, time);
            assert!(f32_factor.abs_diff(u8_factor) <= 1);
        }
    }

    #[test]
    fn ease_out_factor_approximates_f32() {
        let animation = Animation::ease_out(Duration::from_millis(500));
        for time in 0..512 {
            let f32_factor = factor_f32(&animation, time);
            let u8_factor = factor(&animation, time);
            assert!(f32_factor.abs_diff(u8_factor) <= 2);
        }
    }

    #[test]
    fn ease_in_out_factor_approximates_f32() {
        let animation = Animation::ease_in_out(Duration::from_millis(500));
        for time in 0..512 {
            let f32_factor = factor_f32(&animation, time);
            let u8_factor = factor(&animation, time);
            assert!(f32_factor.abs_diff(u8_factor) <= 2);
        }
    }

    #[test]
    fn linear_animation_factor_bounds() {
        let animation = Animation::linear(Duration::from_millis(100));
        assert_eq!(factor(&animation, 0), 0);
        assert_eq!(factor(&animation, 50), 127);
        assert_eq!(factor(&animation, 100), 255);
        assert_eq!(factor(&animation, 101), 255);
        assert_eq!(factor(&animation, 1500), 255);
    }

    #[test]
    fn ease_in_animation_factor_bounds() {
        let animation = Animation::ease_in(Duration::from_millis(100));
        assert_eq!(factor(&animation, 0), 0);
        assert_eq!(factor(&animation, 100), 255);
        assert_eq!(factor(&animation, 101), 255);
        assert_eq!(factor(&animation, 1500), 255);
    }

    #[test]
    fn ease_out_animation_factor_bounds() {
        let animation = Animation::ease_out(Duration::from_millis(100));
        assert_eq!(factor(&animation, 0), 0);
        assert_eq!(factor(&animation, 100), 255);
        assert_eq!(factor(&animation, 101), 255);
        assert_eq!(factor(&animation, 1500), 255);
    }

    #[test]
    fn ease_in_cubic_factor_approximates_f32() {
        let animation = Animation::ease_in_cubic(Duration::from_millis(500));
        for time in 0..512 {
            let f32_factor = factor_f32(&animation, time);
            let u8_factor = factor(&animation, time);
            assert!(f32_factor.abs_diff(u8_factor) <= 2);
        }
    }

    #[test]
    fn ease_out_cubic_factor_approximates_f32() {
        let animation = Animation::ease_out_cubic(Duration::from_millis(500));
        for time in 0..512 {
            let f32_factor = factor_f32(&animation, time);
            let u8_factor = factor(&animation, time);
            assert!(f32_factor.abs_diff(u8_factor) <= 3);
        }
    }

    #[test]
    fn ease_out_bounce_factor_approximates_f32() {
        let animation = Animation::ease_out_bounce(Duration::from_millis(500));
        for time in 0..512 {
            let f32_factor = factor_f32(&animation, time);
            let u8_factor = factor(&animation, time);
            assert!(
                f32_factor.abs_diff(u8_factor) <= 3,
                "Expected {u8_factor} to be nearly {f32_factor} at t={time}"
            );
        }
    }

    #[test]
    fn ease_in_cubic_animation_factor_bounds() {
        let animation = Animation::ease_in_cubic(Duration::from_millis(100));
        assert_eq!(factor(&animation, 0), 0);
        assert_eq!(factor(&animation, 100), 255);
        assert_eq!(factor(&animation, 101), 255);
        assert_eq!(factor(&animation, 1500), 255);
    }

    #[test]
    fn ease_out_cubic_animation_factor_bounds() {
        let animation = Animation::ease_out_cubic(Duration::from_millis(100));
        assert_eq!(factor(&animation, 0), 0);
        assert_eq!(factor(&animation, 100), 255);
        assert_eq!(factor(&animation, 101), 255);
        assert_eq!(factor(&animation, 1500), 255);
    }

    #[test]
    fn ease_out_bounce_animation_factor_bounds() {
        let animation = Animation::ease_out_bounce(Duration::from_millis(100));
        assert_eq!(factor(&animation, 0), 0);
        assert_eq!(factor(&animation, 100), 255);
        assert_eq!(factor(&animation, 101), 255);
        assert_eq!(factor(&animation, 1500), 255);
    }
}