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