1use std::marker::PhantomData;
2
3use gpui2::{div, relative, Div};
4
5use crate::settings::user_settings;
6use crate::{
7 h_stack, v_stack, Avatar, ClickHandler, Icon, IconColor, IconElement, IconSize, Label,
8 LabelColor,
9};
10use crate::{prelude::*, Button};
11
12#[derive(Clone, Copy, Default, Debug, PartialEq)]
13pub enum ListItemVariant {
14 /// The list item extends to the far left and right of the list.
15 FullWidth,
16 #[default]
17 Inset,
18}
19
20#[derive(Element)]
21pub struct ListHeader<S: 'static + Send + Sync> {
22 state_type: PhantomData<S>,
23 label: SharedString,
24 left_icon: Option<Icon>,
25 variant: ListItemVariant,
26 state: InteractionState,
27 toggleable: Toggleable,
28}
29
30impl<S: 'static + Send + Sync> ListHeader<S> {
31 pub fn new(label: impl Into<SharedString>) -> Self {
32 Self {
33 state_type: PhantomData,
34 label: label.into(),
35 left_icon: None,
36 variant: ListItemVariant::default(),
37 state: InteractionState::default(),
38 toggleable: Toggleable::Toggleable(ToggleState::Toggled),
39 }
40 }
41
42 pub fn set_toggle(mut self, toggle: ToggleState) -> Self {
43 self.toggleable = toggle.into();
44 self
45 }
46
47 pub fn set_toggleable(mut self, toggleable: Toggleable) -> Self {
48 self.toggleable = toggleable;
49 self
50 }
51
52 pub fn set_left_icon(mut self, left_icon: Option<Icon>) -> Self {
53 self.left_icon = left_icon;
54 self
55 }
56
57 pub fn state(mut self, state: InteractionState) -> Self {
58 self.state = state;
59 self
60 }
61
62 fn disclosure_control(&self) -> Div<S> {
63 let is_toggleable = self.toggleable != Toggleable::NotToggleable;
64 let is_toggled = Toggleable::is_toggled(&self.toggleable);
65
66 match (is_toggleable, is_toggled) {
67 (false, _) => div(),
68 (_, true) => div().child(
69 IconElement::new(Icon::ChevronDown)
70 .color(IconColor::Muted)
71 .size(IconSize::Small),
72 ),
73 (_, false) => div().child(
74 IconElement::new(Icon::ChevronRight)
75 .color(IconColor::Muted)
76 .size(IconSize::Small),
77 ),
78 }
79 }
80
81 fn label_color(&self) -> LabelColor {
82 match self.state {
83 InteractionState::Disabled => LabelColor::Disabled,
84 _ => Default::default(),
85 }
86 }
87
88 fn icon_color(&self) -> IconColor {
89 match self.state {
90 InteractionState::Disabled => IconColor::Disabled,
91 _ => Default::default(),
92 }
93 }
94
95 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
96 let color = ThemeColor::new(cx);
97
98 let is_toggleable = self.toggleable != Toggleable::NotToggleable;
99 let is_toggled = self.toggleable.is_toggled();
100
101 let disclosure_control = self.disclosure_control();
102
103 h_stack()
104 .flex_1()
105 .w_full()
106 .bg(color.surface)
107 .when(self.state == InteractionState::Focused, |this| {
108 this.border().border_color(color.border_focused)
109 })
110 .relative()
111 .child(
112 div()
113 .h_5()
114 .when(self.variant == ListItemVariant::Inset, |this| this.px_2())
115 .flex()
116 .flex_1()
117 .w_full()
118 .gap_1()
119 .items_center()
120 .child(
121 div()
122 .flex()
123 .gap_1()
124 .items_center()
125 .children(self.left_icon.map(|i| {
126 IconElement::new(i)
127 .color(IconColor::Muted)
128 .size(IconSize::Small)
129 }))
130 .child(Label::new(self.label.clone()).color(LabelColor::Muted)),
131 )
132 .child(disclosure_control),
133 )
134 }
135}
136
137#[derive(Element)]
138pub struct ListSubHeader<S: 'static + Send + Sync> {
139 state_type: PhantomData<S>,
140 label: SharedString,
141 left_icon: Option<Icon>,
142 variant: ListItemVariant,
143}
144
145impl<S: 'static + Send + Sync> ListSubHeader<S> {
146 pub fn new(label: impl Into<SharedString>) -> Self {
147 Self {
148 state_type: PhantomData,
149 label: label.into(),
150 left_icon: None,
151 variant: ListItemVariant::default(),
152 }
153 }
154
155 pub fn left_icon(mut self, left_icon: Option<Icon>) -> Self {
156 self.left_icon = left_icon;
157 self
158 }
159
160 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
161 let color = ThemeColor::new(cx);
162
163 h_stack().flex_1().w_full().relative().py_1().child(
164 div()
165 .h_6()
166 .when(self.variant == ListItemVariant::Inset, |this| this.px_2())
167 .flex()
168 .flex_1()
169 .w_full()
170 .gap_1()
171 .items_center()
172 .justify_between()
173 .child(
174 div()
175 .flex()
176 .gap_1()
177 .items_center()
178 .children(self.left_icon.map(|i| {
179 IconElement::new(i)
180 .color(IconColor::Muted)
181 .size(IconSize::Small)
182 }))
183 .child(Label::new(self.label.clone()).color(LabelColor::Muted)),
184 ),
185 )
186 }
187}
188
189#[derive(Clone)]
190pub enum LeftContent {
191 Icon(Icon),
192 Avatar(SharedString),
193}
194
195#[derive(Default, PartialEq, Copy, Clone)]
196pub enum ListEntrySize {
197 #[default]
198 Small,
199 Medium,
200}
201
202#[derive(Element)]
203pub enum ListItem<S: 'static + Send + Sync> {
204 Entry(ListEntry<S>),
205 Details(ListDetailsEntry<S>),
206 Separator(ListSeparator<S>),
207 Header(ListSubHeader<S>),
208}
209
210impl<S: 'static + Send + Sync> From<ListEntry<S>> for ListItem<S> {
211 fn from(entry: ListEntry<S>) -> Self {
212 Self::Entry(entry)
213 }
214}
215
216impl<S: 'static + Send + Sync> From<ListDetailsEntry<S>> for ListItem<S> {
217 fn from(entry: ListDetailsEntry<S>) -> Self {
218 Self::Details(entry)
219 }
220}
221
222impl<S: 'static + Send + Sync> From<ListSeparator<S>> for ListItem<S> {
223 fn from(entry: ListSeparator<S>) -> Self {
224 Self::Separator(entry)
225 }
226}
227
228impl<S: 'static + Send + Sync> From<ListSubHeader<S>> for ListItem<S> {
229 fn from(entry: ListSubHeader<S>) -> Self {
230 Self::Header(entry)
231 }
232}
233
234impl<S: 'static + Send + Sync> ListItem<S> {
235 fn render(&mut self, view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
236 match self {
237 ListItem::Entry(entry) => div().child(entry.render(view, cx)),
238 ListItem::Separator(separator) => div().child(separator.render(view, cx)),
239 ListItem::Header(header) => div().child(header.render(view, cx)),
240 ListItem::Details(details) => div().child(details.render(view, cx)),
241 }
242 }
243
244 pub fn new(label: Label<S>) -> Self {
245 Self::Entry(ListEntry::new(label))
246 }
247
248 pub fn as_entry(&mut self) -> Option<&mut ListEntry<S>> {
249 if let Self::Entry(entry) = self {
250 Some(entry)
251 } else {
252 None
253 }
254 }
255}
256
257#[derive(Element)]
258pub struct ListEntry<S: 'static + Send + Sync> {
259 disclosure_control_style: DisclosureControlVisibility,
260 indent_level: u32,
261 label: Option<Label<S>>,
262 left_content: Option<LeftContent>,
263 variant: ListItemVariant,
264 size: ListEntrySize,
265 state: InteractionState,
266 toggle: Option<ToggleState>,
267 overflow: OverflowStyle,
268}
269
270impl<S: 'static + Send + Sync> ListEntry<S> {
271 pub fn new(label: Label<S>) -> Self {
272 Self {
273 disclosure_control_style: DisclosureControlVisibility::default(),
274 indent_level: 0,
275 label: Some(label),
276 variant: ListItemVariant::default(),
277 left_content: None,
278 size: ListEntrySize::default(),
279 state: InteractionState::default(),
280 // TODO: Should use Toggleable::NotToggleable
281 // or remove Toggleable::NotToggleable from the system
282 toggle: None,
283 overflow: OverflowStyle::Hidden,
284 }
285 }
286 pub fn set_variant(mut self, variant: ListItemVariant) -> Self {
287 self.variant = variant;
288 self
289 }
290 pub fn set_indent_level(mut self, indent_level: u32) -> Self {
291 self.indent_level = indent_level;
292 self
293 }
294
295 pub fn set_toggle(mut self, toggle: ToggleState) -> Self {
296 self.toggle = Some(toggle);
297 self
298 }
299
300 pub fn set_left_content(mut self, left_content: LeftContent) -> Self {
301 self.left_content = Some(left_content);
302 self
303 }
304
305 pub fn set_left_icon(mut self, left_icon: Icon) -> Self {
306 self.left_content = Some(LeftContent::Icon(left_icon));
307 self
308 }
309
310 pub fn set_left_avatar(mut self, left_avatar: impl Into<SharedString>) -> Self {
311 self.left_content = Some(LeftContent::Avatar(left_avatar.into()));
312 self
313 }
314
315 pub fn set_state(mut self, state: InteractionState) -> Self {
316 self.state = state;
317 self
318 }
319
320 pub fn set_size(mut self, size: ListEntrySize) -> Self {
321 self.size = size;
322 self
323 }
324
325 pub fn set_disclosure_control_style(
326 mut self,
327 disclosure_control_style: DisclosureControlVisibility,
328 ) -> Self {
329 self.disclosure_control_style = disclosure_control_style;
330 self
331 }
332
333 fn label_color(&self) -> LabelColor {
334 match self.state {
335 InteractionState::Disabled => LabelColor::Disabled,
336 _ => Default::default(),
337 }
338 }
339
340 fn icon_color(&self) -> IconColor {
341 match self.state {
342 InteractionState::Disabled => IconColor::Disabled,
343 _ => Default::default(),
344 }
345 }
346
347 fn disclosure_control(
348 &mut self,
349 cx: &mut ViewContext<S>,
350 ) -> Option<impl Element<ViewState = S>> {
351 let color = ThemeColor::new(cx);
352
353 let disclosure_control_icon = if let Some(ToggleState::Toggled) = self.toggle {
354 IconElement::new(Icon::ChevronDown)
355 } else {
356 IconElement::new(Icon::ChevronRight)
357 }
358 .color(IconColor::Muted)
359 .size(IconSize::Small);
360
361 match (self.toggle, self.disclosure_control_style) {
362 (Some(_), DisclosureControlVisibility::OnHover) => {
363 Some(div().absolute().neg_left_5().child(disclosure_control_icon))
364 }
365 (Some(_), DisclosureControlVisibility::Always) => {
366 Some(div().child(disclosure_control_icon))
367 }
368 (None, _) => None,
369 }
370 }
371
372 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
373 let color = ThemeColor::new(cx);
374 let color = ThemeColor::new(cx);
375 let settings = user_settings(cx);
376
377 let left_content = match self.left_content.clone() {
378 Some(LeftContent::Icon(i)) => Some(
379 h_stack().child(
380 IconElement::new(i)
381 .size(IconSize::Small)
382 .color(IconColor::Muted),
383 ),
384 ),
385 Some(LeftContent::Avatar(src)) => Some(h_stack().child(Avatar::new(src))),
386 None => None,
387 };
388
389 let sized_item = match self.size {
390 ListEntrySize::Small => div().h_6(),
391 ListEntrySize::Medium => div().h_7(),
392 };
393
394 div()
395 .relative()
396 .group("")
397 .bg(color.surface)
398 .when(self.state == InteractionState::Focused, |this| {
399 this.border().border_color(color.border_focused)
400 })
401 .child(
402 sized_item
403 .when(self.variant == ListItemVariant::Inset, |this| this.px_2())
404 // .ml(rems(0.75 * self.indent_level as f32))
405 .children((0..self.indent_level).map(|_| {
406 div()
407 .w(*settings.list_indent_depth)
408 .h_full()
409 .flex()
410 .justify_center()
411 .group_hover("", |style| style.bg(color.border_focused))
412 .child(
413 h_stack()
414 .child(div().w_px().h_full())
415 .child(div().w_px().h_full().bg(color.border)),
416 )
417 }))
418 .flex()
419 .gap_1()
420 .items_center()
421 .relative()
422 .children(self.disclosure_control(cx))
423 .children(left_content)
424 .children(self.label.take()),
425 )
426 }
427}
428
429struct ListDetailsEntryHandlers<S: 'static + Send + Sync> {
430 click: Option<ClickHandler<S>>,
431}
432
433impl<S: 'static + Send + Sync> Default for ListDetailsEntryHandlers<S> {
434 fn default() -> Self {
435 Self { click: None }
436 }
437}
438
439#[derive(Element)]
440pub struct ListDetailsEntry<S: 'static + Send + Sync> {
441 label: SharedString,
442 meta: Option<SharedString>,
443 left_content: Option<LeftContent>,
444 handlers: ListDetailsEntryHandlers<S>,
445 actions: Option<Vec<Button<S>>>,
446 // TODO: make this more generic instead of
447 // specifically for notifications
448 seen: bool,
449}
450
451impl<S: 'static + Send + Sync> ListDetailsEntry<S> {
452 pub fn new(label: impl Into<SharedString>) -> Self {
453 Self {
454 label: label.into(),
455 meta: None,
456 left_content: None,
457 handlers: ListDetailsEntryHandlers::default(),
458 actions: None,
459 seen: false,
460 }
461 }
462
463 pub fn meta(mut self, meta: impl Into<SharedString>) -> Self {
464 self.meta = Some(meta.into());
465 self
466 }
467
468 pub fn seen(mut self, seen: bool) -> Self {
469 self.seen = seen;
470 self
471 }
472
473 pub fn on_click(mut self, handler: ClickHandler<S>) -> Self {
474 self.handlers.click = Some(handler);
475 self
476 }
477
478 pub fn actions(mut self, actions: Vec<Button<S>>) -> Self {
479 self.actions = Some(actions);
480 self
481 }
482
483 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
484 let color = ThemeColor::new(cx);
485 let settings = user_settings(cx);
486
487 let (item_bg, item_bg_hover, item_bg_active) = match self.seen {
488 true => (
489 color.ghost_element,
490 color.ghost_element_hover,
491 color.ghost_element_active,
492 ),
493 false => (
494 color.filled_element,
495 color.filled_element_hover,
496 color.filled_element_active,
497 ),
498 };
499
500 let label_color = match self.seen {
501 true => LabelColor::Muted,
502 false => LabelColor::Default,
503 };
504
505 v_stack()
506 .relative()
507 .group("")
508 .bg(item_bg)
509 .px_1()
510 .py_1_5()
511 .w_full()
512 .line_height(relative(1.2))
513 .child(Label::new(self.label.clone()).color(label_color))
514 .when(self.meta.is_some(), |this| {
515 this.child(Label::new(self.meta.clone().unwrap()).color(LabelColor::Muted))
516 })
517 .child(
518 h_stack()
519 .gap_1()
520 .justify_end()
521 .children(self.actions.take().unwrap_or_default().into_iter()),
522 )
523 }
524}
525
526#[derive(Clone, Element)]
527pub struct ListSeparator<S: 'static + Send + Sync> {
528 state_type: PhantomData<S>,
529}
530
531impl<S: 'static + Send + Sync> ListSeparator<S> {
532 pub fn new() -> Self {
533 Self {
534 state_type: PhantomData,
535 }
536 }
537
538 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
539 let color = ThemeColor::new(cx);
540
541 div().h_px().w_full().bg(color.border)
542 }
543}
544
545#[derive(Element)]
546pub struct List<S: 'static + Send + Sync> {
547 items: Vec<ListItem<S>>,
548 empty_message: SharedString,
549 header: Option<ListHeader<S>>,
550 toggleable: Toggleable,
551}
552
553impl<S: 'static + Send + Sync> List<S> {
554 pub fn new(items: Vec<ListItem<S>>) -> Self {
555 Self {
556 items,
557 empty_message: "No items".into(),
558 header: None,
559 toggleable: Toggleable::default(),
560 }
561 }
562
563 pub fn empty_message(mut self, empty_message: impl Into<SharedString>) -> Self {
564 self.empty_message = empty_message.into();
565 self
566 }
567
568 pub fn header(mut self, header: ListHeader<S>) -> Self {
569 self.header = Some(header);
570 self
571 }
572
573 pub fn set_toggle(mut self, toggle: ToggleState) -> Self {
574 self.toggleable = toggle.into();
575 self
576 }
577
578 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
579 let color = ThemeColor::new(cx);
580 let is_toggleable = self.toggleable != Toggleable::NotToggleable;
581 let is_toggled = Toggleable::is_toggled(&self.toggleable);
582
583 let list_content = match (self.items.is_empty(), is_toggled) {
584 (_, false) => div(),
585 (false, _) => div().children(self.items.drain(..)),
586 (true, _) => {
587 div().child(Label::new(self.empty_message.clone()).color(LabelColor::Muted))
588 }
589 };
590
591 v_stack()
592 .py_1()
593 .children(
594 self.header
595 .take()
596 .map(|header| header.set_toggleable(self.toggleable)),
597 )
598 .child(list_content)
599 }
600}