Skip to repository content236 lines · 8.7 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T04:45:40.262Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
list_header.rs
1use std::sync::Arc;
2
3use crate::{Disclosure, IconButtonShape, prelude::*};
4use component::{Component, ComponentScope, example_group_with_title, single_example};
5use gpui::{AnyElement, ClickEvent};
6use theme::UiDensity;
7
8#[derive(IntoElement, RegisterComponent)]
9pub struct ListHeader {
10 /// The label of the header.
11 label: SharedString,
12 /// A slot for content that appears before the label, like an icon or avatar.
13 start_slot: Option<AnyElement>,
14 /// A slot for content that appears after the label, usually on the other side of the header.
15 /// This might be a button, a disclosure arrow, a face pile, etc.
16 end_slot: Option<AnyElement>,
17 /// A slot for content that appears on hover after the label
18 /// It will obscure the `end_slot` when visible.
19 end_hover_slot: Option<AnyElement>,
20 toggle: Option<bool>,
21 disclosure_shape: Option<IconButtonShape>,
22 on_toggle: Option<Arc<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
23 inset: bool,
24 selected: bool,
25 height: Option<DefiniteLength>,
26}
27
28impl ListHeader {
29 pub fn new(label: impl Into<SharedString>) -> Self {
30 Self {
31 label: label.into(),
32 start_slot: None,
33 end_slot: None,
34 end_hover_slot: None,
35 inset: false,
36 toggle: None,
37 disclosure_shape: None,
38 on_toggle: None,
39 selected: false,
40 height: None,
41 }
42 }
43
44 pub fn height(mut self, height: impl Into<DefiniteLength>) -> Self {
45 self.height = Some(height.into());
46 self
47 }
48
49 pub fn toggle(mut self, toggle: impl Into<Option<bool>>) -> Self {
50 self.toggle = toggle.into();
51 self
52 }
53
54 /// Sets the shape of the disclosure button.
55 pub fn disclosure_shape(mut self, shape: IconButtonShape) -> Self {
56 self.disclosure_shape = Some(shape);
57 self
58 }
59
60 pub fn on_toggle(
61 mut self,
62 on_toggle: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
63 ) -> Self {
64 self.on_toggle = Some(Arc::new(on_toggle));
65 self
66 }
67
68 pub fn start_slot<E: IntoElement>(mut self, start_slot: impl Into<Option<E>>) -> Self {
69 self.start_slot = start_slot.into().map(IntoElement::into_any_element);
70 self
71 }
72
73 pub fn end_slot<E: IntoElement>(mut self, end_slot: impl Into<Option<E>>) -> Self {
74 self.end_slot = end_slot.into().map(IntoElement::into_any_element);
75 self
76 }
77
78 pub fn end_hover_slot<E: IntoElement>(mut self, end_hover_slot: impl Into<Option<E>>) -> Self {
79 self.end_hover_slot = end_hover_slot.into().map(IntoElement::into_any_element);
80 self
81 }
82
83 pub fn inset(mut self, inset: bool) -> Self {
84 self.inset = inset;
85 self
86 }
87}
88
89impl Toggleable for ListHeader {
90 fn toggle_state(mut self, selected: bool) -> Self {
91 self.selected = selected;
92 self
93 }
94}
95
96impl RenderOnce for ListHeader {
97 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
98 let ui_density = theme::theme_settings(cx).ui_density(cx);
99
100 h_flex()
101 .id(self.label.clone())
102 .w_full()
103 .relative()
104 .group("list_header")
105 .child(
106 div()
107 .map(|this| match self.height {
108 Some(height) => this.h(height),
109 None => match ui_density {
110 UiDensity::Comfortable => this.h_5(),
111 _ => this.h_7(),
112 },
113 })
114 .when(self.inset, |this| this.px_2())
115 .when(self.selected, |this| {
116 this.bg(cx.theme().colors().ghost_element_selected)
117 })
118 .flex()
119 .flex_1()
120 .items_center()
121 .justify_between()
122 .w_full()
123 .gap(DynamicSpacing::Base04.rems(cx))
124 .child(
125 h_flex()
126 .gap(DynamicSpacing::Base04.rems(cx))
127 .children(self.toggle.map(|is_open| {
128 Disclosure::new("toggle", is_open)
129 .when_some(self.disclosure_shape, |this, shape| {
130 this.shape(shape)
131 })
132 .on_toggle_expanded(self.on_toggle.clone())
133 }))
134 .child(
135 div()
136 .id("label_container")
137 .flex()
138 .gap(DynamicSpacing::Base04.rems(cx))
139 .items_center()
140 .children(self.start_slot)
141 .child(Label::new(self.label.clone()).color(Color::Muted))
142 .when_some(self.on_toggle, |this, on_toggle| {
143 this.on_click(move |event, window, cx| {
144 on_toggle(event, window, cx)
145 })
146 }),
147 ),
148 )
149 .child(h_flex().children(self.end_slot))
150 .when_some(self.end_hover_slot, |this, end_hover_slot| {
151 this.child(
152 div()
153 .absolute()
154 .right_0()
155 .visible_on_hover("list_header")
156 .child(end_hover_slot),
157 )
158 }),
159 )
160 }
161}
162
163impl Component for ListHeader {
164 fn scope() -> ComponentScope {
165 ComponentScope::DataDisplay
166 }
167
168 fn description() -> &'static str {
169 "A header component for lists with support for icons, actions, \
170 and collapsible sections."
171 }
172
173 fn preview(_window: &mut Window, _cx: &mut App) -> AnyElement {
174 v_flex()
175 .gap_6()
176 .children(vec![
177 example_group_with_title(
178 "Basic Headers",
179 vec![
180 single_example(
181 "Simple",
182 ListHeader::new("Section Header").into_any_element(),
183 ),
184 single_example(
185 "With Icon",
186 ListHeader::new("Files")
187 .start_slot(Icon::new(IconName::File))
188 .into_any_element(),
189 ),
190 single_example(
191 "With End Slot",
192 ListHeader::new("Recent")
193 .end_slot(Label::new("5").color(Color::Muted))
194 .into_any_element(),
195 ),
196 ],
197 ),
198 example_group_with_title(
199 "Collapsible Headers",
200 vec![
201 single_example(
202 "Expanded",
203 ListHeader::new("Expanded Section")
204 .toggle(Some(true))
205 .into_any_element(),
206 ),
207 single_example(
208 "Collapsed",
209 ListHeader::new("Collapsed Section")
210 .toggle(Some(false))
211 .into_any_element(),
212 ),
213 ],
214 ),
215 example_group_with_title(
216 "States",
217 vec![
218 single_example(
219 "Selected",
220 ListHeader::new("Selected Header")
221 .toggle_state(true)
222 .into_any_element(),
223 ),
224 single_example(
225 "Inset",
226 ListHeader::new("Inset Header")
227 .inset(true)
228 .into_any_element(),
229 ),
230 ],
231 ),
232 ])
233 .into_any_element()
234 }
235}
236