keybinding.rs

 1use gpui::Render;
 2use itertools::Itertools;
 3use story::Story;
 4
 5use crate::{KeyBinding, prelude::*};
 6
 7pub struct KeybindingStory;
 8
 9pub fn binding(key: &str) -> gpui::KeyBinding {
10    gpui::KeyBinding::new_no_action(key, None)
11}
12
13impl Render for KeybindingStory {
14    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
15        let all_modifier_permutations = ["ctrl", "alt", "cmd", "shift"].into_iter().permutations(2);
16
17        Story::container(cx)
18            .child(Story::title_for::<KeyBinding>(cx))
19            .child(Story::label("Single Key", cx))
20            .child(KeyBinding::new_from_gpui(binding("Z"), cx))
21            .child(Story::label("Single Key with Modifier", cx))
22            .child(
23                div()
24                    .flex()
25                    .gap_3()
26                    .child(KeyBinding::new_from_gpui(binding("ctrl-c"), cx))
27                    .child(KeyBinding::new_from_gpui(binding("alt-c"), cx))
28                    .child(KeyBinding::new_from_gpui(binding("cmd-c"), cx))
29                    .child(KeyBinding::new_from_gpui(binding("shift-c"), cx)),
30            )
31            .child(Story::label("Single Key with Modifier (Permuted)", cx))
32            .child(
33                div().flex().flex_col().children(
34                    all_modifier_permutations
35                        .chunks(4)
36                        .into_iter()
37                        .map(|chunk| {
38                            div()
39                                .flex()
40                                .gap_4()
41                                .py_3()
42                                .children(chunk.map(|permutation| {
43                                    KeyBinding::new_from_gpui(
44                                        binding(&(permutation.into_iter().join("-") + "-x")),
45                                        cx,
46                                    )
47                                }))
48                        }),
49                ),
50            )
51            .child(Story::label("Single Key with All Modifiers", cx))
52            .child(KeyBinding::new_from_gpui(
53                binding("ctrl-alt-cmd-shift-z"),
54                cx,
55            ))
56            .child(Story::label("Chord", cx))
57            .child(KeyBinding::new_from_gpui(binding("a z"), cx))
58            .child(Story::label("Chord with Modifier", cx))
59            .child(KeyBinding::new_from_gpui(binding("ctrl-a shift-z"), cx))
60            .child(KeyBinding::new_from_gpui(binding("fn-s"), cx))
61            .child(Story::label("Single Key with All Modifiers (Linux)", cx))
62            .child(
63                KeyBinding::new_from_gpui(binding("ctrl-alt-cmd-shift-z"), cx)
64                    .platform_style(PlatformStyle::Linux),
65            )
66            .child(Story::label("Chord (Linux)", cx))
67            .child(
68                KeyBinding::new_from_gpui(binding("a z"), cx).platform_style(PlatformStyle::Linux),
69            )
70            .child(Story::label("Chord with Modifier (Linux)", cx))
71            .child(
72                KeyBinding::new_from_gpui(binding("ctrl-a shift-z"), cx)
73                    .platform_style(PlatformStyle::Linux),
74            )
75            .child(
76                KeyBinding::new_from_gpui(binding("fn-s"), cx).platform_style(PlatformStyle::Linux),
77            )
78            .child(Story::label("Single Key with All Modifiers (Windows)", cx))
79            .child(
80                KeyBinding::new_from_gpui(binding("ctrl-alt-cmd-shift-z"), cx)
81                    .platform_style(PlatformStyle::Windows),
82            )
83            .child(Story::label("Chord (Windows)", cx))
84            .child(
85                KeyBinding::new_from_gpui(binding("a z"), cx)
86                    .platform_style(PlatformStyle::Windows),
87            )
88            .child(Story::label("Chord with Modifier (Windows)", cx))
89            .child(
90                KeyBinding::new_from_gpui(binding("ctrl-a shift-z"), cx)
91                    .platform_style(PlatformStyle::Windows),
92            )
93            .child(
94                KeyBinding::new_from_gpui(binding("fn-s"), cx)
95                    .platform_style(PlatformStyle::Windows),
96            )
97    }
98}