kitchen_ui/widgets/
section.rs1use 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#[derive(IntoElement)]
15pub struct Section {
16 title: SharedString,
17 extra: Option<AnyElement>,
18 boxed: bool,
19 children: Vec<AnyElement>,
20}
21
22impl Section {
23 pub fn new(title: impl Into<SharedString>) -> Self {
26 Self { title: title.into(), extra: None, boxed: false, children: Vec::new() }
27 }
28
29 pub fn extra(mut self, e: impl IntoElement) -> Self {
31 self.extra = Some(e.into_any_element());
32 self
33 }
34
35 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}