use gpui::{Context, Corner, InteractiveElement, IntoElement, SharedString, Styled, Window};
use crate::{button::Button, menu::PopupMenu, popover::Popover, Selectable};
pub trait DropdownMenu: Styled + Selectable + InteractiveElement + IntoElement + 'static {
fn dropdown_menu(
self,
f: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
) -> Popover<PopupMenu> {
self.dropdown_menu_with_anchor(Corner::TopLeft, f)
}
fn dropdown_menu_with_anchor(
mut self,
anchor: impl Into<Corner>,
f: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
) -> Popover<PopupMenu> {
let style = self.style().clone();
let id = self.interactivity().element_id.clone();
Popover::new(SharedString::from(format!("dropdown-menu:{:?}", id)))
.no_style()
.trigger(self)
.trigger_style(style)
.anchor(anchor.into())
.content(move |window, cx| {
PopupMenu::build(window, cx, |menu, window, cx| f(menu, window, cx))
})
}
}
impl DropdownMenu for Button {}