1use crate::{
2 h_stack, prelude::*, v_stack, Icon, IconElement, KeyBinding, Label, List, ListItem,
3 ListSeparator, ListSubHeader,
4};
5use gpui::{
6 px, Action, AppContext, DismissEvent, Div, EventEmitter, FocusHandle, FocusableView,
7 IntoElement, Render, View, VisualContext,
8};
9use menu::{SelectFirst, SelectLast, SelectNext, SelectPrev};
10use std::{rc::Rc, time::Duration};
11
12pub enum ContextMenuItem {
13 Separator,
14 Header(SharedString),
15 Entry {
16 label: SharedString,
17 icon: Option<Icon>,
18 handler: Rc<dyn Fn(&mut WindowContext)>,
19 action: Option<Box<dyn Action>>,
20 },
21}
22
23pub struct ContextMenu {
24 items: Vec<ContextMenuItem>,
25 focus_handle: FocusHandle,
26 selected_index: Option<usize>,
27}
28
29impl FocusableView for ContextMenu {
30 fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
31 self.focus_handle.clone()
32 }
33}
34
35impl EventEmitter<DismissEvent> for ContextMenu {}
36
37impl ContextMenu {
38 pub fn build(
39 cx: &mut WindowContext,
40 f: impl FnOnce(Self, &mut WindowContext) -> Self,
41 ) -> View<Self> {
42 // let handle = cx.view().downgrade();
43 cx.build_view(|cx| {
44 f(
45 Self {
46 items: Default::default(),
47 focus_handle: cx.focus_handle(),
48 selected_index: None,
49 },
50 cx,
51 )
52 })
53 }
54
55 pub fn header(mut self, title: impl Into<SharedString>) -> Self {
56 self.items.push(ContextMenuItem::Header(title.into()));
57 self
58 }
59
60 pub fn separator(mut self) -> Self {
61 self.items.push(ContextMenuItem::Separator);
62 self
63 }
64
65 pub fn entry(
66 mut self,
67 label: impl Into<SharedString>,
68 on_click: impl Fn(&mut WindowContext) + 'static,
69 ) -> Self {
70 self.items.push(ContextMenuItem::Entry {
71 label: label.into(),
72 handler: Rc::new(on_click),
73 icon: None,
74 action: None,
75 });
76 self
77 }
78
79 pub fn action(mut self, label: impl Into<SharedString>, action: Box<dyn Action>) -> Self {
80 self.items.push(ContextMenuItem::Entry {
81 label: label.into(),
82 action: Some(action.boxed_clone()),
83 handler: Rc::new(move |cx| cx.dispatch_action(action.boxed_clone())),
84 icon: None,
85 });
86 self
87 }
88
89 pub fn link(mut self, label: impl Into<SharedString>, action: Box<dyn Action>) -> Self {
90 self.items.push(ContextMenuItem::Entry {
91 label: label.into(),
92 action: Some(action.boxed_clone()),
93 handler: Rc::new(move |cx| cx.dispatch_action(action.boxed_clone())),
94 icon: Some(Icon::Link),
95 });
96 self
97 }
98
99 pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
100 if let Some(ContextMenuItem::Entry { handler, .. }) =
101 self.selected_index.and_then(|ix| self.items.get(ix))
102 {
103 (handler)(cx)
104 }
105 cx.emit(DismissEvent);
106 }
107
108 pub fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
109 cx.emit(DismissEvent);
110 }
111
112 fn select_first(&mut self, _: &SelectFirst, cx: &mut ViewContext<Self>) {
113 self.selected_index = self.items.iter().position(|item| item.is_selectable());
114 cx.notify();
115 }
116
117 fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
118 for (ix, item) in self.items.iter().enumerate().rev() {
119 if item.is_selectable() {
120 self.selected_index = Some(ix);
121 cx.notify();
122 break;
123 }
124 }
125 }
126
127 fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
128 if let Some(ix) = self.selected_index {
129 for (ix, item) in self.items.iter().enumerate().skip(ix + 1) {
130 if item.is_selectable() {
131 self.selected_index = Some(ix);
132 cx.notify();
133 break;
134 }
135 }
136 } else {
137 self.select_first(&Default::default(), cx);
138 }
139 }
140
141 pub fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
142 if let Some(ix) = self.selected_index {
143 for (ix, item) in self.items.iter().enumerate().take(ix).rev() {
144 if item.is_selectable() {
145 self.selected_index = Some(ix);
146 cx.notify();
147 break;
148 }
149 }
150 } else {
151 self.select_last(&Default::default(), cx);
152 }
153 }
154
155 pub fn on_action_dispatch(&mut self, dispatched: &Box<dyn Action>, cx: &mut ViewContext<Self>) {
156 if let Some(ix) = self.items.iter().position(|item| {
157 if let ContextMenuItem::Entry {
158 action: Some(action),
159 ..
160 } = item
161 {
162 action.partial_eq(&**dispatched)
163 } else {
164 false
165 }
166 }) {
167 self.selected_index = Some(ix);
168 cx.notify();
169 let action = dispatched.boxed_clone();
170 cx.spawn(|this, mut cx| async move {
171 cx.background_executor()
172 .timer(Duration::from_millis(50))
173 .await;
174 this.update(&mut cx, |this, cx| {
175 cx.dispatch_action(action);
176 this.cancel(&Default::default(), cx)
177 })
178 })
179 .detach_and_log_err(cx);
180 } else {
181 cx.propagate()
182 }
183 }
184}
185
186impl ContextMenuItem {
187 fn is_selectable(&self) -> bool {
188 matches!(self, Self::Entry { .. })
189 }
190}
191
192impl Render for ContextMenu {
193 type Element = Div;
194
195 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
196 div().elevation_2(cx).flex().flex_row().child(
197 v_stack()
198 .min_w(px(200.))
199 .track_focus(&self.focus_handle)
200 .on_mouse_down_out(cx.listener(|this, _, cx| this.cancel(&Default::default(), cx)))
201 .key_context("menu")
202 .on_action(cx.listener(ContextMenu::select_first))
203 .on_action(cx.listener(ContextMenu::select_last))
204 .on_action(cx.listener(ContextMenu::select_next))
205 .on_action(cx.listener(ContextMenu::select_prev))
206 .on_action(cx.listener(ContextMenu::confirm))
207 .on_action(cx.listener(ContextMenu::cancel))
208 .map(|mut el| {
209 for item in self.items.iter() {
210 if let ContextMenuItem::Entry {
211 action: Some(action),
212 ..
213 } = item
214 {
215 el = el.on_boxed_action(
216 action,
217 cx.listener(ContextMenu::on_action_dispatch),
218 );
219 }
220 }
221 el
222 })
223 .on_blur(cx.listener(|this, _, cx| this.cancel(&Default::default(), cx)))
224 .flex_none()
225 .child(
226 List::new().children(self.items.iter().enumerate().map(
227 |(ix, item)| match item {
228 ContextMenuItem::Separator => ListSeparator.into_any_element(),
229 ContextMenuItem::Header(header) => {
230 ListSubHeader::new(header.clone()).into_any_element()
231 }
232 ContextMenuItem::Entry {
233 label,
234 handler,
235 icon,
236 action,
237 } => {
238 let handler = handler.clone();
239 let dismiss = cx.listener(|_, _, cx| cx.emit(DismissEvent));
240
241 let label_element = if let Some(icon) = icon {
242 h_stack()
243 .gap_1()
244 .child(Label::new(label.clone()))
245 .child(IconElement::new(*icon))
246 .into_any_element()
247 } else {
248 Label::new(label.clone()).into_any_element()
249 };
250
251 ListItem::new(label.clone())
252 .child(
253 h_stack()
254 .w_full()
255 .justify_between()
256 .child(label_element)
257 .children(action.as_ref().and_then(|action| {
258 KeyBinding::for_action(&**action, cx)
259 .map(|binding| div().ml_1().child(binding))
260 })),
261 )
262 .selected(Some(ix) == self.selected_index)
263 .on_click(move |event, cx| {
264 handler(cx);
265 dismiss(event, cx)
266 })
267 .into_any_element()
268 }
269 },
270 )),
271 ),
272 )
273 }
274}