Skip to main content

kitchen_ui/widgets/
section.rs

1//! The titled section.
2
3use gpui_kit::component::{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/// A titled block of a panel: a short caps title (with something at its right, if given), then
12/// its rows (added as children), [`theme::SM`] apart. Boxed ([`Section::boxed`]), the rows sit in
13/// a bordered box.
14#[derive(IntoElement)]
15pub struct Section {
16    title: SharedString,
17    extra: Option<AnyElement>,
18    boxed: bool,
19    children: Vec<AnyElement>,
20}
21
22impl Section {
23    /// A section titled `title` (shown in capitals: a short label, designparser
24    /// `all-caps-fatigue`).
25    pub fn new(title: impl Into<SharedString>) -> Self {
26        Self { title: title.into(), extra: None, boxed: false, children: Vec::new() }
27    }
28
29    /// Something at the right of the title (a count, a button).
30    pub fn extra(mut self, e: impl IntoElement) -> Self {
31        self.extra = Some(e.into_any_element());
32        self
33    }
34
35    /// The rows in a bordered box (dsp-kitchen's sections): padding [`theme::MD`], 1.5 × the row
36    /// gap (designparser `padding-gap-ratio`).
37    pub fn boxed(mut self) -> Self {
38        self.boxed = true;
39        self
40    }
41}
42
43impl ParentElement for Section {
44    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
45        self.children.extend(elements);
46    }
47}
48
49impl RenderOnce for Section {
50    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
51        let Self { title, extra, boxed, children } = self;
52        let head = h_flex()
53            .w_full()
54            .min_h(px(20.))
55            .gap(px(theme::SM))
56            .child(
57                div()
58                    .flex_1()
59                    .min_w_0()
60                    .truncate()
61                    .text_xs()
62                    .font_weight(FontWeight::SEMIBOLD)
63                    .text_color(color::text_muted())
64                    .child(title.to_uppercase()),
65            )
66            .children(extra);
67        let body = v_flex().w_full().min_w_0().gap(px(theme::SM)).children(children).when(boxed, |d| {
68            d.p(px(theme::MD))
69                .rounded(px(theme::RADIUS_LG))
70                .border_1()
71                .border_color(color::border())
72                .bg(color::raised())
73        });
74        v_flex().w_full().min_w_0().gap(px(theme::SM)).child(head).child(body)
75    }
76}