icedwright-probe 0.1.3

What an iced app links for icedwright: semantic annotations, typed test ids and named tasks, all pass-through unless a feature turns them on
Documentation
//! Conversions between [`WireEvent`] and the `iced_core` events the probe delivers.

use iced_core::keyboard::key::{Code, NativeCode, Physical};
use iced_core::keyboard::{self, Key, Location, Modifiers};
use iced_core::mouse::{self, Button, ScrollDelta};
use iced_core::{Event, Point, SmolStr, window};
use icedwright_protocol::{
    WireButton, WireEvent, WireKey, WireLocation, WireModifiers, WirePhysicalKey, WireScrollDelta,
};
use icedwright_types::input::KeyCode;

/// The iced event for a wire event.
#[must_use]
pub fn to_iced(event: &WireEvent) -> Event {
    match event {
        WireEvent::CursorMoved { position } => Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(position.x, position.y),
        }),
        WireEvent::CursorEntered => Event::Mouse(mouse::Event::CursorEntered),
        WireEvent::CursorLeft => Event::Mouse(mouse::Event::CursorLeft),
        WireEvent::ButtonPressed { button } => {
            Event::Mouse(mouse::Event::ButtonPressed(button_to_iced(*button)))
        }
        WireEvent::ButtonReleased { button } => {
            Event::Mouse(mouse::Event::ButtonReleased(button_to_iced(*button)))
        }
        WireEvent::WheelScrolled { delta } => Event::Mouse(mouse::Event::WheelScrolled {
            delta: match *delta {
                WireScrollDelta::Lines { x, y } => ScrollDelta::Lines { x, y },
                WireScrollDelta::Pixels { x, y } => ScrollDelta::Pixels { x, y },
            },
        }),
        WireEvent::KeyPressed {
            key,
            modified_key,
            physical_key,
            location,
            modifiers,
            text,
            repeat,
        } => Event::Keyboard(keyboard::Event::KeyPressed {
            key: key_to_iced(key),
            modified_key: key_to_iced(modified_key),
            physical_key: physical_to_iced(*physical_key),
            location: location_to_iced(*location),
            modifiers: modifiers_to_iced(*modifiers),
            text: text.as_deref().map(SmolStr::new),
            repeat: *repeat,
        }),
        WireEvent::KeyReleased {
            key,
            modified_key,
            physical_key,
            location,
            modifiers,
        } => Event::Keyboard(keyboard::Event::KeyReleased {
            key: key_to_iced(key),
            modified_key: key_to_iced(modified_key),
            physical_key: physical_to_iced(*physical_key),
            location: location_to_iced(*location),
            modifiers: modifiers_to_iced(*modifiers),
        }),
        WireEvent::ModifiersChanged { modifiers } => Event::Keyboard(
            keyboard::Event::ModifiersChanged(modifiers_to_iced(*modifiers)),
        ),
        WireEvent::WindowFocused => Event::Window(window::Event::Focused),
        WireEvent::WindowUnfocused => Event::Window(window::Event::Unfocused),
    }
}

/// The wire event for an iced event, or `None` for events the protocol does not carry.
#[must_use]
pub fn from_iced(event: &Event) -> Option<WireEvent> {
    Some(match event {
        Event::Mouse(mouse::Event::CursorMoved { position }) => WireEvent::CursorMoved {
            position: icedwright_types::Point::new(position.x, position.y),
        },
        Event::Mouse(mouse::Event::CursorEntered) => WireEvent::CursorEntered,
        Event::Mouse(mouse::Event::CursorLeft) => WireEvent::CursorLeft,
        Event::Mouse(mouse::Event::ButtonPressed(button)) => WireEvent::ButtonPressed {
            button: button_from_iced(*button),
        },
        Event::Mouse(mouse::Event::ButtonReleased(button)) => WireEvent::ButtonReleased {
            button: button_from_iced(*button),
        },
        Event::Mouse(mouse::Event::WheelScrolled { delta }) => WireEvent::WheelScrolled {
            delta: match *delta {
                ScrollDelta::Lines { x, y } => WireScrollDelta::Lines { x, y },
                ScrollDelta::Pixels { x, y } => WireScrollDelta::Pixels { x, y },
            },
        },
        Event::Keyboard(keyboard::Event::KeyPressed {
            key,
            modified_key,
            physical_key,
            location,
            modifiers,
            text,
            repeat,
        }) => WireEvent::KeyPressed {
            key: key_from_iced(key),
            modified_key: key_from_iced(modified_key),
            physical_key: physical_from_iced(*physical_key),
            location: location_from_iced(*location),
            modifiers: modifiers_from_iced(*modifiers),
            text: text.as_ref().map(ToString::to_string),
            repeat: *repeat,
        },
        Event::Keyboard(keyboard::Event::KeyReleased {
            key,
            modified_key,
            physical_key,
            location,
            modifiers,
        }) => WireEvent::KeyReleased {
            key: key_from_iced(key),
            modified_key: key_from_iced(modified_key),
            physical_key: physical_from_iced(*physical_key),
            location: location_from_iced(*location),
            modifiers: modifiers_from_iced(*modifiers),
        },
        Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
            WireEvent::ModifiersChanged {
                modifiers: modifiers_from_iced(*modifiers),
            }
        }
        Event::Window(window::Event::Focused) => WireEvent::WindowFocused,
        Event::Window(window::Event::Unfocused) => WireEvent::WindowUnfocused,
        _ => return None,
    })
}

fn button_to_iced(button: WireButton) -> Button {
    match button {
        WireButton::Left => Button::Left,
        WireButton::Right => Button::Right,
        WireButton::Middle => Button::Middle,
        WireButton::Back => Button::Back,
        WireButton::Forward => Button::Forward,
        WireButton::Other(n) => Button::Other(n),
    }
}

fn button_from_iced(button: Button) -> WireButton {
    match button {
        Button::Left => WireButton::Left,
        Button::Right => WireButton::Right,
        Button::Middle => WireButton::Middle,
        Button::Back => WireButton::Back,
        Button::Forward => WireButton::Forward,
        Button::Other(n) => WireButton::Other(n),
    }
}

fn key_to_iced(key: &WireKey) -> Key {
    match key {
        WireKey::Named(named) => Key::Named((*named).into()),
        WireKey::Character(text) => Key::Character(SmolStr::new(text)),
        WireKey::Unidentified => Key::Unidentified,
    }
}

fn key_from_iced(key: &Key) -> WireKey {
    match key {
        Key::Named(named) => WireKey::Named((*named).into()),
        Key::Character(text) => WireKey::Character(text.to_string()),
        Key::Unidentified => WireKey::Unidentified,
    }
}

fn physical_to_iced(key: WirePhysicalKey) -> Physical {
    match key {
        WirePhysicalKey::Code(code) => Physical::Code(Code::from(code)),
        WirePhysicalKey::Unidentified => Physical::Unidentified(NativeCode::Unidentified),
    }
}

fn physical_from_iced(key: Physical) -> WirePhysicalKey {
    match key {
        Physical::Code(code) => KeyCode::try_from(code)
            .map(WirePhysicalKey::Code)
            .unwrap_or(WirePhysicalKey::Unidentified),
        Physical::Unidentified(_) => WirePhysicalKey::Unidentified,
    }
}

fn location_to_iced(location: WireLocation) -> Location {
    match location {
        WireLocation::Standard => Location::Standard,
        WireLocation::Left => Location::Left,
        WireLocation::Right => Location::Right,
        WireLocation::Numpad => Location::Numpad,
    }
}

fn location_from_iced(location: Location) -> WireLocation {
    match location {
        Location::Standard => WireLocation::Standard,
        Location::Left => WireLocation::Left,
        Location::Right => WireLocation::Right,
        Location::Numpad => WireLocation::Numpad,
    }
}

fn modifiers_to_iced(modifiers: WireModifiers) -> Modifiers {
    let mut held = Modifiers::empty();
    held.set(Modifiers::SHIFT, modifiers.shift);
    held.set(Modifiers::CTRL, modifiers.control);
    held.set(Modifiers::ALT, modifiers.alt);
    held.set(Modifiers::LOGO, modifiers.logo);
    held
}

fn modifiers_from_iced(modifiers: Modifiers) -> WireModifiers {
    WireModifiers {
        shift: modifiers.shift(),
        control: modifiers.control(),
        alt: modifiers.alt(),
        logo: modifiers.logo(),
    }
}

#[cfg(test)]
mod tests {
    use icedwright_types::input::NamedKey;

    use super::*;

    #[test]
    fn every_variant_round_trips_through_iced() {
        let modifiers = WireModifiers {
            shift: true,
            control: true,
            alt: false,
            logo: true,
        };
        let events = [
            WireEvent::CursorMoved {
                position: icedwright_types::Point::new(3.5, 4.0),
            },
            WireEvent::CursorEntered,
            WireEvent::CursorLeft,
            WireEvent::ButtonPressed {
                button: WireButton::Left,
            },
            WireEvent::ButtonPressed {
                button: WireButton::Back,
            },
            WireEvent::ButtonReleased {
                button: WireButton::Other(7),
            },
            WireEvent::ButtonReleased {
                button: WireButton::Forward,
            },
            WireEvent::ButtonReleased {
                button: WireButton::Middle,
            },
            WireEvent::ButtonReleased {
                button: WireButton::Right,
            },
            WireEvent::WheelScrolled {
                delta: WireScrollDelta::Lines { x: 1.0, y: -2.0 },
            },
            WireEvent::WheelScrolled {
                delta: WireScrollDelta::Pixels { x: 0.5, y: 8.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: true,
            },
            WireEvent::KeyReleased {
                key: WireKey::Named(NamedKey::Enter),
                modified_key: WireKey::Unidentified,
                physical_key: WirePhysicalKey::Unidentified,
                location: WireLocation::Numpad,
                modifiers,
            },
            WireEvent::KeyReleased {
                key: WireKey::Named(NamedKey::Shift),
                modified_key: WireKey::Named(NamedKey::Shift),
                physical_key: WirePhysicalKey::Code(KeyCode::ShiftLeft),
                location: WireLocation::Left,
                modifiers: WireModifiers::default(),
            },
            WireEvent::KeyReleased {
                key: WireKey::Named(NamedKey::Shift),
                modified_key: WireKey::Named(NamedKey::Shift),
                physical_key: WirePhysicalKey::Code(KeyCode::ShiftRight),
                location: WireLocation::Right,
                modifiers: WireModifiers::default(),
            },
            WireEvent::ModifiersChanged { modifiers },
            WireEvent::WindowFocused,
            WireEvent::WindowUnfocused,
        ];
        for event in events {
            assert_eq!(
                from_iced(&to_iced(&event)),
                Some(event.clone()),
                "{event:?}"
            );
        }
    }

    #[test]
    fn events_the_wire_does_not_carry_give_none() {
        assert_eq!(
            from_iced(&Event::Window(window::Event::CloseRequested)),
            None
        );
    }
}