gpui_material_components/
empty_state.rs1use gpui::{
2 AnyElement, App, ElementId, InteractiveElement, IntoElement, ParentElement, Pixels, RenderOnce,
3 Role, SharedString, StatefulInteractiveElement, Styled, Window, div, prelude::FluentBuilder,
4 px,
5};
6use gpui_base::TestSupportExt;
7use gpui_material_foundation::{
8 ShapePolygon, Surface, color, current_material_theme, wrapped_text,
9};
10use gpui_material_theme::{
11 ColorRole, CornerShape, Direction, MaterialShape, TypeRole, TypographyEmphasis,
12};
13
14const DEFAULT_MAX_WIDTH: Pixels = px(360.);
15const ICON_CONTAINER_SIZE: Pixels = px(72.);
16const ICON_SIZE: Pixels = px(32.);
17const ACTION_GAP: Pixels = px(12.);
18const ACTIONS_TOP_MARGIN: Pixels = px(24.);
19
20enum EmptyStateVisual {
21 Icon(AnyElement),
22 Custom(AnyElement),
23}
24
25enum SupportingSlot {
26 Text(SharedString),
27 Custom(AnyElement),
28}
29
30enum IconContainerShape {
31 Polygon(ShapePolygon),
32 Corners(CornerShape),
33}
34
35#[derive(IntoElement)]
40pub struct EmptyState {
41 id: ElementId,
42 title: SharedString,
43 title_role: TypeRole,
44 supporting: Option<SupportingSlot>,
45 visual: Option<EmptyStateVisual>,
46 icon_container_shape: IconContainerShape,
47 icon_container_role: ColorRole,
48 icon_content_role: ColorRole,
49 primary_action: Option<AnyElement>,
50 secondary_action: Option<AnyElement>,
51 max_width: Pixels,
52}
53
54impl EmptyState {
55 pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
57 Self {
58 id: id.into(),
59 title: title.into(),
60 title_role: TypeRole::TitleLarge,
61 supporting: None,
62 visual: None,
63 icon_container_shape: IconContainerShape::Polygon(MaterialShape::Cookie7Sided.into()),
64 icon_container_role: ColorRole::SecondaryContainer,
65 icon_content_role: ColorRole::OnSecondaryContainer,
66 primary_action: None,
67 secondary_action: None,
68 max_width: DEFAULT_MAX_WIDTH,
69 }
70 }
71
72 pub fn supporting(mut self, text: impl Into<SharedString>) -> Self {
74 self.supporting = Some(SupportingSlot::Text(text.into()));
75 self
76 }
77
78 pub fn supporting_content(mut self, element: impl IntoElement) -> Self {
80 self.supporting = Some(SupportingSlot::Custom(element.into_any_element()));
81 self
82 }
83
84 pub fn icon(mut self, icon: impl IntoElement) -> Self {
86 self.visual = Some(EmptyStateVisual::Icon(icon.into_any_element()));
87 self
88 }
89
90 pub fn icon_container_shape(mut self, shape: impl Into<ShapePolygon>) -> Self {
94 self.icon_container_shape = IconContainerShape::Polygon(shape.into());
95 self
96 }
97
98 pub fn shape(mut self, shape: CornerShape) -> Self {
100 self.icon_container_shape = IconContainerShape::Corners(shape);
101 self
102 }
103
104 pub fn icon_container_color_role(mut self, container: ColorRole, content: ColorRole) -> Self {
107 self.icon_container_role = container;
108 self.icon_content_role = content;
109 self
110 }
111
112 pub fn visual(mut self, element: impl IntoElement) -> Self {
114 self.visual = Some(EmptyStateVisual::Custom(element.into_any_element()));
115 self
116 }
117
118 pub fn primary_action(mut self, action: impl IntoElement) -> Self {
120 self.primary_action = Some(action.into_any_element());
121 self
122 }
123
124 pub fn secondary_action(mut self, action: impl IntoElement) -> Self {
126 self.secondary_action = Some(action.into_any_element());
127 self
128 }
129
130 pub fn max_width(mut self, pixels: Pixels) -> Self {
133 self.max_width = resolve_max_width(pixels);
134 self
135 }
136
137 pub fn title_role(mut self, role: TypeRole) -> Self {
139 self.title_role = role;
140 self
141 }
142}
143
144fn resolve_max_width(width: Pixels) -> Pixels {
145 let raw = f32::from(width);
146 if raw.is_finite() && raw > 0. {
147 width
148 } else {
149 DEFAULT_MAX_WIDTH
150 }
151}
152
153impl RenderOnce for EmptyState {
154 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
155 let theme = current_material_theme(window, cx);
156 let direction = theme.layout.direction;
157 let on_surface = color(theme.colors.color(ColorRole::OnSurface));
158 let on_surface_variant = color(theme.colors.color(ColorRole::OnSurfaceVariant));
159 let icon_fg = color(theme.colors.color(self.icon_content_role));
160
161 let visual_slot = self.visual.map(|visual| match visual {
162 EmptyStateVisual::Icon(icon) => {
163 let surface = Surface::new(self.icon_container_role).content_color(icon_fg);
164 let surface = match self.icon_container_shape {
165 IconContainerShape::Polygon(polygon) => surface.polygon_shape(polygon),
166 IconContainerShape::Corners(corners) => surface.shape(corners),
167 };
168 div()
169 .id((self.id.clone(), "icon-container"))
170 .test_support()
171 .mb(px(20.))
172 .child(
173 surface
174 .size(ICON_CONTAINER_SIZE)
175 .flex()
176 .items_center()
177 .justify_center()
178 .text_size(ICON_SIZE)
179 .child(icon),
180 )
181 .into_any_element()
182 }
183 EmptyStateVisual::Custom(element) => div()
184 .id((self.id.clone(), "visual"))
185 .test_support()
186 .mb(px(20.))
187 .flex()
188 .items_center()
189 .justify_center()
190 .child(element)
191 .into_any_element(),
192 });
193
194 let title = wrapped_text(
195 self.title.clone(),
196 self.title_role.style(TypographyEmphasis::Baseline),
197 1.,
198 on_surface,
199 window,
200 cx,
201 )
202 .w_full()
203 .text_center()
204 .id((self.id.clone(), "title"))
205 .test_support()
206 .role(Role::Label)
207 .aria_label(self.title);
208
209 let supporting_slot = self.supporting.map(|slot| match slot {
210 SupportingSlot::Text(text) => div()
211 .w_full()
212 .mt(px(8.))
213 .child(
214 wrapped_text(
215 text.clone(),
216 TypeRole::BodyMedium.style(TypographyEmphasis::Baseline),
217 1.,
218 on_surface_variant,
219 window,
220 cx,
221 )
222 .w_full()
223 .text_center()
224 .id((self.id.clone(), "supporting"))
225 .test_support()
226 .role(Role::Label)
227 .aria_label(text),
228 )
229 .into_any_element(),
230 SupportingSlot::Custom(element) => div()
231 .id((self.id.clone(), "supporting"))
232 .test_support()
233 .w_full()
234 .mt(px(8.))
235 .flex()
236 .justify_center()
237 .child(element)
238 .into_any_element(),
239 });
240
241 let has_actions = self.primary_action.is_some() || self.secondary_action.is_some();
242 let actions_slot = has_actions.then(|| {
243 div()
244 .id((self.id.clone(), "actions"))
245 .test_support()
246 .mt(ACTIONS_TOP_MARGIN)
247 .flex()
248 .flex_row()
249 .flex_wrap()
250 .items_center()
251 .justify_center()
252 .gap(ACTION_GAP)
253 .when(direction == Direction::Rtl, Styled::flex_row_reverse)
254 .children(self.primary_action)
255 .children(self.secondary_action)
256 });
257
258 div()
259 .id(self.id.clone())
260 .test_support()
261 .w_full()
262 .flex()
263 .flex_col()
264 .items_center()
265 .justify_center()
266 .px(px(24.))
267 .py(px(32.))
268 .child(
269 div()
270 .id((self.id, "content"))
271 .test_support()
272 .w_full()
273 .max_w(self.max_width)
274 .flex()
275 .flex_col()
276 .items_center()
277 .children(visual_slot)
278 .child(title)
279 .children(supporting_slot)
280 .children(actions_slot),
281 )
282 }
283}
284
285#[cfg(test)]
286mod tests {
287 use super::*;
288
289 #[test]
290 fn empty_state_max_width_clamps_invalid_values_to_default() {
291 assert_eq!(resolve_max_width(px(420.)), px(420.));
292 assert_eq!(resolve_max_width(px(0.)), DEFAULT_MAX_WIDTH);
293 assert_eq!(resolve_max_width(px(-120.)), DEFAULT_MAX_WIDTH);
294 assert_eq!(resolve_max_width(px(f32::NAN)), DEFAULT_MAX_WIDTH);
295 assert_eq!(resolve_max_width(px(f32::INFINITY)), DEFAULT_MAX_WIDTH);
296 }
297}