keybinding.rs

  1use gpui::NoAction;
  2use gpui::Render;
  3use itertools::Itertools;
  4use settings::KeybindSource;
  5use story::Story;
  6
  7use crate::{KeyBinding, prelude::*};
  8
  9pub struct KeybindingStory;
 10
 11pub fn binding(key: &str) -> gpui::KeyBinding {
 12    gpui::KeyBinding::new(key, NoAction {}, None)
 13}
 14
 15impl Render for KeybindingStory {
 16    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 17        let all_modifier_permutations = ["ctrl", "alt", "cmd", "shift"].into_iter().permutations(2);
 18
 19        const SOURCE: KeybindSource = KeybindSource::Base;
 20
 21        Story::container(cx)
 22            .child(Story::title_for::<KeyBinding>(cx))
 23            .child(Story::label("Single Key", cx))
 24            .child(KeyBinding::from_keystrokes(
 25                binding("Z").keystrokes().into(),
 26                SOURCE,
 27            ))
 28            .child(Story::label("Single Key with Modifier", cx))
 29            .child(
 30                div()
 31                    .flex()
 32                    .gap_3()
 33                    .child(KeyBinding::from_keystrokes(
 34                        binding("ctrl-c").keystrokes().into(),
 35                        SOURCE,
 36                    ))
 37                    .child(KeyBinding::from_keystrokes(
 38                        binding("alt-c").keystrokes().into(),
 39                        SOURCE,
 40                    ))
 41                    .child(KeyBinding::from_keystrokes(
 42                        binding("cmd-c").keystrokes().into(),
 43                        SOURCE,
 44                    ))
 45                    .child(KeyBinding::from_keystrokes(
 46                        binding("shift-c").keystrokes().into(),
 47                        SOURCE,
 48                    )),
 49            )
 50            .child(Story::label("Single Key with Modifier (Permuted)", cx))
 51            .child(
 52                div().flex().flex_col().children(
 53                    all_modifier_permutations
 54                        .chunks(4)
 55                        .into_iter()
 56                        .map(|chunk| {
 57                            div()
 58                                .flex()
 59                                .gap_4()
 60                                .py_3()
 61                                .children(chunk.map(|permutation| {
 62                                    KeyBinding::from_keystrokes(
 63                                        binding(&(permutation.join("-") + "-x"))
 64                                            .keystrokes()
 65                                            .into(),
 66                                        SOURCE,
 67                                    )
 68                                }))
 69                        }),
 70                ),
 71            )
 72            .child(Story::label("Single Key with All Modifiers", cx))
 73            .child(KeyBinding::from_keystrokes(
 74                binding("ctrl-alt-cmd-shift-z").keystrokes().into(),
 75                SOURCE,
 76            ))
 77            .child(Story::label("Chord", cx))
 78            .child(KeyBinding::from_keystrokes(
 79                binding("a z").keystrokes().into(),
 80                SOURCE,
 81            ))
 82            .child(Story::label("Chord with Modifier", cx))
 83            .child(KeyBinding::from_keystrokes(
 84                binding("ctrl-a shift-z").keystrokes().into(),
 85                SOURCE,
 86            ))
 87            .child(KeyBinding::from_keystrokes(
 88                binding("fn-s").keystrokes().into(),
 89                SOURCE,
 90            ))
 91            .child(Story::label("Single Key with All Modifiers (Linux)", cx))
 92            .child(
 93                KeyBinding::from_keystrokes(
 94                    binding("ctrl-alt-cmd-shift-z").keystrokes().into(),
 95                    SOURCE,
 96                )
 97                .platform_style(PlatformStyle::Linux),
 98            )
 99            .child(Story::label("Chord (Linux)", cx))
100            .child(
101                KeyBinding::from_keystrokes(binding("a z").keystrokes().into(), SOURCE)
102                    .platform_style(PlatformStyle::Linux),
103            )
104            .child(Story::label("Chord with Modifier (Linux)", cx))
105            .child(
106                KeyBinding::from_keystrokes(binding("ctrl-a shift-z").keystrokes().into(), SOURCE)
107                    .platform_style(PlatformStyle::Linux),
108            )
109            .child(
110                KeyBinding::from_keystrokes(binding("fn-s").keystrokes().into(), SOURCE)
111                    .platform_style(PlatformStyle::Linux),
112            )
113            .child(Story::label("Single Key with All Modifiers (Windows)", cx))
114            .child(
115                KeyBinding::from_keystrokes(
116                    binding("ctrl-alt-cmd-shift-z").keystrokes().into(),
117                    SOURCE,
118                )
119                .platform_style(PlatformStyle::Windows),
120            )
121            .child(Story::label("Chord (Windows)", cx))
122            .child(
123                KeyBinding::from_keystrokes(binding("a z").keystrokes().into(), SOURCE)
124                    .platform_style(PlatformStyle::Windows),
125            )
126            .child(Story::label("Chord with Modifier (Windows)", cx))
127            .child(
128                KeyBinding::from_keystrokes(binding("ctrl-a shift-z").keystrokes().into(), SOURCE)
129                    .platform_style(PlatformStyle::Windows),
130            )
131            .child(
132                KeyBinding::from_keystrokes(binding("fn-s").keystrokes().into(), SOURCE)
133                    .platform_style(PlatformStyle::Windows),
134            )
135    }
136}