| 1 | use crate::{ActiveTheme, Disableable, StyledExt, h_flex}; |
| 2 | use gpui::{ |
| 3 | AnyElement, App, ClickEvent, ElementId, InteractiveElement, IntoElement, MouseButton, |
| 4 | ParentElement, RenderOnce, Role, SharedString, StatefulInteractiveElement as _, |
| 5 | StyleRefinement, Styled, Window, prelude::FluentBuilder as _, |
| 6 | }; |
| 7 | use gpui_base::TestSupportExt as _; |
| 8 | use smallvec::SmallVec; |
| 9 | |
| 10 | #[derive(IntoElement)] |
| 11 | pub(crate) struct MenuItemElement { |
| 12 | id: ElementId, |
| 13 | aria_label: Option<SharedString>, |
| 14 | style: StyleRefinement, |
| 15 | disabled: bool, |
| 16 | selected: bool, |
| 17 | on_click: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>, |
| 18 | on_hover: Option<Box<dyn Fn(&bool, &mut Window, &mut App) + 'static>>, |
| 19 | children: SmallVec<[AnyElement; 2]>, |
| 20 | } |
| 21 | |
| 22 | impl MenuItemElement { |
| 23 | /// Create a new MenuItem with the given ID. |
| 24 | pub(crate) fn new(id: impl Into<ElementId>) -> Self { |
| 25 | let id: ElementId = id.into(); |
| 26 | Self { |
| 27 | id: id.clone(), |
| 28 | aria_label: None, |
| 29 | style: StyleRefinement::default(), |
| 30 | disabled: false, |
| 31 | selected: false, |
| 32 | on_click: None, |
| 33 | on_hover: None, |
| 34 | children: SmallVec::new(), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// Set ListItem as the selected item style. |
| 39 | pub(crate) fn selected(mut self, selected: bool) -> Self { |
| 40 | self.selected = selected; |
| 41 | self |
| 42 | } |
| 43 | |
| 44 | /// Set the accessible label for the menu item. |
| 45 | pub(crate) fn aria_label(mut self, label: impl Into<SharedString>) -> Self { |
| 46 | self.aria_label = Some(label.into()); |
| 47 | self |
| 48 | } |
| 49 | |
| 50 | /// Set the disabled state of the MenuItem. |
| 51 | pub(crate) fn disabled(mut self, disabled: bool) -> Self { |
| 52 | self.disabled = disabled; |
| 53 | self |
| 54 | } |
| 55 | |
| 56 | /// Set a handler for when the MenuItem is clicked. |
| 57 | pub(crate) fn on_click( |
| 58 | mut self, |
| 59 | handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 60 | ) -> Self { |
| 61 | self.on_click = Some(Box::new(handler)); |
| 62 | self |
| 63 | } |
| 64 | |
| 65 | /// Set a handler for when the mouse enters the MenuItem. |
| 66 | #[allow(unused)] |
| 67 | pub fn on_hover(mut self, handler: impl Fn(&bool, &mut Window, &mut App) + 'static) -> Self { |
| 68 | self.on_hover = Some(Box::new(handler)); |
| 69 | self |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl Disableable for MenuItemElement { |
| 74 | fn disabled(mut self, disabled: bool) -> Self { |
| 75 | self.disabled = disabled; |
| 76 | self |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | impl Styled for MenuItemElement { |
| 81 | fn style(&mut self) -> &mut gpui::StyleRefinement { |
| 82 | &mut self.style |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl ParentElement for MenuItemElement { |
| 87 | fn extend(&mut self, elements: impl IntoIterator<Item = gpui::AnyElement>) { |
| 88 | self.children.extend(elements); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl RenderOnce for MenuItemElement { |
| 93 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 94 | h_flex() |
| 95 | .id(self.id) |
| 96 | .test_support() |
| 97 | .role(Role::MenuItem) |
| 98 | .when_some(self.aria_label, |this, label| this.aria_label(label)) |
| 99 | .aria_selected(self.selected) |
| 100 | .gap_x_1() |
| 101 | .py_1() |
| 102 | .px_2() |
| 103 | .text_base() |
| 104 | .text_color(cx.theme().foreground) |
| 105 | .relative() |
| 106 | .items_center() |
| 107 | .justify_between() |
| 108 | .refine_style(&self.style) |
| 109 | .when_some(self.on_hover, |this, on_hover| { |
| 110 | this.on_hover(move |hovered, window, cx| (on_hover)(hovered, window, cx)) |
| 111 | }) |
| 112 | .when(!self.disabled, |this| { |
| 113 | // Not `group_hover` on the item's own group: GPUI styles an |
| 114 | // element before registering its group, and that fallback keeps |
| 115 | // the pointer's item lit after a key moves the highlight away. |
| 116 | this.hover(|this| { |
| 117 | this.bg(cx.theme().tokens.sidebar_accent) |
| 118 | .text_color(cx.theme().sidebar_accent_foreground) |
| 119 | }) |
| 120 | .when(self.selected, |this| { |
| 121 | this.bg(cx.theme().tokens.sidebar_accent) |
| 122 | .text_color(cx.theme().sidebar_accent_foreground) |
| 123 | }) |
| 124 | .when_some(self.on_click, |this, on_click| { |
| 125 | this.on_mouse_down(MouseButton::Left, move |_, _, cx| { |
| 126 | cx.stop_propagation(); |
| 127 | }) |
| 128 | .on_click(on_click) |
| 129 | }) |
| 130 | }) |
| 131 | .when(self.disabled, |this| { |
| 132 | this.text_color(cx.theme().muted_foreground) |
| 133 | }) |
| 134 | .children(self.children) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #[cfg(test)] |
| 139 | mod tests { |
| 140 | use super::*; |
| 141 | |
| 142 | #[gpui::test] |
| 143 | fn aria_label_sets_accessible_name(_cx: &mut gpui::TestAppContext) { |
| 144 | let item = MenuItemElement::new("open").aria_label("Open"); |
| 145 | |
| 146 | assert_eq!(item.aria_label, Some("Open".into())); |
| 147 | } |
| 148 | } |