1use chromiumoxide_cdp::cdp::browser_protocol::dom::Quad;
4use chromiumoxide_cdp::cdp::browser_protocol::input::{
5 DispatchMouseEventParams, DispatchMouseEventType, MouseButton,
6};
7use chromiumoxide_cdp::cdp::browser_protocol::page::Viewport;
8
9#[derive(Debug, Copy, Clone, PartialEq)]
10pub struct Point {
11 pub x: f64,
12 pub y: f64,
13}
14
15impl Point {
16 pub fn new(x: f64, y: f64) -> Self {
18 Self { x, y }
19 }
20
21 fn area(&self, other: &Self) -> f64 {
22 (self.x * other.y - other.x * self.y) / 2.
23 }
24}
25
26impl std::ops::Add<Point> for Point {
27 type Output = Self;
28
29 fn add(self, other: Self) -> Self {
30 Self {
31 x: self.x + other.x,
32 y: self.y + other.y,
33 }
34 }
35}
36
37impl std::ops::Sub<Point> for Point {
38 type Output = Self;
39
40 fn sub(self, other: Self) -> Self {
41 Self {
42 x: self.x - other.x,
43 y: self.y - other.y,
44 }
45 }
46}
47
48impl std::ops::Div<f64> for Point {
49 type Output = Self;
50
51 fn div(self, other: f64) -> Self {
52 Self {
53 x: self.x / other,
54 y: self.y / other,
55 }
56 }
57}
58
59#[derive(Default, Debug, Copy, Clone, PartialEq)]
60pub struct Delta {
61 pub delta_x: f64,
63 pub delta_y: f64,
65}
66
67impl Delta {
68 pub fn new(delta_x: f64, delta_y: f64) -> Self {
70 Self { delta_x, delta_y }
71 }
72
73 pub fn area(&self, other: &Self) -> f64 {
74 (self.delta_x * other.delta_y - other.delta_x * self.delta_y) / 2.
75 }
76}
77
78impl From<Point> for DispatchMouseEventParams {
80 fn from(el: Point) -> DispatchMouseEventParams {
81 let mut params =
82 DispatchMouseEventParams::new(DispatchMouseEventType::MousePressed, el.x, el.y);
83 params.button = Some(MouseButton::Left);
84 params.click_count = Some(1);
85 params
86 }
87}
88
89#[derive(Debug, Copy, Clone)]
90pub struct ElementQuad {
91 pub top_left: Point,
92 pub top_right: Point,
93 pub bottom_right: Point,
94 pub bottom_left: Point,
95}
96
97impl ElementQuad {
98 pub fn from_quad(quad: &Quad) -> Self {
99 assert_eq!(quad.inner().len(), 8);
100 let raw_quad = quad.inner();
101 Self {
102 top_left: Point {
103 x: raw_quad[0],
104 y: raw_quad[1],
105 },
106 top_right: Point {
107 x: raw_quad[2],
108 y: raw_quad[3],
109 },
110 bottom_right: Point {
111 x: raw_quad[4],
112 y: raw_quad[5],
113 },
114 bottom_left: Point {
115 x: raw_quad[6],
116 y: raw_quad[7],
117 },
118 }
119 }
120
121 pub fn quad_center(&self) -> Point {
122 Point {
123 x: (self.top_left.x + self.top_right.x + self.bottom_right.x + self.bottom_left.x) / 4.,
124 y: (self.top_left.y + self.top_right.y + self.bottom_right.y + self.bottom_left.y) / 4.,
125 }
126 }
127 pub fn quad_area(&self) -> f64 {
130 let area = self.top_left.area(&self.top_right)
131 + self.top_right.area(&self.bottom_right)
132 + self.bottom_right.area(&self.bottom_left)
133 + self.bottom_left.area(&self.top_left);
134 area.abs()
135 }
136
137 pub fn height(&self) -> f64 {
138 self.bottom_left.y - self.top_left.y
139 }
140
141 pub fn width(&self) -> f64 {
142 self.top_right.x - self.top_left.x
143 }
144
145 pub fn aspect_ratio(&self) -> f64 {
147 self.width() / self.height()
148 }
149
150 pub fn most_left(&self) -> f64 {
152 self.top_right
153 .x
154 .min(self.top_left.x)
155 .min(self.bottom_right.x)
156 .min(self.bottom_left.x)
157 }
158
159 pub fn most_right(&self) -> f64 {
161 self.top_right
162 .x
163 .max(self.top_left.x)
164 .max(self.bottom_right.x)
165 .max(self.bottom_left.x)
166 }
167
168 pub fn most_top(&self) -> f64 {
170 self.top_right
171 .y
172 .min(self.top_left.y)
173 .min(self.bottom_right.y)
174 .min(self.bottom_left.y)
175 }
176
177 pub fn most_bottom(&self) -> f64 {
179 self.top_right
180 .y
181 .max(self.top_left.y)
182 .max(self.bottom_right.y)
183 .max(self.bottom_left.y)
184 }
185
186 pub fn strictly_above(&self, other: &Self) -> bool {
189 self.most_bottom() < other.most_top()
190 }
191
192 pub fn above(&self, other: &Self) -> bool {
195 self.most_bottom() <= other.most_top()
196 }
197
198 pub fn strictly_below(&self, other: &Self) -> bool {
201 self.most_top() > other.most_bottom()
202 }
203
204 pub fn below(&self, other: &Self) -> bool {
207 self.most_top() >= other.most_bottom()
208 }
209
210 pub fn strictly_left_of(&self, other: &Self) -> bool {
213 self.most_right() < other.most_left()
214 }
215
216 pub fn left_of(&self, other: &Self) -> bool {
219 self.most_right() <= other.most_left()
220 }
221
222 pub fn strictly_right_of(&self, other: &Self) -> bool {
225 self.most_left() > other.most_right()
226 }
227
228 pub fn right_of(&self, other: &Self) -> bool {
231 self.most_left() >= other.most_right()
232 }
233
234 pub fn within_horizontal_bounds_of(&self, other: &Self) -> bool {
236 self.most_left() >= other.most_left() && self.most_right() <= other.most_right()
237 }
238
239 pub fn within_vertical_bounds_of(&self, other: &Self) -> bool {
241 self.most_top() >= other.most_top() && self.most_bottom() <= other.most_bottom()
242 }
243
244 pub fn within_bounds_of(&self, other: &Self) -> bool {
246 self.within_horizontal_bounds_of(other) && self.within_vertical_bounds_of(other)
247 }
248}
249
250#[derive(Debug, Clone)]
251pub struct BoxModel {
252 pub content: ElementQuad,
253 pub padding: ElementQuad,
254 pub border: ElementQuad,
255 pub margin: ElementQuad,
256 pub width: u32,
257 pub height: u32,
258}
259
260impl BoxModel {
261 pub fn content_viewport(&self) -> Viewport {
263 Viewport {
264 x: self.content.top_left.x,
265 y: self.content.top_left.y,
266 width: self.content.width(),
267 height: self.content.height(),
268 scale: 1.0,
269 }
270 }
271
272 pub fn padding_viewport(&self) -> Viewport {
274 Viewport {
275 x: self.padding.top_left.x,
276 y: self.padding.top_left.y,
277 width: self.padding.width(),
278 height: self.padding.height(),
279 scale: 1.0,
280 }
281 }
282
283 pub fn border_viewport(&self) -> Viewport {
285 Viewport {
286 x: self.border.top_left.x,
287 y: self.border.top_left.y,
288 width: self.border.width(),
289 height: self.border.height(),
290 scale: 1.0,
291 }
292 }
293
294 pub fn margin_viewport(&self) -> Viewport {
296 Viewport {
297 x: self.margin.top_left.x,
298 y: self.margin.top_left.y,
299 width: self.margin.width(),
300 height: self.margin.height(),
301 scale: 1.0,
302 }
303 }
304}
305
306#[derive(Debug, Clone)]
307pub struct BoundingBox {
308 pub x: f64,
310 pub y: f64,
312 pub width: f64,
314 pub height: f64,
316}