Skip to main content

icedwright_protocol/
event.rs

1//! A serde mirror of the parts of `iced_core::Event` the probe accepts. The probe converts
2//! between this and iced's events; traces store it too.
3
4use icedwright_types::Point;
5use icedwright_types::input::{KeyCode, NamedKey};
6use serde::{Deserialize, Serialize};
7
8/// One input event.
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10#[serde(tag = "type", rename_all = "snake_case")]
11pub enum WireEvent {
12    /// The pointer moved to a position in logical window coordinates.
13    CursorMoved {
14        /// Where it is now.
15        position: Point,
16    },
17    /// The pointer entered the window.
18    CursorEntered,
19    /// The pointer left the window.
20    CursorLeft,
21    /// A mouse button went down.
22    ButtonPressed {
23        /// Which one.
24        button: WireButton,
25    },
26    /// A mouse button came up.
27    ButtonReleased {
28        /// Which one.
29        button: WireButton,
30    },
31    /// The wheel or a touchpad scrolled.
32    WheelScrolled {
33        /// By how much.
34        delta: WireScrollDelta,
35    },
36    /// A key went down.
37    KeyPressed {
38        /// The key, with modifiers not applied.
39        key: WireKey,
40        /// The key with modifiers applied.
41        modified_key: WireKey,
42        /// The physical key.
43        physical_key: WirePhysicalKey,
44        /// Where on the keyboard it is.
45        location: WireLocation,
46        /// The modifiers held.
47        modifiers: WireModifiers,
48        /// The text the press produces.
49        #[serde(default)]
50        text: Option<String>,
51        /// Whether it is an auto-repeat.
52        #[serde(default)]
53        repeat: bool,
54    },
55    /// A key came up.
56    KeyReleased {
57        /// The key, with modifiers not applied.
58        key: WireKey,
59        /// The key with modifiers applied.
60        modified_key: WireKey,
61        /// The physical key.
62        physical_key: WirePhysicalKey,
63        /// Where on the keyboard it is.
64        location: WireLocation,
65        /// The modifiers held.
66        modifiers: WireModifiers,
67    },
68    /// The held modifiers changed.
69    ModifiersChanged {
70        /// The modifiers held now.
71        modifiers: WireModifiers,
72    },
73    /// The window gained focus.
74    WindowFocused,
75    /// The window lost focus.
76    WindowUnfocused,
77}
78
79/// A mouse button.
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
81#[serde(rename_all = "snake_case")]
82pub enum WireButton {
83    /// The primary button.
84    Left,
85    /// The secondary button.
86    Right,
87    /// The wheel button.
88    Middle,
89    /// Back.
90    Back,
91    /// Forward.
92    Forward,
93    /// Any other, by number.
94    Other(u16),
95}
96
97/// How far a scroll moved.
98#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
99#[serde(rename_all = "snake_case")]
100pub enum WireScrollDelta {
101    /// In lines, as a mouse wheel reports.
102    Lines {
103        /// Horizontal lines.
104        x: f32,
105        /// Vertical lines.
106        y: f32,
107    },
108    /// In logical pixels, as a touchpad reports.
109    Pixels {
110        /// Horizontal pixels.
111        x: f32,
112        /// Vertical pixels.
113        y: f32,
114    },
115}
116
117/// A logical key.
118#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
119#[serde(rename_all = "snake_case")]
120pub enum WireKey {
121    /// A named key such as `Enter`.
122    Named(NamedKey),
123    /// The text a key types.
124    Character(String),
125    /// A key with no known meaning.
126    Unidentified,
127}
128
129/// A physical key.
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
131#[serde(rename_all = "snake_case")]
132pub enum WirePhysicalKey {
133    /// A key code.
134    Code(KeyCode),
135    /// A key the platform could not name.
136    Unidentified,
137}
138
139/// Where a key is on the keyboard.
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
141#[serde(rename_all = "snake_case")]
142pub enum WireLocation {
143    /// The main block.
144    #[default]
145    Standard,
146    /// The left of a pair, such as left Shift.
147    Left,
148    /// The right of a pair.
149    Right,
150    /// The numeric keypad.
151    Numpad,
152}
153
154/// The modifiers held.
155#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
156#[serde(default)]
157pub struct WireModifiers {
158    /// Shift.
159    pub shift: bool,
160    /// Control.
161    pub control: bool,
162    /// Alt.
163    pub alt: bool,
164    /// The logo key.
165    pub logo: bool,
166}
167
168/// What the widgets did with an event.
169#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
170#[serde(rename_all = "snake_case")]
171pub enum WireStatus {
172    /// No widget captured it.
173    Ignored,
174    /// A widget captured it.
175    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}