chromiumoxide/
layout.rs

1//! Code based on [rust-headless-chrome](https://github.com/atroche/rust-headless-chrome/blob/master/src/browser/tab/element/box_model.rs)
2
3use 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    /// Create a new Point instance
17    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    /// X delta in CSS pixels for mouse wheel event (default: 0).
62    pub delta_x: f64,
63    /// Y delta in CSS pixels for mouse wheel event (default: 0).
64    pub delta_y: f64,
65}
66
67impl Delta {
68    /// Create a new Delta instance
69    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
78/// Converts a point into Left-Down-Single-Mouseclick
79impl 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    /// Compute sum of all directed areas of adjacent triangles
128    /// https://en.wikipedia.org/wiki/Polygon#Simple_polygons
129    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    /// The width divided by the height
146    pub fn aspect_ratio(&self) -> f64 {
147        self.width() / self.height()
148    }
149
150    /// The most left (smallest) x-coordinate
151    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    /// The most right (largest) x-coordinate
160    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    /// The most top (smallest) y-coordinate
169    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    /// The most bottom (largest) y-coordinate
178    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    /// If the most bottom point of `self` is above the most top point of
187    /// `other`
188    pub fn strictly_above(&self, other: &Self) -> bool {
189        self.most_bottom() < other.most_top()
190    }
191
192    /// If the most bottom point of `self` is above or on the same line as the
193    /// most top point of `other`
194    pub fn above(&self, other: &Self) -> bool {
195        self.most_bottom() <= other.most_top()
196    }
197
198    /// If the most top point of `self` is below the most bottom point of
199    /// `other`
200    pub fn strictly_below(&self, other: &Self) -> bool {
201        self.most_top() > other.most_bottom()
202    }
203
204    /// If the most top point of `self` is below or on the same line as the
205    /// most bottom point of `other`
206    pub fn below(&self, other: &Self) -> bool {
207        self.most_top() >= other.most_bottom()
208    }
209
210    /// If the most right point of `self` is left of the most left point of
211    /// `other`
212    pub fn strictly_left_of(&self, other: &Self) -> bool {
213        self.most_right() < other.most_left()
214    }
215
216    /// If the most right point of `self` is left or on the same line as the
217    /// most left point of `other`
218    pub fn left_of(&self, other: &Self) -> bool {
219        self.most_right() <= other.most_left()
220    }
221
222    /// If the most left point of `self` is right of the most right point of
223    /// `other`
224    pub fn strictly_right_of(&self, other: &Self) -> bool {
225        self.most_left() > other.most_right()
226    }
227
228    /// If the most left point of `self` is right or on the same line as the
229    /// most right point of `other`
230    pub fn right_of(&self, other: &Self) -> bool {
231        self.most_left() >= other.most_right()
232    }
233
234    /// If `self` is within the left/right boundaries defined by `other`.
235    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    /// If `self` is within the top/bottom boundaries defined by `other`.
240    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    /// If `self` is within the boundaries defined by `other`.
245    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    /// Create a `Viewport` equal to the content-box, using a scale of 1.0
262    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    /// Create a `Viewport` equal to the padding-box, using a scale of 1.0
273    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    /// Create a `Viewport` equal to the border-box, using a scale of 1.0
284    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    /// Create a `Viewport` equal to the margin-box, using a scale of 1.0
295    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    /// the x coordinate of the element in pixels.
309    pub x: f64,
310    /// the y coordinate of the element in pixels.
311    pub y: f64,
312    /// the width of the element in pixels.
313    pub width: f64,
314    /// the height of the element in pixels.
315    pub height: f64,
316}