Skip to main content

fc_ui/capabilities/primitives/
skeleton.rs

1//! Skeleton component - Loading placeholder with pulsing animation effect.
2
3use crate::capabilities::foundation::theme::use_theme;
4use gpui::{prelude::FluentBuilder as _, *};
5use std::time::Duration;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum SkeletonVariant {
9    #[default]
10    Text,
11    Circle,
12    Rect,
13}
14
15#[derive(IntoElement)]
16pub struct Skeleton {
17    base: Div,
18    variant: SkeletonVariant,
19    secondary: bool,
20}
21
22impl Skeleton {
23    pub fn new() -> Self {
24        Self {
25            base: div(),
26            variant: SkeletonVariant::default(),
27            secondary: false,
28        }
29    }
30
31    pub fn variant(mut self, variant: SkeletonVariant) -> Self {
32        self.variant = variant;
33        self
34    }
35
36    pub fn secondary(mut self, secondary: bool) -> Self {
37        self.secondary = secondary;
38        self
39    }
40}
41
42impl RenderOnce for Skeleton {
43    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
44        let theme = use_theme(cx);
45
46        let base_color = if self.secondary {
47            theme.tokens.muted.opacity(0.5)
48        } else {
49            theme.tokens.muted
50        };
51
52        self.base
53            .when(self.variant == SkeletonVariant::Text, |this| {
54                this.w_full().h(px(16.0)).rounded(theme.tokens.radius_md)
55            })
56            .when(self.variant == SkeletonVariant::Circle, |this| {
57                this.rounded_full()
58            })
59            .when(self.variant == SkeletonVariant::Rect, |this| {
60                this.rounded(theme.tokens.radius_md)
61            })
62            .bg(base_color)
63            .with_animation(
64                "skeleton-pulse",
65                Animation::new(Duration::from_secs(2))
66                    .repeat()
67                    .with_easing(ease_in_out),
68                move |this, delta| {
69                    let opacity = 1.0 - (delta * 0.3);
70                    this.opacity(opacity)
71                },
72            )
73    }
74}
75
76impl Default for Skeleton {
77    fn default() -> Self {
78        Self::new()
79    }
80}
81
82impl InteractiveElement for Skeleton {
83    fn interactivity(&mut self) -> &mut Interactivity {
84        self.base.interactivity()
85    }
86}
87
88impl StatefulInteractiveElement for Skeleton {}
89
90impl ParentElement for Skeleton {
91    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
92        self.base.extend(elements)
93    }
94}
95
96impl Styled for Skeleton {
97    fn style(&mut self) -> &mut StyleRefinement {
98        self.base.style()
99    }
100}