tauri_plugin_prevent_default/shortcut/
keyboard.rs

1use super::ModifierKey;
2use crate::display;
3use std::fmt;
4
5#[derive(Debug)]
6pub struct KeyboardShortcut {
7  key: String,
8  modifiers: Vec<ModifierKey>,
9}
10
11impl KeyboardShortcut {
12  pub fn new(key: impl AsRef<str>) -> Self {
13    Self {
14      key: key.as_ref().to_owned(),
15      modifiers: Vec::new(),
16    }
17  }
18
19  /// Initialize a new keyboard shortcut builder with the specified key.
20  pub fn builder(key: impl AsRef<str>) -> KeyboardShortcutBuilder {
21    KeyboardShortcutBuilder::new(key)
22  }
23
24  /// Create a new keyboard shortcut with the specified key and modifiers.
25  pub fn with_modifiers(key: impl AsRef<str>, modifiers: &[ModifierKey]) -> Self {
26    Self::builder(key).modifiers(modifiers).build()
27  }
28
29  /// Create a new keyboard shortcut with the specified key and the `AltKey` modifier.
30  ///
31  /// # Example
32  /// ```
33  /// use tauri_plugin_prevent_default::KeyboardShortcut;
34  /// use tauri_plugin_prevent_default::ModifierKey::AltKey;
35  ///
36  /// // Both of these are equivalent.
37  /// tauri_plugin_prevent_default::Builder::new()
38  ///   .shortcut(KeyboardShortcut::with_alt("A"))
39  ///   .shortcut(KeyboardShortcut::with_modifiers("A", &[AltKey]))
40  ///   .build();
41  /// ```
42  pub fn with_alt(key: impl AsRef<str>) -> Self {
43    Self::builder(key).alt_key().build()
44  }
45
46  /// Create a new keyboard shortcut with the specified key and the `CtrlKey` modifier.
47  pub fn with_ctrl(key: impl AsRef<str>) -> Self {
48    Self::builder(key).ctrl_key().build()
49  }
50
51  /// Create a new keyboard shortcut with the specified key and the `CtrlKey` and `MetaKey` modifiers.
52  pub fn with_ctrl_meta(key: impl AsRef<str>) -> Self {
53    Self::builder(key)
54      .modifiers(&[ModifierKey::CtrlKey, ModifierKey::MetaKey])
55      .build()
56  }
57
58  /// Create a new keyboard shortcut with the specified key and the `CtrlKey` and `ShiftKey` modifiers.
59  pub fn with_ctrl_shift(key: impl AsRef<str>) -> Self {
60    Self::builder(key)
61      .modifiers(&[ModifierKey::CtrlKey, ModifierKey::ShiftKey])
62      .build()
63  }
64
65  /// Create a new keyboard shortcut with the specified key and the `MetaKey` modifier.
66  pub fn with_meta(key: impl AsRef<str>) -> Self {
67    Self::builder(key).meta_key().build()
68  }
69
70  /// Create a new keyboard shortcut with the specified key and the `ShiftKey` modifier.
71  pub fn with_shift(key: impl AsRef<str>) -> Self {
72    Self::builder(key).shift_key().build()
73  }
74
75  /// Create a new keyboard shortcut with the specified key and the `ShiftKey` and `AltKey` modifiers.
76  pub fn with_shift_alt(key: impl AsRef<str>) -> Self {
77    Self::builder(key)
78      .modifiers(&[ModifierKey::ShiftKey, ModifierKey::AltKey])
79      .build()
80  }
81
82  /// Create a new keyboard shortcut with the specified key and the `ShiftKey` and `MetaKey` modifiers.
83  pub fn with_shift_meta(key: impl AsRef<str>) -> Self {
84    Self::builder(key)
85      .modifiers(&[ModifierKey::ShiftKey, ModifierKey::MetaKey])
86      .build()
87  }
88
89  /// The key of the shortcut.
90  pub fn key(&self) -> &str {
91    &self.key
92  }
93
94  /// The modifiers of the shortcut.
95  pub fn modifiers(&self) -> &[ModifierKey] {
96    self.modifiers.as_slice()
97  }
98}
99
100impl fmt::Display for KeyboardShortcut {
101  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102    write!(f, "{}", display::keyboard(&self.key, &self.modifiers))
103  }
104}
105
106#[derive(Debug)]
107pub struct KeyboardShortcutBuilder {
108  key: String,
109  modifiers: Vec<ModifierKey>,
110}
111
112impl KeyboardShortcutBuilder {
113  /// Create a new keyboard shortcut builder with the specified key.
114  pub fn new(key: impl AsRef<str>) -> Self {
115    Self {
116      key: key.as_ref().to_owned(),
117      modifiers: Vec::new(),
118    }
119  }
120
121  /// Add a modifier to the shortcut.
122  #[must_use]
123  pub fn modifier(mut self, modifier: ModifierKey) -> Self {
124    self.modifiers.push(modifier);
125    self
126  }
127
128  /// Add multiple modifiers to the shortcut.
129  #[must_use]
130  pub fn modifiers(mut self, modifiers: &[ModifierKey]) -> Self {
131    self.modifiers.extend_from_slice(modifiers);
132    self
133  }
134
135  /// Add the `AltKey` modifier to the shortcut.
136  #[must_use]
137  pub fn alt_key(mut self) -> Self {
138    self.modifiers.push(ModifierKey::AltKey);
139    self
140  }
141
142  /// Add the `CtrlKey` modifier to the shortcut.
143  #[must_use]
144  pub fn ctrl_key(mut self) -> Self {
145    self.modifiers.push(ModifierKey::CtrlKey);
146    self
147  }
148
149  /// Add the `MetaKey` modifier to the shortcut.
150  #[must_use]
151  pub fn meta_key(mut self) -> Self {
152    self.modifiers.push(ModifierKey::MetaKey);
153    self
154  }
155
156  /// Add the `ShiftKey` modifier to the shortcut.
157  #[must_use]
158  pub fn shift_key(mut self) -> Self {
159    self.modifiers.push(ModifierKey::ShiftKey);
160    self
161  }
162
163  /// Build the keyboard shortcut.
164  pub fn build(self) -> KeyboardShortcut {
165    KeyboardShortcut {
166      key: self.key,
167      modifiers: self.modifiers,
168    }
169  }
170}