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
use crate::display::pointer_to_string;
use crate::listener::EventListener;
use std::fmt;
use strum::{Display as EnumDisplay, EnumIs, EnumString};
use tauri::{Runtime, Window};

#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, EnumDisplay, EnumIs, EnumString)]
#[strum(serialize_all = "lowercase")]
pub enum PointerEvent {
  ContextMenu,
}

#[derive(Debug)]
pub struct PointerShortcut<R: Runtime> {
  event: PointerEvent,
  pub(super) listeners: Vec<EventListener<R>>,
}

impl<R: Runtime> PointerShortcut<R> {
  pub fn new(event: PointerEvent) -> Self {
    Self { event, listeners: Vec::new() }
  }

  pub fn builder(event: PointerEvent) -> PointerShortcutBuilder<R> {
    PointerShortcutBuilder::new(event)
  }

  pub fn event(&self) -> PointerEvent {
    self.event
  }
}

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

#[derive(Debug)]
pub struct PointerShortcutBuilder<R: Runtime> {
  event: PointerEvent,
  listeners: Vec<EventListener<R>>,
}

impl<R: Runtime> PointerShortcutBuilder<R> {
  pub fn new(event: PointerEvent) -> Self {
    Self { event, listeners: Vec::new() }
  }

  /// 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) -> PointerShortcut<R> {
    PointerShortcut {
      event: self.event,
      listeners: self.listeners,
    }
  }
}