#![allow(dead_code, reason = "feature-gated")]
use crate::{JsInstant, js_int32, js_number};
#[doc = crate::_tags!(event web uid)]
#[doc = crate::_doc_location!("sys/os/browser/web")]
#[repr(C)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub enum WebEventKind {
#[default]
Unknown = 0,
Click = 1,
KeyDown = 2,
KeyUp = 3,
MouseDown = 4,
MouseUp = 5,
MouseMove = 6,
PointerDown = 7,
PointerUp = 8,
PointerMove = 9,
GamepadPoll = 10,
Resize = 11,
}
impl WebEventKind {
pub const fn from_repr(from: u8) -> Self {
use WebEventKind as E;
match from {
1 => E::Click,
2 => E::KeyDown,
3 => E::KeyUp,
4 => E::MouseDown,
5 => E::MouseUp,
6 => E::MouseMove,
7 => E::PointerDown,
8 => E::PointerUp,
9 => E::PointerMove,
10 => E::GamepadPoll,
11 => E::Resize,
_ => E::Unknown,
}
}
pub const fn as_str(self) -> &'static str {
use WebEventKind as E;
match self {
E::Click => "click",
E::KeyDown => "keydown",
E::KeyUp => "keyup",
E::MouseDown => "mousedown",
E::MouseUp => "mouseup",
E::MouseMove => "mousemove",
E::PointerDown => "pointerdown",
E::PointerUp => "pointerup",
E::PointerMove => "pointermove",
E::GamepadPoll => "gamepadpoll",
E::Resize => "resize",
E::Unknown => "none",
}
}
}
#[doc = crate::_tags!(event web)]
#[doc = crate::_doc_location!("sys/os/browser/web")]
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct WebEventMouse {
pub x: js_number,
pub y: js_number,
pub button: u8,
pub buttons: u8,
pub etype: WebEventKind,
pub timestamp: JsInstant,
}
impl WebEventMouse {
pub(crate) fn new(
x: js_number,
y: js_number,
button: u8,
buttons: u8,
etype: WebEventKind,
timestamp: JsInstant,
) -> Self {
Self { x, y, button, buttons, etype, timestamp }
}
}
#[doc = crate::_tags!(event web)]
#[doc = crate::_doc_location!("sys/os/browser/web")]
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct WebEventPointer {
pub x: js_number,
pub y: js_number,
pub pressure: js_number,
pub id: js_int32,
pub tilt_x: i8,
pub tilt_y: i8,
pub twist: u16,
pub etype: WebEventKind,
pub timestamp: JsInstant,
}
impl WebEventPointer {
#[allow(clippy::too_many_arguments)] #[rustfmt::skip]
pub(crate) fn new(
x: js_number,
y: js_number,
pressure: js_number,
id: js_int32,
tilt_x: i8,
tilt_y: i8,
twist: u16,
etype: WebEventKind,
timestamp: JsInstant,
) -> Self {
Self { x, y, pressure, id, tilt_x, tilt_y, twist, etype, timestamp }
}
}
#[doc = crate::_tags!(interaction web)]
#[doc = crate::_doc_location!("sys/os/browser/web")]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum WebKeyLocation {
#[default]
Standard = 0,
Left = 1,
Right = 2,
NumPad = 3,
}
impl WebKeyLocation {
pub const fn from_repr(from: u8) -> Self {
use WebKeyLocation as L;
match from {
0 => L::Standard,
1 => L::Left,
2 => L::Right,
3 => L::NumPad,
_ => L::Standard,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test] #[rustfmt::skip]
fn sizes_of() {
assert_eq![ 4, size_of::<WebEventKind>()]; assert_eq![32, size_of::<WebEventMouse>()]; assert_eq![48, size_of::<WebEventPointer>()]; assert_eq![ 1, size_of::<WebKeyLocation>()]; }
#[test]
fn js_event_conversions() {
assert_eq!(WebEventKind::from_repr(2), WebEventKind::KeyDown);
assert_eq!(WebEventKind::from_repr(3), WebEventKind::KeyUp);
assert_eq!(WebEventKind::from_repr(99), WebEventKind::Unknown);
}
}