list_item.rs

  1#![allow(missing_docs)]
  2
  3use std::sync::Arc;
  4
  5use gpui::{px, AnyElement, AnyView, ClickEvent, MouseButton, MouseDownEvent, Pixels};
  6use smallvec::SmallVec;
  7
  8use crate::{prelude::*, Disclosure};
  9
 10#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
 11pub enum ListItemSpacing {
 12    #[default]
 13    Dense,
 14    ExtraDense,
 15    Sparse,
 16}
 17
 18#[derive(IntoElement)]
 19pub struct ListItem {
 20    id: ElementId,
 21    disabled: bool,
 22    selected: bool,
 23    spacing: ListItemSpacing,
 24    indent_level: usize,
 25    indent_step_size: Pixels,
 26    /// A slot for content that appears before the children, like an icon or avatar.
 27    start_slot: Option<AnyElement>,
 28    /// A slot for content that appears after the children, usually on the other side of the header.
 29    /// This might be a button, a disclosure arrow, a face pile, etc.
 30    end_slot: Option<AnyElement>,
 31    /// A slot for content that appears on hover after the children
 32    /// It will obscure the `end_slot` when visible.
 33    end_hover_slot: Option<AnyElement>,
 34    toggle: Option<bool>,
 35    inset: bool,
 36    on_click: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
 37    on_toggle: Option<Arc<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
 38    tooltip: Option<Box<dyn Fn(&mut Window, &mut App) -> AnyView + 'static>>,
 39    on_secondary_mouse_down: Option<Box<dyn Fn(&MouseDownEvent, &mut Window, &mut App) + 'static>>,
 40    children: SmallVec<[AnyElement; 2]>,
 41    selectable: bool,
 42    outlined: bool,
 43    overflow_x: bool,
 44    focused: Option<bool>,
 45}
 46
 47impl ListItem {
 48    pub fn new(id: impl Into<ElementId>) -> Self {
 49        Self {
 50            id: id.into(),
 51            disabled: false,
 52            selected: false,
 53            spacing: ListItemSpacing::Dense,
 54            indent_level: 0,
 55            indent_step_size: px(12.),
 56            start_slot: None,
 57            end_slot: None,
 58            end_hover_slot: None,
 59            toggle: None,
 60            inset: false,
 61            on_click: None,
 62            on_secondary_mouse_down: None,
 63            on_toggle: None,
 64            tooltip: None,
 65            children: SmallVec::new(),
 66            selectable: true,
 67            outlined: false,
 68            overflow_x: false,
 69            focused: None,
 70        }
 71    }
 72
 73    pub fn spacing(mut self, spacing: ListItemSpacing) -> Self {
 74        self.spacing = spacing;
 75        self
 76    }
 77
 78    pub fn selectable(mut self, has_hover: bool) -> Self {
 79        self.selectable = has_hover;
 80        self
 81    }
 82
 83    pub fn on_click(
 84        mut self,
 85        handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
 86    ) -> Self {
 87        self.on_click = Some(Box::new(handler));
 88        self
 89    }
 90
 91    pub fn on_secondary_mouse_down(
 92        mut self,
 93        handler: impl Fn(&MouseDownEvent, &mut Window, &mut App) + 'static,
 94    ) -> Self {
 95        self.on_secondary_mouse_down = Some(Box::new(handler));
 96        self
 97    }
 98
 99    pub fn tooltip(mut self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static) -> Self {
100        self.tooltip = Some(Box::new(tooltip));
101        self
102    }
103
104    pub fn inset(mut self, inset: bool) -> Self {
105        self.inset = inset;
106        self
107    }
108
109    pub fn indent_level(mut self, indent_level: usize) -> Self {
110        self.indent_level = indent_level;
111        self
112    }
113
114    pub fn indent_step_size(mut self, indent_step_size: Pixels) -> Self {
115        self.indent_step_size = indent_step_size;
116        self
117    }
118
119    pub fn toggle(mut self, toggle: impl Into<Option<bool>>) -> Self {
120        self.toggle = toggle.into();
121        self
122    }
123
124    pub fn on_toggle(
125        mut self,
126        on_toggle: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
127    ) -> Self {
128        self.on_toggle = Some(Arc::new(on_toggle));
129        self
130    }
131
132    pub fn start_slot<E: IntoElement>(mut self, start_slot: impl Into<Option<E>>) -> Self {
133        self.start_slot = start_slot.into().map(IntoElement::into_any_element);
134        self
135    }
136
137    pub fn end_slot<E: IntoElement>(mut self, end_slot: impl Into<Option<E>>) -> Self {
138        self.end_slot = end_slot.into().map(IntoElement::into_any_element);
139        self
140    }
141
142    pub fn end_hover_slot<E: IntoElement>(mut self, end_hover_slot: impl Into<Option<E>>) -> Self {
143        self.end_hover_slot = end_hover_slot.into().map(IntoElement::into_any_element);
144        self
145    }
146
147    pub fn outlined(mut self) -> Self {
148        self.outlined = true;
149        self
150    }
151
152    pub fn overflow_x(mut self) -> Self {
153        self.overflow_x = true;
154        self
155    }
156
157    pub fn focused(mut self, focused: bool) -> Self {
158        self.focused = Some(focused);
159        self
160    }
161}
162
163impl Disableable for ListItem {
164    fn disabled(mut self, disabled: bool) -> Self {
165        self.disabled = disabled;
166        self
167    }
168}
169
170impl Toggleable for ListItem {
171    fn toggle_state(mut self, selected: bool) -> Self {
172        self.selected = selected;
173        self
174    }
175}
176
177impl ParentElement for ListItem {
178    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
179        self.children.extend(elements)
180    }
181}
182
183impl RenderOnce for ListItem {
184    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
185        h_flex()
186            .id(self.id)
187            .w_full()
188            .relative()
189            // When an item is inset draw the indent spacing outside of the item
190            .when(self.inset, |this| {
191                this.ml(self.indent_level as f32 * self.indent_step_size)
192                    .px(DynamicSpacing::Base04.rems(cx))
193            })
194            .when(!self.inset && !self.disabled, |this| {
195                this
196                    // TODO: Add focus state
197                    // .when(self.state == InteractionState::Focused, |this| {
198                    .when_some(self.focused, |this, focused| {
199                        if focused {
200                            this.border_1()
201                                .border_color(cx.theme().colors().border_focused)
202                        } else {
203                            this.border_1()
204                        }
205                    })
206                    .when(self.selectable, |this| {
207                        this.hover(|style| style.bg(cx.theme().colors().ghost_element_hover))
208                            .active(|style| style.bg(cx.theme().colors().ghost_element_active))
209                            .when(self.outlined, |this| this.rounded_md())
210                            .when(self.selected, |this| {
211                                this.bg(cx.theme().colors().ghost_element_selected)
212                            })
213                    })
214            })
215            .child(
216                h_flex()
217                    .id("inner_list_item")
218                    .group("list_item")
219                    .w_full()
220                    .relative()
221                    .items_center()
222                    .gap_1()
223                    .px(DynamicSpacing::Base06.rems(cx))
224                    .map(|this| match self.spacing {
225                        ListItemSpacing::Dense => this,
226                        ListItemSpacing::ExtraDense => this.py_neg_px(),
227                        ListItemSpacing::Sparse => this.py_1(),
228                    })
229                    .when(self.inset && !self.disabled, |this| {
230                        this
231                            // TODO: Add focus state
232                            //.when(self.state == InteractionState::Focused, |this| {
233                            .when_some(self.focused, |this, focused| {
234                                if focused {
235                                    this.border_1()
236                                        .border_color(cx.theme().colors().border_focused)
237                                } else {
238                                    this.border_1()
239                                }
240                            })
241                            .when(self.selectable, |this| {
242                                this.hover(|style| {
243                                    style.bg(cx.theme().colors().ghost_element_hover)
244                                })
245                                .active(|style| style.bg(cx.theme().colors().ghost_element_active))
246                                .when(self.selected, |this| {
247                                    this.bg(cx.theme().colors().ghost_element_selected)
248                                })
249                            })
250                    })
251                    .when_some(
252                        self.on_click.filter(|_| !self.disabled),
253                        |this, on_click| this.cursor_pointer().on_click(on_click),
254                    )
255                    .when(self.outlined, |this| {
256                        this.border_1()
257                            .border_color(cx.theme().colors().border)
258                            .rounded_md()
259                            .overflow_hidden()
260                    })
261                    .when_some(self.on_secondary_mouse_down, |this, on_mouse_down| {
262                        this.on_mouse_down(MouseButton::Right, move |event, window, cx| {
263                            (on_mouse_down)(event, window, cx)
264                        })
265                    })
266                    .when_some(self.tooltip, |this, tooltip| this.tooltip(tooltip))
267                    .map(|this| {
268                        if self.inset {
269                            this.rounded_md()
270                        } else {
271                            // When an item is not inset draw the indent spacing inside of the item
272                            this.ml(self.indent_level as f32 * self.indent_step_size)
273                        }
274                    })
275                    .children(self.toggle.map(|is_open| {
276                        div()
277                            .flex()
278                            .absolute()
279                            .left(rems(-1.))
280                            .when(is_open, |this| this.visible_on_hover(""))
281                            .child(Disclosure::new("toggle", is_open).on_toggle(self.on_toggle))
282                    }))
283                    .child(
284                        h_flex()
285                            .flex_grow()
286                            .flex_shrink_0()
287                            .flex_basis(relative(0.25))
288                            .gap(DynamicSpacing::Base06.rems(cx))
289                            .map(|list_content| {
290                                if self.overflow_x {
291                                    list_content
292                                } else {
293                                    list_content.overflow_hidden()
294                                }
295                            })
296                            .children(self.start_slot)
297                            .children(self.children),
298                    )
299                    .when_some(self.end_slot, |this, end_slot| {
300                        this.justify_between().child(
301                            h_flex()
302                                .flex_shrink()
303                                .overflow_hidden()
304                                .when(self.end_hover_slot.is_some(), |this| {
305                                    this.visible()
306                                        .group_hover("list_item", |this| this.invisible())
307                                })
308                                .child(end_slot),
309                        )
310                    })
311                    .when_some(self.end_hover_slot, |this, end_hover_slot| {
312                        this.child(
313                            h_flex()
314                                .h_full()
315                                .absolute()
316                                .right(DynamicSpacing::Base06.rems(cx))
317                                .top_0()
318                                .visible_on_hover("list_item")
319                                .child(end_hover_slot),
320                        )
321                    }),
322            )
323    }
324}