keystroke.rs

  1use anyhow::anyhow;
  2use serde::Deserialize;
  3use smallvec::SmallVec;
  4use std::fmt::Write;
  5
  6/// A keystroke and associated metadata generated by the platform
  7#[derive(Clone, Debug, Eq, PartialEq, Default, Deserialize, Hash)]
  8pub struct Keystroke {
  9    /// the state of the modifier keys at the time the keystroke was generated
 10    pub modifiers: Modifiers,
 11
 12    /// key is the character printed on the key that was pressed
 13    /// e.g. for option-s, key is "s"
 14    pub key: String,
 15
 16    /// ime_key is the character inserted by the IME engine when that key was pressed.
 17    /// e.g. for option-s, ime_key is "รŸ"
 18    pub ime_key: Option<String>,
 19}
 20
 21impl Keystroke {
 22    /// When matching a key we cannot know whether the user intended to type
 23    /// the ime_key or the key itself. On some non-US keyboards keys we use in our
 24    /// bindings are behind option (for example `$` is typed `alt-รง` on a Czech keyboard),
 25    /// and on some keyboards the IME handler converts a sequence of keys into a
 26    /// specific character (for example `"` is typed as `" space` on a brazilian keyboard).
 27    ///
 28    /// This method generates a list of potential keystroke candidates that could be matched
 29    /// against when resolving a keybinding.
 30    pub(crate) fn match_candidates(&self) -> SmallVec<[Keystroke; 2]> {
 31        let mut possibilities = SmallVec::new();
 32        match self.ime_key.as_ref() {
 33            Some(ime_key) => {
 34                if ime_key != &self.key {
 35                    possibilities.push(Keystroke {
 36                        modifiers: Modifiers {
 37                            control: self.modifiers.control,
 38                            alt: false,
 39                            shift: false,
 40                            command: false,
 41                            function: false,
 42                        },
 43                        key: ime_key.to_string(),
 44                        ime_key: None,
 45                    });
 46                }
 47                possibilities.push(Keystroke {
 48                    ime_key: None,
 49                    ..self.clone()
 50                });
 51            }
 52            None => possibilities.push(self.clone()),
 53        }
 54        possibilities
 55    }
 56
 57    /// key syntax is:
 58    /// [ctrl-][alt-][shift-][cmd-][fn-]key[->ime_key]
 59    /// ime_key syntax is only used for generating test events,
 60    /// when matching a key with an ime_key set will be matched without it.
 61    pub fn parse(source: &str) -> anyhow::Result<Self> {
 62        let mut control = false;
 63        let mut alt = false;
 64        let mut shift = false;
 65        let mut command = false;
 66        let mut function = false;
 67        let mut key = None;
 68        let mut ime_key = None;
 69
 70        let mut components = source.split('-').peekable();
 71        while let Some(component) = components.next() {
 72            match component {
 73                "ctrl" => control = true,
 74                "alt" => alt = true,
 75                "shift" => shift = true,
 76                "cmd" => command = true,
 77                "fn" => function = true,
 78                _ => {
 79                    if let Some(next) = components.peek() {
 80                        if next.is_empty() && source.ends_with('-') {
 81                            key = Some(String::from("-"));
 82                            break;
 83                        } else if next.len() > 1 && next.starts_with('>') {
 84                            key = Some(String::from(component));
 85                            ime_key = Some(String::from(&next[1..]));
 86                            components.next();
 87                        } else {
 88                            return Err(anyhow!("Invalid keystroke `{}`", source));
 89                        }
 90                    } else {
 91                        key = Some(String::from(component));
 92                    }
 93                }
 94            }
 95        }
 96
 97        let key = key.ok_or_else(|| anyhow!("Invalid keystroke `{}`", source))?;
 98
 99        Ok(Keystroke {
100            modifiers: Modifiers {
101                control,
102                alt,
103                shift,
104                command,
105                function,
106            },
107            key,
108            ime_key,
109        })
110    }
111}
112
113impl std::fmt::Display for Keystroke {
114    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115        if self.modifiers.control {
116            f.write_char('^')?;
117        }
118        if self.modifiers.alt {
119            f.write_char('โŒฅ')?;
120        }
121        if self.modifiers.command {
122            f.write_char('โŒ˜')?;
123        }
124        if self.modifiers.shift {
125            f.write_char('โ‡ง')?;
126        }
127        let key = match self.key.as_str() {
128            "backspace" => 'โŒซ',
129            "up" => 'โ†‘',
130            "down" => 'โ†“',
131            "left" => 'โ†',
132            "right" => 'โ†’',
133            "tab" => 'โ‡ฅ',
134            "escape" => 'โŽ‹',
135            key => {
136                if key.len() == 1 {
137                    key.chars().next().unwrap().to_ascii_uppercase()
138                } else {
139                    return f.write_str(key);
140                }
141            }
142        };
143        f.write_char(key)
144    }
145}
146
147/// The state of the modifier keys at some point in time
148#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Deserialize, Hash)]
149pub struct Modifiers {
150    /// The control key
151    pub control: bool,
152
153    /// The alt key
154    /// Sometimes also known as the 'meta' key
155    pub alt: bool,
156
157    /// The shift key
158    pub shift: bool,
159
160    /// The command key, on macos
161    /// the windows key, on windows
162    pub command: bool,
163
164    /// The function key
165    pub function: bool,
166}
167
168impl Modifiers {
169    /// Returns true if any modifier key is pressed
170    pub fn modified(&self) -> bool {
171        self.control || self.alt || self.shift || self.command || self.function
172    }
173
174    /// helper method for Modifiers with no modifiers
175    pub fn none() -> Modifiers {
176        Default::default()
177    }
178
179    /// helper method for Modifiers with just command
180    pub fn command() -> Modifiers {
181        Modifiers {
182            command: true,
183            ..Default::default()
184        }
185    }
186
187    /// helper method for Modifiers with just shift
188    pub fn shift() -> Modifiers {
189        Modifiers {
190            shift: true,
191            ..Default::default()
192        }
193    }
194
195    /// helper method for Modifiers with command + shift
196    pub fn command_shift() -> Modifiers {
197        Modifiers {
198            shift: true,
199            command: true,
200            ..Default::default()
201        }
202    }
203}