Skip to main content

rustenium/input/
mouse.rs

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