1use std::sync::Arc;
2
3use component::{Component, ComponentScope, example_group_with_title, single_example};
4use gpui::{AnyElement, AnyView, ClickEvent, MouseButton, MouseDownEvent, Pixels, px};
5use smallvec::SmallVec;
6
7use crate::{Disclosure, prelude::*};
8
9#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
10pub enum ListItemSpacing {
11 #[default]
12 Dense,
13 ExtraDense,
14 Sparse,
15}
16
17#[derive(Default)]
18enum EndSlotVisibility {
19 #[default]
20 Always,
21 OnHover,
22 SwapOnHover(AnyElement),
23}
24
25#[derive(IntoElement, RegisterComponent)]
26pub struct ListItem {
27 id: ElementId,
28 group_name: Option<SharedString>,
29 disabled: bool,
30 selected: bool,
31 spacing: ListItemSpacing,
32 indent_level: usize,
33 indent_step_size: Pixels,
34 start_slot: Option<AnyElement>,
36 end_slot: Option<AnyElement>,
39 end_slot_visibility: EndSlotVisibility,
40 toggle: Option<bool>,
41 inset: bool,
42 on_click: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
43 on_hover: Option<Box<dyn Fn(&bool, &mut Window, &mut App) + 'static>>,
44 on_toggle: Option<Arc<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
45 tooltip: Option<Box<dyn Fn(&mut Window, &mut App) -> AnyView + 'static>>,
46 on_secondary_mouse_down: Option<Box<dyn Fn(&MouseDownEvent, &mut Window, &mut App) + 'static>>,
47 children: SmallVec<[AnyElement; 2]>,
48 selectable: bool,
49 always_show_disclosure_icon: bool,
50 outlined: bool,
51 rounded: bool,
52 overflow_x: bool,
53 focused: Option<bool>,
54 docked_right: bool,
55 height: Option<DefiniteLength>,
56}
57
58impl ListItem {
59 pub fn new(id: impl Into<ElementId>) -> Self {
60 Self {
61 id: id.into(),
62 group_name: None,
63 disabled: false,
64 selected: false,
65 spacing: ListItemSpacing::Dense,
66 indent_level: 0,
67 indent_step_size: px(12.),
68 start_slot: None,
69 end_slot: None,
70 end_slot_visibility: EndSlotVisibility::default(),
71 toggle: None,
72 inset: false,
73 on_click: None,
74 on_secondary_mouse_down: None,
75 on_toggle: None,
76 on_hover: None,
77 tooltip: None,
78 children: SmallVec::new(),
79 selectable: true,
80 always_show_disclosure_icon: false,
81 outlined: false,
82 rounded: false,
83 overflow_x: false,
84 focused: None,
85 docked_right: false,
86 height: None,
87 }
88 }
89
90 pub fn group_name(mut self, group_name: impl Into<SharedString>) -> Self {
91 self.group_name = Some(group_name.into());
92 self
93 }
94
95 pub fn spacing(mut self, spacing: ListItemSpacing) -> Self {
96 self.spacing = spacing;
97 self
98 }
99
100 pub fn selectable(mut self, has_hover: bool) -> Self {
101 self.selectable = has_hover;
102 self
103 }
104
105 pub fn always_show_disclosure_icon(mut self, show: bool) -> Self {
106 self.always_show_disclosure_icon = show;
107 self
108 }
109
110 pub fn on_click(
111 mut self,
112 handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
113 ) -> Self {
114 self.on_click = Some(Box::new(handler));
115 self
116 }
117
118 pub fn on_hover(mut self, handler: impl Fn(&bool, &mut Window, &mut App) + 'static) -> Self {
119 self.on_hover = Some(Box::new(handler));
120 self
121 }
122
123 pub fn on_secondary_mouse_down(
124 mut self,
125 handler: impl Fn(&MouseDownEvent, &mut Window, &mut App) + 'static,
126 ) -> Self {
127 self.on_secondary_mouse_down = Some(Box::new(handler));
128 self
129 }
130
131 pub fn tooltip(mut self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static) -> Self {
132 self.tooltip = Some(Box::new(tooltip));
133 self
134 }
135
136 pub fn inset(mut self, inset: bool) -> Self {
137 self.inset = inset;
138 self
139 }
140
141 pub fn indent_level(mut self, indent_level: usize) -> Self {
142 self.indent_level = indent_level;
143 self
144 }
145
146 pub fn indent_step_size(mut self, indent_step_size: Pixels) -> Self {
147 self.indent_step_size = indent_step_size;
148 self
149 }
150
151 pub fn toggle(mut self, toggle: impl Into<Option<bool>>) -> Self {
152 self.toggle = toggle.into();
153 self
154 }
155
156 pub fn on_toggle(
157 mut self,
158 on_toggle: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
159 ) -> Self {
160 self.on_toggle = Some(Arc::new(on_toggle));
161 self
162 }
163
164 pub fn start_slot<E: IntoElement>(mut self, start_slot: impl Into<Option<E>>) -> Self {
165 self.start_slot = start_slot.into().map(IntoElement::into_any_element);
166 self
167 }
168
169 pub fn end_slot<E: IntoElement>(mut self, end_slot: impl Into<Option<E>>) -> Self {
170 self.end_slot = end_slot.into().map(IntoElement::into_any_element);
171 self
172 }
173
174 pub fn end_slot_on_hover<E: IntoElement>(mut self, end_slot_on_hover: E) -> Self {
175 self.end_slot_visibility =
176 EndSlotVisibility::SwapOnHover(end_slot_on_hover.into_any_element());
177 self
178 }
179
180 pub fn show_end_slot_on_hover(mut self) -> Self {
181 self.end_slot_visibility = EndSlotVisibility::OnHover;
182 self
183 }
184
185 pub fn outlined(mut self) -> Self {
186 self.outlined = true;
187 self
188 }
189
190 pub fn rounded(mut self) -> Self {
191 self.rounded = true;
192 self
193 }
194
195 pub fn overflow_x(mut self) -> Self {
196 self.overflow_x = true;
197 self
198 }
199
200 pub fn focused(mut self, focused: bool) -> Self {
201 self.focused = Some(focused);
202 self
203 }
204
205 pub fn docked_right(mut self, docked_right: bool) -> Self {
206 self.docked_right = docked_right;
207 self
208 }
209
210 pub fn height(mut self, height: impl Into<DefiniteLength>) -> Self {
211 self.height = Some(height.into());
212 self
213 }
214}
215
216impl Disableable for ListItem {
217 fn disabled(mut self, disabled: bool) -> Self {
218 self.disabled = disabled;
219 self
220 }
221}
222
223impl Toggleable for ListItem {
224 fn toggle_state(mut self, selected: bool) -> Self {
225 self.selected = selected;
226 self
227 }
228}
229
230impl ParentElement for ListItem {
231 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
232 self.children.extend(elements)
233 }
234}
235
236impl RenderOnce for ListItem {
237 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
238 h_flex()
239 .id(self.id)
240 .when_some(self.group_name, |this, group| this.group(group))
241 .w_full()
242 .when_some(self.height, |this, height| this.h(height))
243 .relative()
244 .when(self.inset, |this| {
246 this.ml(self.indent_level as f32 * self.indent_step_size)
247 .px(DynamicSpacing::Base04.rems(cx))
248 })
249 .when(!self.inset, |this| {
250 this.when_some(self.focused, |this, focused| {
251 if focused && !self.disabled {
252 this.border_1()
253 .when(self.docked_right, |this| this.border_r_2())
254 .border_color(semantic::border_focused(cx))
255 } else {
256 this.border_1()
257 }
258 })
259 .when(self.selectable && !self.disabled, |this| {
260 this.hover(|style| style.bg(semantic::hover_bg(cx)))
261 .active(|style| style.bg(semantic::active_bg(cx)))
262 .when(self.outlined, |this| this.rounded_sm())
263 .when(self.selected, |this| this.bg(semantic::active_bg(cx)))
264 })
265 })
266 .when(self.rounded, |this| this.rounded_sm())
267 .when_some(self.on_hover, |this, on_hover| this.on_hover(on_hover))
268 .child(
269 h_flex()
270 .id("inner_list_item")
271 .group("list_item")
272 .w_full()
273 .relative()
274 .gap_1()
275 .px(DynamicSpacing::Base06.rems(cx))
276 .map(|this| match self.spacing {
277 ListItemSpacing::Dense => this,
278 ListItemSpacing::ExtraDense => this.py_neg_px(),
279 ListItemSpacing::Sparse => this.py_1(),
280 })
281 .when(self.inset, |this| {
282 this.when_some(self.focused, |this, focused| {
283 if focused && !self.disabled {
284 this.border_1().border_color(semantic::border_focused(cx))
285 } else {
286 this.border_1()
287 }
288 })
289 .when(self.selectable && !self.disabled, |this| {
290 this.hover(|style| style.bg(semantic::hover_bg(cx)))
291 .active(|style| style.bg(semantic::active_bg(cx)))
292 .when(self.selected, |this| this.bg(semantic::active_bg(cx)))
293 })
294 })
295 .when_some(
296 self.on_click.filter(|_| !self.disabled),
297 |this, on_click| this.cursor_pointer().on_click(on_click),
298 )
299 .when(self.outlined, |this| {
300 this.border_1()
301 .border_color(semantic::border(cx))
302 .rounded_sm()
303 .overflow_hidden()
304 })
305 .when_some(self.on_secondary_mouse_down, |this, on_mouse_down| {
306 this.on_mouse_down(MouseButton::Right, move |event, window, cx| {
307 (on_mouse_down)(event, window, cx)
308 })
309 })
310 .when_some(self.tooltip, |this, tooltip| this.tooltip(tooltip))
311 .map(|this| {
312 if self.inset {
313 this.rounded_sm()
314 } else {
315 this.ml(self.indent_level as f32 * self.indent_step_size)
317 }
318 })
319 .children(self.toggle.map(|is_open| {
320 div()
321 .flex()
322 .absolute()
323 .left(rems(-1.))
324 .when(is_open && !self.always_show_disclosure_icon, |this| {
325 this.visible_on_hover("")
326 })
327 .child(
328 Disclosure::new("toggle", is_open)
329 .on_toggle_expanded(self.on_toggle),
330 )
331 }))
332 .child(
333 h_flex()
334 .flex_grow()
335 .flex_shrink_0()
336 .flex_basis(relative(0.25))
337 .gap(DynamicSpacing::Base06.rems(cx))
338 .map(|list_content| {
339 if self.overflow_x {
340 list_content
341 } else {
342 list_content.overflow_hidden()
343 }
344 })
345 .children(self.start_slot)
346 .children(self.children),
347 )
348 .when(self.end_slot.is_some(), |this| this.justify_between())
349 .when_some(self.end_slot, |this, end_slot| {
350 this.child(match self.end_slot_visibility {
351 EndSlotVisibility::Always => {
352 h_flex().flex_shrink().overflow_hidden().child(end_slot)
353 }
354 EndSlotVisibility::OnHover => h_flex()
355 .flex_shrink()
356 .overflow_hidden()
357 .visible_on_hover("list_item")
358 .child(end_slot),
359 EndSlotVisibility::SwapOnHover(hover_slot) => h_flex()
360 .relative()
361 .flex_shrink()
362 .child(h_flex().visible_on_hover("list_item").child(hover_slot))
363 .child(
364 h_flex()
365 .absolute()
366 .inset_0()
367 .justify_end()
368 .overflow_hidden()
369 .group_hover("list_item", |this| this.invisible())
370 .child(end_slot),
371 ),
372 })
373 }),
374 )
375 }
376}
377
378impl Component for ListItem {
379 fn scope() -> ComponentScope {
380 ComponentScope::DataDisplay
381 }
382
383 fn description() -> Option<&'static str> {
384 Some(
385 "A flexible list item component with support for icons, actions, disclosure toggles, and hierarchical display.",
386 )
387 }
388
389 fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
390 Some(
391 v_flex()
392 .gap_6()
393 .children(vec![
394 example_group_with_title(
395 "Basic List Items",
396 vec![
397 single_example(
398 "Simple",
399 ListItem::new("simple")
400 .child(Label::new("Simple list item"))
401 .into_any_element(),
402 ),
403 single_example(
404 "With Icon",
405 ListItem::new("with_icon")
406 .start_slot(Icon::new(IconName::File))
407 .child(Label::new("List item with icon"))
408 .into_any_element(),
409 ),
410 single_example(
411 "Selected",
412 ListItem::new("selected")
413 .toggle_state(true)
414 .start_slot(Icon::new(IconName::Check))
415 .child(Label::new("Selected item"))
416 .into_any_element(),
417 ),
418 ],
419 ),
420 example_group_with_title(
421 "List Item Spacing",
422 vec![
423 single_example(
424 "Dense",
425 ListItem::new("dense")
426 .spacing(ListItemSpacing::Dense)
427 .child(Label::new("Dense spacing"))
428 .into_any_element(),
429 ),
430 single_example(
431 "Extra Dense",
432 ListItem::new("extra_dense")
433 .spacing(ListItemSpacing::ExtraDense)
434 .child(Label::new("Extra dense spacing"))
435 .into_any_element(),
436 ),
437 single_example(
438 "Sparse",
439 ListItem::new("sparse")
440 .spacing(ListItemSpacing::Sparse)
441 .child(Label::new("Sparse spacing"))
442 .into_any_element(),
443 ),
444 ],
445 ),
446 example_group_with_title(
447 "With Slots",
448 vec![
449 single_example(
450 "End Slot",
451 ListItem::new("end_slot")
452 .child(Label::new("Item with end slot"))
453 .end_slot(Icon::new(IconName::ChevronRight))
454 .into_any_element(),
455 ),
456 single_example(
457 "With Toggle",
458 ListItem::new("with_toggle")
459 .toggle(Some(true))
460 .child(Label::new("Expandable item"))
461 .into_any_element(),
462 ),
463 ],
464 ),
465 example_group_with_title(
466 "States",
467 vec![
468 single_example(
469 "Disabled",
470 ListItem::new("disabled")
471 .disabled(true)
472 .child(Label::new("Disabled item"))
473 .into_any_element(),
474 ),
475 single_example(
476 "Non-selectable",
477 ListItem::new("non_selectable")
478 .selectable(false)
479 .child(Label::new("Non-selectable item"))
480 .into_any_element(),
481 ),
482 ],
483 ),
484 ])
485 .into_any_element(),
486 )
487 }
488}