1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::str::FromStr;

use web_sys::{Event, KeyboardEvent, MouseEvent, WheelEvent};

use super::{Platform, WinRef};

pub struct WinMouse<'a>(pub &'a MouseEvent);
impl<'a> types::IntoEvt<Platform, types::EvtMouse> for WinMouse<'a> {
	fn to_evt(&self, _win: WinRef<'_>) -> Option<types::EvtMouse> {
		let button = match self.0.button() {
			0 => types::Button::Left,
			1 => types::Button::Middle,
			2 => types::Button::Right,
			3 => types::Button::Mouse(0),
			4 => types::Button::Mouse(1),
			_ => return None,
		};
		let phase = match self.0.type_().as_str() {
			"mousedown" => types::Phase::Start,
			"mousemove" => types::Phase::Update,
			"mouseup" => types::Phase::End,
			_ => return None,
		};
		let point = (self.0.x() as f32, self.0.y() as f32);
		let metas = {
			let mut metas = types::Metas::EMPTY;
			if self.0.shift_key() {
				metas.insert(types::Metas::SHIFT);
			}
			if self.0.ctrl_key() {
				metas.insert(types::Metas::CONTROL);
			}
			if self.0.alt_key() {
				metas.insert(types::Metas::ALT);
			}
			if self.0.meta_key() {
				metas.insert(types::Metas::META);
			}
			metas
		};
		Some(types::EvtMouse::new(button, phase, point, metas))
	}
}
pub struct WinWheel<'a>(pub &'a WheelEvent);
impl<'a> types::IntoEvt<Platform, types::EvtWheel> for WinWheel<'a> {
	fn to_evt(&self, _win: WinRef<'_>) -> Option<types::EvtWheel> {
		let x = self.0.delta_x() as f32;
		let y = self.0.delta_y() as f32;
		let z = self.0.delta_z() as f32;
		match self.0.delta_mode() {
			WheelEvent::DOM_DELTA_PIXEL => Some(types::EvtWheel::Pixel(x, y, z)),
			WheelEvent::DOM_DELTA_LINE => Some(types::EvtWheel::Line(x, y, z)),
			WheelEvent::DOM_DELTA_PAGE => Some(types::EvtWheel::Page(x, y, z)),
			_ => None,
		}
	}
}
pub struct WinKey<'a>(pub &'a KeyboardEvent);
impl<'a> types::IntoEvt<Platform, types::EvtKey> for WinKey<'a> {
	fn to_evt(&self, _win: WinRef<'_>) -> Option<types::EvtKey> {
		let key = types::Key::from_str(&self.0.key()).unwrap_or(types::Key::Unidentified(0));
		let code = types::Code::from_str(&self.0.code()).unwrap_or(types::Code::Unidentified(0));
		let state = match self.0.type_().as_str() {
			"keydown" => types::State::Down,
			"keyup" => types::State::Up,
			_ => return None,
		};
		let location = match self.0.location() {
			KeyboardEvent::DOM_KEY_LOCATION_STANDARD => types::Location::Standard,
			KeyboardEvent::DOM_KEY_LOCATION_NUMPAD => types::Location::Numpad,
			KeyboardEvent::DOM_KEY_LOCATION_LEFT => types::Location::Left,
			KeyboardEvent::DOM_KEY_LOCATION_RIGHT => types::Location::Right,
			_ => return None,
		};
		let metas = {
			let mut metas = types::Metas::EMPTY;
			if self.0.shift_key() {
				metas.insert(types::Metas::SHIFT);
			}
			if self.0.ctrl_key() {
				metas.insert(types::Metas::CONTROL);
			}
			if self.0.alt_key() {
				metas.insert(types::Metas::ALT);
			}
			if self.0.meta_key() {
				metas.insert(types::Metas::META);
			}
			metas
		};
		let repeat = self.0.repeat();
		let composing = self.0.is_composing();
		Some(types::EvtKey::new(key, code, state, location, metas, repeat, composing))
	}
}
pub struct WinTouch<'a>(pub &'a Event);
impl<'a> types::IntoEvt<Platform, types::EvtTouch> for WinTouch<'a> {
	fn to_evt(&self, _win: WinRef<'_>) -> Option<types::EvtTouch> {
		None
	}
}