mod control;
mod floating;
mod surface;
mod trigger;
pub use control::{Dropdown, dropdown_menu};
pub use floating::floating;
pub use trigger::{TriggerButton, trigger};
use gpui_kit::base::{DeferredPopover, GlobalState};
use gpui_kit::component::color_picker::{ColorPickerState, ColorSelect};
use gpui_kit::component::menu::{ContextMenu, ContextMenuExt as _, PopupMenu};
use gpui_kit::component::select::{Select, SelectDelegate, SelectItem, SelectState};
use gpui_kit::{
AnyView, App, Bounds, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable,
InteractiveElement, IntoElement, ParentElement, Pixels, Render, Styled, Subscription, Window,
};
pub fn select<D>(state: &Entity<SelectState<D>>) -> Select<D>
where
D: SelectDelegate + 'static,
<D::Item as SelectItem>::Value: PartialEq + Clone,
{
Select::new(state)
}
pub fn color_select(state: &Entity<ColorPickerState>) -> ColorSelect {
ColorSelect::new(state)
}
pub trait AnchoredContextMenuExt: InteractiveElement + ParentElement + Styled + Sized {
#[track_caller]
fn anchored_context_menu(
self,
builder: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
) -> ContextMenu<Self> {
self.context_menu(builder)
}
}
impl<T: InteractiveElement + ParentElement + Styled> AnchoredContextMenuExt for T {}
struct Content {
view: AnyView,
focus: FocusHandle,
registration: DeferredPopover,
dismissal: Dismissal,
}
enum Dismissal {
Surface,
NativeMenu { _subscription: Subscription },
}
pub struct Anchor {
bounds: Bounds<Pixels>,
trigger_focus: FocusHandle,
surface_focus: FocusHandle,
content: Option<Content>,
}
impl Anchor {
pub fn new(cx: &mut Context<Self>) -> Self {
Self {
bounds: Bounds::default(),
trigger_focus: cx.focus_handle(),
surface_focus: cx.focus_handle(),
content: None,
}
}
pub fn is_open(&self) -> bool {
self.content.is_some()
}
pub fn bounds(&self) -> Bounds<Pixels> {
self.bounds
}
pub fn set_bounds(&mut self, bounds: Bounds<Pixels>, cx: &mut Context<Self>) {
if self.bounds != bounds {
self.bounds = bounds;
cx.notify();
}
}
pub fn show<T>(&mut self, body: Entity<T>, window: &mut Window, cx: &mut Context<Self>)
where
T: Render + Focusable + 'static,
{
if let Some(content) = &mut self.content {
content.dismissal = Dismissal::Surface;
}
let focus = body.focus_handle(cx);
self.content = Some(Content {
view: body.into(),
focus: focus.clone(),
registration: GlobalState::register_deferred_popover(cx),
dismissal: Dismissal::Surface,
});
focus.focus(window, cx);
cx.notify();
}
pub fn dismiss(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let Some(Content {
view: _view,
focus,
registration,
dismissal,
}) = self.content.take()
else {
return;
};
if self.surface_focus.contains_focused(window, cx) || focus.contains_focused(window, cx) {
self.trigger_focus.focus(window, cx);
}
drop(registration);
drop(dismissal);
cx.emit(DismissEvent);
cx.notify();
}
fn show_menu(&mut self, body: Entity<PopupMenu>, window: &mut Window, cx: &mut Context<Self>) {
self.show(body.clone(), window, cx);
let anchor = cx.weak_entity();
if let Some(content) = &mut self.content {
let subscription = window.subscribe(&body, cx, move |_, _, window, cx| {
_ = anchor.update(cx, |anchor, cx| anchor.dismiss(window, cx));
});
content.dismissal = Dismissal::NativeMenu {
_subscription: subscription,
};
}
}
pub fn show_menu_with(
&mut self,
window: &mut Window,
cx: &mut Context<Self>,
build: impl FnOnce(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu,
) {
let focus = self.trigger_focus.clone();
let menu = PopupMenu::build(window, cx, |menu, window, cx| {
build(menu.action_context(focus), window, cx)
});
self.show_menu(menu, window, cx);
}
}
impl EventEmitter<DismissEvent> for Anchor {}
impl Focusable for Anchor {
fn focus_handle(&self, _: &App) -> FocusHandle {
self.trigger_focus.clone()
}
}
impl Render for Anchor {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
surface::render(self, cx)
}
}
#[cfg(test)]
mod runtime_tests;