Skip to main content

nsh_core/geometry/
taffy.rs

1//! The one boundary between NSH's geometry and taffy's: taffy lays out in
2//! bare `f32` (points, as NSH gives them) and percentages as fractions,
3//! so conversions only wrap or unwrap. Rems resolve at [`REM_SIZE`], the
4//! rem of style strings.
5
6use taffy::{Dimension, LengthPercentage, LengthPercentageAuto};
7
8use super::{AbsoluteLength, Bounds, DefiniteLength, Edges, Length, Point, Points, REM_SIZE, Size};
9
10impl From<AbsoluteLength> for LengthPercentage {
11    #[inline]
12    fn from(l: AbsoluteLength) -> Self {
13        LengthPercentage::length(l.to_points(REM_SIZE).0)
14    }
15}
16
17impl From<DefiniteLength> for LengthPercentage {
18    #[inline]
19    fn from(l: DefiniteLength) -> Self {
20        match l {
21            DefiniteLength::Absolute(a) => a.into(),
22            DefiniteLength::Fraction(f) => LengthPercentage::percent(f.0),
23        }
24    }
25}
26
27impl From<DefiniteLength> for LengthPercentageAuto {
28    #[inline]
29    fn from(l: DefiniteLength) -> Self {
30        match l {
31            DefiniteLength::Absolute(a) => LengthPercentageAuto::length(a.to_points(REM_SIZE).0),
32            DefiniteLength::Fraction(f) => LengthPercentageAuto::percent(f.0),
33        }
34    }
35}
36
37impl From<Length> for LengthPercentageAuto {
38    #[inline]
39    fn from(l: Length) -> Self {
40        match l {
41            Length::Definite(d) => d.into(),
42            Length::Auto => LengthPercentageAuto::auto(),
43        }
44    }
45}
46
47impl From<DefiniteLength> for Dimension {
48    #[inline]
49    fn from(l: DefiniteLength) -> Self {
50        match l {
51            DefiniteLength::Absolute(a) => Dimension::length(a.to_points(REM_SIZE).0),
52            DefiniteLength::Fraction(f) => Dimension::percent(f.0),
53        }
54    }
55}
56
57impl From<Length> for Dimension {
58    #[inline]
59    fn from(l: Length) -> Self {
60        match l {
61            Length::Definite(d) => d.into(),
62            Length::Auto => Dimension::auto(),
63        }
64    }
65}
66
67impl From<taffy::Size<f32>> for Size<Points> {
68    #[inline]
69    fn from(s: taffy::Size<f32>) -> Self {
70        Size::new(Points(s.width), Points(s.height))
71    }
72}
73
74impl From<Size<Points>> for taffy::Size<f32> {
75    #[inline]
76    fn from(s: Size<Points>) -> Self {
77        taffy::Size {
78            width: s.width.0,
79            height: s.height.0,
80        }
81    }
82}
83
84impl From<taffy::Point<f32>> for Point<Points> {
85    #[inline]
86    fn from(p: taffy::Point<f32>) -> Self {
87        Point::new(Points(p.x), Points(p.y))
88    }
89}
90
91impl From<taffy::Rect<f32>> for Edges<Points> {
92    #[inline]
93    fn from(r: taffy::Rect<f32>) -> Self {
94        Edges {
95            top: Points(r.top),
96            right: Points(r.right),
97            bottom: Points(r.bottom),
98            left: Points(r.left),
99        }
100    }
101}
102
103/// A laid out node's border box relative to its parent.
104impl From<&taffy::Layout> for Bounds<Points> {
105    #[inline]
106    fn from(l: &taffy::Layout) -> Self {
107        Bounds::new(l.location.into(), l.size.into())
108    }
109}
110
111impl From<Size<Length>> for taffy::Size<Dimension> {
112    #[inline]
113    fn from(s: Size<Length>) -> Self {
114        taffy::Size {
115            width: s.width.into(),
116            height: s.height.into(),
117        }
118    }
119}
120
121impl From<Size<DefiniteLength>> for taffy::Size<LengthPercentage> {
122    #[inline]
123    fn from(s: Size<DefiniteLength>) -> Self {
124        taffy::Size {
125            width: s.width.into(),
126            height: s.height.into(),
127        }
128    }
129}
130
131impl From<Edges<DefiniteLength>> for taffy::Rect<LengthPercentage> {
132    #[inline]
133    fn from(e: Edges<DefiniteLength>) -> Self {
134        taffy::Rect {
135            top: e.top.into(),
136            right: e.right.into(),
137            bottom: e.bottom.into(),
138            left: e.left.into(),
139        }
140    }
141}
142
143impl From<Edges<Length>> for taffy::Rect<LengthPercentageAuto> {
144    #[inline]
145    fn from(e: Edges<Length>) -> Self {
146        taffy::Rect {
147            top: e.top.into(),
148            right: e.right.into(),
149            bottom: e.bottom.into(),
150            left: e.left.into(),
151        }
152    }
153}