use crate::lang::prog::ffi::js::{JsInstant, JsNumFmt, js_number};
use crate::ui::event::{
EventButtons, EventKind, EventKindTimed, EventTimestamp, EventWheel, EventWheelUnit, KeyMods,
};
use crate::{Timed, impl_trait, is};
#[doc = crate::_tags!(event web)]
#[doc = crate::_doc_meta!{
location("sys/os/browser/web"),
test_size_of(WebEventWheel = 48|384; niche Option),
}]
#[repr(C)]
#[derive(Copy, Clone, PartialEq)]
pub struct WebEventWheel {
pub x: js_number, pub y: js_number, pub delta_x: js_number, pub delta_y: js_number,
pub buttons: u8, pub mods: KeyMods, pub unit: EventWheelUnit, pub timestamp: JsInstant, }
impl_trait! { fmt::Debug for WebEventWheel |self, f| {
f.debug_struct("WebEventWheel")
.field("x", &JsNumFmt::<2>(self.x))
.field("y", &JsNumFmt::<2>(self.y))
.field("delta_x", &JsNumFmt::<2>(self.delta_x))
.field("delta_y", &JsNumFmt::<2>(self.delta_y))
.field("buttons", &EventButtons::from_bits(self.buttons))
.field("mods", &self.mods)
.field("unit", &self.unit)
.field("timestamp", &self.timestamp)
.finish()
}}
impl WebEventWheel {
#[allow(clippy::too_many_arguments)]
pub const fn new(
x: js_number,
y: js_number,
delta_x: js_number,
delta_y: js_number,
buttons: u8,
mods: KeyMods,
unit: EventWheelUnit,
timestamp: JsInstant,
) -> Self {
Self {
x,
y,
delta_x,
delta_y,
buttons,
mods,
unit,
timestamp,
}
}
pub const fn buttons(self) -> EventButtons {
EventButtons::from_bits(self.buttons)
}
pub const fn web_buttons(self) -> u8 {
self.buttons
}
pub const fn to_kind_timed(self) -> EventKindTimed {
let kind = EventKind::Wheel(EventWheel {
delta_x: self.delta_x as i32,
delta_y: self.delta_y as i32,
unit: self.unit,
x: self.x as i32,
y: self.y as i32,
buttons: EventButtons::from_bits(self.buttons),
mods: self.mods,
});
let timestamp = Some(EventTimestamp::from_js(self.timestamp));
EventKindTimed::new(kind, timestamp)
}
pub const fn from_event_wheel_timed(
from: Timed<EventWheel, Option<EventTimestamp>>,
) -> WebEventWheel {
let timestamp = is![let Some(t) = from.time, t.to_js(), JsInstant { ms: 0.0 }];
WebEventWheel {
x: from.value.x as js_number,
y: from.value.y as js_number,
delta_x: from.value.delta_x as js_number,
delta_y: from.value.delta_y as js_number,
buttons: from.value.buttons.bits(),
mods: from.value.mods,
unit: from.value.unit,
timestamp,
}
}
}
impl From<WebEventWheel> for EventKindTimed {
fn from(from: WebEventWheel) -> Self {
from.to_kind_timed()
}
}
impl From<Timed<EventWheel, Option<EventTimestamp>>> for WebEventWheel {
fn from(from: Timed<EventWheel, Option<EventTimestamp>>) -> Self {
Self::from_event_wheel_timed(from)
}
}