Show action key bindings in context menus

Max Brunsfeld created

Change summary

crates/project_panel2/src/project_panel.rs | 28 ++++++++--------
crates/terminal_view2/src/terminal_view.rs |  9 +++-
crates/ui2/src/components/context_menu.rs  | 39 +++++++++++++++++------
3 files changed, 49 insertions(+), 27 deletions(-)

Detailed changes

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)

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!()

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)