Skip to main content

tauri_plugin_user_input/
models.rs

1use std::time::UNIX_EPOCH;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Deserialize, Serialize)]
6#[serde(rename_all = "camelCase")]
7pub struct PingRequest {
8    pub value: Option<String>,
9}
10
11#[derive(Debug, Clone, Default, Deserialize, Serialize)]
12#[serde(rename_all = "camelCase")]
13pub struct PingResponse {
14    pub value: Option<String>,
15}
16
17#[derive(Debug, Serialize, Deserialize, Clone)]
18#[serde(rename_all = "camelCase")]
19pub struct InputEvent {
20    pub event_type: EventType,
21    pub time: u128,
22    #[serde(flatten)]
23    pub data: InputEventData,
24}
25
26#[derive(Debug, Serialize, Deserialize, Clone)]
27#[serde(rename_all = "camelCase")]
28pub enum InputEventData {
29    Button(monio::Button),
30    Position {
31        x: f64,
32        y: f64,
33    },
34    DeltaPosition {
35        #[serde(rename = "deltaX")]
36        delta_x: i64,
37        #[serde(rename = "deltaY")]
38        delta_y: i64,
39    },
40    Key(monio::Key),
41}
42
43#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize, Eq, Hash)]
44pub enum EventType {
45    KeyPress,
46    KeyRelease,
47    KeyClick,
48    ButtonPress,
49    ButtonRelease,
50    ButtonClick,
51    MouseMove,
52    MouseDragged,
53    Wheel,
54}
55
56impl From<monio::Event> for InputEvent {
57    fn from(event: monio::Event) -> Self {
58        let data = match event.event_type {
59            monio::EventType::MousePressed
60            | monio::EventType::MouseReleased
61            | monio::EventType::MouseClicked => {
62                if let Some(mouse) = &event.mouse {
63                    InputEventData::Button(mouse.button.unwrap_or(monio::Button::Unknown(0)))
64                } else {
65                    InputEventData::Button(monio::Button::Unknown(0))
66                }
67            }
68            monio::EventType::MouseMoved | monio::EventType::MouseDragged => {
69                if let Some(mouse) = &event.mouse {
70                    InputEventData::Position {
71                        x: mouse.x,
72                        y: mouse.y,
73                    }
74                } else {
75                    InputEventData::Position { x: 0.0, y: 0.0 }
76                }
77            }
78            monio::EventType::MouseWheel => {
79                if let Some(wheel) = &event.wheel {
80                    let (delta_x, delta_y) = match wheel.direction {
81                        monio::ScrollDirection::Up => (0, wheel.delta as i64),
82                        monio::ScrollDirection::Down => (0, -(wheel.delta as i64)),
83                        monio::ScrollDirection::Left => (-(wheel.delta as i64), 0),
84                        monio::ScrollDirection::Right => (wheel.delta as i64, 0),
85                    };
86                    InputEventData::DeltaPosition { delta_x, delta_y }
87                } else {
88                    InputEventData::DeltaPosition {
89                        delta_x: 0,
90                        delta_y: 0,
91                    }
92                }
93            }
94            monio::EventType::KeyPressed
95            | monio::EventType::KeyReleased
96            | monio::EventType::KeyTyped => {
97                if let Some(kb) = &event.keyboard {
98                    InputEventData::Key(kb.key)
99                } else {
100                    InputEventData::Key(monio::Key::Unknown(0))
101                }
102            }
103            monio::EventType::HookEnabled | monio::EventType::HookDisabled => {
104                InputEventData::Key(monio::Key::Unknown(0))
105            }
106        };
107
108        let event_type = match event.event_type {
109            monio::EventType::KeyPressed => EventType::KeyPress,
110            monio::EventType::KeyReleased | monio::EventType::KeyTyped => EventType::KeyRelease,
111            monio::EventType::MousePressed => EventType::ButtonPress,
112            monio::EventType::MouseReleased | monio::EventType::MouseClicked => {
113                EventType::ButtonRelease
114            }
115            monio::EventType::MouseMoved => EventType::MouseMove,
116            monio::EventType::MouseDragged => EventType::MouseDragged,
117            monio::EventType::MouseWheel => EventType::Wheel,
118            monio::EventType::HookEnabled | monio::EventType::HookDisabled => EventType::KeyPress, // fallback, these won't typically be emitted
119        };
120
121        Self {
122            event_type,
123            time: event
124                .time
125                .duration_since(UNIX_EPOCH)
126                .unwrap_or_default()
127                .as_millis(),
128            data,
129        }
130    }
131}
132
133#[cfg(test)]
134mod tests {
135    use super::*;
136
137    #[test]
138    fn test_enigo_button() {
139        let b = enigo::Button::Left;
140        let b_str = format!("{:?}", b);
141        assert_eq!(b_str, "Left");
142        let btn: enigo::Button = serde_json::from_str(&format!("\"{}\"", "L")).unwrap();
143        assert_eq!(btn, enigo::Button::Left);
144        let btn: enigo::Button = serde_json::from_str(&format!("\"{}\"", "Left")).unwrap();
145        assert_eq!(btn, enigo::Button::Left);
146    }
147
148    #[test]
149    fn test_enigo_coordinate() {
150        let c = enigo::Coordinate::Abs;
151        let c_str = format!("{:?}", c);
152        assert_eq!(c_str, "Abs");
153        let coord: enigo::Coordinate = serde_json::from_str(&format!("\"{}\"", "Abs")).unwrap();
154        assert_eq!(coord, enigo::Coordinate::Abs);
155    }
156}