arc-ui 0.4.0

Physics-first native Rust desktop UI component library & charts for GPUI (120+ interactive components ported from Arc).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Ported from Arc (MIT) © 2026 Elia Kuratli
//! Reference: `Arc源码/arc-library/registry/components/empty-state/empty-state.tsx` & `.module.css`
//!
//! P6 Component #3: `empty-state`
//!
//! - Centered placeholder panel with a `48×48` rounded icon tile (`Radii::PANEL`, `surface_muted`,
//!   `1px` border), title, concise description (`max-width: 288px`), and optional action cluster.
//! - When `icon`, `title`, or `description` changes in place (e.g. toggling between "No results"
//!   and "Inbox zero"), the icon crossfades/scales on `motion.spring.snappy`, the copy rises in
//!   over `duration.standard` (`240ms`, `ease.enter`) while the previous copy lifts out over
//!   `duration.fast` (`160ms`), and the copy frame smoothly springs its height via
//!   `motion.spring.smooth`.

use std::{cell::RefCell, rc::Rc};

use arc_motion::{
    BlurFallback, CubicBezier, arc_spring_with_options, is_reduced_motion, motion_now,
    spring::SpringOptions,
};
use arc_tokens::{ActiveArcTheme as _, Radii, Spacing, Typography};
use gpui_kit::{
    AnyElement, App, ElementId, Hsla, IntoElement, ParentElement, RenderOnce, Role, SharedString,
    Styled, Window, assets::IconName, base::StyledExt as _, div, prelude::*, px, svg,
};

type ActionBuilder = Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>;

/// Samples `(old_alpha, old_y_px, new_alpha, new_y_px)` for title/description in-place swap.
pub fn empty_state_copy_sample(elapsed_secs: f64, reduced: bool) -> (f32, f32, f32, f32) {
    if reduced {
        let t = (elapsed_secs / 0.12).clamp(0.0, 1.0) as f32;
        return ((1.0 - t).clamp(0.0, 1.0), 0.0, t, 0.0);
    }
    let out_t = (elapsed_secs / 0.16).clamp(0.0, 1.0) as f32;
    let out_eased = CubicBezier::new(0.22, 1.0, 0.36, 1.0).sample(out_t);
    let out_blur = BlurFallback::evaluate(px(out_eased * 2.0), false);
    let old_alpha = ((1.0 - out_eased) * out_blur.opacity_multiplier).clamp(0.0, 1.0);
    let old_y = -4.5 * out_eased;

    let in_t = (elapsed_secs / 0.24).clamp(0.0, 1.0) as f32;
    let in_eased = CubicBezier::new(0.16, 1.0, 0.3, 1.0).sample(in_t);
    let in_blur = BlurFallback::evaluate(px((1.0 - in_eased) * 4.0), false);
    let new_alpha = (in_eased * in_blur.opacity_multiplier).clamp(0.0, 1.0);
    let new_y = 4.5 * (1.0 - in_eased);
    (old_alpha, old_y, new_alpha, new_y)
}

/// Samples `(old_alpha, new_alpha, new_scale)` for icon crossfade on icon swap.
pub fn empty_state_icon_sample(elapsed_secs: f64, reduced: bool) -> (f32, f32, f32) {
    if reduced {
        let t = (elapsed_secs / 0.12).clamp(0.0, 1.0) as f32;
        return ((1.0 - t).clamp(0.0, 1.0), t, 1.0);
    }
    let out_t = (elapsed_secs / 0.16).clamp(0.0, 1.0) as f32;
    let old_alpha = (1.0 - CubicBezier::new(0.22, 1.0, 0.36, 1.0).sample(out_t)).clamp(0.0, 1.0);

    let in_t = (elapsed_secs / 0.26).clamp(0.0, 1.0) as f32;
    let in_eased = CubicBezier::new(0.16, 1.0, 0.3, 1.0).sample(in_t);
    let scale = 0.6 + 0.4 * in_eased;
    (old_alpha, in_eased.clamp(0.0, 1.0), scale)
}

#[derive(Clone)]
struct EmptyStateInternal {
    last_title: SharedString,
    prev_title: Option<SharedString>,
    title_changed_at: f64,
    last_desc: SharedString,
    prev_desc: Option<SharedString>,
    desc_changed_at: f64,
    last_icon: IconName,
    prev_icon: Option<IconName>,
    icon_changed_at: f64,
    measured_copy_h: Rc<RefCell<f32>>,
    copy_changed_at: f64,
}

/// Centered empty-state container with smooth in-place copy, icon, and height transitions (`empty-state`).
#[derive(IntoElement)]
pub struct EmptyState {
    id: ElementId,
    title: SharedString,
    description: SharedString,
    icon: IconName,
    action: Option<ActionBuilder>,
    label: Option<SharedString>,
}

impl EmptyState {
    pub fn new(
        id: impl Into<ElementId>,
        title: impl Into<SharedString>,
        description: impl Into<SharedString>,
    ) -> Self {
        Self {
            id: id.into(),
            title: title.into(),
            description: description.into(),
            icon: IconName::Folder,
            action: None,
            label: None,
        }
    }

    pub fn icon(mut self, icon: IconName) -> Self {
        self.icon = icon;
        self
    }

    pub fn action(
        mut self,
        builder: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
    ) -> Self {
        self.action = Some(Rc::new(builder));
        self
    }

    pub fn label(mut self, label: impl Into<SharedString>) -> Self {
        self.label = Some(label.into());
        self
    }
}

fn render_copy_slot(
    current: SharedString,
    previous: Option<SharedString>,
    elapsed: f64,
    text_size: gpui_kit::Pixels,
    font_medium: bool,
    color: Hsla,
    reduced: bool,
) -> AnyElement {
    let (old_alpha, old_y, new_alpha, new_y) = if previous.is_some() {
        empty_state_copy_sample(elapsed, reduced)
    } else {
        (0.0, 0.0, 1.0, 0.0)
    };

    let outgoing = previous.and_then(|prev| {
        if old_alpha <= 0.01 {
            None
        } else {
            let mut el = div()
                .absolute()
                .left_0()
                .right_0()
                .top(px(old_y))
                .opacity(old_alpha)
                .text_center()
                .text_size(text_size)
                .text_color(color)
                .child(prev);
            if font_medium {
                el = el.font_medium();
            }
            Some(el)
        }
    });

    let mut incoming = div()
        .relative()
        .top(px(new_y))
        .opacity(new_alpha)
        .w_full()
        .text_center()
        .text_size(text_size)
        .text_color(color)
        .child(current);
    if font_medium {
        incoming = incoming.font_medium();
    }

    div()
        .relative()
        .w_full()
        .when_some(outgoing, |el, out| el.child(out))
        .child(incoming)
        .into_any_element()
}

impl RenderOnce for EmptyState {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.arc_theme().clone();
        let colors = theme.colors;
        let motion = theme.motion;
        let reduced = is_reduced_motion(cx);
        let now = motion_now(cx);

        let state_cell = window
            .use_keyed_state(
                ElementId::NamedChild(self.id.clone().into(), SharedString::from("es-state")),
                cx,
                |_, _| {
                    Rc::new(RefCell::new(EmptyStateInternal {
                        last_title: self.title.clone(),
                        prev_title: None,
                        title_changed_at: now - 1.0,
                        last_desc: self.description.clone(),
                        prev_desc: None,
                        desc_changed_at: now - 1.0,
                        last_icon: self.icon,
                        prev_icon: None,
                        icon_changed_at: now - 1.0,
                        measured_copy_h: Rc::new(RefCell::new(0.0)),
                        copy_changed_at: now - 1.0,
                    }))
                },
            )
            .read(cx)
            .clone();

        let (
            prev_title,
            title_elapsed,
            prev_desc,
            desc_elapsed,
            prev_icon,
            icon_elapsed,
            copy_h_ref,
            copy_recently_changed,
            animating,
        ) = {
            let mut st = state_cell.borrow_mut();
            if st.last_title != self.title {
                st.prev_title = Some(st.last_title.clone());
                st.last_title = self.title.clone();
                st.title_changed_at = now;
                st.copy_changed_at = now;
            }
            if st.last_desc != self.description {
                st.prev_desc = Some(st.last_desc.clone());
                st.last_desc = self.description.clone();
                st.desc_changed_at = now;
                st.copy_changed_at = now;
            }
            if st.last_icon != self.icon {
                st.prev_icon = Some(st.last_icon);
                st.last_icon = self.icon;
                st.icon_changed_at = now;
            }

            let t_dt = (now - st.title_changed_at).max(0.0);
            let d_dt = (now - st.desc_changed_at).max(0.0);
            let i_dt = (now - st.icon_changed_at).max(0.0);
            if st.prev_title.is_some() && t_dt >= 0.28 {
                st.prev_title = None;
            }
            if st.prev_desc.is_some() && d_dt >= 0.28 {
                st.prev_desc = None;
            }
            if st.prev_icon.is_some() && i_dt >= 0.30 {
                st.prev_icon = None;
            }
            let anim = st.prev_title.is_some() || st.prev_desc.is_some() || st.prev_icon.is_some();
            let recent = (now - st.copy_changed_at).max(0.0) < 0.6;
            (
                st.prev_title.clone(),
                t_dt,
                st.prev_desc.clone(),
                d_dt,
                st.prev_icon,
                i_dt,
                st.measured_copy_h.clone(),
                recent,
                anim,
            )
        };

        if animating && !reduced {
            window.request_animation_frame();
        }

        let (old_icon_alpha, new_icon_alpha, new_icon_scale) = if prev_icon.is_some() {
            empty_state_icon_sample(icon_elapsed, reduced)
        } else {
            (0.0, 1.0, 1.0)
        };

        let icon_tile = div()
            .relative()
            .w(Spacing::S12)
            .h(Spacing::S12)
            .rounded(Radii::PANEL)
            .border_1()
            .border_color(colors.border)
            .bg(colors.surface_muted)
            .flex()
            .items_center()
            .justify_center()
            .when_some(
                prev_icon.filter(|_| old_icon_alpha > 0.01),
                |el, prev_ic| {
                    el.child(
                        div()
                            .absolute()
                            .inset_0()
                            .opacity(old_icon_alpha)
                            .flex()
                            .items_center()
                            .justify_center()
                            .child(
                                svg()
                                    .path(prev_ic.path())
                                    .size(px(22.0))
                                    .text_color(colors.text_secondary),
                            ),
                    )
                },
            )
            .child(
                div()
                    .opacity(new_icon_alpha)
                    .flex()
                    .items_center()
                    .justify_center()
                    .child(
                        svg()
                            .path(self.icon.path())
                            .size(px(24.0 * new_icon_scale))
                            .text_color(colors.text_secondary),
                    ),
            );

        let target_copy_h = *copy_h_ref.borrow();
        let animated_copy_h = if !reduced && target_copy_h > 10.0 {
            Some(
                arc_spring_with_options(
                    ElementId::NamedChild(self.id.clone().into(), SharedString::from("copy-h")),
                    target_copy_h,
                    SpringOptions::new(motion.spring.smooth).with_initial_value(target_copy_h),
                    window,
                    cx,
                )
                .value,
            )
        } else {
            None
        };

        let measure_copy_h = copy_h_ref.clone();
        let copy_content = div()
            .w_full()
            .flex()
            .flex_col()
            .items_center()
            .pt(Spacing::S5)
            .child(render_copy_slot(
                self.title,
                prev_title,
                title_elapsed,
                Typography::TEXT_BASE,
                true,
                colors.foreground,
                reduced,
            ))
            .child(
                div()
                    .w_full()
                    .max_w(px(288.0))
                    .mt(Spacing::S2)
                    .child(render_copy_slot(
                        self.description,
                        prev_desc,
                        desc_elapsed,
                        Typography::TEXT_SM,
                        false,
                        colors.text_secondary,
                        reduced,
                    )),
            );

        let mut copy_frame = div()
            .w_full()
            .flex()
            .flex_col()
            .items_center()
            .child(copy_content)
            .on_children_prepainted(move |bounds, window, _| {
                if let Some(b) = bounds.first() {
                    let h = f32::from(b.size.height);
                    let prev = *measure_copy_h.borrow();
                    if h > 10.0 && (prev - h).abs() > 0.5 {
                        *measure_copy_h.borrow_mut() = h;
                        window.request_animation_frame();
                    }
                }
            });

        if let Some(h) = animated_copy_h
            && copy_recently_changed
            && (h - target_copy_h).abs() > 0.5
        {
            copy_frame = copy_frame.h(px(h.max(10.0))).overflow_hidden();
        }

        let action_el = self.action.map(|act| {
            div()
                .mt(Spacing::S5)
                .flex()
                .flex_row()
                .flex_wrap()
                .items_center()
                .justify_center()
                .gap(Spacing::S3)
                .child(act(window, cx))
        });

        let _ = self.label;
        div()
            .id(self.id)
            .role(Role::Region)
            .w_full()
            .px(Spacing::S5)
            .py(Spacing::S10)
            .flex()
            .flex_col()
            .items_center()
            .child(icon_tile)
            .child(copy_frame)
            .when_some(action_el, |el, act| el.child(act))
            .into_any_element()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_state_copy_and_icon_samples_converge() {
        let (oa0, _, na0, ny0) = empty_state_copy_sample(0.0, false);
        assert!(oa0 > 0.9 && na0 == 0.0 && (ny0 - 4.5).abs() < 0.01);

        let (oa1, _, na1, ny1) = empty_state_copy_sample(0.30, false);
        assert_eq!(oa1, 0.0);
        assert_eq!(na1, 1.0);
        assert_eq!(ny1, 0.0);

        let (_, na_i, scale_i) = empty_state_icon_sample(0.30, false);
        assert_eq!(na_i, 1.0);
        assert_eq!(scale_i, 1.0);
    }
}