Add keyboard control over context menus
Co-authored-by: Conrad <conrad@zed.dev>
This commit is contained in:
parent
77acba9e4c
commit
ac34229118
5 changed files with 125 additions and 65 deletions
|
@ -2,10 +2,11 @@ use crate::{
|
|||
h_stack, prelude::*, v_stack, KeyBinding, Label, List, ListItem, ListSeparator, ListSubHeader,
|
||||
};
|
||||
use gpui::{
|
||||
overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, ClickEvent, DismissEvent,
|
||||
DispatchPhase, Div, EventEmitter, FocusHandle, FocusableView, IntoElement, LayoutId,
|
||||
ManagedView, MouseButton, MouseDownEvent, Pixels, Point, Render, View, VisualContext,
|
||||
overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, DismissEvent, DispatchPhase,
|
||||
Div, EventEmitter, FocusHandle, FocusableView, IntoElement, LayoutId, ManagedView, MouseButton,
|
||||
MouseDownEvent, Pixels, Point, Render, View, VisualContext,
|
||||
};
|
||||
use menu::{SelectFirst, SelectLast, SelectNext, SelectPrev};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
pub enum ContextMenuItem {
|
||||
|
@ -13,7 +14,7 @@ pub enum ContextMenuItem {
|
|||
Header(SharedString),
|
||||
Entry {
|
||||
label: SharedString,
|
||||
click_handler: Rc<dyn Fn(&ClickEvent, &mut WindowContext)>,
|
||||
handler: Rc<dyn Fn(&mut WindowContext)>,
|
||||
key_binding: Option<KeyBinding>,
|
||||
},
|
||||
}
|
||||
|
@ -21,6 +22,7 @@ pub enum ContextMenuItem {
|
|||
pub struct ContextMenu {
|
||||
items: Vec<ContextMenuItem>,
|
||||
focus_handle: FocusHandle,
|
||||
selected_index: Option<usize>,
|
||||
}
|
||||
|
||||
impl FocusableView for ContextMenu {
|
||||
|
@ -42,6 +44,7 @@ impl ContextMenu {
|
|||
Self {
|
||||
items: Default::default(),
|
||||
focus_handle: cx.focus_handle(),
|
||||
selected_index: None,
|
||||
},
|
||||
cx,
|
||||
)
|
||||
|
@ -61,11 +64,11 @@ impl ContextMenu {
|
|||
pub fn entry(
|
||||
mut self,
|
||||
label: impl Into<SharedString>,
|
||||
on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
|
||||
on_click: impl Fn(&mut WindowContext) + 'static,
|
||||
) -> Self {
|
||||
self.items.push(ContextMenuItem::Entry {
|
||||
label: label.into(),
|
||||
click_handler: Rc::new(on_click),
|
||||
handler: Rc::new(on_click),
|
||||
key_binding: None,
|
||||
});
|
||||
self
|
||||
|
@ -80,19 +83,72 @@ impl ContextMenu {
|
|||
self.items.push(ContextMenuItem::Entry {
|
||||
label: label.into(),
|
||||
key_binding: KeyBinding::for_action(&*action, cx),
|
||||
click_handler: Rc::new(move |_, cx| cx.dispatch_action(action.boxed_clone())),
|
||||
handler: Rc::new(move |cx| cx.dispatch_action(action.boxed_clone())),
|
||||
});
|
||||
self
|
||||
}
|
||||
|
||||
pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
|
||||
// todo!()
|
||||
if let Some(ContextMenuItem::Entry { handler, .. }) =
|
||||
self.selected_index.and_then(|ix| self.items.get(ix))
|
||||
{
|
||||
(handler)(cx)
|
||||
}
|
||||
cx.emit(DismissEvent::Dismiss);
|
||||
}
|
||||
|
||||
pub fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
|
||||
cx.emit(DismissEvent::Dismiss);
|
||||
}
|
||||
|
||||
fn select_first(&mut self, _: &SelectFirst, cx: &mut ViewContext<Self>) {
|
||||
self.selected_index = self.items.iter().position(|item| item.is_selectable());
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
|
||||
for (ix, item) in self.items.iter().enumerate().rev() {
|
||||
if item.is_selectable() {
|
||||
self.selected_index = Some(ix);
|
||||
cx.notify();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
|
||||
if let Some(ix) = self.selected_index {
|
||||
for (ix, item) in self.items.iter().enumerate().skip(ix + 1) {
|
||||
if item.is_selectable() {
|
||||
self.selected_index = Some(ix);
|
||||
cx.notify();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.select_first(&Default::default(), cx);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
|
||||
if let Some(ix) = self.selected_index {
|
||||
for (ix, item) in self.items.iter().enumerate().take(ix).rev() {
|
||||
if item.is_selectable() {
|
||||
self.selected_index = Some(ix);
|
||||
cx.notify();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.select_last(&Default::default(), cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ContextMenuItem {
|
||||
fn is_selectable(&self) -> bool {
|
||||
matches!(self, Self::Entry { .. })
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ContextMenu {
|
||||
|
@ -103,52 +159,52 @@ impl Render for ContextMenu {
|
|||
v_stack()
|
||||
.min_w(px(200.))
|
||||
.track_focus(&self.focus_handle)
|
||||
.on_mouse_down_out(
|
||||
cx.listener(|this: &mut Self, _, cx| this.cancel(&Default::default(), cx)),
|
||||
)
|
||||
// .on_action(ContextMenu::select_first)
|
||||
// .on_action(ContextMenu::select_last)
|
||||
// .on_action(ContextMenu::select_next)
|
||||
// .on_action(ContextMenu::select_prev)
|
||||
.on_mouse_down_out(cx.listener(|this, _, cx| this.cancel(&Default::default(), cx)))
|
||||
.key_context("menu")
|
||||
.on_action(cx.listener(ContextMenu::select_first))
|
||||
.on_action(cx.listener(ContextMenu::select_last))
|
||||
.on_action(cx.listener(ContextMenu::select_next))
|
||||
.on_action(cx.listener(ContextMenu::select_prev))
|
||||
.on_action(cx.listener(ContextMenu::confirm))
|
||||
.on_action(cx.listener(ContextMenu::cancel))
|
||||
.flex_none()
|
||||
// .bg(cx.theme().colors().elevated_surface_background)
|
||||
// .border()
|
||||
// .border_color(cx.theme().colors().border)
|
||||
.child(
|
||||
List::new().children(self.items.iter().map(|item| match item {
|
||||
ContextMenuItem::Separator => ListSeparator::new().into_any_element(),
|
||||
ContextMenuItem::Header(header) => {
|
||||
ListSubHeader::new(header.clone()).into_any_element()
|
||||
}
|
||||
ContextMenuItem::Entry {
|
||||
label: entry,
|
||||
click_handler: callback,
|
||||
key_binding,
|
||||
} => {
|
||||
let callback = callback.clone();
|
||||
let dismiss = cx.listener(|_, _, cx| cx.emit(DismissEvent::Dismiss));
|
||||
List::new().children(self.items.iter().enumerate().map(
|
||||
|(ix, item)| match item {
|
||||
ContextMenuItem::Separator => ListSeparator::new().into_any_element(),
|
||||
ContextMenuItem::Header(header) => {
|
||||
ListSubHeader::new(header.clone()).into_any_element()
|
||||
}
|
||||
ContextMenuItem::Entry {
|
||||
label: entry,
|
||||
handler: callback,
|
||||
key_binding,
|
||||
} => {
|
||||
let callback = callback.clone();
|
||||
let dismiss =
|
||||
cx.listener(|_, _, cx| cx.emit(DismissEvent::Dismiss));
|
||||
|
||||
ListItem::new(entry.clone())
|
||||
.child(
|
||||
h_stack()
|
||||
.w_full()
|
||||
.justify_between()
|
||||
.child(Label::new(entry.clone()))
|
||||
.children(
|
||||
key_binding
|
||||
.clone()
|
||||
.map(|binding| div().ml_1().child(binding)),
|
||||
),
|
||||
)
|
||||
.on_click(move |event, cx| {
|
||||
callback(event, cx);
|
||||
dismiss(event, cx)
|
||||
})
|
||||
.into_any_element()
|
||||
}
|
||||
})),
|
||||
ListItem::new(entry.clone())
|
||||
.child(
|
||||
h_stack()
|
||||
.w_full()
|
||||
.justify_between()
|
||||
.child(Label::new(entry.clone()))
|
||||
.children(
|
||||
key_binding
|
||||
.clone()
|
||||
.map(|binding| div().ml_1().child(binding)),
|
||||
),
|
||||
)
|
||||
.selected(Some(ix) == self.selected_index)
|
||||
.on_click(move |event, cx| {
|
||||
callback(cx);
|
||||
dismiss(event, cx)
|
||||
})
|
||||
.into_any_element()
|
||||
}
|
||||
},
|
||||
)),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue