Skip to main content

kitchen_ui/widgets/
empty.rs

1//! The empty state.
2
3use gpui_kit::component::{Icon, h_flex, v_flex};
4use gpui_kit::prelude::FluentBuilder as _;
5use gpui_kit::{
6    AnyElement, App, FontWeight, IntoElement, ParentElement, RenderOnce, SharedString, Styled as _, Window, div, px,
7};
8
9use crate::theme::{self, color};
10
11/// Centred icon, title and a line of explanation, with actions under it (added as children).
12#[derive(IntoElement)]
13pub struct EmptyState {
14    icon: Icon,
15    title: SharedString,
16    description: SharedString,
17    actions: Vec<AnyElement>,
18}
19
20impl EmptyState {
21    /// An empty state with `icon`, `title` and `description` (one or two sentences: what is
22    /// missing and how to get it).
23    pub fn new(icon: impl Into<Icon>, title: impl Into<SharedString>, description: impl Into<SharedString>) -> Self {
24        Self { icon: icon.into(), title: title.into(), description: description.into(), actions: Vec::new() }
25    }
26}
27
28impl ParentElement for EmptyState {
29    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
30        self.actions.extend(elements);
31    }
32}
33
34impl RenderOnce for EmptyState {
35    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
36        let has_actions = !self.actions.is_empty();
37        v_flex()
38            .size_full()
39            .items_center()
40            .justify_center()
41            .gap(px(theme::SM))
42            .p(px(theme::LG))
43            .child(self.icon.size(px(32.)).text_color(color::text_muted()))
44            .child(div().pt(px(theme::XS)).text_lg().font_weight(FontWeight::SEMIBOLD).text_center().child(self.title))
45            // A readable line length; long words wrap instead of overflowing
46            .child(
47                div().max_w(px(460.)).text_sm().text_center().text_color(color::text_muted()).child(self.description),
48            )
49            .when(has_actions, |d| {
50                d.child(
51                    h_flex().pt(px(theme::SM)).gap(px(theme::SM)).flex_wrap().justify_center().children(self.actions),
52                )
53            })
54    }
55}