1use crate::{h_flex, prelude::*, Disclosure, Label};
2use gpui::{AnyElement, ClickEvent};
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 fn render(self, cx: &mut WindowContext) -> impl IntoElement {
79 h_flex()
80 .id(self.label.clone())
81 .w_full()
82 .relative()
83 .group("list_header")
84 .child(
85 div()
86 .h_7()
87 .when(self.inset, |this| this.px_2())
88 .when(self.selected, |this| {
89 this.bg(cx.theme().colors().ghost_element_selected)
90 })
91 .flex()
92 .flex_1()
93 .items_center()
94 .justify_between()
95 .w_full()
96 .gap_1()
97 .child(
98 h_flex()
99 .gap_1()
100 .children(self.toggle.map(|is_open| {
101 Disclosure::new("toggle", is_open).on_toggle(self.on_toggle)
102 }))
103 .child(
104 div()
105 .flex()
106 .gap_1()
107 .items_center()
108 .children(self.start_slot)
109 .child(Label::new(self.label.clone()).color(Color::Muted)),
110 ),
111 )
112 .child(h_flex().children(self.end_slot))
113 .when_some(self.end_hover_slot, |this, end_hover_slot| {
114 this.child(
115 div()
116 .absolute()
117 .right_0()
118 .visible_on_hover("list_header")
119 .child(end_hover_slot),
120 )
121 }),
122 )
123 }
124}