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 Rendered = Div;
 15
 16    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
 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        let mut icon: Option<Icon> = None;
 69
 70        if keystroke.key == "left".to_string() {
 71            icon = Some(Icon::ArrowLeft);
 72        } else if keystroke.key == "right".to_string() {
 73            icon = Some(Icon::ArrowRight);
 74        } else if keystroke.key == "up".to_string() {
 75            icon = Some(Icon::ArrowUp);
 76        } else if keystroke.key == "down".to_string() {
 77            icon = Some(Icon::ArrowDown);
 78        }
 79
 80        icon
 81    }
 82
 83    pub fn new(key_binding: gpui::KeyBinding) -> Self {
 84        Self { key_binding }
 85    }
 86}
 87
 88#[derive(IntoElement)]
 89pub struct Key {
 90    key: SharedString,
 91}
 92
 93impl RenderOnce for Key {
 94    type Rendered = Div;
 95
 96    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
 97        let single_char = self.key.len() == 1;
 98
 99        div()
100            .py_0()
101            .when_else(
102                single_char,
103                |el| el.w(rems(14. / 16.)).flex().flex_none().justify_center(),
104                |el| el.px_0p5(),
105            )
106            .h(rems(14. / 16.))
107            .text_ui()
108            .line_height(relative(1.))
109            .text_color(cx.theme().colors().text)
110            .child(self.key.clone())
111    }
112}
113
114impl Key {
115    pub fn new(key: impl Into<SharedString>) -> Self {
116        Self { key: key.into() }
117    }
118}
119
120#[derive(IntoElement)]
121pub struct KeyIcon {
122    icon: Icon,
123}
124
125impl RenderOnce for KeyIcon {
126    type Rendered = Div;
127
128    fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
129        div()
130            .w(rems(14. / 16.))
131            .child(IconElement::new(self.icon).size(IconSize::Small))
132    }
133}
134
135impl KeyIcon {
136    pub fn new(icon: Icon) -> Self {
137        Self { icon }
138    }
139}