Skip to main content

rustenium/input/
mouse.rs

1use std::future::Future;
2use std::sync::Arc;
3use rustenium_bidi_definitions::browsing_context::types::BrowsingContext;
4use tokio::sync::Mutex;
5use crate::error::bidi::InputError;
6
7#[derive(Debug, Clone, Default)]
8pub struct MouseMoveOptions {
9    pub steps: Option<usize>,
10    pub origin: Option<rustenium_bidi_definitions::input::types::Origin>,
11}
12
13
14#[derive(Default, Clone)]
15pub struct MouseMoveOptionsBuilder {
16    steps: Option<usize>,
17    origin: Option<rustenium_bidi_definitions::input::types::Origin>,
18}
19
20impl MouseMoveOptionsBuilder {
21    pub fn steps(mut self, v: usize) -> Self { self.steps = Some(v); self }
22    pub fn origin(mut self, v: rustenium_bidi_definitions::input::types::Origin) -> Self { self.origin = Some(v); self }
23    pub fn build(self) -> MouseMoveOptions {
24        MouseMoveOptions { steps: self.steps, origin: self.origin }
25    }
26}
27
28#[derive(Debug, Clone, Default)]
29pub struct MouseClickOptions {
30    pub button: Option<MouseButton>,
31    pub count: Option<u64>,
32    pub delay: Option<u64>,
33    pub origin: Option<rustenium_bidi_definitions::input::types::Origin>,
34}
35
36
37#[derive(Default, Clone)]
38pub struct MouseClickOptionsBuilder {
39    button: Option<MouseButton>,
40    count: Option<u64>,
41    delay: Option<u64>,
42    origin: Option<rustenium_bidi_definitions::input::types::Origin>,
43}
44
45impl MouseClickOptionsBuilder {
46    pub fn button(mut self, v: MouseButton) -> Self { self.button = Some(v); self }
47    pub fn count(mut self, v: u64) -> Self { self.count = Some(v); self }
48    pub fn delay(mut self, v: u64) -> Self { self.delay = Some(v); self }
49    pub fn origin(mut self, v: rustenium_bidi_definitions::input::types::Origin) -> Self { self.origin = Some(v); self }
50    pub fn build(self) -> MouseClickOptions {
51        MouseClickOptions { button: self.button, count: self.count, delay: self.delay, origin: self.origin }
52    }
53}
54
55#[derive(Debug, Clone, Default)]
56pub struct MouseOptions {
57    pub button: Option<MouseButton>,
58}
59
60
61#[derive(Default, Clone)]
62pub struct MouseOptionsBuilder {
63    button: Option<MouseButton>,
64}
65
66impl MouseOptionsBuilder {
67    pub fn button(mut self, v: MouseButton) -> Self { self.button = Some(v); self }
68    pub fn build(self) -> MouseOptions {
69        MouseOptions { button: self.button }
70    }
71}
72
73#[derive(Debug, Clone, Default)]
74pub struct MouseWheelOptions {
75    pub delta_x: Option<i64>,
76    pub delta_y: Option<i64>,
77}
78
79
80#[derive(Default, Clone)]
81pub struct MouseWheelOptionsBuilder {
82    delta_x: Option<i64>,
83    delta_y: Option<i64>,
84}
85
86impl MouseWheelOptionsBuilder {
87    pub fn delta_x(mut self, v: i64) -> Self { self.delta_x = Some(v); self }
88    pub fn delta_y(mut self, v: i64) -> Self { self.delta_y = Some(v); self }
89    pub fn build(self) -> MouseWheelOptions {
90        MouseWheelOptions { delta_x: self.delta_x, delta_y: self.delta_y }
91    }
92}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq)]
95pub enum MouseButton {
96    Left = 0,
97    Middle = 1,
98    Right = 2,
99    Back = 3,
100    Forward = 4,
101}
102
103#[derive(Debug, Clone, Copy, Default)]
104pub struct Point {
105    pub x: f64,
106    pub y: f64,
107}
108
109pub trait Mouse {
110    fn get_last_position(&self) -> Arc<Mutex<Point>>;
111    fn set_last_position(&self, point: Point);
112
113    fn reset(&self, context: &BrowsingContext) -> impl Future<Output = Result<(), InputError>>;
114
115    fn move_to(
116        &self,
117        point: Point,
118        context: &BrowsingContext,
119        options: MouseMoveOptions,
120    ) -> impl Future<Output = Result<(), InputError>>;
121
122    fn down(
123        &self,
124        context: &BrowsingContext,
125        options: MouseOptions,
126    ) -> impl Future<Output = Result<(), InputError>>;
127
128    fn up(
129        &self,
130        context: &BrowsingContext,
131        options: MouseOptions,
132    ) -> impl Future<Output = Result<(), InputError>>;
133
134    fn click(
135        &self,
136        point: Option<Point>,
137        context: &BrowsingContext,
138        options: MouseClickOptions,
139    ) -> impl Future<Output = Result<(), InputError>>;
140
141    fn wheel(
142        &self,
143        context: &BrowsingContext,
144        options: MouseWheelOptions,
145    ) -> impl Future<Output = Result<(), InputError>>;
146}