use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum HostEvent {
PointerMoved {
x: f32,
y: f32,
},
PointerDown {
x: f32,
y: f32,
button: PointerButton,
},
PointerUp {
x: f32,
y: f32,
button: PointerButton,
},
Scroll {
dx: f32,
dy: f32,
},
Zoom {
delta: f32,
},
Text(String),
Key {
key: String,
pressed: bool,
modifiers: ModifiersState,
},
Focus(bool),
WindowResized {
width: f32,
height: f32,
},
CommandSurface {
surface_id: String,
action: String,
payload: Option<serde_json::Value>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PointerButton {
Primary,
Secondary,
Middle,
Back,
Forward,
Other(u16),
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModifiersState {
pub alt: bool,
pub ctrl: bool,
pub shift: bool,
pub mac_cmd: bool,
pub command: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn modifiers_default_is_all_false() {
let m = ModifiersState::default();
assert!(!m.alt && !m.ctrl && !m.shift && !m.mac_cmd && !m.command);
}
#[test]
fn host_event_serde_round_trip() {
let event = HostEvent::Key {
key: "Enter".to_string(),
pressed: true,
modifiers: ModifiersState {
ctrl: true,
..ModifiersState::default()
},
};
let json = serde_json::to_string(&event).expect("serialize");
let decoded: HostEvent = serde_json::from_str(&json).expect("deserialize");
assert_eq!(decoded, event);
}
#[test]
fn command_surface_event_round_trips_with_payload() {
let payload = serde_json::json!({"arg": 42, "label": "submit"});
let event = HostEvent::CommandSurface {
surface_id: "command_palette".into(),
action: "invoke".into(),
payload: Some(payload.clone()),
};
let json = serde_json::to_string(&event).unwrap();
let decoded: HostEvent = serde_json::from_str(&json).unwrap();
assert_eq!(decoded, event);
}
#[test]
fn pointer_button_other_preserves_value() {
let b = PointerButton::Other(42);
let json = serde_json::to_string(&b).unwrap();
let back: PointerButton = serde_json::from_str(&json).unwrap();
assert_eq!(b, back);
}
}