Skip to main content

gpui_component/
skeleton.rs

1use crate::{ActiveTheme, StyledExt};
2use gpui::ColorExt as _;
3use gpui::{
4    Animation, AnimationExt, IntoElement, RenderOnce, StyleRefinement, Styled, bounce, div,
5    ease_in_out,
6};
7use instant::Duration;
8
9/// A skeleton loading placeholder element.
10#[derive(IntoElement)]
11pub struct Skeleton {
12    style: StyleRefinement,
13    secondary: bool,
14}
15
16impl Skeleton {
17    /// Create a new Skeleton element.
18    pub fn new() -> Self {
19        Self {
20            style: StyleRefinement::default(),
21            secondary: false,
22        }
23    }
24
25    /// Set use secondary color.
26    pub fn secondary(mut self) -> Self {
27        self.secondary = true;
28        self
29    }
30}
31
32impl Styled for Skeleton {
33    fn style(&mut self) -> &mut gpui::StyleRefinement {
34        &mut self.style
35    }
36}
37
38impl RenderOnce for Skeleton {
39    fn render(self, _: &mut gpui::Window, cx: &mut gpui::App) -> impl IntoElement {
40        div()
41            .w_full()
42            .h_4()
43            .bg(if self.secondary {
44                cx.theme().skeleton.opacity(0.5).into()
45            } else {
46                cx.theme().skeleton
47            })
48            .refine_style(&self.style)
49            .with_animation(
50                "skeleton",
51                Animation::new(Duration::from_secs(2))
52                    .repeat()
53                    .with_easing(bounce(ease_in_out)),
54                move |this, delta| {
55                    let v = 1.0 - delta * 0.5;
56                    this.opacity(v)
57                },
58            )
59    }
60}