Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Show action key bindings in context menus
Max Brunsfeld6bf7ad71ebb3
diff --git a/crates/project_panel2/src/project_panel.rs b/crates/project_panel2/src/project_panel.rsindex 2ee2e3ab1fe..891355ea1f9 100644--- a/crates/project_panel2/src/project_panel.rs+++ b/crates/project_panel2/src/project_panel.rs@@ -398,6 +398,7 @@ impl ProjectPanel {menu = menu.action("Add Folder to Project",Box::new(workspace::AddFolderToProject),+ cx,);if is_root {menu = menu.entry(@@ -412,35 +413,35 @@ impl ProjectPanel {}menu = menu- .action("New File", Box::new(NewFile))- .action("New Folder", Box::new(NewDirectory))+ .action("New File", Box::new(NewFile), cx)+ .action("New Folder", Box::new(NewDirectory), cx).separator()- .action("Cut", Box::new(Cut))- .action("Copy", Box::new(Copy));+ .action("Cut", Box::new(Cut), cx)+ .action("Copy", Box::new(Copy), cx);if let Some(clipboard_entry) = self.clipboard_entry {if clipboard_entry.worktree_id() == worktree_id {- menu = menu.action("Paste", Box::new(Paste));+ menu = menu.action("Paste", Box::new(Paste), cx);}}menu = menu.separator()- .action("Copy Path", Box::new(CopyPath))- .action("Copy Relative Path", Box::new(CopyRelativePath))+ .action("Copy Path", Box::new(CopyPath), cx)+ .action("Copy Relative Path", Box::new(CopyRelativePath), cx).separator()- .action("Reveal in Finder", Box::new(RevealInFinder));+ .action("Reveal in Finder", Box::new(RevealInFinder), cx);if is_dir {menu = menu- .action("Open in Terminal", Box::new(OpenInTerminal))- .action("Search Inside", Box::new(NewSearchInDirectory))+ .action("Open in Terminal", Box::new(OpenInTerminal), cx)+ .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 {- menu = menu.action("Delete", Box::new(Delete));+ menu = menu.action("Delete", Box::new(Delete), cx);}menu@@ -658,7 +659,6 @@ impl ProjectPanel {}fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {- dbg!("odd");self.edit_state = None;self.update_visible_entries(None, cx);cx.focus(&self.focus_handle);@@ -1385,7 +1385,7 @@ impl ProjectPanel {}).child(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 {div().text_color(filename_text_color)diff --git a/crates/terminal_view2/src/terminal_view.rs b/crates/terminal_view2/src/terminal_view.rsindex 9f3ed313880..b007d58c34b 100644--- a/crates/terminal_view2/src/terminal_view.rs+++ b/crates/terminal_view2/src/terminal_view.rs@@ -298,9 +298,12 @@ impl TerminalView {position: gpui::Point<Pixels>,cx: &mut ViewContext<Self>,) {- self.context_menu = Some(ContextMenu::build(cx, |menu, _| {- menu.action("Clear", Box::new(Clear))- .action("Close", Box::new(CloseActiveItem { save_intent: None }))+ self.context_menu = Some(ContextMenu::build(cx, |menu, cx| {+ menu.action("Clear", Box::new(Clear), cx).action(+ "Close",+ Box::new(CloseActiveItem { save_intent: None }),+ cx,+ )}));dbg!(&position);// todo!()diff --git a/crates/ui2/src/components/context_menu.rs b/crates/ui2/src/components/context_menu.rsindex 3510256c0ce..b5ef8cf1608 100644--- a/crates/ui2/src/components/context_menu.rs+++ b/crates/ui2/src/components/context_menu.rs@@ -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::{overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, ClickEvent, DismissEvent,DispatchPhase, Div, EventEmitter, FocusHandle, FocusableView, IntoElement, LayoutId,@@ -9,7 +9,11 @@ use std::{cell::RefCell, rc::Rc};pub enum ContextMenuItem {Separator,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 {@@ -57,16 +61,26 @@ impl ContextMenu {label: impl Into<SharedString>,on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static,) -> Self {- self.items- .push(ContextMenuItem::Entry(label.into(), Rc::new(on_click)));+ self.items.push(ContextMenuItem::Entry {+ label: label.into(),+ click_handler: Rc::new(on_click),+ key_binding: None,+ });self}- pub fn action(self, label: impl Into<SharedString>, action: Box<dyn Action>) -> Self {- // todo: add the keybindings to the list entry- self.entry(label.into(), move |_, cx| {- cx.dispatch_action(action.boxed_clone())- })+ pub fn action(+ mut self,+ label: impl Into<SharedString>,+ 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>) {@@ -106,12 +120,17 @@ impl Render for ContextMenu {ContextMenuItem::Header(header) => {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 dismiss = cx.listener(|_, _, cx| cx.emit(DismissEvent::Dismiss));ListItem::new(entry.clone()).child(Label::new(entry.clone()))+ .children(key_binding.clone()).on_click(move |event, cx| {callback(event, cx);dismiss(event, cx)