keybinding.rs

  1use crate::{h_stack, prelude::*, Icon, IconElement, IconSize};
  2use gpui::{relative, rems, Action, Div, FocusHandle, IntoElement, Keystroke};
  3
  4#[derive(IntoElement, Clone)]
  5pub struct KeyBinding {
  6    /// A keybinding consists of a key and a set of modifier keys.
  7    /// More then one keybinding produces a chord.
  8    ///
  9    /// This should always contain at least one element.
 10    key_binding: gpui::KeyBinding,
 11}
 12
 13impl RenderOnce for KeyBinding {
 14    type Output = Div;
 15
 16    fn render(self, cx: &mut WindowContext) -> Self::Output {
 17        h_stack()
 18            .flex_none()
 19            .gap_2()
 20            .children(self.key_binding.keystrokes().iter().map(|keystroke| {
 21                let key_icon = Self::icon_for_key(&keystroke);
 22
 23                h_stack()
 24                    .flex_none()
 25                    .gap_0p5()
 26                    .bg(cx.theme().colors().element_background)
 27                    .p_0p5()
 28                    .rounded_sm()
 29                    .when(keystroke.modifiers.function, |el| el.child(Key::new("fn")))
 30                    .when(keystroke.modifiers.control, |el| {
 31                        el.child(KeyIcon::new(Icon::Control))
 32                    })
 33                    .when(keystroke.modifiers.alt, |el| {
 34                        el.child(KeyIcon::new(Icon::Option))
 35                    })
 36                    .when(keystroke.modifiers.command, |el| {
 37                        el.child(KeyIcon::new(Icon::Command))
 38                    })
 39                    .when(keystroke.modifiers.shift, |el| {
 40                        el.child(KeyIcon::new(Icon::Shift))
 41                    })
 42                    .when_some(key_icon, |el, icon| el.child(KeyIcon::new(icon)))
 43                    .when(key_icon.is_none(), |el| {
 44                        el.child(Key::new(keystroke.key.to_uppercase().clone()))
 45                    })
 46            }))
 47    }
 48}
 49
 50impl KeyBinding {
 51    pub fn for_action(action: &dyn Action, cx: &mut WindowContext) -> Option<Self> {
 52        let key_binding = cx.bindings_for_action(action).last().cloned()?;
 53        Some(Self::new(key_binding))
 54    }
 55
 56    // like for_action(), but lets you specify the context from which keybindings
 57    // are matched.
 58    pub fn for_action_in(
 59        action: &dyn Action,
 60        focus: &FocusHandle,
 61        cx: &mut WindowContext,
 62    ) -> Option<Self> {
 63        let key_binding = cx.bindings_for_action_in(action, focus).last().cloned()?;
 64        Some(Self::new(key_binding))
 65    }
 66
 67    fn icon_for_key(keystroke: &Keystroke) -> Option<Icon> {
 68        match keystroke.key.as_str() {
 69            "left" => Some(Icon::ArrowLeft),
 70            "right" => Some(Icon::ArrowRight),
 71            "up" => Some(Icon::ArrowUp),
 72            "down" => Some(Icon::ArrowDown),
 73            "backspace" => Some(Icon::Backspace),
 74            "delete" => Some(Icon::Delete),
 75            "enter" => Some(Icon::Return),
 76            "escape" => Some(Icon::Escape),
 77            "return" => Some(Icon::Return),
 78            "space" => Some(Icon::Space),
 79            "tab" => Some(Icon::Tab),
 80            _ => None,
 81        }
 82    }
 83
 84    pub fn new(key_binding: gpui::KeyBinding) -> Self {
 85        Self { key_binding }
 86    }
 87}
 88
 89#[derive(IntoElement)]
 90pub struct Key {
 91    key: SharedString,
 92}
 93
 94impl RenderOnce for Key {
 95    type Output = Div;
 96
 97    fn render(self, cx: &mut WindowContext) -> Self::Output {
 98        let single_char = self.key.len() == 1;
 99
100        div()
101            .py_0()
102            .map(|this| {
103                if single_char {
104                    this.w(rems(14. / 16.)).flex().flex_none().justify_center()
105                } else {
106                    this.px_0p5()
107                }
108            })
109            .h(rems(14. / 16.))
110            .text_ui()
111            .line_height(relative(1.))
112            .text_color(cx.theme().colors().text)
113            .child(self.key.clone())
114    }
115}
116
117impl Key {
118    pub fn new(key: impl Into<SharedString>) -> Self {
119        Self { key: key.into() }
120    }
121}
122
123#[derive(IntoElement)]
124pub struct KeyIcon {
125    icon: Icon,
126}
127
128impl RenderOnce for KeyIcon {
129    type Output = Div;
130
131    fn render(self, _cx: &mut WindowContext) -> Self::Output {
132        div()
133            .w(rems(14. / 16.))
134            .child(IconElement::new(self.icon).size(IconSize::Small))
135    }
136}
137
138impl KeyIcon {
139    pub fn new(icon: Icon) -> Self {
140        Self { icon }
141    }
142}