Skip to main content

hpx_browser/layout/
query.rs

1use serde::Serialize;
2
3use crate::layout::layout_unit::LayoutUnit;
4
5#[derive(Debug, Clone, Copy, Serialize, Default)]
6pub struct DOMRect {
7    pub x: f64,
8    pub y: f64,
9    pub width: f64,
10    pub height: f64,
11    pub top: f64,
12    pub right: f64,
13    pub bottom: f64,
14    pub left: f64,
15}
16
17impl DOMRect {
18    pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
19        let qx = LayoutUnit::from_f64_px(x).to_f64_px();
20        let qy = LayoutUnit::from_f64_px(y).to_f64_px();
21        let qw = LayoutUnit::from_f64_px(width).to_f64_px();
22        let qh = LayoutUnit::from_f64_px(height).to_f64_px();
23        Self {
24            x: qx,
25            y: qy,
26            width: qw,
27            height: qh,
28            top: qy,
29            right: qx + qw,
30            bottom: qy + qh,
31            left: qx,
32        }
33    }
34
35    pub fn from_taffy_layout(layout: &taffy::Layout) -> Self {
36        let x = LayoutUnit::from_taffy_f32(layout.location.x).to_f64_px();
37        let y = LayoutUnit::from_taffy_f32(layout.location.y).to_f64_px();
38        let w = LayoutUnit::from_taffy_f32(layout.size.width).to_f64_px();
39        let h = LayoutUnit::from_taffy_f32(layout.size.height).to_f64_px();
40        Self::new(x, y, w, h)
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn dom_rect_new() {
50        let r = DOMRect::new(10.0, 20.0, 100.0, 50.0);
51        assert_eq!(r.x, 10.0);
52        assert_eq!(r.y, 20.0);
53        assert_eq!(r.width, 100.0);
54        assert_eq!(r.height, 50.0);
55        assert_eq!(r.top, 20.0);
56        assert_eq!(r.right, 110.0);
57        assert_eq!(r.bottom, 70.0);
58        assert_eq!(r.left, 10.0);
59    }
60
61    #[test]
62    fn dom_rect_quantized() {
63        let r = DOMRect::new(1.3, 0.0, 200.0, 100.0);
64        assert_eq!(r.x, 1.296875);
65    }
66}