1use std::sync::atomic::{AtomicUsize, Ordering};
6
7use bitflags::bitflags;
8use keyboard_types::{Code, 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, MallocSizeOf, 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, MallocSizeOf, Serialize)]
53pub enum InputEvent {
54 EditingAction(ClipboardAction),
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, MallocSizeOf, 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, Copy, Debug, Deserialize, PartialEq, Serialize)]
83pub enum EditingDirection {
84 Forward,
85 Backward,
86}
87
88#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
90pub enum EditingMotion {
91 Character,
92 Grapheme,
93 Word,
94 Line,
95 LineStartOrEnd,
96 Page,
97 DocumentStartOrEnd,
98}
99
100#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
102pub enum ModifySelection {
103 Yes,
104 No,
105}
106
107#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
109pub enum ClipboardAction {
110 Copy,
111 Cut,
112 Paste,
113}
114
115#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
119pub enum EditingAction {
120 Backspace(EditingMotion),
121 Clipboard(ClipboardAction),
122 Delete,
123 DeleteSurroundingBefore(usize),
124 DeleteSurroundingAfter(usize),
125 EndComposition,
126 InsertCompositionText(String),
127 InsertNewline,
128 InsertParagraph,
129 InsertText(String),
130 MoveCursor(EditingDirection, EditingMotion, ModifySelection),
131 SelectAll,
132 SetCompositionCursor(Option<(usize, usize)>),
133 StartComposition,
134}
135
136impl InputEvent {
137 pub fn point(&self) -> Option<WebViewPoint> {
138 match self {
139 InputEvent::EditingAction(..) => None,
140 #[cfg(feature = "gamepad")]
141 InputEvent::Gamepad(..) => None,
142 InputEvent::Ime(..) => None,
143 InputEvent::Keyboard(..) => None,
144 InputEvent::MouseButton(event) => Some(event.point),
145 InputEvent::MouseMove(event) => Some(event.point),
146 InputEvent::MouseLeftViewport(_) => None,
147 InputEvent::Touch(event) => Some(event.point),
148 InputEvent::Wheel(event) => Some(event.point),
149 }
150 }
151}
152
153#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
154pub struct KeyboardEvent {
155 pub event: ::keyboard_types::KeyboardEvent,
156}
157
158impl KeyboardEvent {
159 pub fn new(keyboard_event: ::keyboard_types::KeyboardEvent) -> Self {
160 Self {
161 event: keyboard_event,
162 }
163 }
164
165 pub fn new_without_event(
166 state: KeyState,
167 key: Key,
168 code: Code,
169 location: Location,
170 modifiers: Modifiers,
171 repeat: bool,
172 is_composing: bool,
173 ) -> Self {
174 Self::new(::keyboard_types::KeyboardEvent {
175 state,
176 key,
177 code,
178 location,
179 modifiers,
180 repeat,
181 is_composing,
182 })
183 }
184
185 pub fn from_state_and_key(state: KeyState, key: Key) -> Self {
186 Self::new(::keyboard_types::KeyboardEvent {
187 state,
188 key,
189 ..::keyboard_types::KeyboardEvent::default()
190 })
191 }
192}
193
194#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
195pub struct MouseButtonEvent {
196 pub action: MouseButtonAction,
197 pub button: MouseButton,
198 pub point: WebViewPoint,
199 #[doc(hidden)]
200 pub is_compatibility_event_for_touch: bool,
203}
204
205impl MouseButtonEvent {
206 pub fn new(action: MouseButtonAction, button: MouseButton, point: WebViewPoint) -> Self {
207 Self {
208 action,
209 button,
210 point,
211 is_compatibility_event_for_touch: false,
212 }
213 }
214
215 #[doc(hidden)]
216 pub fn new_compatibility_for_touch(
217 action: MouseButtonAction,
218 button: MouseButton,
219 point: WebViewPoint,
220 ) -> Self {
221 Self {
222 action,
223 button,
224 point,
225 is_compatibility_event_for_touch: true,
226 }
227 }
228}
229
230#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
234pub enum MouseButton {
235 #[doc(hidden)]
236 None,
237 Primary,
238 Auxiliary,
239 Secondary,
240 Back,
241 Forward,
242 Other(u16),
243}
244
245impl<T: Into<i128>> From<T> for MouseButton {
246 fn from(value: T) -> Self {
247 let value = value.into();
248 match value {
249 -1 => MouseButton::None,
250 0 => MouseButton::Primary,
251 1 => MouseButton::Auxiliary,
252 2 => MouseButton::Secondary,
253 3 => MouseButton::Back,
254 4 => MouseButton::Forward,
255 _ => MouseButton::Other(value as u16),
256 }
257 }
258}
259
260impl From<MouseButton> for i16 {
261 fn from(value: MouseButton) -> Self {
262 match value {
263 MouseButton::None => -1,
264 MouseButton::Primary => 0,
265 MouseButton::Auxiliary => 1,
266 MouseButton::Secondary => 2,
267 MouseButton::Back => 3,
268 MouseButton::Forward => 4,
269 MouseButton::Other(value) => value as i16,
270 }
271 }
272}
273
274#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
276pub enum MouseButtonAction {
277 Down,
279 Up,
281}
282
283#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
284pub struct MouseMoveEvent {
285 pub point: WebViewPoint,
286 #[doc(hidden)]
287 pub is_compatibility_event_for_touch: bool,
290}
291
292impl MouseMoveEvent {
293 pub fn new(point: WebViewPoint) -> Self {
294 Self {
295 point,
296 is_compatibility_event_for_touch: false,
297 }
298 }
299
300 #[doc(hidden)]
301 pub fn new_compatibility_for_touch(point: WebViewPoint) -> Self {
302 Self {
303 point,
304 is_compatibility_event_for_touch: true,
305 }
306 }
307}
308
309#[derive(Clone, Copy, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
310pub struct MouseLeftViewportEvent {
311 pub focus_moving_to_another_iframe: bool,
312}
313
314#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
316pub enum TouchEventType {
317 Down,
319 Move,
321 Up,
323 Cancel,
325}
326
327#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
331pub struct TouchId(pub i32);
332
333#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
337pub enum TouchPointerType {
338 Pen,
339 Touch,
340}
341
342#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
343pub struct TouchEvent {
344 pub event_type: TouchEventType,
345 pub touch_id: TouchId,
346 pub point: WebViewPoint,
347 pub pointer_type: TouchPointerType,
348 cancelable: bool,
350 pub can_synthesize_mouse_events: bool,
352}
353
354impl TouchEvent {
355 pub fn new(
356 event_type: TouchEventType,
357 touch_id: TouchId,
358 point: WebViewPoint,
359 pointer_type: TouchPointerType,
360 ) -> Self {
361 TouchEvent {
362 event_type,
363 touch_id,
364 point,
365 pointer_type,
366 cancelable: true,
367 can_synthesize_mouse_events: false,
368 }
369 }
370
371 #[doc(hidden)]
372 pub fn disable_cancelable(&mut self) {
373 self.cancelable = false;
374 }
375
376 #[doc(hidden)]
377 pub fn is_cancelable(&self) -> bool {
378 self.cancelable
379 }
380}
381
382#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
384pub enum WheelMode {
385 DeltaPixel = 0x00,
387 DeltaLine = 0x01,
389 DeltaPage = 0x02,
391}
392
393#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
395pub struct WheelDelta {
396 pub x: f64,
399 pub y: f64,
402 pub z: f64,
404 pub mode: WheelMode,
406}
407
408#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
409pub struct WheelEvent {
410 pub delta: WheelDelta,
411 pub point: WebViewPoint,
412}
413
414impl WheelEvent {
415 pub fn new(mut delta: WheelDelta, point: WebViewPoint, modifiers: Modifiers) -> Self {
419 if modifiers.contains(Modifiers::SHIFT) && delta.x == 0. && delta.z == 0. {
423 std::mem::swap(&mut delta.x, &mut delta.y);
424 }
425 WheelEvent { delta, point }
426 }
427}
428
429#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
431pub enum ImeEvent {
432 Update(String, Option<(usize, usize)>),
436 Commit(String),
441 DeleteSurrounding { before: usize, after: usize },
444 Dismissed,
446}
447
448impl ImeEvent {
449 pub fn is_terminating(&self) -> bool {
451 match self {
452 ImeEvent::Update(string, _) if string.is_empty() => true,
453 ImeEvent::Commit(..) => true,
454 _ => false,
455 }
456 }
457}
458
459#[cfg(feature = "gamepad")]
460#[derive(
461 Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize,
462)]
463pub struct GamepadIndex(pub usize);
465
466#[cfg(feature = "gamepad")]
467#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
468pub struct GamepadInputBounds {
470 pub axis_bounds: (f64, f64),
472 pub button_bounds: (f64, f64),
474}
475
476#[cfg(feature = "gamepad")]
477#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
478pub struct GamepadSupportedHapticEffects {
480 pub supports_dual_rumble: bool,
482 pub supports_trigger_rumble: bool,
484}
485
486#[cfg(feature = "gamepad")]
487#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
488pub enum GamepadEvent {
490 Connected(
494 GamepadIndex,
495 String,
496 GamepadInputBounds,
497 GamepadSupportedHapticEffects,
498 ),
499 Disconnected(GamepadIndex),
503 Updated(GamepadIndex, GamepadUpdateType),
507}
508
509#[cfg(feature = "gamepad")]
510#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
511pub enum GamepadUpdateType {
513 Axis(usize, f64),
517 Button(usize, f64),
521}