Skip to main content

fc_ui/capabilities/effects/
skeleton_loader.rs

1//! Skeleton loader - renders shimmer placeholders when loading, transitions to content when ready.
2
3use gpui::prelude::FluentBuilder as _;
4use gpui::*;
5use std::time::Duration;
6
7use crate::capabilities::foundation::theme::use_theme;
8
9pub struct SkeletonLoaderState {
10    is_loading: bool,
11    transition_version: usize,
12}
13
14impl SkeletonLoaderState {
15    pub fn new() -> Self {
16        Self {
17            is_loading: true,
18            transition_version: 0,
19        }
20    }
21
22    pub fn set_loading(&mut self, loading: bool, cx: &mut Context<Self>) {
23        if self.is_loading != loading {
24            self.is_loading = loading;
25            self.transition_version = self.transition_version.wrapping_add(1);
26            cx.notify();
27        }
28    }
29
30    pub fn is_loading(&self) -> bool {
31        self.is_loading
32    }
33}
34
35impl Default for SkeletonLoaderState {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41/// 加载骨架屏。每行 shimmer 扫光在 -200px..600px 区间内循环移动,
42/// 适用于宽度不超过约 600px 的容器。
43#[derive(IntoElement)]
44pub struct SkeletonLoader {
45    id: ElementId,
46    state: Entity<SkeletonLoaderState>,
47    lines: usize,
48    line_height: Pixels,
49    line_gap: Pixels,
50    shimmer_duration: Duration,
51    children: Vec<AnyElement>,
52    style: StyleRefinement,
53}
54
55impl SkeletonLoader {
56    pub fn new(id: impl Into<ElementId>, state: Entity<SkeletonLoaderState>) -> Self {
57        Self {
58            id: id.into(),
59            state,
60            lines: 3,
61            line_height: px(16.0),
62            line_gap: px(12.0),
63            shimmer_duration: Duration::from_millis(1500),
64            children: Vec::new(),
65            style: StyleRefinement::default(),
66        }
67    }
68
69    pub fn lines(mut self, lines: usize) -> Self {
70        self.lines = lines.max(1);
71        self
72    }
73
74    pub fn line_height(mut self, height: Pixels) -> Self {
75        self.line_height = height;
76        self
77    }
78
79    pub fn line_gap(mut self, gap: Pixels) -> Self {
80        self.line_gap = gap;
81        self
82    }
83
84    pub fn shimmer_duration(mut self, duration: Duration) -> Self {
85        self.shimmer_duration = duration;
86        self
87    }
88}
89
90impl Styled for SkeletonLoader {
91    fn style(&mut self) -> &mut StyleRefinement {
92        &mut self.style
93    }
94}
95
96impl ParentElement for SkeletonLoader {
97    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
98        self.children.extend(elements);
99    }
100}
101
102fn skeleton_line_width_pct(index: usize) -> f32 {
103    match index % 5 {
104        0 => 1.0,
105        1 => 0.85,
106        2 => 0.92,
107        3 => 0.6,
108        4 => 0.78,
109        _ => 1.0,
110    }
111}
112
113impl RenderOnce for SkeletonLoader {
114    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
115        let theme = use_theme(cx);
116        let user_style = self.style;
117        let loader_state = self.state.read(cx);
118        let is_loading = loader_state.is_loading;
119        let version = loader_state.transition_version;
120        let shimmer_color = theme.tokens.muted;
121        let highlight_color = hsla(0.0, 0.0, 1.0, 0.15);
122
123        let mut wrapper = div().id(self.id.clone()).relative().w_full();
124
125        if is_loading {
126            let mut skeleton_container = div().flex().flex_col().gap(self.line_gap).w_full();
127
128            for line_idx in 0..self.lines {
129                let width_pct = skeleton_line_width_pct(line_idx);
130                let shimmer_dur = self.shimmer_duration;
131                let anim_id =
132                    ElementId::Name(format!("skeleton-shimmer-{}-{line_idx}", self.id).into());
133
134                skeleton_container = skeleton_container.child(
135                    div()
136                        .w(relative(width_pct))
137                        .h(self.line_height)
138                        .rounded(theme.tokens.radius_md)
139                        .bg(shimmer_color)
140                        .overflow_hidden()
141                        .relative()
142                        .child(
143                            div()
144                                .absolute()
145                                .top_0()
146                                .bottom_0()
147                                .w(px(200.0))
148                                .bg(gpui::linear_gradient(
149                                    90.0,
150                                    gpui::linear_color_stop(transparent_black(), 0.0),
151                                    gpui::linear_color_stop(highlight_color, 1.0),
152                                ))
153                                .with_animation(
154                                    anim_id,
155                                    Animation::new(shimmer_dur)
156                                        .repeat()
157                                        .with_easing(gpui::linear),
158                                    move |this, delta| {
159                                        let start = px(-200.0);
160                                        let end = px(600.0);
161                                        let current = start + (end - start) * delta;
162                                        this.left(current)
163                                    },
164                                ),
165                        ),
166                );
167            }
168
169            wrapper = wrapper.child(skeleton_container);
170        } else {
171            let content = div().w_full().children(self.children).with_animation(
172                ElementId::Name(format!("{}-skeleton-fade-{version}", self.id).into()),
173                Animation::new(Duration::from_millis(300)).with_easing(gpui::ease_in_out),
174                move |el, delta| el.opacity(delta),
175            );
176
177            wrapper = wrapper.child(content);
178        }
179
180        wrapper.map(|this| {
181            let mut el = this;
182            el.style().refine(&user_style);
183            el
184        })
185    }
186}