1use crate::{h_stack, prelude::*, Icon, IconElement, IconSize};
2use gpui::{relative, rems, Action, Div, 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 // todo! this last is arbitrary, we want to prefer users key bindings over defaults,
53 // and vim over normal (in vim mode), etc.
54 let key_binding = cx.bindings_for_action(action).last().cloned()?;
55 Some(Self::new(key_binding))
56 }
57
58 fn icon_for_key(keystroke: &Keystroke) -> Option<Icon> {
59 let mut icon: Option<Icon> = None;
60
61 if keystroke.key == "left".to_string() {
62 icon = Some(Icon::ArrowLeft);
63 } else if keystroke.key == "right".to_string() {
64 icon = Some(Icon::ArrowRight);
65 } else if keystroke.key == "up".to_string() {
66 icon = Some(Icon::ArrowUp);
67 } else if keystroke.key == "down".to_string() {
68 icon = Some(Icon::ArrowDown);
69 }
70
71 icon
72 }
73
74 pub fn new(key_binding: gpui::KeyBinding) -> Self {
75 Self { key_binding }
76 }
77}
78
79#[derive(IntoElement)]
80pub struct Key {
81 key: SharedString,
82}
83
84impl RenderOnce for Key {
85 type Rendered = Div;
86
87 fn render(self, cx: &mut WindowContext) -> Self::Rendered {
88 let single_char = self.key.len() == 1;
89
90 div()
91 .py_0()
92 .when(single_char, |el| {
93 el.w(rems(14. / 16.)).flex().flex_none().justify_center()
94 })
95 .when(!single_char, |el| el.px_0p5())
96 .h(rems(14. / 16.))
97 .text_ui()
98 .line_height(relative(1.))
99 .text_color(cx.theme().colors().text)
100 .child(self.key.clone())
101 }
102}
103
104impl Key {
105 pub fn new(key: impl Into<SharedString>) -> Self {
106 Self { key: key.into() }
107 }
108}
109
110#[derive(IntoElement)]
111pub struct KeyIcon {
112 icon: Icon,
113}
114
115impl RenderOnce for KeyIcon {
116 type Rendered = Div;
117
118 fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
119 div()
120 .w(rems(14. / 16.))
121 .child(IconElement::new(self.icon).size(IconSize::Small))
122 }
123}
124
125impl KeyIcon {
126 pub fn new(icon: Icon) -> Self {
127 Self { icon }
128 }
129}