icedwright-protocol 0.1.0

Wire types for icedwright: the raw semantic tree a live app records and the typed remote protocol
Documentation
//! A serde mirror of the parts of `iced_core::Event` the probe accepts. The probe converts
//! between this and iced's events; traces store it too.

use icedwright_types::Point;
use icedwright_types::input::{KeyCode, NamedKey};
use serde::{Deserialize, Serialize};

/// One input event.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum WireEvent {
    /// The pointer moved to a position in logical window coordinates.
    CursorMoved {
        /// Where it is now.
        position: Point,
    },
    /// The pointer entered the window.
    CursorEntered,
    /// The pointer left the window.
    CursorLeft,
    /// A mouse button went down.
    ButtonPressed {
        /// Which one.
        button: WireButton,
    },
    /// A mouse button came up.
    ButtonReleased {
        /// Which one.
        button: WireButton,
    },
    /// The wheel or a touchpad scrolled.
    WheelScrolled {
        /// By how much.
        delta: WireScrollDelta,
    },
    /// A key went down.
    KeyPressed {
        /// The key, with modifiers not applied.
        key: WireKey,
        /// The key with modifiers applied.
        modified_key: WireKey,
        /// The physical key.
        physical_key: WirePhysicalKey,
        /// Where on the keyboard it is.
        location: WireLocation,
        /// The modifiers held.
        modifiers: WireModifiers,
        /// The text the press produces.
        #[serde(default)]
        text: Option<String>,
        /// Whether it is an auto-repeat.
        #[serde(default)]
        repeat: bool,
    },
    /// A key came up.
    KeyReleased {
        /// The key, with modifiers not applied.
        key: WireKey,
        /// The key with modifiers applied.
        modified_key: WireKey,
        /// The physical key.
        physical_key: WirePhysicalKey,
        /// Where on the keyboard it is.
        location: WireLocation,
        /// The modifiers held.
        modifiers: WireModifiers,
    },
    /// The held modifiers changed.
    ModifiersChanged {
        /// The modifiers held now.
        modifiers: WireModifiers,
    },
    /// The window gained focus.
    WindowFocused,
    /// The window lost focus.
    WindowUnfocused,
}

/// A mouse button.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireButton {
    /// The primary button.
    Left,
    /// The secondary button.
    Right,
    /// The wheel button.
    Middle,
    /// Back.
    Back,
    /// Forward.
    Forward,
    /// Any other, by number.
    Other(u16),
}

/// How far a scroll moved.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireScrollDelta {
    /// In lines, as a mouse wheel reports.
    Lines {
        /// Horizontal lines.
        x: f32,
        /// Vertical lines.
        y: f32,
    },
    /// In logical pixels, as a touchpad reports.
    Pixels {
        /// Horizontal pixels.
        x: f32,
        /// Vertical pixels.
        y: f32,
    },
}

/// A logical key.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireKey {
    /// A named key such as `Enter`.
    Named(NamedKey),
    /// The text a key types.
    Character(String),
    /// A key with no known meaning.
    Unidentified,
}

/// A physical key.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WirePhysicalKey {
    /// A key code.
    Code(KeyCode),
    /// A key the platform could not name.
    Unidentified,
}

/// Where a key is on the keyboard.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireLocation {
    /// The main block.
    #[default]
    Standard,
    /// The left of a pair, such as left Shift.
    Left,
    /// The right of a pair.
    Right,
    /// The numeric keypad.
    Numpad,
}

/// The modifiers held.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct WireModifiers {
    /// Shift.
    pub shift: bool,
    /// Control.
    pub control: bool,
    /// Alt.
    pub alt: bool,
    /// The logo key.
    pub logo: bool,
}

/// What the widgets did with an event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WireStatus {
    /// No widget captured it.
    Ignored,
    /// A widget captured it.
    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(())
    }
}