Skip to main content

bhoo/systems/material/
skeleton.rs

1use crate::*;
2
3/// Animation mode for `Frame::skeleton_with_options`.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum SkeletonAnimation {
6    None,
7    #[default]
8    Pulse,
9    Wave,
10}
11
12/// Configuration for `Frame::skeleton_with_options`.
13#[derive(Clone, Copy, Debug)]
14pub struct SkeletonOptions {
15    pub animation: SkeletonAnimation,
16    pub radius: f32,
17}
18
19impl Default for SkeletonOptions {
20    fn default() -> Self {
21        Self {
22            animation: SkeletonAnimation::Pulse,
23            radius: 4.0,
24        }
25    }
26}
27
28impl Frame<'_> {
29    /// Paint a noninteractive loading placeholder with optional MUI-style pulse.
30    pub fn skeleton(&mut self, bounds: Rect, radius: f32, animated: bool) {
31        self.skeleton_with_options(
32            bounds,
33            SkeletonOptions {
34                animation: if animated { SkeletonAnimation::Pulse } else { SkeletonAnimation::None },
35                radius,
36            },
37        );
38    }
39
40    /// Paint a loading placeholder supporting static (`None`), `Pulse`, and MUI `Wave` shimmer.
41    pub fn skeleton_with_options(&mut self, bounds: Rect, options: SkeletonOptions) {
42        let visible = bounds.intersect(self.clip);
43        if visible.width <= 0.0 || visible.height <= 0.0 {
44            return;
45        }
46        let anim = if self.reduced_motion() {
47            SkeletonAnimation::None
48        } else {
49            options.animation
50        };
51        match anim {
52            SkeletonAnimation::None => {
53                self.rect(bounds, options.radius, self.ui.theme.text.opacity(0.11));
54            }
55            SkeletonAnimation::Pulse => {
56                self.ui.continuous = true;
57                let phase = ((self.ui.time - 0.5).max(0.0) % 2.0) as f32 / 2.0;
58                let half = if phase < 0.5 { phase * 2.0 } else { (1.0 - phase) * 2.0 };
59                let opacity = 1.0 - 0.6 * crate::timing::Easing::Cubic([0.42, 0.0, 0.58, 1.0]).sample(half);
60                self.rect(bounds, options.radius, self.ui.theme.text.opacity(0.11 * opacity));
61            }
62            SkeletonAnimation::Wave => {
63                self.ui.continuous = true;
64                self.rect(bounds, options.radius, self.ui.theme.text.opacity(0.11));
65                let phase = ((self.ui.time.max(0.0) % 1.6) / 1.6) as f32;
66                let band_w = (bounds.width * 0.45).max(28.0);
67                let sweep_x = bounds.x - band_w + (bounds.width + band_w * 2.0) * phase;
68                self.within(bounds, |f| {
69                    f.paint_rect(
70                        Rect { x: sweep_x, y: bounds.y, width: band_w, height: bounds.height },
71                        options.radius,
72                        f.theme().text.opacity(0.08),
73                    );
74                });
75            }
76        }
77    }
78}
79
80#[cfg(test)]
81#[path = "skeleton.spec.rs"]
82mod tests;