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};
11use webrender_api::ExternalScrollId;
12
13use crate::WebViewPoint;
14
15#[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)]
28 pub struct InputEventResult: u8 {
29 const DefaultPrevented = 1 << 0;
31 const Consumed = 1 << 1;
37 }
38}
39
40#[derive(Clone, Debug, Deserialize, Serialize)]
42pub enum InputEvent {
43 EditingAction(EditingActionEvent),
44 Gamepad(GamepadEvent),
45 Ime(ImeEvent),
46 Keyboard(KeyboardEvent),
47 MouseButton(MouseButtonEvent),
48 MouseLeftViewport(MouseLeftViewportEvent),
49 MouseMove(MouseMoveEvent),
50 Scroll(ScrollEvent),
51 Touch(TouchEvent),
52 Wheel(WheelEvent),
53}
54
55#[derive(Clone, Debug, Deserialize, Serialize)]
56pub struct InputEventAndId {
57 pub event: InputEvent,
58 pub id: InputEventId,
59}
60
61impl From<InputEvent> for InputEventAndId {
62 fn from(event: InputEvent) -> Self {
63 Self {
64 event,
65 id: InputEventId::new(),
66 }
67 }
68}
69
70#[derive(Clone, Debug, Deserialize, Serialize)]
72pub enum EditingActionEvent {
73 Copy,
74 Cut,
75 Paste,
76}
77
78impl InputEvent {
79 pub fn point(&self) -> Option<WebViewPoint> {
80 match self {
81 InputEvent::EditingAction(..) => None,
82 InputEvent::Gamepad(..) => None,
83 InputEvent::Ime(..) => None,
84 InputEvent::Keyboard(..) => None,
85 InputEvent::MouseButton(event) => Some(event.point),
86 InputEvent::MouseMove(event) => Some(event.point),
87 InputEvent::MouseLeftViewport(_) => None,
88 InputEvent::Touch(event) => Some(event.point),
89 InputEvent::Wheel(event) => Some(event.point),
90 InputEvent::Scroll(..) => None,
91 }
92 }
93}
94
95#[derive(Clone, Debug, Default, Deserialize, Serialize)]
96pub struct KeyboardEvent {
97 pub event: ::keyboard_types::KeyboardEvent,
98}
99
100impl KeyboardEvent {
101 pub fn new(keyboard_event: ::keyboard_types::KeyboardEvent) -> Self {
102 Self {
103 event: keyboard_event,
104 }
105 }
106
107 pub fn new_without_event(
108 state: KeyState,
109 key: Key,
110 code: Code,
111 location: Location,
112 modifiers: Modifiers,
113 repeat: bool,
114 is_composing: bool,
115 ) -> Self {
116 Self::new(::keyboard_types::KeyboardEvent {
117 state,
118 key,
119 code,
120 location,
121 modifiers,
122 repeat,
123 is_composing,
124 })
125 }
126
127 pub fn from_state_and_key(state: KeyState, key: Key) -> Self {
128 Self::new(::keyboard_types::KeyboardEvent {
129 state,
130 key,
131 ..::keyboard_types::KeyboardEvent::default()
132 })
133 }
134}
135
136#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
137pub struct MouseButtonEvent {
138 pub action: MouseButtonAction,
139 pub button: MouseButton,
140 pub point: WebViewPoint,
141}
142
143impl MouseButtonEvent {
144 pub fn new(action: MouseButtonAction, button: MouseButton, point: WebViewPoint) -> Self {
145 Self {
146 action,
147 button,
148 point,
149 }
150 }
151}
152
153#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
154pub enum MouseButton {
155 Left,
156 Middle,
157 Right,
158 Back,
159 Forward,
160 Other(u16),
161}
162
163impl<T: Into<u64>> From<T> for MouseButton {
164 fn from(value: T) -> Self {
165 let value = value.into();
166 match value {
167 0 => MouseButton::Left,
168 1 => MouseButton::Middle,
169 2 => MouseButton::Right,
170 3 => MouseButton::Back,
171 4 => MouseButton::Forward,
172 _ => MouseButton::Other(value as u16),
173 }
174 }
175}
176
177impl From<MouseButton> for i16 {
178 fn from(value: MouseButton) -> Self {
179 match value {
180 MouseButton::Left => 0,
181 MouseButton::Middle => 1,
182 MouseButton::Right => 2,
183 MouseButton::Back => 3,
184 MouseButton::Forward => 4,
185 MouseButton::Other(value) => value as i16,
186 }
187 }
188}
189
190#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
192pub enum MouseButtonAction {
193 Down,
195 Up,
197}
198
199#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
200pub struct MouseMoveEvent {
201 pub point: WebViewPoint,
202 pub is_compatibility_event_for_touch: bool,
203}
204
205impl MouseMoveEvent {
206 pub fn new(point: WebViewPoint) -> Self {
207 Self {
208 point,
209 is_compatibility_event_for_touch: false,
210 }
211 }
212
213 pub fn new_compatibility_for_touch(point: WebViewPoint) -> Self {
214 Self {
215 point,
216 is_compatibility_event_for_touch: true,
217 }
218 }
219}
220
221#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
222pub struct MouseLeftViewportEvent {
223 pub focus_moving_to_another_iframe: bool,
224}
225
226#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
228pub enum TouchEventType {
229 Down,
231 Move,
233 Up,
235 Cancel,
237}
238
239#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
243pub struct TouchId(pub i32);
244
245#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
246pub struct TouchEvent {
247 pub event_type: TouchEventType,
248 pub id: TouchId,
249 pub point: WebViewPoint,
250 cancelable: bool,
252}
253
254impl TouchEvent {
255 pub fn new(event_type: TouchEventType, id: TouchId, point: WebViewPoint) -> Self {
256 TouchEvent {
257 event_type,
258 id,
259 point,
260 cancelable: true,
261 }
262 }
263
264 #[doc(hidden)]
265 pub fn disable_cancelable(&mut self) {
266 self.cancelable = false;
267 }
268
269 #[doc(hidden)]
270 pub fn is_cancelable(&self) -> bool {
271 self.cancelable
272 }
273}
274
275#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
277pub enum WheelMode {
278 DeltaPixel = 0x00,
280 DeltaLine = 0x01,
282 DeltaPage = 0x02,
284}
285
286#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
288pub struct WheelDelta {
289 pub x: f64,
291 pub y: f64,
293 pub z: f64,
295 pub mode: WheelMode,
297}
298
299#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
300pub struct WheelEvent {
301 pub delta: WheelDelta,
302 pub point: WebViewPoint,
303}
304
305impl WheelEvent {
306 pub fn new(delta: WheelDelta, point: WebViewPoint) -> Self {
307 WheelEvent { delta, point }
308 }
309}
310
311#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
312pub struct ScrollEvent {
313 pub external_id: ExternalScrollId,
314}
315
316#[derive(Clone, Debug, Deserialize, Serialize)]
317pub enum ImeEvent {
318 Composition(CompositionEvent),
319 Dismissed,
320}
321
322#[derive(
323 Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize,
324)]
325pub struct GamepadIndex(pub usize);
327
328#[derive(Clone, Debug, Deserialize, Serialize)]
329pub struct GamepadInputBounds {
331 pub axis_bounds: (f64, f64),
333 pub button_bounds: (f64, f64),
335}
336
337#[derive(Clone, Debug, Deserialize, Serialize)]
338pub struct GamepadSupportedHapticEffects {
340 pub supports_dual_rumble: bool,
342 pub supports_trigger_rumble: bool,
344}
345
346#[derive(Clone, Debug, Deserialize, Serialize)]
347pub enum GamepadEvent {
349 Connected(
352 GamepadIndex,
353 String,
354 GamepadInputBounds,
355 GamepadSupportedHapticEffects,
356 ),
357 Disconnected(GamepadIndex),
360 Updated(GamepadIndex, GamepadUpdateType),
363}
364
365#[derive(Clone, Debug, Deserialize, Serialize)]
366pub enum GamepadUpdateType {
368 Axis(usize, f64),
371 Button(usize, f64),
374}