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
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use tauri_hotkey::{parse_hotkey, HotkeyManager};

/// The shortcut manager builder.
#[derive(Default)]
pub struct ShortcutManager(HotkeyManager);

impl ShortcutManager {
  /// Initializes a new instance of the shortcut manager.
  pub fn new() -> Self {
    Default::default()
  }

  /// Determines whether the given hotkey is registered or not.
  pub fn is_registered(&self, shortcut: String) -> crate::api::Result<bool> {
    let hotkey = parse_hotkey(&shortcut)?;
    Ok(self.0.is_registered(&hotkey))
  }

  /// Registers a new shortcut handler.
  pub fn register<H: FnMut() + Send + 'static>(
    &mut self,
    shortcut: String,
    handler: H,
  ) -> crate::api::Result<()> {
    let hotkey = parse_hotkey(&shortcut)?;
    self.0.register(hotkey, handler)?;
    Ok(())
  }

  /// Unregister a previously registered shortcut handler.
  pub fn unregister(&mut self, shortcut: String) -> crate::api::Result<()> {
    let hotkey = parse_hotkey(&shortcut)?;
    self.0.unregister(&hotkey)?;
    Ok(())
  }

  /// Unregisters all shortcuts registered by this application.
  pub fn unregister_all(&mut self) -> crate::api::Result<()> {
    self.0.unregister_all()?;
    Ok(())
  }
}