viewpoint_cdp/protocol/input/
mod.rs

1//! Input domain types.
2//!
3//! The Input domain provides methods for simulating user input events.
4
5use serde::Serialize;
6
7/// Mouse button type.
8#[derive(Debug, Clone, Copy, Serialize)]
9#[serde(rename_all = "lowercase")]
10pub enum MouseButton {
11    None,
12    Left,
13    Middle,
14    Right,
15    Back,
16    Forward,
17}
18
19/// Type of mouse event.
20#[derive(Debug, Clone, Copy, Serialize)]
21#[serde(rename_all = "camelCase")]
22pub enum MouseEventType {
23    MousePressed,
24    MouseReleased,
25    MouseMoved,
26    MouseWheel,
27}
28
29/// Type of key event.
30#[derive(Debug, Clone, Copy, Serialize)]
31#[serde(rename_all = "camelCase")]
32pub enum KeyEventType {
33    KeyDown,
34    KeyUp,
35    RawKeyDown,
36    Char,
37}
38
39/// Parameters for Input.dispatchMouseEvent.
40#[derive(Debug, Clone, Serialize)]
41#[serde(rename_all = "camelCase")]
42pub struct DispatchMouseEventParams {
43    /// Type of the mouse event.
44    #[serde(rename = "type")]
45    pub event_type: MouseEventType,
46    /// X coordinate of the event relative to the main frame's viewport.
47    pub x: f64,
48    /// Y coordinate of the event relative to the main frame's viewport.
49    pub y: f64,
50    /// Mouse button.
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub button: Option<MouseButton>,
53    /// Number of times the mouse button was clicked.
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub click_count: Option<i32>,
56    /// Bit field representing pressed modifier keys.
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub modifiers: Option<i32>,
59    /// Time at which the event occurred.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub timestamp: Option<f64>,
62}
63
64impl DispatchMouseEventParams {
65    /// Create a mouse move event.
66    pub fn mouse_move(x: f64, y: f64) -> Self {
67        Self {
68            event_type: MouseEventType::MouseMoved,
69            x,
70            y,
71            button: None,
72            click_count: None,
73            modifiers: None,
74            timestamp: None,
75        }
76    }
77
78    /// Create a mouse down event.
79    pub fn mouse_down(x: f64, y: f64, button: MouseButton) -> Self {
80        Self {
81            event_type: MouseEventType::MousePressed,
82            x,
83            y,
84            button: Some(button),
85            click_count: Some(1),
86            modifiers: None,
87            timestamp: None,
88        }
89    }
90
91    /// Create a mouse up event.
92    pub fn mouse_up(x: f64, y: f64, button: MouseButton) -> Self {
93        Self {
94            event_type: MouseEventType::MouseReleased,
95            x,
96            y,
97            button: Some(button),
98            click_count: Some(1),
99            modifiers: None,
100            timestamp: None,
101        }
102    }
103
104    /// Create a mouse wheel event.
105    pub fn mouse_wheel(x: f64, y: f64, delta_x: f64, delta_y: f64) -> DispatchMouseWheelParams {
106        DispatchMouseWheelParams {
107            event_type: MouseEventType::MouseWheel,
108            x,
109            y,
110            delta_x,
111            delta_y,
112            modifiers: None,
113            pointer_type: None,
114        }
115    }
116}
117
118/// Parameters for Input.dispatchMouseEvent with wheel.
119#[derive(Debug, Clone, Serialize)]
120#[serde(rename_all = "camelCase")]
121pub struct DispatchMouseWheelParams {
122    /// Type of the mouse event.
123    #[serde(rename = "type")]
124    pub event_type: MouseEventType,
125    /// X coordinate of the event relative to the main frame's viewport.
126    pub x: f64,
127    /// Y coordinate of the event relative to the main frame's viewport.
128    pub y: f64,
129    /// X delta in CSS pixels for mouse wheel event.
130    pub delta_x: f64,
131    /// Y delta in CSS pixels for mouse wheel event.
132    pub delta_y: f64,
133    /// Bit field representing pressed modifier keys.
134    #[serde(skip_serializing_if = "Option::is_none")]
135    pub modifiers: Option<i32>,
136    /// Pointer type (default "mouse").
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub pointer_type: Option<String>,
139}
140
141/// Parameters for Input.dispatchKeyEvent.
142#[derive(Debug, Clone, Serialize)]
143#[serde(rename_all = "camelCase")]
144pub struct DispatchKeyEventParams {
145    /// Type of the key event.
146    #[serde(rename = "type")]
147    pub event_type: KeyEventType,
148    /// Bit field representing pressed modifier keys.
149    #[serde(skip_serializing_if = "Option::is_none")]
150    pub modifiers: Option<i32>,
151    /// Time at which the event occurred.
152    #[serde(skip_serializing_if = "Option::is_none")]
153    pub timestamp: Option<f64>,
154    /// Text as generated by processing a virtual key code.
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub text: Option<String>,
157    /// Text that would have been generated without any modifiers.
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub unmodified_text: Option<String>,
160    /// Unique key identifier.
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub key_identifier: Option<String>,
163    /// Unique DOM defined string value for each key.
164    #[serde(skip_serializing_if = "Option::is_none")]
165    pub code: Option<String>,
166    /// Unique DOM defined string value describing the meaning of the key.
167    #[serde(skip_serializing_if = "Option::is_none")]
168    pub key: Option<String>,
169    /// Windows virtual key code.
170    #[serde(skip_serializing_if = "Option::is_none")]
171    pub windows_virtual_key_code: Option<i32>,
172    /// Native virtual key code.
173    #[serde(skip_serializing_if = "Option::is_none")]
174    pub native_virtual_key_code: Option<i32>,
175    /// Whether the event was generated from auto repeat.
176    #[serde(skip_serializing_if = "Option::is_none")]
177    pub auto_repeat: Option<bool>,
178    /// Whether the event was generated from the keypad.
179    #[serde(skip_serializing_if = "Option::is_none")]
180    pub is_keypad: Option<bool>,
181    /// Whether the event was a system key event.
182    #[serde(skip_serializing_if = "Option::is_none")]
183    pub is_system_key: Option<bool>,
184    /// Editing commands to send with the event.
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub commands: Option<Vec<String>>,
187}
188
189impl DispatchKeyEventParams {
190    /// Create a key down event.
191    pub fn key_down(key: &str) -> Self {
192        Self {
193            event_type: KeyEventType::KeyDown,
194            modifiers: None,
195            timestamp: None,
196            text: None,
197            unmodified_text: None,
198            key_identifier: None,
199            code: Some(key.to_string()),
200            key: Some(key.to_string()),
201            windows_virtual_key_code: None,
202            native_virtual_key_code: None,
203            auto_repeat: None,
204            is_keypad: None,
205            is_system_key: None,
206            commands: None,
207        }
208    }
209
210    /// Create a key up event.
211    pub fn key_up(key: &str) -> Self {
212        Self {
213            event_type: KeyEventType::KeyUp,
214            modifiers: None,
215            timestamp: None,
216            text: None,
217            unmodified_text: None,
218            key_identifier: None,
219            code: Some(key.to_string()),
220            key: Some(key.to_string()),
221            windows_virtual_key_code: None,
222            native_virtual_key_code: None,
223            auto_repeat: None,
224            is_keypad: None,
225            is_system_key: None,
226            commands: None,
227        }
228    }
229
230    /// Create a char event for text input.
231    pub fn char(text: &str) -> Self {
232        Self {
233            event_type: KeyEventType::Char,
234            modifiers: None,
235            timestamp: None,
236            text: Some(text.to_string()),
237            unmodified_text: Some(text.to_string()),
238            key_identifier: None,
239            code: None,
240            key: None,
241            windows_virtual_key_code: None,
242            native_virtual_key_code: None,
243            auto_repeat: None,
244            is_keypad: None,
245            is_system_key: None,
246            commands: None,
247        }
248    }
249}
250
251/// Parameters for Input.insertText.
252#[derive(Debug, Clone, Serialize)]
253#[serde(rename_all = "camelCase")]
254pub struct InsertTextParams {
255    /// The text to insert.
256    pub text: String,
257}
258
259/// Modifier keys bit flags.
260pub mod modifiers {
261    pub const ALT: i32 = 1;
262    pub const CTRL: i32 = 2;
263    pub const META: i32 = 4;
264    pub const SHIFT: i32 = 8;
265}
266
267/// Type of touch event.
268#[derive(Debug, Clone, Copy, Serialize)]
269#[serde(rename_all = "camelCase")]
270pub enum TouchEventType {
271    TouchStart,
272    TouchEnd,
273    TouchMove,
274    TouchCancel,
275}
276
277/// A single touch point.
278#[derive(Debug, Clone, Serialize)]
279#[serde(rename_all = "camelCase")]
280pub struct TouchPoint {
281    /// X coordinate of the touch point.
282    pub x: f64,
283    /// Y coordinate of the touch point.
284    pub y: f64,
285    /// Touch point radius in X direction (default 1.0).
286    #[serde(skip_serializing_if = "Option::is_none")]
287    pub radius_x: Option<f64>,
288    /// Touch point radius in Y direction (default 1.0).
289    #[serde(skip_serializing_if = "Option::is_none")]
290    pub radius_y: Option<f64>,
291    /// Rotation angle (default 0.0).
292    #[serde(skip_serializing_if = "Option::is_none")]
293    pub rotation_angle: Option<f64>,
294    /// Force (default 1.0).
295    #[serde(skip_serializing_if = "Option::is_none")]
296    pub force: Option<f64>,
297    /// Touch point id. Useful for multi-touch.
298    #[serde(skip_serializing_if = "Option::is_none")]
299    pub id: Option<i32>,
300}
301
302impl TouchPoint {
303    /// Create a new touch point at the given coordinates.
304    pub fn new(x: f64, y: f64) -> Self {
305        Self {
306            x,
307            y,
308            radius_x: None,
309            radius_y: None,
310            rotation_angle: None,
311            force: None,
312            id: None,
313        }
314    }
315
316    /// Set the touch point ID.
317    #[must_use]
318    pub fn with_id(mut self, id: i32) -> Self {
319        self.id = Some(id);
320        self
321    }
322}
323
324/// Parameters for Input.dispatchTouchEvent.
325#[derive(Debug, Clone, Serialize)]
326#[serde(rename_all = "camelCase")]
327pub struct DispatchTouchEventParams {
328    /// Type of the touch event.
329    #[serde(rename = "type")]
330    pub event_type: TouchEventType,
331    /// Active touch points on the touch device.
332    pub touch_points: Vec<TouchPoint>,
333    /// Bit field representing pressed modifier keys.
334    #[serde(skip_serializing_if = "Option::is_none")]
335    pub modifiers: Option<i32>,
336    /// Time at which the event occurred.
337    #[serde(skip_serializing_if = "Option::is_none")]
338    pub timestamp: Option<f64>,
339}
340
341impl DispatchTouchEventParams {
342    /// Create a touch start event.
343    pub fn touch_start(x: f64, y: f64) -> Self {
344        Self {
345            event_type: TouchEventType::TouchStart,
346            touch_points: vec![TouchPoint::new(x, y)],
347            modifiers: None,
348            timestamp: None,
349        }
350    }
351
352    /// Create a touch end event.
353    pub fn touch_end() -> Self {
354        Self {
355            event_type: TouchEventType::TouchEnd,
356            touch_points: vec![],
357            modifiers: None,
358            timestamp: None,
359        }
360    }
361
362    /// Create a touch move event.
363    pub fn touch_move(x: f64, y: f64) -> Self {
364        Self {
365            event_type: TouchEventType::TouchMove,
366            touch_points: vec![TouchPoint::new(x, y)],
367            modifiers: None,
368            timestamp: None,
369        }
370    }
371
372    /// Create a touch cancel event.
373    pub fn touch_cancel() -> Self {
374        Self {
375            event_type: TouchEventType::TouchCancel,
376            touch_points: vec![],
377            modifiers: None,
378            timestamp: None,
379        }
380    }
381}
382
383/// Type of drag event.
384#[derive(Debug, Clone, Copy, Serialize)]
385#[serde(rename_all = "camelCase")]
386pub enum DragEventType {
387    DragEnter,
388    DragOver,
389    Drop,
390    DragCancel,
391}
392
393/// Drag data item.
394#[derive(Debug, Clone, Serialize)]
395#[serde(rename_all = "camelCase")]
396pub struct DragDataItem {
397    /// Mime type of the data.
398    pub mime_type: String,
399    /// Depending on the value of `mime_type`, it contains the drag data string or base64-encoded binary data.
400    pub data: String,
401}
402
403/// Drag data.
404#[derive(Debug, Clone, Serialize)]
405#[serde(rename_all = "camelCase")]
406pub struct DragData {
407    /// Items in the drag data.
408    pub items: Vec<DragDataItem>,
409    /// Drag operations mask.
410    #[serde(skip_serializing_if = "Option::is_none")]
411    pub drag_operations_mask: Option<i32>,
412}
413
414impl DragData {
415    /// Create empty drag data.
416    pub fn new() -> Self {
417        Self {
418            items: vec![],
419            drag_operations_mask: None,
420        }
421    }
422
423    /// Add a text item.
424    #[must_use]
425    pub fn with_text(mut self, text: &str) -> Self {
426        self.items.push(DragDataItem {
427            mime_type: "text/plain".to_string(),
428            data: text.to_string(),
429        });
430        self
431    }
432}
433
434impl Default for DragData {
435    fn default() -> Self {
436        Self::new()
437    }
438}
439
440/// Parameters for Input.dispatchDragEvent.
441#[derive(Debug, Clone, Serialize)]
442#[serde(rename_all = "camelCase")]
443pub struct DispatchDragEventParams {
444    /// Type of the drag event.
445    #[serde(rename = "type")]
446    pub event_type: DragEventType,
447    /// X coordinate of the event relative to the main frame's viewport.
448    pub x: f64,
449    /// Y coordinate of the event relative to the main frame's viewport.
450    pub y: f64,
451    /// Drag data.
452    pub data: DragData,
453    /// Bit field representing pressed modifier keys.
454    #[serde(skip_serializing_if = "Option::is_none")]
455    pub modifiers: Option<i32>,
456}