use icedwright_types::Point;
use icedwright_types::input::{KeyCode, NamedKey};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum WireEvent {
CursorMoved {
position: Point,
},
CursorEntered,
CursorLeft,
ButtonPressed {
button: WireButton,
},
ButtonReleased {
button: WireButton,
},
WheelScrolled {
delta: WireScrollDelta,
},
KeyPressed {
key: WireKey,
modified_key: WireKey,
physical_key: WirePhysicalKey,
location: WireLocation,
modifiers: WireModifiers,
#[serde(default)]
text: Option<String>,
#[serde(default)]
repeat: bool,
},
KeyReleased {
key: WireKey,
modified_key: WireKey,
physical_key: WirePhysicalKey,
location: WireLocation,
modifiers: WireModifiers,
},
ModifiersChanged {
modifiers: WireModifiers,
},
WindowFocused,
WindowUnfocused,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireButton {
Left,
Right,
Middle,
Back,
Forward,
Other(u16),
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireScrollDelta {
Lines {
x: f32,
y: f32,
},
Pixels {
x: f32,
y: f32,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireKey {
Named(NamedKey),
Character(String),
Unidentified,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WirePhysicalKey {
Code(KeyCode),
Unidentified,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireLocation {
#[default]
Standard,
Left,
Right,
Numpad,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct WireModifiers {
pub shift: bool,
pub control: bool,
pub alt: bool,
pub logo: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireStatus {
Ignored,
Captured,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_event_round_trips() -> Result<(), serde_json::Error> {
let modifiers = WireModifiers {
shift: true,
..WireModifiers::default()
};
let events = [
WireEvent::CursorMoved {
position: Point::new(1.5, 2.0),
},
WireEvent::CursorEntered,
WireEvent::CursorLeft,
WireEvent::ButtonPressed {
button: WireButton::Left,
},
WireEvent::ButtonReleased {
button: WireButton::Other(9),
},
WireEvent::WheelScrolled {
delta: WireScrollDelta::Lines { x: 0.0, y: -3.0 },
},
WireEvent::WheelScrolled {
delta: WireScrollDelta::Pixels { x: 4.0, y: 0.0 },
},
WireEvent::KeyPressed {
key: WireKey::Character("a".to_owned()),
modified_key: WireKey::Character("A".to_owned()),
physical_key: WirePhysicalKey::Code(KeyCode::KeyA),
location: WireLocation::Standard,
modifiers,
text: Some("A".to_owned()),
repeat: false,
},
WireEvent::KeyReleased {
key: WireKey::Named(NamedKey::Enter),
modified_key: WireKey::Unidentified,
physical_key: WirePhysicalKey::Unidentified,
location: WireLocation::Numpad,
modifiers,
},
WireEvent::ModifiersChanged { modifiers },
WireEvent::WindowFocused,
WireEvent::WindowUnfocused,
];
for event in events {
let json = serde_json::to_string(&event)?;
assert_eq!(serde_json::from_str::<WireEvent>(&json)?, event, "{json}");
}
assert_eq!(
serde_json::to_string(&WireEvent::CursorLeft)?,
r#"{"type":"cursor_left"}"#
);
Ok(())
}
}