use serde::Serialize;
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum MouseButton {
None,
Left,
Middle,
Right,
Back,
Forward,
}
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum MouseEventType {
MousePressed,
MouseReleased,
MouseMoved,
MouseWheel,
}
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum KeyEventType {
KeyDown,
KeyUp,
RawKeyDown,
Char,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchMouseEventParams {
#[serde(rename = "type")]
pub event_type: MouseEventType,
pub x: f64,
pub y: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub button: Option<MouseButton>,
#[serde(skip_serializing_if = "Option::is_none")]
pub click_count: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub modifiers: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<f64>,
}
impl DispatchMouseEventParams {
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,
}
}
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,
}
}
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,
}
}
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,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchMouseWheelParams {
#[serde(rename = "type")]
pub event_type: MouseEventType,
pub x: f64,
pub y: f64,
pub delta_x: f64,
pub delta_y: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub modifiers: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pointer_type: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchKeyEventParams {
#[serde(rename = "type")]
pub event_type: KeyEventType,
#[serde(skip_serializing_if = "Option::is_none")]
pub modifiers: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unmodified_text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub key_identifier: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub windows_virtual_key_code: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub native_virtual_key_code: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub auto_repeat: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_keypad: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_system_key: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub commands: Option<Vec<String>>,
}
impl DispatchKeyEventParams {
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,
}
}
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,
}
}
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,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InsertTextParams {
pub text: String,
}
pub mod modifiers {
pub const ALT: i32 = 1;
pub const CTRL: i32 = 2;
pub const META: i32 = 4;
pub const SHIFT: i32 = 8;
}
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum TouchEventType {
TouchStart,
TouchEnd,
TouchMove,
TouchCancel,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TouchPoint {
pub x: f64,
pub y: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub radius_x: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub radius_y: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rotation_angle: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub force: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<i32>,
}
impl TouchPoint {
pub fn new(x: f64, y: f64) -> Self {
Self {
x,
y,
radius_x: None,
radius_y: None,
rotation_angle: None,
force: None,
id: None,
}
}
#[must_use]
pub fn with_id(mut self, id: i32) -> Self {
self.id = Some(id);
self
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchTouchEventParams {
#[serde(rename = "type")]
pub event_type: TouchEventType,
pub touch_points: Vec<TouchPoint>,
#[serde(skip_serializing_if = "Option::is_none")]
pub modifiers: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<f64>,
}
impl DispatchTouchEventParams {
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,
}
}
pub fn touch_end() -> Self {
Self {
event_type: TouchEventType::TouchEnd,
touch_points: vec![],
modifiers: None,
timestamp: None,
}
}
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,
}
}
pub fn touch_cancel() -> Self {
Self {
event_type: TouchEventType::TouchCancel,
touch_points: vec![],
modifiers: None,
timestamp: None,
}
}
}
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DragEventType {
DragEnter,
DragOver,
Drop,
DragCancel,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DragDataItem {
pub mime_type: String,
pub data: String,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DragData {
pub items: Vec<DragDataItem>,
#[serde(skip_serializing_if = "Option::is_none")]
pub drag_operations_mask: Option<i32>,
}
impl DragData {
pub fn new() -> Self {
Self {
items: vec![],
drag_operations_mask: None,
}
}
#[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()
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchDragEventParams {
#[serde(rename = "type")]
pub event_type: DragEventType,
pub x: f64,
pub y: f64,
pub data: DragData,
#[serde(skip_serializing_if = "Option::is_none")]
pub modifiers: Option<i32>,
}