1use crate::ThemeStyled as _;
2use crate::actions::{Cancel, Confirm, SelectDown, SelectUp};
3use crate::actions::{SelectLeft, SelectRight};
4use crate::menu::menu_item::MenuItemElement;
5use crate::scroll::ScrollableElement;
6use crate::{ActiveTheme, ElementExt, Icon, IconName, Sizable as _, h_flex, v_flex};
7use crate::{Side, Size, kbd::Kbd};
8use gpui::{
9 Action, Anchor, AnyElement, App, AppContext, Bounds, Context, DismissEvent, Edges, Entity,
10 EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement, KeyBinding,
11 ParentElement, Pixels, Rems, Render, Role, ScrollHandle, SharedString,
12 StatefulInteractiveElement, Styled, WeakEntity, Window, anchored, deferred, div,
13 prelude::FluentBuilder, px, rems,
14};
15use gpui::{ClickEvent, Half, MouseDownEvent, OwnedMenuItem, Point, Subscription};
16use gpui_base::{LayoutDirection, LogicalStyledExt as _, TestSupportExt as _};
17
18use std::collections::HashSet;
19use std::rc::Rc;
20
21const CONTEXT: &str = "PopupMenu";
22
23const SCROLL_TO_CHECKED_FRAMES: u8 = 4;
26
27pub fn init(cx: &mut App) {
28 cx.bind_keys([
29 KeyBinding::new("enter", Confirm { secondary: false }, Some(CONTEXT)),
30 KeyBinding::new("escape", Cancel, Some(CONTEXT)),
31 KeyBinding::new("up", SelectUp, Some(CONTEXT)),
32 KeyBinding::new("down", SelectDown, Some(CONTEXT)),
33 KeyBinding::new("left", SelectLeft, Some(CONTEXT)),
34 KeyBinding::new("right", SelectRight, Some(CONTEXT)),
35 ]);
36}
37
38pub enum PopupMenuItem {
40 Separator,
42 Label(SharedString),
44 Item {
46 icon: Option<Icon>,
47 label: SharedString,
48 disabled: bool,
49 checked: bool,
50 is_link: bool,
51 action: Option<Box<dyn Action>>,
52 handler: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
54 },
55 ElementItem {
57 icon: Option<Icon>,
58 disabled: bool,
59 checked: bool,
60 action: Option<Box<dyn Action>>,
61 render: Box<dyn Fn(&mut Window, &mut App) -> AnyElement + 'static>,
62 handler: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
63 },
64 Submenu {
66 icon: Option<Icon>,
67 label: SharedString,
68 disabled: bool,
69 menu: Entity<PopupMenu>,
70 },
71}
72
73impl FluentBuilder for PopupMenuItem {}
74impl PopupMenuItem {
75 #[inline]
77 pub fn new(label: impl Into<SharedString>) -> Self {
78 PopupMenuItem::Item {
79 icon: None,
80 label: label.into(),
81 disabled: false,
82 checked: false,
83 action: None,
84 is_link: false,
85 handler: None,
86 }
87 }
88
89 #[inline]
91 pub fn element<F, E>(builder: F) -> Self
92 where
93 F: Fn(&mut Window, &mut App) -> E + 'static,
94 E: IntoElement,
95 {
96 PopupMenuItem::ElementItem {
97 icon: None,
98 disabled: false,
99 checked: false,
100 action: None,
101 render: Box::new(move |window, cx| builder(window, cx).into_any_element()),
102 handler: None,
103 }
104 }
105
106 #[inline]
108 pub fn submenu(label: impl Into<SharedString>, menu: Entity<PopupMenu>) -> Self {
109 PopupMenuItem::Submenu {
110 icon: None,
111 label: label.into(),
112 disabled: false,
113 menu,
114 }
115 }
116
117 #[inline]
119 pub fn separator() -> Self {
120 PopupMenuItem::Separator
121 }
122
123 #[inline]
125 pub fn label(label: impl Into<SharedString>) -> Self {
126 PopupMenuItem::Label(label.into())
127 }
128
129 pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
133 match &mut self {
134 PopupMenuItem::Item { icon: i, .. } => {
135 *i = Some(icon.into());
136 }
137 PopupMenuItem::ElementItem { icon: i, .. } => {
138 *i = Some(icon.into());
139 }
140 PopupMenuItem::Submenu { icon: i, .. } => {
141 *i = Some(icon.into());
142 }
143 _ => {}
144 }
145 self
146 }
147
148 pub fn action(mut self, action: Box<dyn Action>) -> Self {
152 match &mut self {
153 PopupMenuItem::Item { action: a, .. } => {
154 *a = Some(action);
155 }
156 PopupMenuItem::ElementItem { action: a, .. } => {
157 *a = Some(action);
158 }
159 _ => {}
160 }
161 self
162 }
163
164 pub fn disabled(mut self, disabled: bool) -> Self {
168 match &mut self {
169 PopupMenuItem::Item { disabled: d, .. } => {
170 *d = disabled;
171 }
172 PopupMenuItem::ElementItem { disabled: d, .. } => {
173 *d = disabled;
174 }
175 PopupMenuItem::Submenu { disabled: d, .. } => {
176 *d = disabled;
177 }
178 _ => {}
179 }
180 self
181 }
182
183 pub fn checked(mut self, checked: bool) -> Self {
187 match &mut self {
188 PopupMenuItem::Item { checked: c, .. } => {
189 *c = checked;
190 }
191 PopupMenuItem::ElementItem { checked: c, .. } => {
192 *c = checked;
193 }
194 _ => {}
195 }
196 self
197 }
198
199 pub fn on_click<F>(mut self, handler: F) -> Self
203 where
204 F: Fn(&ClickEvent, &mut Window, &mut App) + 'static,
205 {
206 match &mut self {
207 PopupMenuItem::Item { handler: h, .. } => {
208 *h = Some(Rc::new(handler));
209 }
210 PopupMenuItem::ElementItem { handler: h, .. } => {
211 *h = Some(Rc::new(handler));
212 }
213 _ => {}
214 }
215 self
216 }
217
218 #[inline]
220 pub fn link(label: impl Into<SharedString>, href: impl Into<String>) -> Self {
221 let href = href.into();
222 PopupMenuItem::Item {
223 icon: None,
224 label: label.into(),
225 disabled: false,
226 checked: false,
227 action: None,
228 is_link: true,
229 handler: Some(Rc::new(move |_, _, cx| cx.open_url(&href))),
230 }
231 }
232
233 #[inline]
234 fn is_clickable(&self) -> bool {
235 !matches!(self, PopupMenuItem::Separator)
236 && matches!(
237 self,
238 PopupMenuItem::Item {
239 disabled: false,
240 ..
241 } | PopupMenuItem::ElementItem {
242 disabled: false,
243 ..
244 } | PopupMenuItem::Submenu {
245 disabled: false,
246 ..
247 }
248 )
249 }
250
251 #[inline]
252 fn is_separator(&self) -> bool {
253 matches!(self, PopupMenuItem::Separator)
254 }
255
256 fn has_left_icon(&self, check_side: Side) -> bool {
257 match self {
258 PopupMenuItem::Item { icon, checked, .. } => {
259 icon.is_some() || (check_side.is_left() && *checked)
260 }
261 PopupMenuItem::ElementItem { icon, checked, .. } => {
262 icon.is_some() || (check_side.is_left() && *checked)
263 }
264 PopupMenuItem::Submenu { icon, .. } => icon.is_some(),
265 _ => false,
266 }
267 }
268
269 #[inline]
270 fn is_checked(&self) -> bool {
271 match self {
272 PopupMenuItem::Item { checked, .. } => *checked,
273 PopupMenuItem::ElementItem { checked, .. } => *checked,
274 _ => false,
275 }
276 }
277
278 fn a11y_label(&self) -> Option<SharedString> {
279 match self {
280 PopupMenuItem::Item { label, .. }
281 | PopupMenuItem::Label(label)
282 | PopupMenuItem::Submenu { label, .. } => Some(label.clone()),
283 PopupMenuItem::Separator | PopupMenuItem::ElementItem { .. } => None,
284 }
285 }
286}
287
288pub struct PopupMenu {
289 pub(crate) focus_handle: FocusHandle,
290 pub(crate) menu_items: Vec<PopupMenuItem>,
291 pub(crate) action_context: Option<FocusHandle>,
293 pub(crate) previous_focus_handle: Option<FocusHandle>,
297 pub(crate) trigger_focus_handle: Option<FocusHandle>,
302 selected_index: Option<usize>,
303 min_width: Option<Pixels>,
304 max_width: Option<Pixels>,
305 max_height: Option<Pixels>,
306 bounds: Bounds<Pixels>,
307 size: Size,
308 check_side: Side,
309 checkable: HashSet<usize>,
312
313 parent_menu: Option<WeakEntity<Self>>,
315 scrollable: bool,
316 scroll_to_checked_frames: u8,
319 external_link_icon: bool,
320 scroll_handle: ScrollHandle,
321 submenu_anchor: (Anchor, Pixels),
323
324 priority: usize,
341
342 _subscriptions: Vec<Subscription>,
343}
344
345impl PopupMenu {
346 pub(crate) fn new(cx: &mut App) -> Self {
347 Self {
348 focus_handle: cx.focus_handle(),
349 action_context: None,
350 previous_focus_handle: None,
351 trigger_focus_handle: None,
352 parent_menu: None,
353 menu_items: Vec::new(),
354 selected_index: None,
355 min_width: None,
356 max_width: None,
357 max_height: None,
358 check_side: Side::Left,
359 checkable: HashSet::new(),
360 bounds: Bounds::default(),
361 scrollable: false,
362 scroll_to_checked_frames: SCROLL_TO_CHECKED_FRAMES,
363 scroll_handle: ScrollHandle::default(),
364 external_link_icon: true,
365 size: Size::default(),
366 submenu_anchor: (Anchor::TopLeft, Pixels::ZERO),
367 priority: gpui_base::POPUP_PRIORITY,
368 _subscriptions: vec![],
369 }
370 }
371
372 pub fn build(
373 window: &mut Window,
374 cx: &mut App,
375 f: impl FnOnce(Self, &mut Window, &mut Context<PopupMenu>) -> Self,
376 ) -> Entity<Self> {
377 cx.new(|cx| f(Self::new(cx), window, cx))
378 }
379
380 pub fn action_context(mut self, handle: FocusHandle) -> Self {
388 self.action_context = Some(handle);
389 self
390 }
391
392 pub(crate) fn set_action_context(
393 &mut self,
394 action_context: Option<FocusHandle>,
395 cx: &mut Context<Self>,
396 ) {
397 self.action_context = action_context.clone();
398
399 for item in &self.menu_items {
400 if let PopupMenuItem::Submenu { menu, .. } = item {
401 menu.update(cx, |menu, cx| {
402 menu.set_action_context(action_context.clone(), cx);
403 });
404 }
405 }
406 }
407
408 pub(crate) fn set_previous_focus(
411 &mut self,
412 handle: Option<FocusHandle>,
413 cx: &mut Context<Self>,
414 ) {
415 self.previous_focus_handle = handle.clone();
416
417 for item in &self.menu_items {
418 if let PopupMenuItem::Submenu { menu, .. } = item {
419 menu.update(cx, |menu, cx| {
420 menu.set_previous_focus(handle.clone(), cx);
421 });
422 }
423 }
424 }
425
426 pub(crate) fn set_trigger_focus(
429 &mut self,
430 handle: Option<FocusHandle>,
431 cx: &mut Context<Self>,
432 ) {
433 self.trigger_focus_handle = handle.clone();
434
435 for item in &self.menu_items {
436 if let PopupMenuItem::Submenu { menu, .. } = item {
437 menu.update(cx, |menu, cx| {
438 menu.set_trigger_focus(handle.clone(), cx);
439 });
440 }
441 }
442 }
443
444 pub fn min_w(mut self, width: impl Into<Pixels>) -> Self {
446 self.min_width = Some(width.into());
447 self
448 }
449
450 pub fn max_w(mut self, width: impl Into<Pixels>) -> Self {
453 self.max_width = Some(width.into());
454 self
455 }
456
457 pub fn max_h(mut self, height: impl Into<Pixels>) -> Self {
460 self.max_height = Some(height.into());
461 self
462 }
463
464 pub fn scrollable(mut self, scrollable: bool) -> Self {
466 self.scrollable = scrollable;
467 self
468 }
469
470 pub fn check_side(mut self, side: Side) -> Self {
472 self.check_side = side;
473 self
474 }
475
476 pub fn external_link_icon(mut self, visible: bool) -> Self {
478 self.external_link_icon = visible;
479 self
480 }
481
482 pub fn menu(self, label: impl Into<SharedString>, action: Box<dyn Action>) -> Self {
484 self.menu_with_disabled(label, action, false)
485 }
486
487 pub fn menu_with_enable(
489 mut self,
490 label: impl Into<SharedString>,
491 action: Box<dyn Action>,
492 enable: bool,
493 ) -> Self {
494 self.add_menu_item(label, None, action, !enable, false);
495 self
496 }
497
498 pub fn menu_with_disabled(
500 mut self,
501 label: impl Into<SharedString>,
502 action: Box<dyn Action>,
503 disabled: bool,
504 ) -> Self {
505 self.add_menu_item(label, None, action, disabled, false);
506 self
507 }
508
509 pub fn label(mut self, label: impl Into<SharedString>) -> Self {
511 self.menu_items.push(PopupMenuItem::label(label.into()));
512 self
513 }
514
515 pub fn link(self, label: impl Into<SharedString>, href: impl Into<String>) -> Self {
517 self.link_with_disabled(label, href, false)
518 }
519
520 pub fn link_with_disabled(
522 mut self,
523 label: impl Into<SharedString>,
524 href: impl Into<String>,
525 disabled: bool,
526 ) -> Self {
527 let href = href.into();
528 self.menu_items
529 .push(PopupMenuItem::link(label, href).disabled(disabled));
530 self
531 }
532
533 pub fn link_with_icon(
535 self,
536 label: impl Into<SharedString>,
537 icon: impl Into<Icon>,
538 href: impl Into<String>,
539 ) -> Self {
540 self.link_with_icon_and_disabled(label, icon, href, false)
541 }
542
543 fn link_with_icon_and_disabled(
545 mut self,
546 label: impl Into<SharedString>,
547 icon: impl Into<Icon>,
548 href: impl Into<String>,
549 disabled: bool,
550 ) -> Self {
551 let href = href.into();
552 self.menu_items.push(
553 PopupMenuItem::link(label, href)
554 .icon(icon)
555 .disabled(disabled),
556 );
557 self
558 }
559
560 pub fn menu_with_icon(
562 self,
563 label: impl Into<SharedString>,
564 icon: impl Into<Icon>,
565 action: Box<dyn Action>,
566 ) -> Self {
567 self.menu_with_icon_and_disabled(label, icon, action, false)
568 }
569
570 pub fn menu_with_icon_and_disabled(
572 mut self,
573 label: impl Into<SharedString>,
574 icon: impl Into<Icon>,
575 action: Box<dyn Action>,
576 disabled: bool,
577 ) -> Self {
578 self.add_menu_item(label, Some(icon.into()), action, disabled, false);
579 self
580 }
581
582 pub fn menu_with_check(
584 self,
585 label: impl Into<SharedString>,
586 checked: bool,
587 action: Box<dyn Action>,
588 ) -> Self {
589 self.menu_with_check_and_disabled(label, checked, action, false)
590 }
591
592 pub fn menu_with_check_and_disabled(
594 mut self,
595 label: impl Into<SharedString>,
596 checked: bool,
597 action: Box<dyn Action>,
598 disabled: bool,
599 ) -> Self {
600 self.checkable.insert(self.menu_items.len());
601 self.add_menu_item(label, None, action, disabled, checked);
602 self
603 }
604
605 pub fn menu_element<F, E>(self, action: Box<dyn Action>, builder: F) -> Self
607 where
608 F: Fn(&mut Window, &mut App) -> E + 'static,
609 E: IntoElement,
610 {
611 self.menu_element_with_check(false, action, builder)
612 }
613
614 pub fn menu_element_with_disabled<F, E>(
616 self,
617 action: Box<dyn Action>,
618 disabled: bool,
619 builder: F,
620 ) -> Self
621 where
622 F: Fn(&mut Window, &mut App) -> E + 'static,
623 E: IntoElement,
624 {
625 self.menu_element_with_check_and_disabled(false, action, disabled, builder)
626 }
627
628 pub fn menu_element_with_icon<F, E>(
630 self,
631 icon: impl Into<Icon>,
632 action: Box<dyn Action>,
633 builder: F,
634 ) -> Self
635 where
636 F: Fn(&mut Window, &mut App) -> E + 'static,
637 E: IntoElement,
638 {
639 self.menu_element_with_icon_and_disabled(icon, action, false, builder)
640 }
641
642 pub fn menu_element_with_check<F, E>(
644 self,
645 checked: bool,
646 action: Box<dyn Action>,
647 builder: F,
648 ) -> Self
649 where
650 F: Fn(&mut Window, &mut App) -> E + 'static,
651 E: IntoElement,
652 {
653 self.menu_element_with_check_and_disabled(checked, action, false, builder)
654 }
655
656 fn menu_element_with_icon_and_disabled<F, E>(
658 mut self,
659 icon: impl Into<Icon>,
660 action: Box<dyn Action>,
661 disabled: bool,
662 builder: F,
663 ) -> Self
664 where
665 F: Fn(&mut Window, &mut App) -> E + 'static,
666 E: IntoElement,
667 {
668 self.menu_items.push(
669 PopupMenuItem::element(builder)
670 .action(action)
671 .icon(icon)
672 .disabled(disabled),
673 );
674 self
675 }
676
677 fn menu_element_with_check_and_disabled<F, E>(
679 mut self,
680 checked: bool,
681 action: Box<dyn Action>,
682 disabled: bool,
683 builder: F,
684 ) -> Self
685 where
686 F: Fn(&mut Window, &mut App) -> E + 'static,
687 E: IntoElement,
688 {
689 self.checkable.insert(self.menu_items.len());
690 self.menu_items.push(
691 PopupMenuItem::element(builder)
692 .action(action)
693 .checked(checked)
694 .disabled(disabled),
695 );
696 self
697 }
698
699 pub fn separator(mut self) -> Self {
701 if self.menu_items.is_empty() {
702 return self;
703 }
704
705 if let Some(PopupMenuItem::Separator) = self.menu_items.last() {
706 return self;
707 }
708
709 self.menu_items.push(PopupMenuItem::separator());
710 self
711 }
712
713 pub fn submenu(
715 self,
716 label: impl Into<SharedString>,
717 window: &mut Window,
718 cx: &mut Context<Self>,
719 f: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
720 ) -> Self {
721 self.submenu_with_icon(None, label, window, cx, f)
722 }
723
724 pub fn submenu_with_icon(
726 mut self,
727 icon: Option<Icon>,
728 label: impl Into<SharedString>,
729 window: &mut Window,
730 cx: &mut Context<Self>,
731 f: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
732 ) -> Self {
733 let submenu = PopupMenu::build(window, cx, f);
734 let parent_menu = cx.entity().downgrade();
735 let parent_priority = self.priority;
736 submenu.update(cx, |view, _| {
737 view.parent_menu = Some(parent_menu);
738 view.priority = parent_priority + 1;
739 });
740
741 self.menu_items.push(
742 PopupMenuItem::submenu(label, submenu).when_some(icon, |this, icon| this.icon(icon)),
743 );
744 self
745 }
746
747 pub fn item(mut self, item: impl Into<PopupMenuItem>) -> Self {
749 let item: PopupMenuItem = item.into();
750 self.menu_items.push(item);
751 self
752 }
753
754 pub fn rebuild(
774 &mut self,
775 window: &mut Window,
776 cx: &mut Context<Self>,
777 f: impl FnOnce(Self, &mut Window, &mut Context<Self>) -> Self,
778 ) {
779 let mut menu = std::mem::replace(self, Self::new(cx));
780 menu.menu_items.clear();
781 menu.selected_index = None;
782 *self = f(menu, window, cx);
783 cx.notify();
784 }
785
786 fn add_menu_item(
787 &mut self,
788 label: impl Into<SharedString>,
789 icon: Option<Icon>,
790 action: Box<dyn Action>,
791 disabled: bool,
792 checked: bool,
793 ) -> &mut Self {
794 self.menu_items.push(
795 PopupMenuItem::new(label)
796 .when_some(icon, |item, icon| item.icon(icon))
797 .disabled(disabled)
798 .checked(checked)
799 .action(action),
800 );
801 self
802 }
803
804 pub(super) fn with_menu_items<I>(
805 mut self,
806 items: impl IntoIterator<Item = I>,
807 window: &mut Window,
808 cx: &mut Context<Self>,
809 ) -> Self
810 where
811 I: Into<OwnedMenuItem>,
812 {
813 for item in items {
814 match item.into() {
815 OwnedMenuItem::Action {
816 name,
817 action,
818 checked,
819 disabled,
820 ..
821 } => {
822 self = self.item(
825 PopupMenuItem::new(name)
826 .action(action.boxed_clone())
827 .disabled(disabled)
828 .checked(checked),
829 )
830 }
831 OwnedMenuItem::Separator => {
832 self = self.separator();
833 }
834 OwnedMenuItem::Submenu(submenu) => {
835 self = self.submenu(submenu.name, window, cx, move |menu, window, cx| {
836 menu.with_menu_items(submenu.items.clone(), window, cx)
837 })
838 }
839 OwnedMenuItem::SystemMenu(_) => {}
840 }
841 }
842
843 if self.menu_items.len() > 20 {
844 self.scrollable = true;
845 }
846
847 self
848 }
849
850 pub(crate) fn active_submenu(&self) -> Option<Entity<PopupMenu>> {
851 if let Some(ix) = self.selected_index {
852 if let Some(item) = self.menu_items.get(ix) {
853 return match item {
854 PopupMenuItem::Submenu { menu, .. } => Some(menu.clone()),
855 _ => None,
856 };
857 }
858 }
859
860 None
861 }
862
863 pub fn is_empty(&self) -> bool {
864 self.menu_items.is_empty()
865 }
866
867 fn clickable_menu_items(&self) -> impl Iterator<Item = (usize, &PopupMenuItem)> {
868 self.menu_items
869 .iter()
870 .enumerate()
871 .filter(|(_, item)| item.is_clickable())
872 }
873
874 fn on_click(
875 &mut self,
876 ix: usize,
877 event: &ClickEvent,
878 window: &mut Window,
879 cx: &mut Context<Self>,
880 ) {
881 cx.stop_propagation();
882 window.prevent_default();
883 self.selected_index = Some(ix);
884 self.activate(event, window, cx);
885 }
886
887 fn confirm(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
888 self.activate(&ClickEvent::default(), window, cx);
889 }
890
891 fn activate(&mut self, event: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
892 match self.selected_index {
893 Some(index) => {
894 let item = self.menu_items.get(index);
895 if !item.is_some_and(PopupMenuItem::is_clickable) {
896 return;
897 }
898 match item {
899 Some(PopupMenuItem::Item {
900 handler, action, ..
901 }) => {
902 if let Some(handler) = handler {
903 handler(event, window, cx);
904 } else if let Some(action) = action.as_ref() {
905 self.dispatch_confirm_action(action, window, cx);
906 }
907
908 self.dismiss(&Cancel, window, cx)
909 }
910 Some(PopupMenuItem::ElementItem {
911 handler, action, ..
912 }) => {
913 if let Some(handler) = handler {
914 handler(event, window, cx);
915 } else if let Some(action) = action.as_ref() {
916 self.dispatch_confirm_action(action, window, cx);
917 }
918 self.dismiss(&Cancel, window, cx)
919 }
920 Some(PopupMenuItem::Submenu {
921 disabled: false, ..
922 }) => {
923 self._select_submenu(window, cx);
924 }
925 _ => {}
926 }
927 }
928 _ => {}
929 }
930 }
931
932 fn dispatch_confirm_action(
933 &self,
934 action: &Box<dyn Action>,
935 window: &mut Window,
936 cx: &mut Context<Self>,
937 ) {
938 if let Some(context) = self.resolved_action_context(cx) {
939 context.focus(window, cx);
940 }
941
942 window.dispatch_action(action.boxed_clone(), cx);
943 }
944
945 fn resolved_action_context(&self, cx: &App) -> Option<FocusHandle> {
946 self.action_context.clone().or_else(|| {
947 let parent = self.parent_menu.as_ref()?.upgrade()?;
948 parent.read(cx).resolved_action_context(cx)
949 })
950 }
951
952 fn set_selected_index(&mut self, ix: usize, cx: &mut Context<Self>) {
953 if self.selected_index != Some(ix) {
954 self.selected_index = Some(ix);
955 self.scroll_handle.scroll_to_item(ix);
956 cx.notify();
957 }
958 }
959
960 fn scroll_to_checked_item(&mut self, cx: &mut Context<Self>) {
972 if !self.scrollable || self.scroll_to_checked_frames == 0 {
973 return;
974 }
975
976 let checked_ix = self.menu_items.iter().position(|item| item.is_checked());
979 let Some(ix) = checked_ix.filter(|ix| *ix > 0) else {
980 self.scroll_to_checked_frames = 0;
981 return;
982 };
983
984 match (
985 self.scroll_handle.bounds_for_item(0),
986 self.scroll_handle.bounds_for_item(ix),
987 ) {
988 (Some(first), Some(checked)) => {
989 self.scroll_to_checked_frames = 0;
990 let mut offset = self.scroll_handle.offset();
991 offset.y = first.top() - checked.top();
992 self.scroll_handle.set_offset(offset);
993 }
994 _ => {
995 self.scroll_to_checked_frames -= 1;
996 cx.notify();
997 }
998 }
999 }
1000
1001 fn select_up(&mut self, _: &SelectUp, _: &mut Window, cx: &mut Context<Self>) {
1002 cx.stop_propagation();
1003 let ix = self.selected_index.unwrap_or(0);
1004
1005 if let Some((prev_ix, _)) = self
1006 .menu_items
1007 .iter()
1008 .enumerate()
1009 .rev()
1010 .find(|(i, item)| *i < ix && item.is_clickable())
1011 {
1012 self.set_selected_index(prev_ix, cx);
1013 return;
1014 }
1015
1016 let last_clickable_ix = self.clickable_menu_items().last().map(|(ix, _)| ix);
1017 if let Some(ix) = last_clickable_ix {
1018 self.set_selected_index(ix, cx);
1019 }
1020 }
1021
1022 fn select_down(&mut self, _: &SelectDown, _: &mut Window, cx: &mut Context<Self>) {
1023 cx.stop_propagation();
1024 let Some(ix) = self.selected_index else {
1025 let first_clickable_ix = self.clickable_menu_items().next().map(|(ix, _)| ix);
1026 if let Some(ix) = first_clickable_ix {
1027 self.set_selected_index(ix, cx);
1028 }
1029 return;
1030 };
1031
1032 if let Some((next_ix, _)) = self
1033 .menu_items
1034 .iter()
1035 .enumerate()
1036 .find(|(i, item)| *i > ix && item.is_clickable())
1037 {
1038 self.set_selected_index(next_ix, cx);
1039 return;
1040 }
1041
1042 let first_clickable_ix = self.clickable_menu_items().next().map(|(ix, _)| ix);
1043 if let Some(ix) = first_clickable_ix {
1044 self.set_selected_index(ix, cx);
1045 }
1046 }
1047
1048 fn select_left(&mut self, _: &SelectLeft, window: &mut Window, cx: &mut Context<Self>) {
1049 let handled = if matches!(self.submenu_anchor.0, Anchor::TopLeft | Anchor::BottomLeft) {
1050 self._unselect_submenu(window, cx)
1051 } else {
1052 self._select_submenu(window, cx)
1053 };
1054
1055 if self.parent_side(cx).is_left() {
1056 self._focus_parent_menu(window, cx);
1057 }
1058
1059 if handled {
1060 return;
1061 }
1062
1063 if self.parent_menu.is_none() {
1065 cx.propagate();
1066 }
1067 }
1068
1069 fn select_right(&mut self, _: &SelectRight, window: &mut Window, cx: &mut Context<Self>) {
1070 let handled = if matches!(self.submenu_anchor.0, Anchor::TopLeft | Anchor::BottomLeft) {
1071 self._select_submenu(window, cx)
1072 } else {
1073 self._unselect_submenu(window, cx)
1074 };
1075
1076 if self.parent_side(cx).is_right() {
1077 self._focus_parent_menu(window, cx);
1078 }
1079
1080 if handled {
1081 return;
1082 }
1083
1084 if self.parent_menu.is_none() {
1086 cx.propagate();
1087 }
1088 }
1089
1090 fn _select_submenu(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
1091 if let Some(active_submenu) = self.active_submenu() {
1092 active_submenu.update(cx, |view, cx| {
1094 let first_clickable_ix = view.clickable_menu_items().next().map(|(ix, _)| ix);
1095 if let Some(ix) = first_clickable_ix {
1096 view.set_selected_index(ix, cx);
1097 } else {
1098 view.selected_index = None;
1099 cx.notify();
1100 }
1101 view.focus_handle.focus(window, cx);
1102 });
1103 cx.notify();
1104 return true;
1105 }
1106
1107 return false;
1108 }
1109
1110 fn _unselect_submenu(&mut self, _: &mut Window, cx: &mut Context<Self>) -> bool {
1111 if let Some(active_submenu) = self.active_submenu() {
1112 active_submenu.update(cx, |view, cx| {
1113 view.selected_index = None;
1114 cx.notify();
1115 });
1116 return true;
1117 }
1118
1119 return false;
1120 }
1121
1122 fn _focus_parent_menu(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1123 let Some(parent) = self.parent_menu.as_ref() else {
1124 return;
1125 };
1126 let Some(parent) = parent.upgrade() else {
1127 return;
1128 };
1129
1130 self.selected_index = None;
1131 parent.update(cx, |view, cx| {
1132 view.focus_handle.focus(window, cx);
1133 cx.notify();
1134 });
1135 }
1136
1137 fn parent_side(&self, cx: &App) -> Side {
1138 let Some(parent) = self.parent_menu.as_ref() else {
1139 return Side::Left;
1140 };
1141
1142 let Some(parent) = parent.upgrade() else {
1143 return Side::Left;
1144 };
1145
1146 match parent.read(cx).submenu_anchor.0 {
1147 Anchor::TopLeft | Anchor::BottomLeft => Side::Left,
1148 Anchor::TopRight | Anchor::BottomRight => Side::Right,
1149 _ => Side::Left,
1151 }
1152 }
1153
1154 fn dismiss(&mut self, _: &Cancel, window: &mut Window, cx: &mut Context<Self>) {
1158 self.selected_index = None;
1159 cx.emit(DismissEvent);
1160
1161 let focus_moved_away =
1165 window.focused(cx).is_some() && !self.focus_handle.contains_focused(window, cx);
1166 if !focus_moved_away {
1167 if let Some(handle) = self
1168 .previous_focus_handle
1169 .as_ref()
1170 .or(self.action_context.as_ref())
1171 {
1172 window.focus(handle, cx);
1173 }
1174 }
1175
1176 let Some(parent_menu) = self.parent_menu.clone() else {
1177 return;
1178 };
1179
1180 _ = parent_menu.update(cx, |view, cx| {
1182 view.dismiss(&Cancel, window, cx);
1183 });
1184 }
1185
1186 fn handle_dismiss(
1187 &mut self,
1188 position: &Point<Pixels>,
1189 window: &mut Window,
1190 cx: &mut Context<Self>,
1191 ) {
1192 if let Some(parent) = self.parent_menu.as_ref() {
1194 if let Some(parent) = parent.upgrade() {
1195 if parent.read(cx).bounds.contains(position) {
1196 return;
1197 }
1198 }
1199 }
1200
1201 if self.active_submenu().is_some() {
1206 return;
1207 }
1208
1209 self.dismiss(&Cancel, window, cx);
1210 }
1211
1212 fn on_mouse_down_out(
1213 &mut self,
1214 e: &MouseDownEvent,
1215 window: &mut Window,
1216 cx: &mut Context<Self>,
1217 ) {
1218 self.handle_dismiss(&e.position, window, cx);
1219 }
1220
1221 fn render_key_binding(
1222 &self,
1223 action: Option<Box<dyn Action>>,
1224 window: &mut Window,
1225 cx: &mut Context<Self>,
1226 ) -> Option<Kbd> {
1227 let action = action?;
1228
1229 let action_context = self.resolved_action_context(cx);
1238 [
1239 action_context.as_ref(),
1240 self.trigger_focus_handle.as_ref(),
1241 self.previous_focus_handle.as_ref(),
1242 Some(&self.focus_handle),
1243 ]
1244 .into_iter()
1245 .flatten()
1246 .find_map(|handle| Kbd::binding_for_action_in(action.as_ref(), handle, window))
1247 .or_else(|| Kbd::global_binding_for_action(action.as_ref(), window))
1248 .map(|this| {
1249 this.p_0()
1250 .flex_nowrap()
1251 .border_0()
1252 .bg(gpui::transparent_white())
1253 })
1254 }
1255
1256 fn render_icon(
1257 has_icon: bool,
1258 checked: bool,
1259 icon: Option<Icon>,
1260 _: &mut Window,
1261 _: &mut Context<Self>,
1262 ) -> Option<impl IntoElement> {
1263 if !has_icon {
1264 return None;
1265 }
1266
1267 let icon = if let Some(icon) = icon {
1268 icon
1269 } else if checked {
1270 Icon::new(IconName::Check)
1271 } else {
1272 Icon::empty()
1273 };
1274
1275 Some(icon.xsmall())
1276 }
1277
1278 fn update_submenu_menu_anchor(&mut self, window: &Window, cx: &App) {
1280 let bounds = self.bounds;
1281 let submenu_width = self
1284 .selected_index
1285 .and_then(|ix| match self.menu_items.get(ix) {
1286 Some(PopupMenuItem::Submenu { menu, .. }) => Some(menu.read(cx).bounds.size.width),
1287 _ => None,
1288 })
1289 .filter(|width| *width > Pixels::ZERO)
1290 .unwrap_or(bounds.size.width);
1291 let rem_size = window.rem_size();
1292 let open_left = if LayoutDirection::global(cx).is_rtl() {
1295 bounds.left() - submenu_width >= Pixels::ZERO
1296 } else {
1297 bounds.right() + submenu_width > window.viewport_size().width
1298 };
1299 let (anchor, left) = if open_left {
1300 (Anchor::TopRight, -rems(1.).to_pixels(rem_size))
1301 } else {
1302 (
1303 Anchor::TopLeft,
1304 bounds.size.width - rems(0.5).to_pixels(rem_size),
1305 )
1306 };
1307
1308 let is_bottom_pos = bounds.origin.y + bounds.size.height > window.bounds().size.height;
1309 self.submenu_anchor = if is_bottom_pos {
1310 (anchor.other_side_along(gpui::Axis::Vertical), left)
1311 } else {
1312 (anchor, left)
1313 };
1314 }
1315
1316 fn render_item(
1317 &self,
1318 ix: usize,
1319 item: &PopupMenuItem,
1320 options: RenderOptions,
1321 window: &mut Window,
1322 cx: &mut Context<Self>,
1323 ) -> MenuItemElement {
1324 let has_left_icon = options.has_left_icon;
1325 let is_left_check = options.check_side.is_left() && item.is_checked();
1326 let right_check_icon = if options.check_side.is_right() && item.is_checked() {
1327 Some(Icon::new(IconName::Check).xsmall())
1328 } else {
1329 None
1330 };
1331
1332 let selected = self.selected_index == Some(ix);
1333 const EDGE_PADDING: Pixels = px(4.);
1334 const INNER_PADDING: Rems = rems(0.5);
1335
1336 let is_submenu = matches!(item, PopupMenuItem::Submenu { .. });
1337
1338 let (item_height, radius) = match self.size {
1339 Size::Small => (rems(1.25), options.radius.half()),
1340 _ => (rems(1.625), options.radius),
1341 };
1342
1343 let this = MenuItemElement::new(ix)
1344 .relative()
1345 .text_sm()
1346 .py_0()
1347 .px(INNER_PADDING)
1348 .rounded(radius)
1349 .items_center()
1350 .selected(selected)
1351 .on_hover(cx.listener(move |this, hovered, window, cx| {
1352 if *hovered {
1353 if this.selected_index != Some(ix)
1356 && this.active_submenu().is_some_and(|submenu| {
1357 submenu.focus_handle(cx).contains_focused(window, cx)
1358 })
1359 {
1360 this.focus_handle.focus(window, cx);
1361 }
1362 this.selected_index = Some(ix);
1363 } else if !is_submenu
1364 && this.selected_index == Some(ix)
1365 && !window.last_input_was_keyboard()
1368 {
1369 this.selected_index = None;
1371 }
1372
1373 cx.notify();
1374 }))
1375 .when_some(item.a11y_label(), |this, label| this.aria_label(label))
1376 .checked(
1377 (item.is_checked() || self.checkable.contains(&ix)).then(|| item.is_checked()),
1378 );
1379
1380 match item {
1381 PopupMenuItem::Separator => this
1382 .h_auto()
1383 .p_0()
1384 .my_0p5()
1385 .mx_neg_1()
1386 .border_b(px(2.))
1387 .border_color(cx.theme().border)
1388 .disabled(true),
1389 PopupMenuItem::Label(label) => this.disabled(true).cursor_default().child(
1390 h_flex()
1391 .flex_row_dir(cx)
1392 .cursor_default()
1393 .items_center()
1394 .gap_x_1()
1395 .children(Self::render_icon(has_left_icon, false, None, window, cx))
1396 .child(div().flex_1().child(label.clone())),
1397 ),
1398 PopupMenuItem::ElementItem {
1399 render,
1400 icon,
1401 disabled,
1402 ..
1403 } => this
1404 .when(!disabled, |this| {
1405 this.on_click(cx.listener(move |this, event, window, cx| {
1406 this.on_click(ix, event, window, cx)
1407 }))
1408 })
1409 .disabled(*disabled)
1410 .child(
1411 h_flex()
1412 .flex_row_dir(cx)
1413 .flex_1()
1414 .min_h(item_height)
1415 .items_center()
1416 .gap_x_1()
1417 .children(Self::render_icon(
1418 has_left_icon,
1419 is_left_check,
1420 icon.clone(),
1421 window,
1422 cx,
1423 ))
1424 .child((render)(window, cx))
1425 .children(right_check_icon.map(|icon| icon.ms(rems(0.75), cx))),
1426 ),
1427 PopupMenuItem::Item {
1428 icon,
1429 label,
1430 action,
1431 disabled,
1432 is_link,
1433 ..
1434 } => {
1435 let show_link_icon = *is_link && self.external_link_icon;
1436 let action = action.as_ref().map(|action| action.boxed_clone());
1437 let key = self.render_key_binding(action, window, cx);
1438
1439 this.when(!disabled, |this| {
1440 this.on_click(cx.listener(move |this, event, window, cx| {
1441 this.on_click(ix, event, window, cx)
1442 }))
1443 })
1444 .disabled(*disabled)
1445 .h(item_height)
1446 .gap_x_1()
1447 .children(Self::render_icon(
1448 has_left_icon,
1449 is_left_check,
1450 icon.clone(),
1451 window,
1452 cx,
1453 ))
1454 .child(
1455 h_flex()
1456 .flex_row_dir(cx)
1457 .w_full()
1458 .gap_3()
1459 .items_center()
1460 .justify_between()
1461 .when(!show_link_icon, |this| this.child(label.clone()))
1462 .children(right_check_icon)
1463 .when(show_link_icon, |this| {
1464 this.child(
1465 h_flex()
1466 .flex_row_dir(cx)
1467 .w_full()
1468 .justify_between()
1469 .gap_1p5()
1470 .child(label.clone())
1471 .child(
1472 Icon::new(IconName::ExternalLink)
1473 .xsmall()
1474 .text_color(cx.theme().muted_foreground),
1475 ),
1476 )
1477 })
1478 .children(key),
1479 )
1480 }
1481 PopupMenuItem::Submenu {
1482 icon,
1483 label,
1484 menu,
1485 disabled,
1486 } => this
1487 .when(!disabled, |this| {
1488 this.on_click(cx.listener(move |this, event, window, cx| {
1489 this.on_click(ix, event, window, cx)
1490 }))
1491 })
1492 .selected(selected)
1493 .disabled(*disabled)
1494 .items_start()
1495 .child(
1496 h_flex()
1497 .flex_row_dir(cx)
1498 .min_h(item_height)
1499 .size_full()
1500 .items_center()
1501 .gap_x_1()
1502 .children(Self::render_icon(
1503 has_left_icon,
1504 false,
1505 icon.clone(),
1506 window,
1507 cx,
1508 ))
1509 .child(
1510 h_flex()
1511 .flex_row_dir(cx)
1512 .flex_1()
1513 .gap_2()
1514 .items_center()
1515 .justify_between()
1516 .child(label.clone())
1517 .child(
1518 Icon::new(if LayoutDirection::global(cx).is_rtl() {
1519 IconName::ChevronLeft
1520 } else {
1521 IconName::ChevronRight
1522 })
1523 .xsmall()
1524 .text_color(cx.theme().muted_foreground),
1525 ),
1526 ),
1527 )
1528 .when(selected, |this| {
1529 this.child({
1530 let (anchor, left) = self.submenu_anchor;
1531 let is_bottom_pos =
1532 matches!(anchor, Anchor::BottomLeft | Anchor::BottomRight);
1533 deferred(
1534 anchored()
1535 .anchor(anchor)
1536 .child(
1537 div()
1538 .id("submenu")
1539 .test_support()
1540 .occlude()
1541 .when(is_bottom_pos, |this| this.bottom_0())
1542 .when(!is_bottom_pos, |this| this.top_neg_1())
1543 .left(left)
1544 .child(menu.clone()),
1545 )
1546 .snap_to_window_with_margin(Edges::all(EDGE_PADDING)),
1547 )
1548 .with_priority(self.priority + 1)
1549 })
1550 }),
1551 }
1552 }
1553}
1554
1555impl FluentBuilder for PopupMenu {}
1556impl EventEmitter<DismissEvent> for PopupMenu {}
1557impl Focusable for PopupMenu {
1558 fn focus_handle(&self, _: &App) -> FocusHandle {
1559 self.focus_handle.clone()
1560 }
1561}
1562
1563#[derive(Clone, Copy)]
1564struct RenderOptions {
1565 has_left_icon: bool,
1566 check_side: Side,
1567 radius: Pixels,
1568}
1569
1570impl Render for PopupMenu {
1571 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1572 self.update_submenu_menu_anchor(window, cx);
1573
1574 let parent = cx.entity().downgrade();
1580 let parent_priority = self.priority;
1581 for item in &self.menu_items {
1582 if let PopupMenuItem::Submenu { menu, .. } = item {
1583 if menu.read(cx).parent_menu.is_none() {
1584 menu.update(cx, |menu, _| {
1585 menu.parent_menu = Some(parent.clone());
1586 menu.priority = parent_priority + 1;
1587 });
1588 }
1589 }
1590 }
1591
1592 self.scroll_to_checked_item(cx);
1593
1594 let view = cx.entity();
1595 let items_count = self.menu_items.len();
1596
1597 let max_height = self.max_height.unwrap_or_else(|| {
1598 let window_half_height = window.window_bounds().get_bounds().size.height * 0.5;
1599 window_half_height.min(rems(28.125).to_pixels(window.rem_size()))
1600 });
1601
1602 let has_left_icon = self
1603 .menu_items
1604 .iter()
1605 .any(|item| item.has_left_icon(self.check_side));
1606
1607 let options = RenderOptions {
1608 has_left_icon,
1609 check_side: self.check_side,
1610 radius: cx.theme().radius.min(px(8.)),
1611 };
1612
1613 v_flex()
1614 .id("popup-menu")
1615 .test_support()
1616 .role(Role::Menu)
1617 .key_context(CONTEXT)
1618 .track_focus(&self.focus_handle)
1619 .on_action(cx.listener(Self::select_up))
1620 .on_action(cx.listener(Self::select_down))
1621 .on_action(cx.listener(Self::select_left))
1622 .on_action(cx.listener(Self::select_right))
1623 .on_action(cx.listener(Self::confirm))
1624 .on_action(cx.listener(Self::dismiss))
1625 .on_mouse_down_out(cx.listener(Self::on_mouse_down_out))
1626 .popover_style(cx)
1627 .text_color(cx.theme().popover_foreground)
1628 .relative()
1629 .occlude()
1630 .child(
1631 v_flex()
1632 .id("items")
1633 .p_1()
1634 .gap_y_0p5()
1635 .min_w(rems(8.))
1636 .when_some(self.min_width, |this, min_width| this.min_w(min_width))
1637 .map(|this| match self.max_width {
1638 Some(max_width) => this.max_w(max_width),
1639 None => this.max_w(rems(31.25)),
1640 })
1641 .when(self.scrollable, |this| {
1642 this.max_h(max_height)
1643 .overflow_y_scroll()
1644 .track_scroll(&self.scroll_handle)
1645 })
1646 .children(
1647 self.menu_items
1648 .iter()
1649 .enumerate()
1650 .filter(|(ix, item)| !(*ix + 1 == items_count && item.is_separator()))
1652 .map(|(ix, item)| self.render_item(ix, item, options, window, cx)),
1653 )
1654 .on_prepaint(move |bounds, _, cx| {
1655 view.update(cx, |menu, cx| {
1656 let width_changed = menu.bounds.size.width != bounds.size.width;
1657 menu.bounds = bounds;
1658 if width_changed {
1659 if let Some(parent) = menu
1660 .parent_menu
1661 .as_ref()
1662 .and_then(|parent| parent.upgrade())
1663 {
1664 parent.update(cx, |_, cx| cx.notify());
1665 }
1666 }
1667 });
1668 }),
1669 )
1670 .when(self.scrollable, |this| {
1671 this.vertical_scrollbar(&self.scroll_handle)
1672 })
1673 }
1674}
1675
1676#[cfg(test)]
1677mod tests {
1678 use super::*;
1679
1680 #[gpui::test]
1681 fn custom_menu_item_preserves_mouse_event(cx: &mut gpui::TestAppContext) {
1682 use gpui::{Modifiers, MouseButton, MouseUpEvent};
1683 use std::cell::RefCell;
1684
1685 cx.update(crate::init);
1686 let events = Rc::new(RefCell::new(Vec::new()));
1687 let (_, cx) = cx.add_window_view({
1688 let events = events.clone();
1689 move |_, cx| {
1690 PopupMenu::new(cx).item(
1691 PopupMenuItem::element(|_, _| {
1692 div()
1693 .debug_selector(|| "custom-menu-content".into())
1694 .w(px(100.))
1695 .h(px(20.))
1696 .child("Custom")
1697 })
1698 .on_click(move |event, _, _| events.borrow_mut().push(event.clone())),
1699 )
1700 }
1701 });
1702 cx.update(|window, cx| window.draw(cx).clear(cx));
1703 let position = cx.debug_bounds("custom-menu-content").unwrap().center();
1704 let modifiers = Modifiers {
1705 shift: true,
1706 ..Modifiers::default()
1707 };
1708 cx.simulate_mouse_down(position, MouseButton::Left, modifiers);
1709 cx.simulate_event(MouseUpEvent {
1710 position,
1711 button: MouseButton::Left,
1712 modifiers,
1713 click_count: 2,
1714 });
1715 cx.run_until_parked();
1716 let events = events.borrow();
1717 assert_eq!(events.len(), 1);
1718 assert_eq!(events[0].mouse_position(), Some(position));
1719 assert_eq!(events[0].modifiers(), modifiers);
1720 let ClickEvent::Mouse(event) = &events[0] else {
1721 panic!("expected the original mouse click");
1722 };
1723 assert_eq!(event.up.click_count, 2);
1724 }
1725
1726 #[gpui::test]
1727 fn popup_menu_item_a11y_label_uses_visible_label(cx: &mut gpui::TestAppContext) {
1728 let submenu = cx.update(|cx| cx.new(|cx| PopupMenu::new(cx)));
1729
1730 assert_eq!(PopupMenuItem::new("Open").a11y_label(), Some("Open".into()));
1731 assert_eq!(
1732 PopupMenuItem::link("Docs", "https://example.com").a11y_label(),
1733 Some("Docs".into())
1734 );
1735 assert_eq!(
1736 PopupMenuItem::label("Recent files").a11y_label(),
1737 Some("Recent files".into())
1738 );
1739 assert_eq!(
1740 PopupMenuItem::submenu("More", submenu).a11y_label(),
1741 Some("More".into())
1742 );
1743 assert_eq!(PopupMenuItem::separator().a11y_label(), None);
1744 assert_eq!(PopupMenuItem::element(|_, _| div()).a11y_label(), None);
1745 }
1746}