BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / src / menu / menu_item.rs

menu_item.rs

menu_item.rs 4.8 KiB Download Raw
1use crate::{ActiveTheme, Disableable, StyledExt, h_flex};
2use gpui::{
3 AnyElement, App, ClickEvent, ElementId, InteractiveElement, IntoElement, MouseButton,
4 ParentElement, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
5 StyleRefinement, Styled, Window, prelude::FluentBuilder as _,
6};
7use gpui_base::TestSupportExt as _;
8use smallvec::SmallVec;
9
10#[derive(IntoElement)]
11pub(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
22impl 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
73impl Disableable for MenuItemElement {
74 fn disabled(mut self, disabled: bool) -> Self {
75 self.disabled = disabled;
76 self
77 }
78}
79
80impl Styled for MenuItemElement {
81 fn style(&mut self) -> &mut gpui::StyleRefinement {
82 &mut self.style
83 }
84}
85
86impl ParentElement for MenuItemElement {
87 fn extend(&mut self, elements: impl IntoIterator<Item = gpui::AnyElement>) {
88 self.children.extend(elements);
89 }
90}
91
92impl RenderOnce for MenuItemElement {
93 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
94 let highlight = crate::elevation::flyout(window, cx).hover;
95 h_flex()
96 .id(self.id)
97 .test_support()
98 .role(Role::MenuItem)
99 .when_some(self.aria_label, |this, label| this.aria_label(label))
100 .aria_selected(self.selected)
101 .gap_x_1()
102 .py_1()
103 .px_2()
104 .text_base()
105 .text_color(cx.theme().foreground)
106 .relative()
107 .items_center()
108 .justify_between()
109 .refine_style(&self.style)
110 .when_some(self.on_hover, |this, on_hover| {
111 this.on_hover(move |hovered, window, cx| (on_hover)(hovered, window, cx))
112 })
113 .when(!self.disabled, |this| {
114 // Not `group_hover` on the item's own group: GPUI styles an
115 // element before registering its group, and that fallback keeps
116 // the pointer's item lit after a key moves the highlight away.
117 this.hover(|this| {
118 this.bg(highlight)
119 .text_color(cx.theme().sidebar_accent_foreground)
120 })
121 .when(self.selected, |this| {
122 this.bg(highlight)
123 .text_color(cx.theme().sidebar_accent_foreground)
124 })
125 .when_some(self.on_click, |this, on_click| {
126 this.on_mouse_down(MouseButton::Left, move |_, _, cx| {
127 cx.stop_propagation();
128 })
129 .on_click(on_click)
130 })
131 })
132 .when(self.disabled, |this| {
133 this.text_color(cx.theme().muted_foreground)
134 })
135 .children(self.children)
136 }
137}
138
139#[cfg(test)]
140mod tests {
141 use super::*;
142
143 #[gpui::test]
144 fn aria_label_sets_accessible_name(_cx: &mut gpui::TestAppContext) {
145 let item = MenuItemElement::new("open").aria_label("Open");
146
147 assert_eq!(item.aria_label, Some("Open".into()));
148 }
149}