right_click_menu.rs

  1#![allow(missing_docs)]
  2
  3use std::{cell::RefCell, rc::Rc};
  4
  5use gpui::{
  6    anchored, deferred, div, px, AnchorCorner, AnyElement, Bounds, DismissEvent, DispatchPhase,
  7    Element, ElementId, GlobalElementId, Hitbox, InteractiveElement, IntoElement, LayoutId,
  8    ManagedView, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, View, VisualContext,
  9    WindowContext,
 10};
 11
 12pub struct RightClickMenu<M: ManagedView> {
 13    id: ElementId,
 14    child_builder: Option<Box<dyn FnOnce(bool) -> AnyElement + 'static>>,
 15    menu_builder: Option<Rc<dyn Fn(&mut WindowContext) -> View<M> + 'static>>,
 16    anchor: Option<AnchorCorner>,
 17    attach: Option<AnchorCorner>,
 18}
 19
 20impl<M: ManagedView> RightClickMenu<M> {
 21    pub fn menu(mut self, f: impl Fn(&mut WindowContext) -> View<M> + 'static) -> Self {
 22        self.menu_builder = Some(Rc::new(f));
 23        self
 24    }
 25
 26    pub fn trigger<E: IntoElement + 'static>(mut self, e: E) -> Self {
 27        self.child_builder = Some(Box::new(move |_| e.into_any_element()));
 28        self
 29    }
 30
 31    /// anchor defines which corner of the menu to anchor to the attachment point
 32    /// (by default the cursor position, but see attach)
 33    pub fn anchor(mut self, anchor: AnchorCorner) -> Self {
 34        self.anchor = Some(anchor);
 35        self
 36    }
 37
 38    /// attach defines which corner of the handle to attach the menu's anchor to
 39    pub fn attach(mut self, attach: AnchorCorner) -> Self {
 40        self.attach = Some(attach);
 41        self
 42    }
 43
 44    fn with_element_state<R>(
 45        &mut self,
 46        global_id: &GlobalElementId,
 47        cx: &mut WindowContext,
 48        f: impl FnOnce(&mut Self, &mut MenuHandleElementState<M>, &mut WindowContext) -> R,
 49    ) -> R {
 50        cx.with_optional_element_state::<MenuHandleElementState<M>, _>(
 51            Some(global_id),
 52            |element_state, cx| {
 53                let mut element_state = element_state.unwrap().unwrap_or_default();
 54                let result = f(self, &mut element_state, cx);
 55                (result, Some(element_state))
 56            },
 57        )
 58    }
 59}
 60
 61/// Creates a [`RightClickMenu`]
 62pub fn right_click_menu<M: ManagedView>(id: impl Into<ElementId>) -> RightClickMenu<M> {
 63    RightClickMenu {
 64        id: id.into(),
 65        child_builder: None,
 66        menu_builder: None,
 67        anchor: None,
 68        attach: None,
 69    }
 70}
 71
 72pub struct MenuHandleElementState<M> {
 73    menu: Rc<RefCell<Option<View<M>>>>,
 74    position: Rc<RefCell<Point<Pixels>>>,
 75}
 76
 77impl<M> Clone for MenuHandleElementState<M> {
 78    fn clone(&self) -> Self {
 79        Self {
 80            menu: Rc::clone(&self.menu),
 81            position: Rc::clone(&self.position),
 82        }
 83    }
 84}
 85
 86impl<M> Default for MenuHandleElementState<M> {
 87    fn default() -> Self {
 88        Self {
 89            menu: Rc::default(),
 90            position: Rc::default(),
 91        }
 92    }
 93}
 94
 95pub struct RequestLayoutState {
 96    child_layout_id: Option<LayoutId>,
 97    child_element: Option<AnyElement>,
 98    menu_element: Option<AnyElement>,
 99}
100
101pub struct PrepaintState {
102    hitbox: Hitbox,
103    child_bounds: Option<Bounds<Pixels>>,
104}
105
106impl<M: ManagedView> Element for RightClickMenu<M> {
107    type RequestLayoutState = RequestLayoutState;
108    type PrepaintState = PrepaintState;
109
110    fn id(&self) -> Option<ElementId> {
111        Some(self.id.clone())
112    }
113
114    fn request_layout(
115        &mut self,
116        id: Option<&GlobalElementId>,
117        cx: &mut WindowContext,
118    ) -> (gpui::LayoutId, Self::RequestLayoutState) {
119        self.with_element_state(id.unwrap(), cx, |this, element_state, cx| {
120            let mut menu_layout_id = None;
121
122            let menu_element = element_state.menu.borrow_mut().as_mut().map(|menu| {
123                let mut anchored = anchored().snap_to_window_with_margin(px(8.));
124                if let Some(anchor) = this.anchor {
125                    anchored = anchored.anchor(anchor);
126                }
127                anchored = anchored.position(*element_state.position.borrow());
128
129                let mut element = deferred(anchored.child(div().occlude().child(menu.clone())))
130                    .with_priority(1)
131                    .into_any();
132
133                menu_layout_id = Some(element.request_layout(cx));
134                element
135            });
136
137            let mut child_element = this
138                .child_builder
139                .take()
140                .map(|child_builder| (child_builder)(element_state.menu.borrow().is_some()));
141
142            let child_layout_id = child_element
143                .as_mut()
144                .map(|child_element| child_element.request_layout(cx));
145
146            let layout_id = cx.request_layout(
147                gpui::Style::default(),
148                menu_layout_id.into_iter().chain(child_layout_id),
149            );
150
151            (
152                layout_id,
153                RequestLayoutState {
154                    child_element,
155                    child_layout_id,
156                    menu_element,
157                },
158            )
159        })
160    }
161
162    fn prepaint(
163        &mut self,
164        _id: Option<&GlobalElementId>,
165        bounds: Bounds<Pixels>,
166        request_layout: &mut Self::RequestLayoutState,
167        cx: &mut WindowContext,
168    ) -> PrepaintState {
169        let hitbox = cx.insert_hitbox(bounds, false);
170
171        if let Some(child) = request_layout.child_element.as_mut() {
172            child.prepaint(cx);
173        }
174
175        if let Some(menu) = request_layout.menu_element.as_mut() {
176            menu.prepaint(cx);
177        }
178
179        PrepaintState {
180            hitbox,
181            child_bounds: request_layout
182                .child_layout_id
183                .map(|layout_id| cx.layout_bounds(layout_id)),
184        }
185    }
186
187    fn paint(
188        &mut self,
189        id: Option<&GlobalElementId>,
190        _bounds: Bounds<gpui::Pixels>,
191        request_layout: &mut Self::RequestLayoutState,
192        prepaint_state: &mut Self::PrepaintState,
193        cx: &mut WindowContext,
194    ) {
195        self.with_element_state(id.unwrap(), cx, |this, element_state, cx| {
196            if let Some(mut child) = request_layout.child_element.take() {
197                child.paint(cx);
198            }
199
200            if let Some(mut menu) = request_layout.menu_element.take() {
201                menu.paint(cx);
202                return;
203            }
204
205            let Some(builder) = this.menu_builder.take() else {
206                return;
207            };
208
209            let attach = this.attach;
210            let menu = element_state.menu.clone();
211            let position = element_state.position.clone();
212            let child_bounds = prepaint_state.child_bounds;
213
214            let hitbox_id = prepaint_state.hitbox.id;
215            cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| {
216                if phase == DispatchPhase::Bubble
217                    && event.button == MouseButton::Right
218                    && hitbox_id.is_hovered(cx)
219                {
220                    cx.stop_propagation();
221                    cx.prevent_default();
222
223                    let new_menu = (builder)(cx);
224                    let menu2 = menu.clone();
225                    let previous_focus_handle = cx.focused();
226
227                    cx.subscribe(&new_menu, move |modal, _: &DismissEvent, cx| {
228                        if modal.focus_handle(cx).contains_focused(cx) {
229                            if let Some(previous_focus_handle) = previous_focus_handle.as_ref() {
230                                cx.focus(previous_focus_handle);
231                            }
232                        }
233                        *menu2.borrow_mut() = None;
234                        cx.refresh();
235                    })
236                    .detach();
237                    cx.focus_view(&new_menu);
238                    *menu.borrow_mut() = Some(new_menu);
239                    *position.borrow_mut() = if let Some(child_bounds) = child_bounds {
240                        if let Some(attach) = attach {
241                            attach.corner(child_bounds)
242                        } else {
243                            cx.mouse_position()
244                        }
245                    } else {
246                        cx.mouse_position()
247                    };
248                    cx.refresh();
249                }
250            });
251        })
252    }
253}
254
255impl<M: ManagedView> IntoElement for RightClickMenu<M> {
256    type Element = Self;
257
258    fn into_element(self) -> Self::Element {
259        self
260    }
261}