fc_ui/capabilities/primitives/
empty_state.rs1use crate::capabilities::foundation::theme::use_theme;
2use crate::capabilities::primitives::button::{Button, ButtonVariant};
3use crate::capabilities::primitives::icon::Icon;
4use crate::capabilities::primitives::icon_source::IconSource;
5use gpui::{prelude::FluentBuilder as _, *};
6use std::rc::Rc;
7
8#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
9pub enum EmptyStateSize {
10 Sm,
11 #[default]
12 Md,
13 Lg,
14}
15
16impl EmptyStateSize {
17 fn icon_size(self) -> Pixels {
18 match self {
19 EmptyStateSize::Sm => px(32.0),
20 EmptyStateSize::Md => px(48.0),
21 EmptyStateSize::Lg => px(64.0),
22 }
23 }
24
25 fn title_size(self) -> Pixels {
26 match self {
27 EmptyStateSize::Sm => px(14.0),
28 EmptyStateSize::Md => px(18.0),
29 EmptyStateSize::Lg => px(24.0),
30 }
31 }
32
33 fn description_size(self) -> Pixels {
34 match self {
35 EmptyStateSize::Sm => px(12.0),
36 EmptyStateSize::Md => px(14.0),
37 EmptyStateSize::Lg => px(16.0),
38 }
39 }
40
41 fn gap(self) -> Pixels {
42 match self {
43 EmptyStateSize::Sm => px(12.0),
44 EmptyStateSize::Md => px(16.0),
45 EmptyStateSize::Lg => px(20.0),
46 }
47 }
48}
49
50#[derive(IntoElement)]
51pub struct EmptyState {
52 id: ElementId,
53 icon: Option<IconSource>,
54 title: SharedString,
55 description: Option<SharedString>,
56 action: Option<(SharedString, Rc<dyn Fn(&mut Window, &mut App)>)>,
57 secondary_action: Option<(SharedString, Rc<dyn Fn(&mut Window, &mut App)>)>,
58 size: EmptyStateSize,
59 style: StyleRefinement,
60}
61
62impl EmptyState {
63 pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
64 Self {
65 id: id.into(),
66 icon: None,
67 title: title.into(),
68 description: None,
69 action: None,
70 secondary_action: None,
71 size: EmptyStateSize::default(),
72 style: StyleRefinement::default(),
73 }
74 }
75
76 pub fn icon(mut self, icon: impl Into<IconSource>) -> Self {
77 self.icon = Some(icon.into());
78 self
79 }
80
81 pub fn description(mut self, description: impl Into<SharedString>) -> Self {
82 self.description = Some(description.into());
83 self
84 }
85
86 pub fn action(
87 mut self,
88 label: impl Into<SharedString>,
89 handler: impl Fn(&mut Window, &mut App) + 'static,
90 ) -> Self {
91 self.action = Some((label.into(), Rc::new(handler)));
92 self
93 }
94
95 pub fn secondary_action(
96 mut self,
97 label: impl Into<SharedString>,
98 handler: impl Fn(&mut Window, &mut App) + 'static,
99 ) -> Self {
100 self.secondary_action = Some((label.into(), Rc::new(handler)));
101 self
102 }
103
104 pub fn size(mut self, size: EmptyStateSize) -> Self {
105 self.size = size;
106 self
107 }
108}
109
110impl Styled for EmptyState {
111 fn style(&mut self) -> &mut StyleRefinement {
112 &mut self.style
113 }
114}
115
116impl RenderOnce for EmptyState {
117 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
118 let theme = use_theme(cx);
119 let user_style = self.style;
120 let icon_size = self.size.icon_size();
121 let title_size = self.size.title_size();
122 let description_size = self.size.description_size();
123 let gap = self.size.gap();
124 let id = self.id.clone();
125
126 div()
127 .id(self.id)
128 .flex()
129 .flex_col()
130 .items_center()
131 .justify_center()
132 .gap(gap)
133 .p(px(24.0))
134 .map(|mut this| {
135 this.style().refine(&user_style);
136 this
137 })
138 .when_some(self.icon, |d, icon_src| {
139 d.child(
140 Icon::new(icon_src)
141 .size(icon_size)
142 .color(theme.tokens.muted_foreground),
143 )
144 })
145 .child(
146 div()
147 .flex()
148 .flex_col()
149 .items_center()
150 .gap(px(8.0))
151 .child(
152 div()
153 .text_size(title_size)
154 .font_weight(FontWeight::SEMIBOLD)
155 .text_color(theme.tokens.foreground)
156 .font_family(theme.tokens.font_family.clone())
157 .text_align(TextAlign::Center)
158 .child(self.title),
159 )
160 .when_some(self.description, |d, desc| {
161 d.child(
162 div()
163 .text_size(description_size)
164 .text_color(theme.tokens.muted_foreground)
165 .font_family(theme.tokens.font_family.clone())
166 .text_align(TextAlign::Center)
167 .max_w(px(320.0))
168 .child(desc),
169 )
170 }),
171 )
172 .when(
173 self.action.is_some() || self.secondary_action.is_some(),
174 |d| {
175 d.child(
176 div()
177 .flex()
178 .items_center()
179 .gap(px(12.0))
180 .mt(px(8.0))
181 .when_some(self.action, |d, (label, handler)| {
182 let handler_clone = handler.clone();
183 d.child(
184 Button::new(
185 ElementId::Name(format!("{}-action", id).into()),
186 label,
187 )
188 .variant(ButtonVariant::Default)
189 .on_click(
190 move |_, window, cx| {
191 (handler_clone)(window, cx);
192 },
193 ),
194 )
195 })
196 .when_some(self.secondary_action, |d, (label, handler)| {
197 let handler_clone = handler.clone();
198 d.child(
199 Button::new(
200 ElementId::Name(format!("{}-secondary", id).into()),
201 label,
202 )
203 .variant(ButtonVariant::Ghost)
204 .on_click(
205 move |_, window, cx| {
206 (handler_clone)(window, cx);
207 },
208 ),
209 )
210 }),
211 )
212 },
213 )
214 }
215}