1use std::sync::atomic::{AtomicUsize, Ordering};
6
7use bitflags::bitflags;
8use keyboard_types::{Code, CompositionEvent, Key, KeyState, Location, Modifiers};
9use malloc_size_of_derive::MallocSizeOf;
10use serde::{Deserialize, Serialize};
11
12use crate::WebViewPoint;
13
14#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
16pub struct InputEventId(usize);
17
18static INPUT_EVENT_ID: AtomicUsize = AtomicUsize::new(0);
19
20impl InputEventId {
21 fn new() -> Self {
22 Self(INPUT_EVENT_ID.fetch_add(1, Ordering::Relaxed))
23 }
24}
25
26bitflags! {
27 #[derive(Clone, Copy, Default, Deserialize, PartialEq, Serialize)]
29 pub struct InputEventResult: u8 {
30 const DefaultPrevented = 1 << 0;
32 const Consumed = 1 << 1;
38 const DispatchFailed = 1 << 2;
42 }
43}
44
45#[derive(Deserialize, Serialize)]
46pub struct InputEventOutcome {
47 pub id: InputEventId,
48 pub result: InputEventResult,
49}
50
51#[derive(Clone, Debug, Deserialize, Serialize)]
53pub enum InputEvent {
54 EditingAction(EditingActionEvent),
55 #[cfg(feature = "gamepad")]
56 Gamepad(GamepadEvent),
57 Ime(ImeEvent),
58 Keyboard(KeyboardEvent),
59 MouseButton(MouseButtonEvent),
60 MouseLeftViewport(MouseLeftViewportEvent),
61 MouseMove(MouseMoveEvent),
62 Touch(TouchEvent),
63 Wheel(WheelEvent),
64}
65
66#[derive(Clone, Debug, Deserialize, Serialize)]
67pub struct InputEventAndId {
68 pub event: InputEvent,
69 pub id: InputEventId,
70}
71
72impl From<InputEvent> for InputEventAndId {
73 fn from(event: InputEvent) -> Self {
74 Self {
75 event,
76 id: InputEventId::new(),
77 }
78 }
79}
80
81#[derive(Clone, Debug, Deserialize, Serialize)]
83pub enum EditingActionEvent {
84 Copy,
85 Cut,
86 Paste,
87}
88
89impl InputEvent {
90 pub fn point(&self) -> Option<WebViewPoint> {
91 match self {
92 InputEvent::EditingAction(..) => None,
93 #[cfg(feature = "gamepad")]
94 InputEvent::Gamepad(..) => None,
95 InputEvent::Ime(..) => None,
96 InputEvent::Keyboard(..) => None,
97 InputEvent::MouseButton(event) => Some(event.point),
98 InputEvent::MouseMove(event) => Some(event.point),
99 InputEvent::MouseLeftViewport(_) => None,
100 InputEvent::Touch(event) => Some(event.point),
101 InputEvent::Wheel(event) => Some(event.point),
102 }
103 }
104}
105
106#[derive(Clone, Debug, Default, Deserialize, Serialize)]
107pub struct KeyboardEvent {
108 pub event: ::keyboard_types::KeyboardEvent,
109}
110
111impl KeyboardEvent {
112 pub fn new(keyboard_event: ::keyboard_types::KeyboardEvent) -> Self {
113 Self {
114 event: keyboard_event,
115 }
116 }
117
118 pub fn new_without_event(
119 state: KeyState,
120 key: Key,
121 code: Code,
122 location: Location,
123 modifiers: Modifiers,
124 repeat: bool,
125 is_composing: bool,
126 ) -> Self {
127 Self::new(::keyboard_types::KeyboardEvent {
128 state,
129 key,
130 code,
131 location,
132 modifiers,
133 repeat,
134 is_composing,
135 })
136 }
137
138 pub fn from_state_and_key(state: KeyState, key: Key) -> Self {
139 Self::new(::keyboard_types::KeyboardEvent {
140 state,
141 key,
142 ..::keyboard_types::KeyboardEvent::default()
143 })
144 }
145}
146
147#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
148pub struct MouseButtonEvent {
149 pub action: MouseButtonAction,
150 pub button: MouseButton,
151 pub point: WebViewPoint,
152}
153
154impl MouseButtonEvent {
155 pub fn new(action: MouseButtonAction, button: MouseButton, point: WebViewPoint) -> Self {
156 Self {
157 action,
158 button,
159 point,
160 }
161 }
162}
163
164#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
166pub enum MouseButton {
167 Left,
168 Middle,
169 Right,
170 Back,
171 Forward,
172 Other(u16),
173}
174
175impl<T: Into<u64>> From<T> for MouseButton {
176 fn from(value: T) -> Self {
177 let value = value.into();
178 match value {
179 0 => MouseButton::Left,
180 1 => MouseButton::Middle,
181 2 => MouseButton::Right,
182 3 => MouseButton::Back,
183 4 => MouseButton::Forward,
184 _ => MouseButton::Other(value as u16),
185 }
186 }
187}
188
189impl From<MouseButton> for i16 {
190 fn from(value: MouseButton) -> Self {
191 match value {
192 MouseButton::Left => 0,
193 MouseButton::Middle => 1,
194 MouseButton::Right => 2,
195 MouseButton::Back => 3,
196 MouseButton::Forward => 4,
197 MouseButton::Other(value) => value as i16,
198 }
199 }
200}
201
202#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
204pub enum MouseButtonAction {
205 Down,
207 Up,
209}
210
211#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
212pub struct MouseMoveEvent {
213 pub point: WebViewPoint,
214 #[doc(hidden)]
215 pub is_compatibility_event_for_touch: bool,
218}
219
220impl MouseMoveEvent {
221 pub fn new(point: WebViewPoint) -> Self {
222 Self {
223 point,
224 is_compatibility_event_for_touch: false,
225 }
226 }
227
228 #[doc(hidden)]
229 pub fn new_compatibility_for_touch(point: WebViewPoint) -> Self {
230 Self {
231 point,
232 is_compatibility_event_for_touch: true,
233 }
234 }
235}
236
237#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
238pub struct MouseLeftViewportEvent {
239 pub focus_moving_to_another_iframe: bool,
240}
241
242#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
244pub enum TouchEventType {
245 Down,
247 Move,
249 Up,
251 Cancel,
253}
254
255#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
259pub struct TouchId(pub i32);
260
261#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
265pub enum TouchPointerType {
266 Pen,
267 Touch,
268}
269
270#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
271pub struct TouchEvent {
272 pub event_type: TouchEventType,
273 pub touch_id: TouchId,
274 pub point: WebViewPoint,
275 pub pointer_type: TouchPointerType,
276 cancelable: bool,
278}
279
280impl TouchEvent {
281 pub fn new(
282 event_type: TouchEventType,
283 touch_id: TouchId,
284 point: WebViewPoint,
285 pointer_type: TouchPointerType,
286 ) -> Self {
287 TouchEvent {
288 event_type,
289 touch_id,
290 point,
291 pointer_type,
292 cancelable: true,
293 }
294 }
295
296 #[doc(hidden)]
297 pub fn disable_cancelable(&mut self) {
298 self.cancelable = false;
299 }
300
301 #[doc(hidden)]
302 pub fn is_cancelable(&self) -> bool {
303 self.cancelable
304 }
305}
306
307#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
309pub enum WheelMode {
310 DeltaPixel = 0x00,
312 DeltaLine = 0x01,
314 DeltaPage = 0x02,
316}
317
318#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
320pub struct WheelDelta {
321 pub x: f64,
324 pub y: f64,
327 pub z: f64,
329 pub mode: WheelMode,
331}
332
333#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
334pub struct WheelEvent {
335 pub delta: WheelDelta,
336 pub point: WebViewPoint,
337}
338
339impl WheelEvent {
340 pub fn new(delta: WheelDelta, point: WebViewPoint) -> Self {
341 WheelEvent { delta, point }
342 }
343}
344
345#[derive(Clone, Debug, Deserialize, Serialize)]
347pub enum ImeEvent {
348 Composition(CompositionEvent),
349 Dismissed,
350}
351
352#[cfg(feature = "gamepad")]
353#[derive(
354 Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize,
355)]
356pub struct GamepadIndex(pub usize);
358
359#[cfg(feature = "gamepad")]
360#[derive(Clone, Debug, Deserialize, Serialize)]
361pub struct GamepadInputBounds {
363 pub axis_bounds: (f64, f64),
365 pub button_bounds: (f64, f64),
367}
368
369#[cfg(feature = "gamepad")]
370#[derive(Clone, Debug, Deserialize, Serialize)]
371pub struct GamepadSupportedHapticEffects {
373 pub supports_dual_rumble: bool,
375 pub supports_trigger_rumble: bool,
377}
378
379#[cfg(feature = "gamepad")]
380#[derive(Clone, Debug, Deserialize, Serialize)]
381pub enum GamepadEvent {
383 Connected(
387 GamepadIndex,
388 String,
389 GamepadInputBounds,
390 GamepadSupportedHapticEffects,
391 ),
392 Disconnected(GamepadIndex),
396 Updated(GamepadIndex, GamepadUpdateType),
400}
401
402#[cfg(feature = "gamepad")]
403#[derive(Clone, Debug, Deserialize, Serialize)]
404pub enum GamepadUpdateType {
406 Axis(usize, f64),
410 Button(usize, f64),
414}