Show action key bindings in context menus

This commit is contained in:
Max Brunsfeld 2023-11-28 16:07:42 -08:00
parent bcf449d3fe
commit 6bf7ad71eb
3 changed files with 49 additions and 27 deletions

View file

@ -398,6 +398,7 @@ impl ProjectPanel {
menu = menu.action( menu = menu.action(
"Add Folder to Project", "Add Folder to Project",
Box::new(workspace::AddFolderToProject), Box::new(workspace::AddFolderToProject),
cx,
); );
if is_root { if is_root {
menu = menu.entry( menu = menu.entry(
@ -412,35 +413,35 @@ impl ProjectPanel {
} }
menu = menu menu = menu
.action("New File", Box::new(NewFile)) .action("New File", Box::new(NewFile), cx)
.action("New Folder", Box::new(NewDirectory)) .action("New Folder", Box::new(NewDirectory), cx)
.separator() .separator()
.action("Cut", Box::new(Cut)) .action("Cut", Box::new(Cut), cx)
.action("Copy", Box::new(Copy)); .action("Copy", Box::new(Copy), cx);
if let Some(clipboard_entry) = self.clipboard_entry { if let Some(clipboard_entry) = self.clipboard_entry {
if clipboard_entry.worktree_id() == worktree_id { if clipboard_entry.worktree_id() == worktree_id {
menu = menu.action("Paste", Box::new(Paste)); menu = menu.action("Paste", Box::new(Paste), cx);
} }
} }
menu = menu menu = menu
.separator() .separator()
.action("Copy Path", Box::new(CopyPath)) .action("Copy Path", Box::new(CopyPath), cx)
.action("Copy Relative Path", Box::new(CopyRelativePath)) .action("Copy Relative Path", Box::new(CopyRelativePath), cx)
.separator() .separator()
.action("Reveal in Finder", Box::new(RevealInFinder)); .action("Reveal in Finder", Box::new(RevealInFinder), cx);
if is_dir { if is_dir {
menu = menu menu = menu
.action("Open in Terminal", Box::new(OpenInTerminal)) .action("Open in Terminal", Box::new(OpenInTerminal), cx)
.action("Search Inside", Box::new(NewSearchInDirectory)) .action("Search Inside", Box::new(NewSearchInDirectory), cx)
} }
menu = menu.separator().action("Rename", Box::new(Rename)); menu = menu.separator().action("Rename", Box::new(Rename), cx);
if !is_root { if !is_root {
menu = menu.action("Delete", Box::new(Delete)); menu = menu.action("Delete", Box::new(Delete), cx);
} }
menu menu
@ -658,7 +659,6 @@ impl ProjectPanel {
} }
fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) { fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
dbg!("odd");
self.edit_state = None; self.edit_state = None;
self.update_visible_entries(None, cx); self.update_visible_entries(None, cx);
cx.focus(&self.focus_handle); cx.focus(&self.focus_handle);
@ -1385,7 +1385,7 @@ impl ProjectPanel {
}) })
.child( .child(
if let (Some(editor), true) = (Some(&self.filename_editor), show_editor) { if let (Some(editor), true) = (Some(&self.filename_editor), show_editor) {
div().w_full().child(editor.clone()) div().h_full().w_full().child(editor.clone())
} else { } else {
div() div()
.text_color(filename_text_color) .text_color(filename_text_color)

View file

@ -298,9 +298,12 @@ impl TerminalView {
position: gpui::Point<Pixels>, position: gpui::Point<Pixels>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
self.context_menu = Some(ContextMenu::build(cx, |menu, _| { self.context_menu = Some(ContextMenu::build(cx, |menu, cx| {
menu.action("Clear", Box::new(Clear)) menu.action("Clear", Box::new(Clear), cx).action(
.action("Close", Box::new(CloseActiveItem { save_intent: None })) "Close",
Box::new(CloseActiveItem { save_intent: None }),
cx,
)
})); }));
dbg!(&position); dbg!(&position);
// todo!() // todo!()

View file

@ -1,4 +1,4 @@
use crate::{prelude::*, v_stack, Label, List, ListItem, ListSeparator, ListSubHeader}; use crate::{prelude::*, v_stack, KeyBinding, Label, List, ListItem, ListSeparator, ListSubHeader};
use gpui::{ use gpui::{
overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, ClickEvent, DismissEvent, overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, ClickEvent, DismissEvent,
DispatchPhase, Div, EventEmitter, FocusHandle, FocusableView, IntoElement, LayoutId, DispatchPhase, Div, EventEmitter, FocusHandle, FocusableView, IntoElement, LayoutId,
@ -9,7 +9,11 @@ use std::{cell::RefCell, rc::Rc};
pub enum ContextMenuItem { pub enum ContextMenuItem {
Separator, Separator,
Header(SharedString), Header(SharedString),
Entry(SharedString, Rc<dyn Fn(&ClickEvent, &mut WindowContext)>), Entry {
label: SharedString,
click_handler: Rc<dyn Fn(&ClickEvent, &mut WindowContext)>,
key_binding: Option<KeyBinding>,
},
} }
pub struct ContextMenu { pub struct ContextMenu {
@ -57,16 +61,26 @@ impl ContextMenu {
label: impl Into<SharedString>, label: impl Into<SharedString>,
on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static, on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
) -> Self { ) -> Self {
self.items self.items.push(ContextMenuItem::Entry {
.push(ContextMenuItem::Entry(label.into(), Rc::new(on_click))); label: label.into(),
click_handler: Rc::new(on_click),
key_binding: None,
});
self self
} }
pub fn action(self, label: impl Into<SharedString>, action: Box<dyn Action>) -> Self { pub fn action(
// todo: add the keybindings to the list entry mut self,
self.entry(label.into(), move |_, cx| { label: impl Into<SharedString>,
cx.dispatch_action(action.boxed_clone()) action: Box<dyn Action>,
}) cx: &mut WindowContext,
) -> Self {
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())),
});
self
} }
pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) { pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
@ -106,12 +120,17 @@ impl Render for ContextMenu {
ContextMenuItem::Header(header) => { ContextMenuItem::Header(header) => {
ListSubHeader::new(header.clone()).into_any_element() ListSubHeader::new(header.clone()).into_any_element()
} }
ContextMenuItem::Entry(entry, callback) => { ContextMenuItem::Entry {
label: entry,
click_handler: callback,
key_binding,
} => {
let callback = callback.clone(); let callback = callback.clone();
let dismiss = cx.listener(|_, _, cx| cx.emit(DismissEvent::Dismiss)); let dismiss = cx.listener(|_, _, cx| cx.emit(DismissEvent::Dismiss));
ListItem::new(entry.clone()) ListItem::new(entry.clone())
.child(Label::new(entry.clone())) .child(Label::new(entry.clone()))
.children(key_binding.clone())
.on_click(move |event, cx| { .on_click(move |event, cx| {
callback(event, cx); callback(event, cx);
dismiss(event, cx) dismiss(event, cx)