1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use super::ModifierKey;
use crate::display::keyboard_to_string;
use crate::listener::EventListener;
use std::fmt;
use tauri::{Runtime, Window};

#[derive(Debug)]
pub struct KeyboardShortcut<R: Runtime> {
  key: String,
  modifiers: Vec<ModifierKey>,
  pub(super) listeners: Vec<EventListener<R>>,
}

impl<R: Runtime> KeyboardShortcut<R> {
  pub fn new(key: impl AsRef<str>) -> Self {
    Self {
      key: key.as_ref().to_owned(),
      modifiers: Vec::new(),
      listeners: Vec::new(),
    }
  }

  pub fn with_modifiers(key: impl AsRef<str>, modifiers: &[ModifierKey]) -> Self {
    Self::builder(key).modifiers(modifiers).build()
  }

  pub fn builder(key: impl AsRef<str>) -> KeyboardShortcutBuilder<R> {
    KeyboardShortcutBuilder::new(key)
  }

  pub fn key(&self) -> &str {
    &self.key
  }

  pub fn modifiers(&self) -> &[ModifierKey] {
    self.modifiers.as_slice()
  }
}

impl<R: Runtime> fmt::Display for KeyboardShortcut<R> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "{}", keyboard_to_string(&self.key, &self.modifiers))
  }
}

#[derive(Debug)]
pub struct KeyboardShortcutBuilder<R: Runtime> {
  key: String,
  modifiers: Vec<ModifierKey>,
  listeners: Vec<EventListener<R>>,
}

impl<R: Runtime> KeyboardShortcutBuilder<R> {
  pub fn new(key: impl AsRef<str>) -> Self {
    Self {
      key: key.as_ref().to_owned(),
      modifiers: Vec::new(),
      listeners: Vec::new(),
    }
  }

  pub fn modifier(mut self, modifier: ModifierKey) -> Self {
    self.modifiers.push(modifier);
    self
  }

  pub fn modifiers(mut self, modifiers: &[ModifierKey]) -> Self {
    self.modifiers.extend_from_slice(modifiers);
    self
  }

  pub fn alt_key(mut self) -> Self {
    self.modifiers.push(ModifierKey::AltKey);
    self
  }

  pub fn ctrl_key(mut self) -> Self {
    self.modifiers.push(ModifierKey::CtrlKey);
    self
  }

  pub fn shift_key(mut self) -> Self {
    self.modifiers.push(ModifierKey::ShiftKey);
    self
  }

  /// Set a listener for the shortcut.
  pub fn on<F>(mut self, listener: F) -> Self
  where
    F: Fn(&Window<R>) + Send + Sync + 'static,
  {
    let listener = EventListener::new(listener);
    self.listeners.push(listener);
    self
  }

  pub fn build(self) -> KeyboardShortcut<R> {
    KeyboardShortcut {
      key: self.key,
      modifiers: self.modifiers,
      listeners: self.listeners,
    }
  }
}