add context menu
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, ElementId, InteractiveElement as _, IntoElement, MouseButton, MouseDownEvent,
|
||||
ParentElement as _, RenderOnce, SharedString, Window, div,
|
||||
};
|
||||
|
||||
use crate::menu::{Menu, MenuItem};
|
||||
|
||||
/// Builds the element that opens the menu on right-click.
|
||||
type TargetFn = Box<dyn Fn(&mut Window, &mut App) -> gpui::AnyElement>;
|
||||
/// Builds the menu's items fresh each render.
|
||||
type ItemsFn = Box<dyn Fn(&mut Window, &mut App) -> Vec<MenuItem>>;
|
||||
|
||||
/// A menu that opens at the pointer when its target is right-clicked.
|
||||
#[derive(IntoElement)]
|
||||
pub struct ContextMenu {
|
||||
id: ElementId,
|
||||
target: Option<TargetFn>,
|
||||
items: Option<ItemsFn>,
|
||||
a11y_label: Option<SharedString>,
|
||||
}
|
||||
|
||||
impl ContextMenu {
|
||||
/// Creates a context menu over a target set with [`ContextMenu::target`].
|
||||
pub fn new<I>(id: I) -> Self
|
||||
where
|
||||
I: Into<ElementId>,
|
||||
{
|
||||
Self {
|
||||
id: id.into(),
|
||||
target: None,
|
||||
items: None,
|
||||
a11y_label: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the element that opens the menu on right-click.
|
||||
pub fn target<F>(mut self, target: F) -> Self
|
||||
where
|
||||
F: Fn(&mut Window, &mut App) -> gpui::AnyElement + 'static,
|
||||
{
|
||||
self.target = Some(Box::new(target));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the menu's items, built fresh each render.
|
||||
pub fn items<F>(mut self, items: F) -> Self
|
||||
where
|
||||
F: Fn(&mut Window, &mut App) -> Vec<MenuItem> + 'static,
|
||||
{
|
||||
self.items = Some(Box::new(items));
|
||||
self
|
||||
}
|
||||
|
||||
/// Overrides the menu's accessibility label.
|
||||
pub fn accessibility_label<L>(mut self, label: L) -> Self
|
||||
where
|
||||
L: Into<SharedString>,
|
||||
{
|
||||
self.a11y_label = Some(label.into());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for ContextMenu {
|
||||
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let open = window.with_element_namespace(self.id.clone(), |window| {
|
||||
window
|
||||
.use_keyed_state("context-open", cx, |_, _| false)
|
||||
.clone()
|
||||
});
|
||||
|
||||
let at = window.with_element_namespace(self.id.clone(), |window| {
|
||||
window
|
||||
.use_keyed_state("context-position", cx, |_, _| None)
|
||||
.clone()
|
||||
});
|
||||
|
||||
let is_open = *open.read(cx);
|
||||
let position = *at.read(cx);
|
||||
|
||||
let target = self.target;
|
||||
let at_for_target = at.clone();
|
||||
|
||||
let trigger = move |window: &mut Window, cx: &mut App| {
|
||||
let base = target
|
||||
.as_ref()
|
||||
.map(|build| build(window, cx))
|
||||
.unwrap_or_else(|| div().into_any_element());
|
||||
|
||||
div()
|
||||
.on_mouse_down(MouseButton::Right, {
|
||||
let at = at_for_target.clone();
|
||||
move |event: &MouseDownEvent, _window, cx| {
|
||||
at.update(cx, |at, _| *at = Some(event.position));
|
||||
}
|
||||
})
|
||||
.child(base)
|
||||
.into_any_element()
|
||||
};
|
||||
|
||||
let items = move |window: &mut Window, cx: &mut App| {
|
||||
self.items
|
||||
.as_ref()
|
||||
.map(|build| build(window, cx))
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
Menu::new(self.id)
|
||||
.open(is_open)
|
||||
.on_change(move |next, _window, cx| open.update(cx, |open, _| *open = next))
|
||||
.open_at(position)
|
||||
.trigger_button(MouseButton::Right)
|
||||
.trigger(trigger)
|
||||
.items(items)
|
||||
.when_some(self.a11y_label.clone(), |menu, label| {
|
||||
menu.accessibility_label(label)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,9 @@ pub mod button;
|
||||
#[cfg(feature = "checkbox")]
|
||||
pub mod checkbox;
|
||||
|
||||
#[cfg(feature = "context_menu")]
|
||||
pub mod context_menu;
|
||||
|
||||
#[cfg(feature = "switch")]
|
||||
pub mod switch;
|
||||
|
||||
|
||||
+26
-6
@@ -5,7 +5,7 @@ use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
Animation, AnimationExt as _, AnyElement, App, Bounds, BoxShadow, DispatchPhase, ElementId,
|
||||
Entity, InteractiveElement as _, IntoElement, KeyDownEvent, MouseButton, MouseDownEvent,
|
||||
ParentElement as _, Pixels, RenderOnce, Role, ScrollHandle, SharedString,
|
||||
ParentElement as _, Pixels, Point, RenderOnce, Role, ScrollHandle, SharedString,
|
||||
StatefulInteractiveElement as _, Styled as _, Window, anchored, canvas, deferred, div,
|
||||
ease_out_quint, px,
|
||||
};
|
||||
@@ -146,6 +146,8 @@ pub struct Menu {
|
||||
id: ElementId,
|
||||
open: bool,
|
||||
initial_selection: Option<usize>,
|
||||
trigger_button: MouseButton,
|
||||
open_at: Option<Point<Pixels>>,
|
||||
a11y_label: Option<SharedString>,
|
||||
on_change: Option<ChangeHandler>,
|
||||
trigger: Option<TriggerFn>,
|
||||
@@ -162,6 +164,8 @@ impl Menu {
|
||||
id: id.into(),
|
||||
open: false,
|
||||
initial_selection: None,
|
||||
trigger_button: MouseButton::Left,
|
||||
open_at: None,
|
||||
a11y_label: None,
|
||||
on_change: None,
|
||||
trigger: None,
|
||||
@@ -175,6 +179,18 @@ impl Menu {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the mouse button that toggles the menu; right for context menus.
|
||||
pub fn trigger_button(mut self, button: MouseButton) -> Self {
|
||||
self.trigger_button = button;
|
||||
self
|
||||
}
|
||||
|
||||
/// Positions the panel at this window point when it opens.
|
||||
pub fn open_at(mut self, position: impl Into<Option<Point<Pixels>>>) -> Self {
|
||||
self.open_at = position.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the current open state.
|
||||
pub fn open(mut self, open: bool) -> Self {
|
||||
self.open = open;
|
||||
@@ -264,6 +280,7 @@ impl RenderOnce for Menu {
|
||||
{
|
||||
let on_change = self.on_change.clone();
|
||||
let open = self.open;
|
||||
let trigger_button = self.trigger_button;
|
||||
let cell = trigger_bounds.clone();
|
||||
let was_open = was_open.clone();
|
||||
move |_, _, window, cx| {
|
||||
@@ -277,7 +294,7 @@ impl RenderOnce for Menu {
|
||||
let on_change = on_change.clone();
|
||||
move |event: &MouseDownEvent, phase, window, cx| {
|
||||
if phase != DispatchPhase::Bubble
|
||||
|| event.button != MouseButton::Left
|
||||
|| event.button != trigger_button
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -390,9 +407,7 @@ impl RenderOnce for Menu {
|
||||
window.on_mouse_event({
|
||||
let on_change = on_change.clone();
|
||||
move |event: &MouseDownEvent, phase, window, cx| {
|
||||
if phase != DispatchPhase::Bubble
|
||||
|| event.button != MouseButton::Left
|
||||
{
|
||||
if phase != DispatchPhase::Bubble {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -419,7 +434,12 @@ impl RenderOnce for Menu {
|
||||
.inset_0(),
|
||||
);
|
||||
|
||||
wrapper = wrapper.child(deferred(anchored().child(panel.with_animation(
|
||||
let anchored = match self.open_at {
|
||||
Some(position) => anchored().position(position),
|
||||
None => anchored(),
|
||||
};
|
||||
|
||||
wrapper = wrapper.child(deferred(anchored.child(panel.with_animation(
|
||||
"menu-enter",
|
||||
Animation::new(enter_duration).with_easing(ease_out_quint()),
|
||||
|panel, delta| panel.opacity(delta),
|
||||
|
||||
Reference in New Issue
Block a user