virtual_input/
mouse.rs

1use itertools::Itertools;
2use strum_macros::EnumIter;
3
4pub trait Mouse {
5    fn press(&self, button: MouseButton) -> bool;
6
7    fn release(&self, button: MouseButton) -> bool;
8
9    fn is_pressed(&self, button: MouseButton) -> bool;
10
11    fn is_pressed_all(&self, buttons: Vec<MouseButton>) -> Vec<bool> {
12        buttons
13            .into_iter()
14            .map(|b| self.is_pressed(b))
15            .collect_vec()
16    }
17
18    fn move_to(&self, x: u16, y: u16, display_index: Option<usize>) -> bool;
19
20    fn get_position(&self) -> MousePosition;
21}
22
23#[derive(Debug, Eq, PartialEq, Copy, Clone, EnumIter)]
24pub enum MouseButton {
25    Left,
26    Middle,
27    Right,
28}
29
30#[derive(Debug, Copy, Clone)]
31pub struct MousePosition {
32    pub display_index: usize,
33    pub x: u16,
34    pub y: u16,
35}
36
37pub struct MouseOptedToDisplay<M> {
38    mouse: M,
39    display_index: usize,
40}
41
42impl<M: Mouse> MouseOptedToDisplay<M> {
43    pub fn new(mouse: M, display_index: usize) -> Self {
44        MouseOptedToDisplay {
45            mouse,
46            display_index,
47        }
48    }
49}
50
51impl<M: Mouse> Mouse for MouseOptedToDisplay<M> {
52    fn press(&self, button: MouseButton) -> bool {
53        self.mouse.press(button)
54    }
55
56    fn release(&self, button: MouseButton) -> bool {
57        self.mouse.release(button)
58    }
59
60    fn is_pressed(&self, button: MouseButton) -> bool {
61        self.mouse.is_pressed(button)
62    }
63
64    fn move_to(&self, x: u16, y: u16, display_index: Option<usize>) -> bool {
65        match display_index {
66            None => self.move_to(x, y, Some(self.display_index)),
67            Some(display_index) => self.mouse.move_to(x, y, Some(display_index)),
68        }
69    }
70
71    fn get_position(&self) -> MousePosition {
72        self.mouse.get_position()
73    }
74}