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