1use gpui::Action;
2use strum::EnumIter;
3
4use crate::prelude::*;
5
6#[derive(Component, Clone)]
7pub struct KeyBinding {
8 /// A keybinding consists of a key and a set of modifier keys.
9 /// More then one keybinding produces a chord.
10 ///
11 /// This should always contain at least one element.
12 key_binding: gpui::KeyBinding,
13}
14
15impl KeyBinding {
16 pub fn for_action(action: &dyn Action, cx: &mut WindowContext) -> Option<Self> {
17 // todo! this last is arbitrary, we want to prefer users key bindings over defaults,
18 // and vim over normal (in vim mode), etc.
19 let key_binding = cx.bindings_for_action(action).last().cloned()?;
20 Some(Self::new(key_binding))
21 }
22
23 pub fn new(key_binding: gpui::KeyBinding) -> Self {
24 Self { key_binding }
25 }
26
27 fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
28 div()
29 .flex()
30 .gap_2()
31 .children(self.key_binding.keystrokes().iter().map(|keystroke| {
32 div()
33 .flex()
34 .gap_1()
35 .when(keystroke.modifiers.function, |el| el.child(Key::new("fn")))
36 .when(keystroke.modifiers.control, |el| el.child(Key::new("^")))
37 .when(keystroke.modifiers.alt, |el| el.child(Key::new("⌥")))
38 .when(keystroke.modifiers.command, |el| el.child(Key::new("⌘")))
39 .when(keystroke.modifiers.shift, |el| el.child(Key::new("⇧")))
40 .child(Key::new(keystroke.key.clone()))
41 }))
42 }
43}
44
45#[derive(Component)]
46pub struct Key {
47 key: SharedString,
48}
49
50impl Key {
51 pub fn new(key: impl Into<SharedString>) -> Self {
52 Self { key: key.into() }
53 }
54
55 fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
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
67// NOTE: The order the modifier keys appear in this enum impacts the order in
68// which they are rendered in the UI.
69#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, EnumIter)]
70pub enum ModifierKey {
71 Control,
72 Alt,
73 Command,
74 Shift,
75}
76
77#[cfg(feature = "stories")]
78pub use stories::*;
79
80#[cfg(feature = "stories")]
81mod stories {
82 use super::*;
83 pub use crate::KeyBinding;
84 use crate::{binding, Story};
85 use gpui::{Div, Render};
86 use itertools::Itertools;
87 pub struct KeybindingStory;
88
89 impl Render for KeybindingStory {
90 type Element = Div<Self>;
91
92 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
93 let all_modifier_permutations =
94 ["ctrl", "alt", "cmd", "shift"].into_iter().permutations(2);
95
96 Story::container(cx)
97 .child(Story::title_for::<_, KeyBinding>(cx))
98 .child(Story::label(cx, "Single Key"))
99 .child(KeyBinding::new(binding("Z")))
100 .child(Story::label(cx, "Single Key with Modifier"))
101 .child(
102 div()
103 .flex()
104 .gap_3()
105 .child(KeyBinding::new(binding("ctrl-c")))
106 .child(KeyBinding::new(binding("alt-c")))
107 .child(KeyBinding::new(binding("cmd-c")))
108 .child(KeyBinding::new(binding("shift-c"))),
109 )
110 .child(Story::label(cx, "Single Key with Modifier (Permuted)"))
111 .child(
112 div().flex().flex_col().children(
113 all_modifier_permutations
114 .chunks(4)
115 .into_iter()
116 .map(|chunk| {
117 div()
118 .flex()
119 .gap_4()
120 .py_3()
121 .children(chunk.map(|permutation| {
122 KeyBinding::new(binding(&*(permutation.join("-") + "-x")))
123 }))
124 }),
125 ),
126 )
127 .child(Story::label(cx, "Single Key with All Modifiers"))
128 .child(KeyBinding::new(binding("ctrl-alt-cmd-shift-z")))
129 .child(Story::label(cx, "Chord"))
130 .child(KeyBinding::new(binding("a z")))
131 .child(Story::label(cx, "Chord with Modifier"))
132 .child(KeyBinding::new(binding("ctrl-a shift-z")))
133 .child(KeyBinding::new(binding("fn-s")))
134 }
135 }
136}