popup_menu: Add popup_menu_with_anchor method to control the Anchor for PopupMenu. (#378)

This commit is contained in:
Jason Lee 2024-10-24 11:05:13 +08:00 committed by GitHub
parent b8c8ddd8f5
commit b7a1292d07
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View file

@ -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(

View file

@ -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>) -> PopupMenu + 'static,
) -> Popover<PopupMenu> {
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<AnchorCorner>,
f: impl Fn(PopupMenu, &mut ViewContext<PopupMenu>) -> PopupMenu + 'static,
) -> Popover<PopupMenu> {
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)))
}
}