list_sub_header.rs

 1use crate::prelude::*;
 2use crate::{h_flex, Icon, IconName, IconSize, Label};
 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 Selectable for ListSubHeader {
34    fn selected(mut self, selected: bool) -> Self {
35        self.selected = selected;
36        self
37    }
38}
39
40impl RenderOnce for ListSubHeader {
41    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
42        h_flex().flex_1().w_full().relative().py_1().child(
43            div()
44                .h_6()
45                .when(self.inset, |this| this.px_2())
46                .when(self.selected, |this| {
47                    this.bg(cx.theme().colors().ghost_element_selected)
48                })
49                .flex()
50                .flex_1()
51                .w_full()
52                .gap_1()
53                .items_center()
54                .justify_between()
55                .child(
56                    div()
57                        .flex()
58                        .gap_1()
59                        .items_center()
60                        .children(
61                            self.start_slot
62                                .map(|i| Icon::new(i).color(Color::Muted).size(IconSize::Small)),
63                        )
64                        .child(Label::new(self.label.clone()).color(Color::Muted)),
65                ),
66        )
67    }
68}