From b7a1292d071ab3f7f4a0ad194a2966bdf158d109 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 24 Oct 2024 11:05:13 +0800 Subject: [PATCH] popup_menu: Add `popup_menu_with_anchor` method to control the Anchor for PopupMenu. (#378) --- crates/story/src/popup_story.rs | 2 +- crates/ui/src/popup_menu.rs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/story/src/popup_story.rs b/crates/story/src/popup_story.rs index c4b9a662..7f7f3d3d 100644 --- a/crates/story/src/popup_story.rs +++ b/crates/story/src/popup_story.rs @@ -302,7 +302,7 @@ impl Render for PopupStory { .child( Button::new("popup-menu-11112") .label("Scrollable Menu") - .popup_menu(move |this, _| { + .popup_menu_with_anchor(AnchorCorner::TopRight, move |this, _| { let mut this = this.scrollable(); for i in 0..100 { this = this.menu( diff --git a/crates/ui/src/popup_menu.rs b/crates/ui/src/popup_menu.rs index 540af06b..145d9577 100644 --- a/crates/ui/src/popup_menu.rs +++ b/crates/ui/src/popup_menu.rs @@ -32,14 +32,25 @@ pub fn init(cx: &mut AppContext) { } pub trait PopupMenuExt: Selectable + IntoElement + 'static { + /// Create a popup menu with the given items, anchored to the TopLeft corner fn popup_menu( self, f: impl Fn(PopupMenu, &mut ViewContext) -> PopupMenu + 'static, + ) -> Popover { + self.popup_menu_with_anchor(AnchorCorner::TopLeft, f) + } + + /// Create a popup menu with the given items, anchored to the given corner + fn popup_menu_with_anchor( + self, + anchor: impl Into, + f: impl Fn(PopupMenu, &mut ViewContext) -> PopupMenu + 'static, ) -> Popover { let element_id = self.element_id(); Popover::new(SharedString::from(format!("popup-menu:{:?}", element_id))) .no_style() .trigger(self) + .anchor(anchor.into()) .content(move |cx| PopupMenu::build(cx, |menu, cx| f(menu, cx))) } }