1use std::{cell::RefCell, rc::Rc};
2
3use gpui::{
4 AnyElement, AnyView, App, Bounds, Corner, DismissEvent, DispatchPhase, Element, ElementId,
5 Entity, Focusable as _, GlobalElementId, HitboxBehavior, HitboxId, InteractiveElement,
6 IntoElement, LayoutId, Length, ManagedView, MouseDownEvent, ParentElement, Pixels, Point,
7 Style, Window, anchored, deferred, div, point, prelude::FluentBuilder, px, size,
8};
9
10use crate::prelude::*;
11
12pub trait PopoverTrigger: IntoElement + Clickable + Toggleable + 'static {}
13
14impl<T: IntoElement + Clickable + Toggleable + 'static> PopoverTrigger for T {}
15
16impl<T: Clickable> Clickable for gpui::AnimationElement<T>
17where
18 T: Clickable + 'static,
19{
20 fn on_click(
21 self,
22 handler: impl Fn(&gpui::ClickEvent, &mut Window, &mut App) + 'static,
23 ) -> Self {
24 self.map_element(|e| e.on_click(handler))
25 }
26
27 fn cursor_style(self, cursor_style: gpui::CursorStyle) -> Self {
28 self.map_element(|e| e.cursor_style(cursor_style))
29 }
30}
31
32impl<T: Toggleable> Toggleable for gpui::AnimationElement<T>
33where
34 T: Toggleable + 'static,
35{
36 fn toggle_state(self, selected: bool) -> Self {
37 self.map_element(|e| e.toggle_state(selected))
38 }
39}
40
41pub struct PopoverMenuHandle<M>(Rc<RefCell<Option<PopoverMenuHandleState<M>>>>);
42
43impl<M> Clone for PopoverMenuHandle<M> {
44 fn clone(&self) -> Self {
45 Self(self.0.clone())
46 }
47}
48
49impl<M> Default for PopoverMenuHandle<M> {
50 fn default() -> Self {
51 Self(Rc::default())
52 }
53}
54
55struct PopoverMenuHandleState<M> {
56 menu_builder: Rc<dyn Fn(&mut Window, &mut App) -> Option<Entity<M>>>,
57 menu: Rc<RefCell<Option<Entity<M>>>>,
58 on_open: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
59}
60
61impl<M: ManagedView> PopoverMenuHandle<M> {
62 pub fn show(&self, window: &mut Window, cx: &mut App) {
63 if let Some(state) = self.0.borrow().as_ref() {
64 show_menu(
65 &state.menu_builder,
66 &state.menu,
67 state.on_open.clone(),
68 window,
69 cx,
70 );
71 }
72 }
73
74 pub fn hide(&self, cx: &mut App) {
75 if let Some(state) = self.0.borrow().as_ref()
76 && let Some(menu) = state.menu.borrow().as_ref()
77 {
78 menu.update(cx, |_, cx| cx.emit(DismissEvent));
79 }
80 }
81
82 pub fn toggle(&self, window: &mut Window, cx: &mut App) {
83 if let Some(state) = self.0.borrow().as_ref() {
84 if state.menu.borrow().is_some() {
85 self.hide(cx);
86 } else {
87 self.show(window, cx);
88 }
89 }
90 }
91
92 pub fn is_deployed(&self) -> bool {
93 self.0
94 .borrow()
95 .as_ref()
96 .is_some_and(|state| state.menu.borrow().as_ref().is_some())
97 }
98
99 pub fn is_focused(&self, window: &Window, cx: &App) -> bool {
100 self.0.borrow().as_ref().is_some_and(|state| {
101 state
102 .menu
103 .borrow()
104 .as_ref()
105 .is_some_and(|model| model.focus_handle(cx).is_focused(window))
106 })
107 }
108
109 pub fn refresh_menu(
110 &self,
111 window: &mut Window,
112 cx: &mut App,
113 new_menu_builder: Rc<dyn Fn(&mut Window, &mut App) -> Option<Entity<M>>>,
114 ) {
115 let show_menu = if let Some(state) = self.0.borrow_mut().as_mut() {
116 state.menu_builder = new_menu_builder;
117 state.menu.borrow().is_some()
118 } else {
119 false
120 };
121
122 if show_menu {
123 self.show(window, cx);
124 }
125 }
126}
127
128pub struct PopoverMenu<M: ManagedView> {
129 id: ElementId,
130 child_builder: Option<
131 Box<
132 dyn FnOnce(
133 Rc<RefCell<Option<Entity<M>>>>,
134 Option<Rc<dyn Fn(&mut Window, &mut App) -> Option<Entity<M>> + 'static>>,
135 ) -> AnyElement
136 + 'static,
137 >,
138 >,
139 menu_builder: Option<Rc<dyn Fn(&mut Window, &mut App) -> Option<Entity<M>> + 'static>>,
140 anchor: Corner,
141 attach: Option<Corner>,
142 offset: Option<Point<Pixels>>,
143 trigger_handle: Option<PopoverMenuHandle<M>>,
144 on_open: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
145 full_width: bool,
146}
147
148impl<M: ManagedView> PopoverMenu<M> {
149 /// Returns a new [`PopoverMenu`].
150 pub fn new(id: impl Into<ElementId>) -> Self {
151 Self {
152 id: id.into(),
153 child_builder: None,
154 menu_builder: None,
155 anchor: Corner::TopLeft,
156 attach: None,
157 offset: None,
158 trigger_handle: None,
159 on_open: None,
160 full_width: false,
161 }
162 }
163
164 pub fn full_width(mut self, full_width: bool) -> Self {
165 self.full_width = full_width;
166 self
167 }
168
169 pub fn menu(
170 mut self,
171 f: impl Fn(&mut Window, &mut App) -> Option<Entity<M>> + 'static,
172 ) -> Self {
173 self.menu_builder = Some(Rc::new(f));
174 self
175 }
176
177 pub fn with_handle(mut self, handle: PopoverMenuHandle<M>) -> Self {
178 self.trigger_handle = Some(handle);
179 self
180 }
181
182 pub fn trigger<T: PopoverTrigger>(mut self, t: T) -> Self {
183 let on_open = self.on_open.clone();
184 self.child_builder = Some(Box::new(move |menu, builder| {
185 let open = menu.borrow().is_some();
186 t.toggle_state(open)
187 .when_some(builder, |el, builder| {
188 el.on_click(move |_event, window, cx| {
189 show_menu(&builder, &menu, on_open.clone(), window, cx)
190 })
191 })
192 .into_any_element()
193 }));
194 self
195 }
196
197 /// This method prevents the trigger button tooltip from being seen when the menu is open.
198 pub fn trigger_with_tooltip<T: PopoverTrigger + ButtonCommon>(
199 mut self,
200 t: T,
201 tooltip_builder: impl Fn(&mut Window, &mut App) -> AnyView + 'static,
202 ) -> Self {
203 let on_open = self.on_open.clone();
204 self.child_builder = Some(Box::new(move |menu, builder| {
205 let open = menu.borrow().is_some();
206 t.toggle_state(open)
207 .when_some(builder, |el, builder| {
208 el.on_click(move |_, window, cx| {
209 show_menu(&builder, &menu, on_open.clone(), window, cx)
210 })
211 .when(!open, |t| {
212 t.tooltip(move |window, cx| tooltip_builder(window, cx))
213 })
214 })
215 .into_any_element()
216 }));
217 self
218 }
219
220 /// Defines which corner of the menu to anchor to the attachment point.
221 /// By default, it uses the cursor position. Also see the `attach` method.
222 pub fn anchor(mut self, anchor: Corner) -> Self {
223 self.anchor = anchor;
224 self
225 }
226
227 /// Defines which corner of the handle to attach the menu's anchor to.
228 pub fn attach(mut self, attach: Corner) -> Self {
229 self.attach = Some(attach);
230 self
231 }
232
233 /// Offsets the position of the content by that many pixels.
234 pub fn offset(mut self, offset: Point<Pixels>) -> Self {
235 self.offset = Some(offset);
236 self
237 }
238
239 /// Attaches something upon opening the menu.
240 pub fn on_open(mut self, on_open: Rc<dyn Fn(&mut Window, &mut App)>) -> Self {
241 self.on_open = Some(on_open);
242 self
243 }
244
245 fn resolved_attach(&self) -> Corner {
246 self.attach.unwrap_or(match self.anchor {
247 Corner::TopLeft => Corner::BottomLeft,
248 Corner::TopRight => Corner::BottomRight,
249 Corner::BottomLeft => Corner::TopLeft,
250 Corner::BottomRight => Corner::TopRight,
251 })
252 }
253
254 fn resolved_offset(&self, window: &mut Window) -> Point<Pixels> {
255 self.offset.unwrap_or_else(|| {
256 // Default offset = 4px padding + 1px border
257 let offset = rems_from_px(5.) * window.rem_size();
258 match self.anchor {
259 Corner::TopRight | Corner::BottomRight => point(offset, px(0.)),
260 Corner::TopLeft | Corner::BottomLeft => point(-offset, px(0.)),
261 }
262 })
263 }
264}
265
266fn show_menu<M: ManagedView>(
267 builder: &Rc<dyn Fn(&mut Window, &mut App) -> Option<Entity<M>>>,
268 menu: &Rc<RefCell<Option<Entity<M>>>>,
269 on_open: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
270 window: &mut Window,
271 cx: &mut App,
272) {
273 let previous_focus_handle = window.focused(cx);
274 let Some(new_menu) = (builder)(window, cx) else {
275 return;
276 };
277 let menu2 = menu.clone();
278
279 window
280 .subscribe(&new_menu, cx, move |modal, _: &DismissEvent, window, cx| {
281 if modal.focus_handle(cx).contains_focused(window, cx)
282 && let Some(previous_focus_handle) = previous_focus_handle.as_ref()
283 {
284 window.focus(previous_focus_handle, cx);
285 }
286 *menu2.borrow_mut() = None;
287 window.refresh();
288 })
289 .detach();
290
291 // Since menus are rendered in a deferred fashion, their focus handles are
292 // not linked in the dispatch tree until after the deferred draw callback
293 // runs. We need to wait for that to happen before focusing it, so that
294 // calling `contains_focused` on the parent's focus handle returns `true`
295 // when the menu is focused. This prevents the pane's tab bar buttons from
296 // flickering when opening popover menus.
297 let focus_handle = new_menu.focus_handle(cx);
298 window.on_next_frame(move |window, _cx| {
299 window.on_next_frame(move |window, cx| {
300 window.focus(&focus_handle, cx);
301 });
302 });
303 *menu.borrow_mut() = Some(new_menu);
304 window.refresh();
305
306 if let Some(on_open) = on_open {
307 on_open(window, cx);
308 }
309}
310
311pub struct PopoverMenuElementState<M> {
312 menu: Rc<RefCell<Option<Entity<M>>>>,
313 child_bounds: Option<Bounds<Pixels>>,
314}
315
316impl<M> Clone for PopoverMenuElementState<M> {
317 fn clone(&self) -> Self {
318 Self {
319 menu: Rc::clone(&self.menu),
320 child_bounds: self.child_bounds,
321 }
322 }
323}
324
325impl<M> Default for PopoverMenuElementState<M> {
326 fn default() -> Self {
327 Self {
328 menu: Rc::default(),
329 child_bounds: None,
330 }
331 }
332}
333
334pub struct PopoverMenuFrameState<M: ManagedView> {
335 child_layout_id: Option<LayoutId>,
336 child_element: Option<AnyElement>,
337 menu_element: Option<AnyElement>,
338 menu_handle: Rc<RefCell<Option<Entity<M>>>>,
339}
340
341impl<M: ManagedView> Element for PopoverMenu<M> {
342 type RequestLayoutState = PopoverMenuFrameState<M>;
343 type PrepaintState = Option<HitboxId>;
344
345 fn id(&self) -> Option<ElementId> {
346 Some(self.id.clone())
347 }
348
349 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
350 None
351 }
352
353 fn request_layout(
354 &mut self,
355 global_id: Option<&GlobalElementId>,
356 _inspector_id: Option<&gpui::InspectorElementId>,
357 window: &mut Window,
358 cx: &mut App,
359 ) -> (gpui::LayoutId, Self::RequestLayoutState) {
360 window.with_element_state(
361 global_id.unwrap(),
362 |element_state: Option<PopoverMenuElementState<M>>, window| {
363 let element_state = element_state.unwrap_or_default();
364 let mut menu_layout_id = None;
365
366 let menu_element = element_state.menu.borrow_mut().as_mut().map(|menu| {
367 let offset = self.resolved_offset(window);
368 let mut anchored = anchored()
369 .snap_to_window_with_margin(px(8.))
370 .anchor(self.anchor)
371 .offset(offset);
372 if let Some(child_bounds) = element_state.child_bounds {
373 anchored =
374 anchored.position(child_bounds.corner(self.resolved_attach()) + offset);
375 }
376 let mut element = deferred(anchored.child(div().occlude().child(menu.clone())))
377 .with_priority(1)
378 .into_any();
379
380 menu_layout_id = Some(element.request_layout(window, cx));
381 element
382 });
383
384 let mut child_element = self.child_builder.take().map(|child_builder| {
385 (child_builder)(element_state.menu.clone(), self.menu_builder.clone())
386 });
387
388 if let Some(trigger_handle) = self.trigger_handle.take()
389 && let Some(menu_builder) = self.menu_builder.clone()
390 {
391 *trigger_handle.0.borrow_mut() = Some(PopoverMenuHandleState {
392 menu_builder,
393 menu: element_state.menu.clone(),
394 on_open: self.on_open.clone(),
395 });
396 }
397
398 let child_layout_id = child_element
399 .as_mut()
400 .map(|child_element| child_element.request_layout(window, cx));
401
402 let mut style = Style::default();
403 if self.full_width {
404 style.size = size(relative(1.).into(), Length::Auto);
405 }
406
407 let layout_id = window.request_layout(
408 style,
409 menu_layout_id.into_iter().chain(child_layout_id),
410 cx,
411 );
412
413 (
414 (
415 layout_id,
416 PopoverMenuFrameState {
417 child_element,
418 child_layout_id,
419 menu_element,
420 menu_handle: element_state.menu.clone(),
421 },
422 ),
423 element_state,
424 )
425 },
426 )
427 }
428
429 fn prepaint(
430 &mut self,
431 global_id: Option<&GlobalElementId>,
432 _inspector_id: Option<&gpui::InspectorElementId>,
433 _bounds: Bounds<Pixels>,
434 request_layout: &mut Self::RequestLayoutState,
435 window: &mut Window,
436 cx: &mut App,
437 ) -> Option<HitboxId> {
438 if let Some(child) = request_layout.child_element.as_mut() {
439 child.prepaint(window, cx);
440 }
441
442 if let Some(menu) = request_layout.menu_element.as_mut() {
443 menu.prepaint(window, cx);
444 }
445
446 request_layout.child_layout_id.map(|layout_id| {
447 let bounds = window.layout_bounds(layout_id);
448 window.with_element_state(global_id.unwrap(), |element_state, _cx| {
449 let mut element_state: PopoverMenuElementState<M> = element_state.unwrap();
450 element_state.child_bounds = Some(bounds);
451 ((), element_state)
452 });
453
454 window.insert_hitbox(bounds, HitboxBehavior::Normal).id
455 })
456 }
457
458 fn paint(
459 &mut self,
460 _id: Option<&GlobalElementId>,
461 _inspector_id: Option<&gpui::InspectorElementId>,
462 _: Bounds<gpui::Pixels>,
463 request_layout: &mut Self::RequestLayoutState,
464 child_hitbox: &mut Option<HitboxId>,
465 window: &mut Window,
466 cx: &mut App,
467 ) {
468 if let Some(mut child) = request_layout.child_element.take() {
469 child.paint(window, cx);
470 }
471
472 if let Some(mut menu) = request_layout.menu_element.take() {
473 menu.paint(window, cx);
474
475 if let Some(child_hitbox) = *child_hitbox {
476 let menu_handle = request_layout.menu_handle.clone();
477 // Mouse-downing outside the menu dismisses it, so we don't
478 // want a click on the toggle to re-open it.
479 window.on_mouse_event(move |_: &MouseDownEvent, phase, window, cx| {
480 if phase == DispatchPhase::Bubble && child_hitbox.is_hovered(window) {
481 if let Some(menu) = menu_handle.borrow().as_ref() {
482 menu.update(cx, |_, cx| {
483 cx.emit(DismissEvent);
484 });
485 }
486 cx.stop_propagation();
487 }
488 })
489 }
490 }
491 }
492}
493
494impl<M: ManagedView> IntoElement for PopoverMenu<M> {
495 type Element = Self;
496
497 fn into_element(self) -> Self::Element {
498 self
499 }
500}