viewpoint-cdp 0.4.3

Low-level Chrome DevTools Protocol implementation over WebSocket
Documentation
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Input domain types.
//!
//! The Input domain provides methods for simulating user input events.

use serde::Serialize;

/// Mouse button type.
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum MouseButton {
    None,
    Left,
    Middle,
    Right,
    Back,
    Forward,
}

/// Type of mouse event.
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum MouseEventType {
    MousePressed,
    MouseReleased,
    MouseMoved,
    MouseWheel,
}

/// Type of key event.
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum KeyEventType {
    KeyDown,
    KeyUp,
    RawKeyDown,
    Char,
}

/// Parameters for Input.dispatchMouseEvent.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchMouseEventParams {
    /// Type of the mouse event.
    #[serde(rename = "type")]
    pub event_type: MouseEventType,
    /// X coordinate of the event relative to the main frame's viewport.
    pub x: f64,
    /// Y coordinate of the event relative to the main frame's viewport.
    pub y: f64,
    /// Mouse button.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub button: Option<MouseButton>,
    /// Number of times the mouse button was clicked.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub click_count: Option<i32>,
    /// Bit field representing pressed modifier keys.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modifiers: Option<i32>,
    /// Time at which the event occurred.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<f64>,
}

impl DispatchMouseEventParams {
    /// Create a mouse move event.
    pub fn mouse_move(x: f64, y: f64) -> Self {
        Self {
            event_type: MouseEventType::MouseMoved,
            x,
            y,
            button: None,
            click_count: None,
            modifiers: None,
            timestamp: None,
        }
    }

    /// Create a mouse down event.
    pub fn mouse_down(x: f64, y: f64, button: MouseButton) -> Self {
        Self {
            event_type: MouseEventType::MousePressed,
            x,
            y,
            button: Some(button),
            click_count: Some(1),
            modifiers: None,
            timestamp: None,
        }
    }

    /// Create a mouse up event.
    pub fn mouse_up(x: f64, y: f64, button: MouseButton) -> Self {
        Self {
            event_type: MouseEventType::MouseReleased,
            x,
            y,
            button: Some(button),
            click_count: Some(1),
            modifiers: None,
            timestamp: None,
        }
    }

    /// Create a mouse wheel event.
    pub fn mouse_wheel(x: f64, y: f64, delta_x: f64, delta_y: f64) -> DispatchMouseWheelParams {
        DispatchMouseWheelParams {
            event_type: MouseEventType::MouseWheel,
            x,
            y,
            delta_x,
            delta_y,
            modifiers: None,
            pointer_type: None,
        }
    }
}

/// Parameters for Input.dispatchMouseEvent with wheel.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchMouseWheelParams {
    /// Type of the mouse event.
    #[serde(rename = "type")]
    pub event_type: MouseEventType,
    /// X coordinate of the event relative to the main frame's viewport.
    pub x: f64,
    /// Y coordinate of the event relative to the main frame's viewport.
    pub y: f64,
    /// X delta in CSS pixels for mouse wheel event.
    pub delta_x: f64,
    /// Y delta in CSS pixels for mouse wheel event.
    pub delta_y: f64,
    /// Bit field representing pressed modifier keys.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modifiers: Option<i32>,
    /// Pointer type (default "mouse").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pointer_type: Option<String>,
}

/// Parameters for Input.dispatchKeyEvent.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchKeyEventParams {
    /// Type of the key event.
    #[serde(rename = "type")]
    pub event_type: KeyEventType,
    /// Bit field representing pressed modifier keys.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modifiers: Option<i32>,
    /// Time at which the event occurred.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<f64>,
    /// Text as generated by processing a virtual key code.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    /// Text that would have been generated without any modifiers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unmodified_text: Option<String>,
    /// Unique key identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_identifier: Option<String>,
    /// Unique DOM defined string value for each key.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
    /// Unique DOM defined string value describing the meaning of the key.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key: Option<String>,
    /// Windows virtual key code.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub windows_virtual_key_code: Option<i32>,
    /// Native virtual key code.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub native_virtual_key_code: Option<i32>,
    /// Whether the event was generated from auto repeat.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_repeat: Option<bool>,
    /// Whether the event was generated from the keypad.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_keypad: Option<bool>,
    /// Whether the event was a system key event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_system_key: Option<bool>,
    /// Editing commands to send with the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub commands: Option<Vec<String>>,
}

impl DispatchKeyEventParams {
    /// Create a key down event.
    pub fn key_down(key: &str) -> Self {
        Self {
            event_type: KeyEventType::KeyDown,
            modifiers: None,
            timestamp: None,
            text: None,
            unmodified_text: None,
            key_identifier: None,
            code: Some(key.to_string()),
            key: Some(key.to_string()),
            windows_virtual_key_code: None,
            native_virtual_key_code: None,
            auto_repeat: None,
            is_keypad: None,
            is_system_key: None,
            commands: None,
        }
    }

    /// Create a key up event.
    pub fn key_up(key: &str) -> Self {
        Self {
            event_type: KeyEventType::KeyUp,
            modifiers: None,
            timestamp: None,
            text: None,
            unmodified_text: None,
            key_identifier: None,
            code: Some(key.to_string()),
            key: Some(key.to_string()),
            windows_virtual_key_code: None,
            native_virtual_key_code: None,
            auto_repeat: None,
            is_keypad: None,
            is_system_key: None,
            commands: None,
        }
    }

    /// Create a char event for text input.
    pub fn char(text: &str) -> Self {
        Self {
            event_type: KeyEventType::Char,
            modifiers: None,
            timestamp: None,
            text: Some(text.to_string()),
            unmodified_text: Some(text.to_string()),
            key_identifier: None,
            code: None,
            key: None,
            windows_virtual_key_code: None,
            native_virtual_key_code: None,
            auto_repeat: None,
            is_keypad: None,
            is_system_key: None,
            commands: None,
        }
    }
}

/// Parameters for Input.insertText.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InsertTextParams {
    /// The text to insert.
    pub text: String,
}

/// Modifier keys bit flags.
pub mod modifiers {
    pub const ALT: i32 = 1;
    pub const CTRL: i32 = 2;
    pub const META: i32 = 4;
    pub const SHIFT: i32 = 8;
}

/// Type of touch event.
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum TouchEventType {
    TouchStart,
    TouchEnd,
    TouchMove,
    TouchCancel,
}

/// A single touch point.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TouchPoint {
    /// X coordinate of the touch point.
    pub x: f64,
    /// Y coordinate of the touch point.
    pub y: f64,
    /// Touch point radius in X direction (default 1.0).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub radius_x: Option<f64>,
    /// Touch point radius in Y direction (default 1.0).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub radius_y: Option<f64>,
    /// Rotation angle (default 0.0).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rotation_angle: Option<f64>,
    /// Force (default 1.0).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub force: Option<f64>,
    /// Touch point id. Useful for multi-touch.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
}

impl TouchPoint {
    /// Create a new touch point at the given coordinates.
    pub fn new(x: f64, y: f64) -> Self {
        Self {
            x,
            y,
            radius_x: None,
            radius_y: None,
            rotation_angle: None,
            force: None,
            id: None,
        }
    }

    /// Set the touch point ID.
    #[must_use]
    pub fn with_id(mut self, id: i32) -> Self {
        self.id = Some(id);
        self
    }
}

/// Parameters for Input.dispatchTouchEvent.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchTouchEventParams {
    /// Type of the touch event.
    #[serde(rename = "type")]
    pub event_type: TouchEventType,
    /// Active touch points on the touch device.
    pub touch_points: Vec<TouchPoint>,
    /// Bit field representing pressed modifier keys.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modifiers: Option<i32>,
    /// Time at which the event occurred.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<f64>,
}

impl DispatchTouchEventParams {
    /// Create a touch start event.
    pub fn touch_start(x: f64, y: f64) -> Self {
        Self {
            event_type: TouchEventType::TouchStart,
            touch_points: vec![TouchPoint::new(x, y)],
            modifiers: None,
            timestamp: None,
        }
    }

    /// Create a touch end event.
    pub fn touch_end() -> Self {
        Self {
            event_type: TouchEventType::TouchEnd,
            touch_points: vec![],
            modifiers: None,
            timestamp: None,
        }
    }

    /// Create a touch move event.
    pub fn touch_move(x: f64, y: f64) -> Self {
        Self {
            event_type: TouchEventType::TouchMove,
            touch_points: vec![TouchPoint::new(x, y)],
            modifiers: None,
            timestamp: None,
        }
    }

    /// Create a touch cancel event.
    pub fn touch_cancel() -> Self {
        Self {
            event_type: TouchEventType::TouchCancel,
            touch_points: vec![],
            modifiers: None,
            timestamp: None,
        }
    }
}

/// Type of drag event.
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DragEventType {
    DragEnter,
    DragOver,
    Drop,
    DragCancel,
}

/// Drag data item.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DragDataItem {
    /// Mime type of the data.
    pub mime_type: String,
    /// Depending on the value of `mime_type`, it contains the drag data string or base64-encoded binary data.
    pub data: String,
}

/// Drag data.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DragData {
    /// Items in the drag data.
    pub items: Vec<DragDataItem>,
    /// Drag operations mask.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub drag_operations_mask: Option<i32>,
}

impl DragData {
    /// Create empty drag data.
    pub fn new() -> Self {
        Self {
            items: vec![],
            drag_operations_mask: None,
        }
    }

    /// Add a text item.
    #[must_use]
    pub fn with_text(mut self, text: &str) -> Self {
        self.items.push(DragDataItem {
            mime_type: "text/plain".to_string(),
            data: text.to_string(),
        });
        self
    }
}

impl Default for DragData {
    fn default() -> Self {
        Self::new()
    }
}

/// Parameters for Input.dispatchDragEvent.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchDragEventParams {
    /// Type of the drag event.
    #[serde(rename = "type")]
    pub event_type: DragEventType,
    /// X coordinate of the event relative to the main frame's viewport.
    pub x: f64,
    /// Y coordinate of the event relative to the main frame's viewport.
    pub y: f64,
    /// Drag data.
    pub data: DragData,
    /// Bit field representing pressed modifier keys.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modifiers: Option<i32>,
}