1use crate::{h_stack, prelude::*, Disclosure, Label};
2use gpui::{AnyElement, ClickEvent, Div};
3
4#[derive(IntoElement)]
5pub struct ListHeader {
6 /// The label of the header.
7 label: SharedString,
8 /// A slot for content that appears before the label, like an icon or avatar.
9 start_slot: Option<AnyElement>,
10 /// A slot for content that appears after the label, usually on the other side of the header.
11 /// This might be a button, a disclosure arrow, a face pile, etc.
12 end_slot: Option<AnyElement>,
13 /// A slot for content that appears on hover after the label
14 /// It will obscure the `end_slot` when visible.
15 end_hover_slot: Option<AnyElement>,
16 toggle: Option<bool>,
17 on_toggle: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
18 inset: bool,
19 selected: bool,
20}
21
22impl ListHeader {
23 pub fn new(label: impl Into<SharedString>) -> Self {
24 Self {
25 label: label.into(),
26 start_slot: None,
27 end_slot: None,
28 end_hover_slot: None,
29 inset: false,
30 toggle: None,
31 on_toggle: None,
32 selected: false,
33 }
34 }
35
36 pub fn toggle(mut self, toggle: impl Into<Option<bool>>) -> Self {
37 self.toggle = toggle.into();
38 self
39 }
40
41 pub fn on_toggle(
42 mut self,
43 on_toggle: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
44 ) -> Self {
45 self.on_toggle = Some(Box::new(on_toggle));
46 self
47 }
48
49 pub fn start_slot<E: IntoElement>(mut self, start_slot: impl Into<Option<E>>) -> Self {
50 self.start_slot = start_slot.into().map(IntoElement::into_any_element);
51 self
52 }
53
54 pub fn end_slot<E: IntoElement>(mut self, end_slot: impl Into<Option<E>>) -> Self {
55 self.end_slot = end_slot.into().map(IntoElement::into_any_element);
56 self
57 }
58
59 pub fn end_hover_slot<E: IntoElement>(mut self, end_hover_slot: impl Into<Option<E>>) -> Self {
60 self.end_hover_slot = end_hover_slot.into().map(IntoElement::into_any_element);
61 self
62 }
63
64 pub fn inset(mut self, inset: bool) -> Self {
65 self.inset = inset;
66 self
67 }
68}
69
70impl Selectable for ListHeader {
71 fn selected(mut self, selected: bool) -> Self {
72 self.selected = selected;
73 self
74 }
75}
76
77impl RenderOnce for ListHeader {
78 type Rendered = Div;
79
80 fn render(self, cx: &mut WindowContext) -> Self::Rendered {
81 h_stack().w_full().relative().group("list_header").child(
82 div()
83 .h_7()
84 .when(self.inset, |this| this.px_2())
85 .when(self.selected, |this| {
86 this.bg(cx.theme().colors().ghost_element_selected)
87 })
88 .flex()
89 .flex_1()
90 .items_center()
91 .justify_between()
92 .w_full()
93 .gap_1()
94 .child(
95 h_stack()
96 .gap_1()
97 .children(
98 self.toggle
99 .map(|is_open| Disclosure::new(is_open).on_toggle(self.on_toggle)),
100 )
101 .child(
102 div()
103 .flex()
104 .gap_1()
105 .items_center()
106 .children(self.start_slot)
107 .child(Label::new(self.label.clone()).color(Color::Muted)),
108 ),
109 )
110 .child(h_stack().children(self.end_slot))
111 .when_some(self.end_hover_slot, |this, end_hover_slot| {
112 this.child(
113 div()
114 .absolute()
115 .right_0()
116 .visible_on_hover("list_header")
117 .child(end_hover_slot),
118 )
119 }),
120 )
121 }
122}