| 1 | //! Fallback popup menu for platforms without an OS-native popup (e.g. Linux). |
| 2 | //! |
| 3 | //! It renders gpui-component's drawn [`PopupMenu`] through an overlay held by |
| 4 | //! [`crate::Root`]. Unlike a real native menu it is clipped to the window, but it keeps |
| 5 | //! the [`super::NativeMenu`] API working on every platform. |
| 6 | |
| 7 | use crate::root::WindowState; |
| 8 | use gpui::{ |
| 9 | App, Context, DismissEvent, Entity, FocusHandle, Focusable, IntoElement, ParentElement, Pixels, |
| 10 | Point, Render, Subscription, Window, anchored, deferred, div, px, |
| 11 | }; |
| 12 | |
| 13 | use crate::menu::{PopupMenu, PopupMenuItem}; |
| 14 | |
| 15 | use super::NativeMenuItem; |
| 16 | |
| 17 | /// Overlay held by [`crate::Root`] that renders the active fallback popup menu, if any. |
| 18 | pub(crate) struct FallbackMenuOverlay { |
| 19 | active: Option<ActiveMenu>, |
| 20 | } |
| 21 | |
| 22 | struct ActiveMenu { |
| 23 | menu: Entity<PopupMenu>, |
| 24 | position: Point<Pixels>, |
| 25 | _subscription: Subscription, |
| 26 | } |
| 27 | |
| 28 | impl FallbackMenuOverlay { |
| 29 | pub(crate) fn new() -> Self { |
| 30 | Self { active: None } |
| 31 | } |
| 32 | |
| 33 | /// Build a [`PopupMenu`] from `items` and show it anchored at `position`. |
| 34 | /// |
| 35 | /// `action_context` is the focus handle the selected action is dispatched |
| 36 | /// to, matching the native backends (which dispatch to the window's focus). |
| 37 | #[allow(dead_code)] // Only used where the native menu falls back (e.g. Linux). |
| 38 | fn show( |
| 39 | &mut self, |
| 40 | items: Vec<NativeMenuItem>, |
| 41 | position: Point<Pixels>, |
| 42 | action_context: Option<FocusHandle>, |
| 43 | window: &mut Window, |
| 44 | cx: &mut Context<Self>, |
| 45 | ) { |
| 46 | let menu = build_popup(items, action_context, window, cx); |
| 47 | |
| 48 | let subscription = cx.subscribe(&menu, |this, _, _: &DismissEvent, cx| { |
| 49 | this.active = None; |
| 50 | cx.notify(); |
| 51 | }); |
| 52 | menu.focus_handle(cx).focus(window, cx); |
| 53 | |
| 54 | self.active = Some(ActiveMenu { |
| 55 | menu, |
| 56 | position, |
| 57 | _subscription: subscription, |
| 58 | }); |
| 59 | cx.notify(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// Recursively build a [`PopupMenu`] entity from native menu items. |
| 64 | #[allow(dead_code)] // Only used where the native menu falls back (e.g. Linux). |
| 65 | fn build_popup( |
| 66 | items: Vec<NativeMenuItem>, |
| 67 | action_context: Option<FocusHandle>, |
| 68 | window: &mut Window, |
| 69 | cx: &mut App, |
| 70 | ) -> Entity<PopupMenu> { |
| 71 | PopupMenu::build(window, cx, move |mut menu, window, cx| { |
| 72 | if let Some(handle) = action_context.clone() { |
| 73 | menu = menu.action_context(handle); |
| 74 | } |
| 75 | for item in items { |
| 76 | menu = match item { |
| 77 | NativeMenuItem::Separator => menu.separator(), |
| 78 | NativeMenuItem::Item { |
| 79 | label, |
| 80 | disabled, |
| 81 | checked, |
| 82 | icon: Some(icon), |
| 83 | action: Some(action), |
| 84 | } => menu.item( |
| 85 | PopupMenuItem::new(label) |
| 86 | .icon(*icon) |
| 87 | .action(action) |
| 88 | .disabled(disabled) |
| 89 | .checked(checked), |
| 90 | ), |
| 91 | NativeMenuItem::Item { |
| 92 | label, |
| 93 | disabled, |
| 94 | checked, |
| 95 | icon: None, |
| 96 | action: Some(action), |
| 97 | } => menu.menu_with_check_and_disabled(label, checked, action, disabled), |
| 98 | NativeMenuItem::Item { action: None, .. } => menu, |
| 99 | NativeMenuItem::Submenu { |
| 100 | label, |
| 101 | disabled, |
| 102 | items, |
| 103 | } => { |
| 104 | let submenu = build_popup(items, action_context.clone(), window, cx); |
| 105 | menu.item(PopupMenuItem::submenu(label, submenu).disabled(disabled)) |
| 106 | } |
| 107 | }; |
| 108 | } |
| 109 | menu |
| 110 | }) |
| 111 | } |
| 112 | |
| 113 | impl Render for FallbackMenuOverlay { |
| 114 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 115 | let mut root = div(); |
| 116 | if let Some(active) = self.active.as_ref() { |
| 117 | root = root.child( |
| 118 | deferred( |
| 119 | anchored() |
| 120 | .position(active.position) |
| 121 | .snap_to_window_with_margin(px(8.)) |
| 122 | .child(active.menu.clone()), |
| 123 | ) |
| 124 | .with_priority(gpui_base::POPUP_PRIORITY), |
| 125 | ); |
| 126 | } |
| 127 | root |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /// Show the fallback popup menu through [`crate::Root`]'s overlay. |
| 132 | #[allow(dead_code)] // Only called where the native menu falls back (e.g. Linux). |
| 133 | pub(super) fn show( |
| 134 | items: Vec<NativeMenuItem>, |
| 135 | position: Point<Pixels>, |
| 136 | window: &mut Window, |
| 137 | cx: &mut App, |
| 138 | ) { |
| 139 | let action_context = window.focused(cx); |
| 140 | let Some(overlay) = WindowState::native_menu_overlay(window, cx) else { |
| 141 | return; |
| 142 | }; |
| 143 | overlay.update(cx, |overlay, cx| { |
| 144 | overlay.show(items, position, action_context, window, cx); |
| 145 | }); |
| 146 | } |