Pass `KeyBinding`s to `TextTooltip`s

Marshall Bowers created

Change summary

crates/ui2/src/components/keybinding.rs |  2 
crates/ui2/src/components/tooltip.rs    | 30 ++++++++++++--------------
crates/workspace2/src/workspace2.rs     | 12 +++++++++-
3 files changed, 25 insertions(+), 19 deletions(-)

Detailed changes

crates/ui2/src/components/keybinding.rs 🔗

@@ -3,7 +3,7 @@ use strum::EnumIter;
 
 use crate::prelude::*;
 
-#[derive(Component)]
+#[derive(Component, Clone)]
 pub struct KeyBinding {
     /// A keybinding consists of a key and a set of modifier keys.
     /// More then one keybinding produces a chord.

crates/ui2/src/components/tooltip.rs 🔗

@@ -1,15 +1,13 @@
-use gpui::{div, Component, Div, ParentElement, Render, SharedString, Styled, ViewContext};
+use gpui::{Div, Render};
 use theme2::ActiveTheme;
 
-use crate::{h_stack, v_stack, Label, LabelColor, StyledExt};
+use crate::prelude::*;
+use crate::{h_stack, v_stack, KeyBinding, Label, LabelColor, StyledExt};
 
-use super::keybinding;
-
-#[derive(Clone, Debug)]
 pub struct TextTooltip {
     title: SharedString,
     meta: Option<SharedString>,
-    keybinding: Option<SharedString>,
+    key_binding: Option<KeyBinding>,
 }
 
 impl TextTooltip {
@@ -17,7 +15,7 @@ impl TextTooltip {
         Self {
             title: title.into(),
             meta: None,
-            keybinding: None,
+            key_binding: None,
         }
     }
 
@@ -26,8 +24,8 @@ impl TextTooltip {
         self
     }
 
-    pub fn keybinding(mut self, keybinding: impl Into<SharedString>) -> Self {
-        self.keybinding = Some(keybinding.into());
+    pub fn key_binding(mut self, key_binding: impl Into<Option<KeyBinding>>) -> Self {
+        self.key_binding = key_binding.into();
         self
     }
 }
@@ -43,13 +41,13 @@ impl Render for TextTooltip {
             .text_color(cx.theme().colors().text)
             .py_1()
             .px_2()
-            .child(h_stack().child(self.title.clone()).when_some(
-                self.keybinding.clone(),
-                |this, keybinding| {
-                    this.justify_between()
-                        .child(Label::new(keybinding).color(LabelColor::Muted))
-                },
-            ))
+            .child(
+                h_stack()
+                    .child(self.title.clone())
+                    .when_some(self.key_binding.clone(), |this, key_binding| {
+                        this.justify_between().child(key_binding)
+                    }),
+            )
             .when_some(self.meta.clone(), |this, meta| {
                 this.child(Label::new(meta).color(LabelColor::Muted))
             })

crates/workspace2/src/workspace2.rs 🔗

@@ -69,7 +69,7 @@ use std::{
 };
 use theme2::ActiveTheme;
 pub use toolbar::{ToolbarItemLocation, ToolbarItemView};
-use ui::{h_stack, Button, ButtonVariant, Label, LabelColor, TextTooltip};
+use ui::{h_stack, Button, ButtonVariant, KeyBinding, Label, LabelColor, TextTooltip};
 use util::ResultExt;
 use uuid::Uuid;
 use workspace_settings::{AutosaveSetting, WorkspaceSettings};
@@ -2502,9 +2502,17 @@ impl Workspace {
                                     .color(Some(LabelColor::Muted)),
                             )
                             .tooltip(move |_, cx| {
+                                // todo!() Replace with real action.
+                                #[gpui::action]
+                                struct NoAction {}
+
                                 cx.build_view(|cx| {
                                     TextTooltip::new("Recent Branches")
-                                        .keybinding("⌘B")
+                                        .key_binding(KeyBinding::new(gpui::KeyBinding::new(
+                                            "cmd-b",
+                                            NoAction {},
+                                            None,
+                                        )))
                                         .meta("Only local branches shown")
                                 })
                             }),