1use crate::prelude::*;
2use gpui::{Action, Div, RenderOnce};
3
4#[derive(RenderOnce, 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 Component for KeyBinding {
14 type Rendered = Div;
15
16 fn render(self, cx: &mut WindowContext) -> Self::Rendered {
17 div()
18 .flex()
19 .gap_2()
20 .children(self.key_binding.keystrokes().iter().map(|keystroke| {
21 div()
22 .flex()
23 .gap_1()
24 .when(keystroke.modifiers.function, |el| el.child(Key::new("fn")))
25 .when(keystroke.modifiers.control, |el| el.child(Key::new("^")))
26 .when(keystroke.modifiers.alt, |el| el.child(Key::new("⌥")))
27 .when(keystroke.modifiers.command, |el| el.child(Key::new("⌘")))
28 .when(keystroke.modifiers.shift, |el| el.child(Key::new("⇧")))
29 .child(Key::new(keystroke.key.clone()))
30 }))
31 }
32}
33
34impl KeyBinding {
35 pub fn for_action(action: &dyn Action, cx: &mut WindowContext) -> Option<Self> {
36 // todo! this last is arbitrary, we want to prefer users key bindings over defaults,
37 // and vim over normal (in vim mode), etc.
38 let key_binding = cx.bindings_for_action(action).last().cloned()?;
39 Some(Self::new(key_binding))
40 }
41
42 pub fn new(key_binding: gpui::KeyBinding) -> Self {
43 Self { key_binding }
44 }
45}
46
47#[derive(RenderOnce)]
48pub struct Key {
49 key: SharedString,
50}
51
52impl Component for Key {
53 type Rendered = Div;
54
55 fn render(self, cx: &mut WindowContext) -> Self::Rendered {
56 div()
57 .px_2()
58 .py_0()
59 .rounded_md()
60 .text_ui_sm()
61 .text_color(cx.theme().colors().text)
62 .bg(cx.theme().colors().element_background)
63 .child(self.key.clone())
64 }
65}
66
67impl Key {
68 pub fn new(key: impl Into<SharedString>) -> Self {
69 Self { key: key.into() }
70 }
71}
72
73#[cfg(feature = "stories")]
74pub use stories::*;
75
76#[cfg(feature = "stories")]
77mod stories {
78 use super::*;
79 pub use crate::KeyBinding;
80 use crate::Story;
81 use gpui::{actions, Div, Render};
82 use itertools::Itertools;
83 pub struct KeybindingStory;
84
85 actions!(NoAction);
86
87 pub fn binding(key: &str) -> gpui::KeyBinding {
88 gpui::KeyBinding::new(key, NoAction {}, None)
89 }
90
91 impl Render for KeybindingStory {
92 type Element = Div;
93
94 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
95 let all_modifier_permutations =
96 ["ctrl", "alt", "cmd", "shift"].into_iter().permutations(2);
97
98 Story::container(cx)
99 .child(Story::title_for::<KeyBinding>(cx))
100 .child(Story::label(cx, "Single Key"))
101 .child(KeyBinding::new(binding("Z")))
102 .child(Story::label(cx, "Single Key with Modifier"))
103 .child(
104 div()
105 .flex()
106 .gap_3()
107 .child(KeyBinding::new(binding("ctrl-c")))
108 .child(KeyBinding::new(binding("alt-c")))
109 .child(KeyBinding::new(binding("cmd-c")))
110 .child(KeyBinding::new(binding("shift-c"))),
111 )
112 .child(Story::label(cx, "Single Key with Modifier (Permuted)"))
113 .child(
114 div().flex().flex_col().children(
115 all_modifier_permutations
116 .chunks(4)
117 .into_iter()
118 .map(|chunk| {
119 div()
120 .flex()
121 .gap_4()
122 .py_3()
123 .children(chunk.map(|permutation| {
124 KeyBinding::new(binding(&*(permutation.join("-") + "-x")))
125 }))
126 }),
127 ),
128 )
129 .child(Story::label(cx, "Single Key with All Modifiers"))
130 .child(KeyBinding::new(binding("ctrl-alt-cmd-shift-z")))
131 .child(Story::label(cx, "Chord"))
132 .child(KeyBinding::new(binding("a z")))
133 .child(Story::label(cx, "Chord with Modifier"))
134 .child(KeyBinding::new(binding("ctrl-a shift-z")))
135 .child(KeyBinding::new(binding("fn-s")))
136 }
137 }
138}