use std::cell::Cell; use std::rc::Rc; use gpui::{ Anchor, AnyElement, App, Context, DismissEvent, Element, ElementId, Entity, FocusHandle, Focusable, GlobalElementId, InspectorElementId, InteractiveElement, IntoElement, LayoutId, Pixels, RenderOnce, SharedString, Styled, Window, prelude::FluentBuilder, }; use crate::{ Selectable, button::Button, fluent::DROPDOWN_OFFSET, menu::PopupMenu, popover::Popover, }; /// A dropdown menu trait for buttons and other interactive elements pub trait DropdownMenu: Styled + Selectable + InteractiveElement + IntoElement + 'static { /// Create a dropdown menu with the given items, anchored to the TopLeft corner fn dropdown_menu( self, f: impl Fn(PopupMenu, &mut Window, &mut Context) -> PopupMenu + 'static, ) -> DropdownMenuPopover { self.dropdown_menu_with_anchor(Anchor::TopLeft, f) } /// Create a dropdown menu with the given items, anchored to the given corner fn dropdown_menu_with_anchor( mut self, anchor: impl Into, f: impl Fn(PopupMenu, &mut Window, &mut Context) -> PopupMenu + 'static, ) -> DropdownMenuPopover { let id = self.interactivity().element_id.clone(); DropdownMenuPopover::new(id.unwrap_or(0.into()), anchor, self, f) } } impl DropdownMenu for Button {} #[derive(IntoElement)] pub struct DropdownMenuPopover { id: ElementId, anchor: Anchor, trigger: T, builder: Rc) -> PopupMenu>, on_open_change: Option>, open: Option, listbox: bool, } impl DropdownMenuPopover where T: Selectable + IntoElement + 'static, { fn new( id: ElementId, anchor: impl Into, trigger: T, builder: impl Fn(PopupMenu, &mut Window, &mut Context) -> PopupMenu + 'static, ) -> Self { Self { id: SharedString::from(format!("dropdown-menu:{:?}", id)).into(), anchor: anchor.into(), trigger, builder: Rc::new(builder), on_open_change: None, open: None, listbox: false, } } /// Control the open state from outside; pair it with `on_open_change`. pub fn open(mut self, open: bool) -> Self { self.open = Some(open); self } /// Open the menu as a Fluent `Listbox`: left aligned under the trigger, /// 2 px below it and at least as wide, with a check column. pub fn listbox(mut self) -> Self { self.listbox = true; self } /// Set the anchor corner for the dropdown menu popover. pub fn anchor(mut self, anchor: impl Into) -> Self { self.anchor = anchor.into(); self } /// Add a callback to be called when the menu opens or closes. /// /// The `&bool` parameter is the **new open state**. pub fn on_open_change( mut self, callback: impl Fn(&bool, &mut Window, &mut App) + 'static, ) -> Self { self.on_open_change = Some(Rc::new(callback)); self } } #[derive(Default)] struct DropdownMenuState { menu: Option>, } #[derive(Default)] struct TriggerWidth(Rc>); impl RenderOnce for DropdownMenuPopover where T: Selectable + IntoElement + 'static, { fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let width_key = SharedString::from(format!("{}:trigger-width", self.id)); let trigger_width = window .use_keyed_state(width_key, cx, |_, _| TriggerWidth::default()) .read(cx) .0 .clone(); TriggerFocus::new( self.id.clone(), trigger_width.clone(), move |trigger_focus, window, cx| { self.render_popover(trigger_focus, trigger_width, window, cx) .into_any_element() }, ) } } impl DropdownMenuPopover where T: Selectable + IntoElement + 'static, { fn render_popover( self, trigger_focus: FocusHandle, trigger_width: Rc>, window: &mut Window, cx: &mut App, ) -> Popover { let builder = self.builder.clone(); let listbox = self.listbox; let menu_state = window.use_keyed_state(self.id.clone(), cx, |_, _| DropdownMenuState::default()); Popover::new(SharedString::from(format!("popover:{}", self.id))) .appearance(false) .overlay_closable(false) .trigger(self.trigger) .anchor(self.anchor) .when(listbox, |this| this.offset(DROPDOWN_OFFSET)) .when_some(self.open, |this, open| this.open(open)) .when_some(self.on_open_change, |this, callback| { this.on_open_change(move |open, window, cx| callback(open, window, cx)) }) .content(move |_, window, cx| { // Here is special logic to only create the PopupMenu once and reuse it. // Because this `content` will called in every time render, so we need to store the menu // in state to avoid recreating at every render. // // And we also need to rebuild the menu when it is dismissed, to rebuild menu items // dynamically for support `dropdown_menu` method, so we listen for DismissEvent below. let menu = match menu_state.read(cx).menu.clone() { Some(menu) => menu, None => { let builder = builder.clone(); let width = trigger_width.get(); let menu = PopupMenu::build(window, cx, move |menu, window, cx| { let menu = if listbox { menu.listbox(width) } else { menu }; builder(menu, window, cx) }); menu.update(cx, |menu, cx| { menu.set_trigger_focus(Some(trigger_focus.clone()), cx) }); menu_state.update(cx, |state, _| { state.menu = Some(menu.clone()); }); menu.focus_handle(cx).focus(window, cx); // Listen for dismiss events from the PopupMenu to close the popover. // // Hold Weak handles here, not strong clones: the listener // lives as long as `menu`, which `menu_state` owns, so a // strong capture would close the cycle // `menu_state -> menu -> listener -> menu_state` and leak // the `PopupMenu` and `DropdownMenuState` when the trigger // stops being rendered while the menu is open. let popover_state = cx.entity().downgrade(); window .subscribe(&menu, cx, { let menu_state = menu_state.downgrade(); move |_, _: &DismissEvent, window, cx| { if let Some(popover_state) = popover_state.upgrade() { popover_state.update(cx, |state, cx| { state.dismiss(window, cx); }); } _ = menu_state.update(cx, |state, _| { state.menu = None; }); } }) .detach(); menu.clone() } }; menu.clone() }) } } type TriggerFocusBuild = Box AnyElement>; /// Registers a focus handle on the trigger's dispatch node without ever /// focusing it, so the menu opened from the trigger can resolve its shortcut /// hints against the trigger's key contexts on the frame it opens. GPUI looks /// a handle up in the previously rendered frame; the trigger was in it when /// the menu was not yet. struct TriggerFocus { id: ElementId, width: Rc>, build: Option, } #[derive(Default)] struct TriggerFocusState { focus_handle: Option, } struct TriggerFocusFrame { focus_handle: FocusHandle, child: AnyElement, } impl TriggerFocus { fn new( id: ElementId, width: Rc>, build: impl FnOnce(FocusHandle, &mut Window, &mut App) -> AnyElement + 'static, ) -> Self { Self { id, width, build: Some(Box::new(build)), } } } impl IntoElement for TriggerFocus { type Element = Self; fn into_element(self) -> Self::Element { self } } impl Element for TriggerFocus { type RequestLayoutState = TriggerFocusFrame; type PrepaintState = (); fn id(&self) -> Option { Some(self.id.clone()) } fn source_location(&self) -> Option<&'static std::panic::Location<'static>> { None } fn request_layout( &mut self, id: Option<&GlobalElementId>, _: Option<&InspectorElementId>, window: &mut Window, cx: &mut App, ) -> (LayoutId, Self::RequestLayoutState) { let focus_handle = window.with_optional_element_state::(id, |state, _| { let mut state = state.flatten().unwrap_or_default(); let focus_handle = state .focus_handle .get_or_insert_with(|| cx.focus_handle()) .clone(); (focus_handle, Some(state)) }); let build = self.build.take().expect("TriggerFocus is laid out once"); let mut child = build(focus_handle.clone(), window, cx); let layout_id = child.request_layout(window, cx); ( layout_id, TriggerFocusFrame { focus_handle, child, }, ) } fn prepaint( &mut self, _: Option<&GlobalElementId>, _: Option<&InspectorElementId>, bounds: gpui::Bounds, frame: &mut Self::RequestLayoutState, window: &mut Window, cx: &mut App, ) { self.width.set(bounds.size.width); window.set_focus_handle(&frame.focus_handle, cx); frame.child.prepaint(window, cx); } fn paint( &mut self, _: Option<&GlobalElementId>, _: Option<&InspectorElementId>, _: gpui::Bounds, frame: &mut Self::RequestLayoutState, _: &mut Self::PrepaintState, window: &mut Window, cx: &mut App, ) { frame.child.paint(window, cx); } } #[cfg(test)] mod tests { use super::*; use gpui::{ KeyBinding, MouseButton, ParentElement as _, Render, TestAppContext, WeakEntity, actions, div, point, px, }; use std::cell::{Cell, RefCell}; actions!(dropdown_menu_test, [CopyText]); const CONTEXT: &str = "dropdown_menu_test"; /// The story shape: the key binding lives in the key context of the /// trigger's ancestor, the menu names no `action_context`, and other /// content outside that context paints after the trigger. struct TestRoot { frames: Rc>, } impl Render for TestRoot { fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { self.frames.set(self.frames.get() + 1); div() .size_full() .child( div() .key_context(CONTEXT) .on_action(|_: &CopyText, _, _| {}) .child( Button::new("trigger") .label("Edit") .w(px(100.)) .h(px(30.)) .dropdown_menu(|menu, _, _| menu.menu("Copy", Box::new(CopyText))), ), ) .child(div().child("Status")) } } /// Records the `PopupMenu` the dropdown builds, so a test can observe /// its release through a weak handle. struct MenuProbeRoot { menu: Rc>>>, } impl Render for MenuProbeRoot { fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { let slot = self.menu.clone(); div().size_full().child( Button::new("trigger") .label("Edit") .w(px(100.)) .h(px(30.)) .dropdown_menu(move |menu, _, cx| { *slot.borrow_mut() = Some(cx.weak_entity()); menu.menu("Copy", Box::new(CopyText)) }), ) } } /// Opening the menu and closing the window without dismissing it must /// release the `PopupMenu`: the dismiss subscription lives as long as the /// menu, which `DropdownMenuState` owns, so strong captures in it formed /// a cycle that no element-state collection could break. The state still /// holds the menu while it is open, so the menu's release also proves the /// `DropdownMenuState` was released. /// /// The popover must go too: it holds the deferred-popover registration /// while open, and a leaked one keeps every later right-click menu /// stepping aside as if a popup were still showing. #[gpui::test] fn open_without_dismiss_releases_the_menu(cx: &mut TestAppContext) { cx.update(|cx| crate::init(cx)); let menu = Rc::new(RefCell::new(None::>)); { let (_, cx) = cx.add_window_view(|_, _| MenuProbeRoot { menu: menu.clone() }); cx.update(|window, cx| window.draw(cx).clear(cx)); // Click the trigger; the menu opens and is left open. cx.simulate_mouse_down( point(px(10.), px(10.)), MouseButton::Left, Default::default(), ); cx.run_until_parked(); assert!( menu.borrow() .as_ref() .and_then(|menu| menu.upgrade()) .is_some(), "the menu must be open before the window closes" ); // Close the window without dismissing the menu. cx.update(|window, _| window.remove_window()); cx.run_until_parked(); } assert!( menu.borrow() .as_ref() .and_then(|menu| menu.upgrade()) .is_none(), "the PopupMenu must be released with the window" ); cx.update(|cx| { assert!( !gpui_base::GlobalState::is_in_deferred_context(cx), "the popover's deferred registration must be released with the window" ) }); } #[gpui::test] fn shortcut_hint_is_painted_on_the_frame_the_menu_opens(cx: &mut TestAppContext) { cx.update(|cx| { crate::init(cx); cx.bind_keys([KeyBinding::new("ctrl-c", CopyText, Some(CONTEXT))]); }); let frames = Rc::new(Cell::new(0)); let (_, cx) = cx.add_window_view({ let frames = frames.clone(); move |_, _| TestRoot { frames } }); // The popup host captures its trigger bounds on the first frame. cx.update(|window, cx| window.draw(cx).clear(cx)); let frames_before_open = frames.get(); cx.simulate_mouse_down( point(px(10.), px(10.)), MouseButton::Left, Default::default(), ); assert_eq!( frames.get(), frames_before_open + 1, "the press must be followed by exactly one frame for this to test the first one" ); assert!( cx.debug_bounds("kbd:ctrl-c").is_some(), "the shortcut hint must be painted on the same frame as its item" ); } }