header_row.rs

 1use component::{example_group_with_title, single_example};
 2use gpui::{AnyElement, App, IntoElement, RenderOnce, Window};
 3use ui::{Label, prelude::*};
 4
 5#[derive(IntoElement, RegisterComponent)]
 6pub struct HeaderRow {
 7    label: SharedString,
 8    end_slot: Option<AnyElement>,
 9}
10
11impl HeaderRow {
12    pub fn new(label: impl Into<SharedString>) -> Self {
13        Self {
14            label: label.into(),
15            end_slot: None,
16        }
17    }
18
19    pub fn end_slot(mut self, slot: impl IntoElement) -> Self {
20        self.end_slot = Some(slot.into_any_element());
21        self
22    }
23}
24
25impl RenderOnce for HeaderRow {
26    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
27        h_flex()
28            .h(px(32.))
29            .w_full()
30            .px_5()
31            .justify_between()
32            .child(Label::new(self.label))
33            .when_some(self.end_slot, |this, slot| this.child(slot))
34    }
35}
36
37impl Component for HeaderRow {
38    fn scope() -> ComponentScope {
39        ComponentScope::Layout
40    }
41
42    fn sort_name() -> &'static str {
43        "RowHeader"
44    }
45
46    fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
47        let examples = example_group_with_title(
48            "HeaderRow Examples",
49            vec![
50                single_example(
51                    "Simple Header",
52                    HeaderRow::new("Pick a Theme").into_any_element(),
53                ),
54                single_example(
55                    "Header with Button",
56                    HeaderRow::new("Pick a Theme")
57                        .end_slot(
58                            Button::new("more_themes", "More Themes")
59                                .style(ButtonStyle::Subtle)
60                                .color(Color::Muted),
61                        )
62                        .into_any_element(),
63                ),
64                single_example(
65                    "Header with Icon Button",
66                    HeaderRow::new("Settings")
67                        .end_slot(
68                            IconButton::new("refresh", IconName::RotateCw)
69                                .style(ButtonStyle::Subtle),
70                        )
71                        .into_any_element(),
72                ),
73            ],
74        );
75
76        Some(v_flex().p_4().gap_4().child(examples).into_any_element())
77    }
78}