gpui_component/
skeleton.rs

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