list_sub_header.rs

 1use crate::prelude::*;
 2use crate::{Icon, IconName, IconSize, Label, h_flex};
 3
 4#[derive(IntoElement)]
 5pub struct ListSubHeader {
 6    label: SharedString,
 7    start_slot: Option<IconName>,
 8    inset: bool,
 9    selected: bool,
10}
11
12impl ListSubHeader {
13    pub fn new(label: impl Into<SharedString>) -> Self {
14        Self {
15            label: label.into(),
16            start_slot: None,
17            inset: false,
18            selected: false,
19        }
20    }
21
22    pub fn left_icon(mut self, left_icon: Option<IconName>) -> Self {
23        self.start_slot = left_icon;
24        self
25    }
26
27    pub fn inset(mut self, inset: bool) -> Self {
28        self.inset = inset;
29        self
30    }
31}
32
33impl Toggleable for ListSubHeader {
34    fn toggle_state(mut self, selected: bool) -> Self {
35        self.selected = selected;
36        self
37    }
38}
39
40impl RenderOnce for ListSubHeader {
41    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
42        h_flex()
43            .flex_1()
44            .w_full()
45            .relative()
46            .pb(DynamicSpacing::Base04.rems(cx))
47            .px(DynamicSpacing::Base02.rems(cx))
48            .child(
49                div()
50                    .h_5()
51                    .when(self.inset, |this| this.px_2())
52                    .when(self.selected, |this| {
53                        this.bg(cx.theme().colors().ghost_element_selected)
54                    })
55                    .flex()
56                    .flex_1()
57                    .w_full()
58                    .gap_1()
59                    .items_center()
60                    .justify_between()
61                    .child(
62                        div()
63                            .flex()
64                            .gap_1()
65                            .items_center()
66                            .children(
67                                self.start_slot.map(|i| {
68                                    Icon::new(i).color(Color::Muted).size(IconSize::Small)
69                                }),
70                            )
71                            .child(
72                                Label::new(self.label.clone())
73                                    .color(Color::Muted)
74                                    .size(LabelSize::Small),
75                            ),
76                    ),
77            )
78    }
79}