1use icedwright_types::Point;
5use icedwright_types::input::{KeyCode, NamedKey};
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10#[serde(tag = "type", rename_all = "snake_case")]
11pub enum WireEvent {
12 CursorMoved {
14 position: Point,
16 },
17 CursorEntered,
19 CursorLeft,
21 ButtonPressed {
23 button: WireButton,
25 },
26 ButtonReleased {
28 button: WireButton,
30 },
31 WheelScrolled {
33 delta: WireScrollDelta,
35 },
36 KeyPressed {
38 key: WireKey,
40 modified_key: WireKey,
42 physical_key: WirePhysicalKey,
44 location: WireLocation,
46 modifiers: WireModifiers,
48 #[serde(default)]
50 text: Option<String>,
51 #[serde(default)]
53 repeat: bool,
54 },
55 KeyReleased {
57 key: WireKey,
59 modified_key: WireKey,
61 physical_key: WirePhysicalKey,
63 location: WireLocation,
65 modifiers: WireModifiers,
67 },
68 ModifiersChanged {
70 modifiers: WireModifiers,
72 },
73 WindowFocused,
75 WindowUnfocused,
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
81#[serde(rename_all = "snake_case")]
82pub enum WireButton {
83 Left,
85 Right,
87 Middle,
89 Back,
91 Forward,
93 Other(u16),
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
99#[serde(rename_all = "snake_case")]
100pub enum WireScrollDelta {
101 Lines {
103 x: f32,
105 y: f32,
107 },
108 Pixels {
110 x: f32,
112 y: f32,
114 },
115}
116
117#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
119#[serde(rename_all = "snake_case")]
120pub enum WireKey {
121 Named(NamedKey),
123 Character(String),
125 Unidentified,
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
131#[serde(rename_all = "snake_case")]
132pub enum WirePhysicalKey {
133 Code(KeyCode),
135 Unidentified,
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
141#[serde(rename_all = "snake_case")]
142pub enum WireLocation {
143 #[default]
145 Standard,
146 Left,
148 Right,
150 Numpad,
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
156#[serde(default)]
157pub struct WireModifiers {
158 pub shift: bool,
160 pub control: bool,
162 pub alt: bool,
164 pub logo: bool,
166}
167
168#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
170#[serde(rename_all = "snake_case")]
171pub enum WireStatus {
172 Ignored,
174 Captured,
176}
177
178#[cfg(test)]
179mod tests {
180 use super::*;
181
182 #[test]
183 fn every_event_round_trips() -> Result<(), serde_json::Error> {
184 let modifiers = WireModifiers {
185 shift: true,
186 ..WireModifiers::default()
187 };
188 let events = [
189 WireEvent::CursorMoved {
190 position: Point::new(1.5, 2.0),
191 },
192 WireEvent::CursorEntered,
193 WireEvent::CursorLeft,
194 WireEvent::ButtonPressed {
195 button: WireButton::Left,
196 },
197 WireEvent::ButtonReleased {
198 button: WireButton::Other(9),
199 },
200 WireEvent::WheelScrolled {
201 delta: WireScrollDelta::Lines { x: 0.0, y: -3.0 },
202 },
203 WireEvent::WheelScrolled {
204 delta: WireScrollDelta::Pixels { x: 4.0, y: 0.0 },
205 },
206 WireEvent::KeyPressed {
207 key: WireKey::Character("a".to_owned()),
208 modified_key: WireKey::Character("A".to_owned()),
209 physical_key: WirePhysicalKey::Code(KeyCode::KeyA),
210 location: WireLocation::Standard,
211 modifiers,
212 text: Some("A".to_owned()),
213 repeat: false,
214 },
215 WireEvent::KeyReleased {
216 key: WireKey::Named(NamedKey::Enter),
217 modified_key: WireKey::Unidentified,
218 physical_key: WirePhysicalKey::Unidentified,
219 location: WireLocation::Numpad,
220 modifiers,
221 },
222 WireEvent::ModifiersChanged { modifiers },
223 WireEvent::WindowFocused,
224 WireEvent::WindowUnfocused,
225 ];
226 for event in events {
227 let json = serde_json::to_string(&event)?;
228 assert_eq!(serde_json::from_str::<WireEvent>(&json)?, event, "{json}");
229 }
230 assert_eq!(
231 serde_json::to_string(&WireEvent::CursorLeft)?,
232 r#"{"type":"cursor_left"}"#
233 );
234 Ok(())
235 }
236}