1use crate::BorderRadius;
5use crate::Coord;
6pub struct PhysicalPx;
9
10pub struct LogicalPx;
13pub type LogicalLength = euclid::Length<Coord, LogicalPx>;
14pub type LogicalRect = euclid::Rect<Coord, LogicalPx>;
15pub type LogicalPoint = euclid::Point2D<Coord, LogicalPx>;
16pub type LogicalSize = euclid::Size2D<Coord, LogicalPx>;
17pub type LogicalVector = euclid::Vector2D<Coord, LogicalPx>;
18pub type LogicalBorderRadius = BorderRadius<Coord, LogicalPx>;
19pub type PhysicalBorderRadius = BorderRadius<f32, PhysicalPx>;
20pub type ItemTransform = euclid::Transform2D<f32, LogicalPx, LogicalPx>;
21
22pub type ScaleFactor = euclid::Scale<f32, LogicalPx, PhysicalPx>;
23
24pub trait SizeLengths {
25 type LengthType;
26 fn width_length(&self) -> Self::LengthType;
27 fn height_length(&self) -> Self::LengthType;
28}
29
30impl<T: Copy, U> SizeLengths for euclid::Size2D<T, U> {
31 type LengthType = euclid::Length<T, U>;
32 fn width_length(&self) -> Self::LengthType {
33 euclid::Length::new(self.width)
34 }
35 fn height_length(&self) -> Self::LengthType {
36 euclid::Length::new(self.height)
37 }
38}
39
40pub trait PointLengths {
41 type LengthType;
42 fn x_length(&self) -> Self::LengthType;
43 fn y_length(&self) -> Self::LengthType;
44}
45
46impl<T: Copy, U> PointLengths for euclid::Point2D<T, U> {
47 type LengthType = euclid::Length<T, U>;
48 fn x_length(&self) -> Self::LengthType {
49 euclid::Length::new(self.x)
50 }
51 fn y_length(&self) -> Self::LengthType {
52 euclid::Length::new(self.y)
53 }
54}
55
56impl<T: Copy, U> PointLengths for euclid::Vector2D<T, U> {
57 type LengthType = euclid::Length<T, U>;
58 fn x_length(&self) -> Self::LengthType {
59 euclid::Length::new(self.x)
60 }
61 fn y_length(&self) -> Self::LengthType {
62 euclid::Length::new(self.y)
63 }
64}
65
66pub trait RectLengths {
67 type SizeType;
68 type LengthType;
69 fn size_length(&self) -> Self::SizeType;
70 fn width_length(&self) -> Self::LengthType;
71 fn height_length(&self) -> Self::LengthType;
72}
73
74impl<T: Copy, U> RectLengths for euclid::Rect<T, U> {
75 type LengthType = euclid::Length<T, U>;
76 type SizeType = euclid::Size2D<T, U>;
77 fn size_length(&self) -> Self::SizeType {
78 euclid::Size2D::new(self.size.width, self.size.height)
79 }
80 fn width_length(&self) -> Self::LengthType {
81 self.size_length().width_length()
82 }
83 fn height_length(&self) -> Self::LengthType {
84 self.size_length().height_length()
85 }
86}
87
88pub fn logical_size_from_api(size: crate::api::LogicalSize) -> LogicalSize {
91 size.to_euclid()
92}
93
94pub fn logical_point_from_api(position: crate::api::LogicalPosition) -> LogicalPoint {
95 position.to_euclid()
96}
97
98pub fn logical_position_to_api(pos: LogicalPoint) -> crate::api::LogicalPosition {
99 crate::api::LogicalPosition::from_euclid(pos)
100}
101
102pub fn logical_size_to_api(size: LogicalSize) -> crate::api::LogicalSize {
103 crate::api::LogicalSize::from_euclid(size)
104}
105
106#[derive(Debug, Default, Copy, Clone, PartialEq)]
108#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
109#[repr(C)]
110pub struct LogicalEdges {
111 pub top: f32,
113 pub bottom: f32,
115 pub left: f32,
117 pub right: f32,
119}
120
121impl LogicalEdges {
122 pub const fn new(top: f32, bottom: f32, left: f32, right: f32) -> Self {
125 Self { top, bottom, left, right }
126 }
127
128 #[inline]
130 pub const fn top(&self) -> LogicalLength {
131 LogicalLength::new(self.top as crate::Coord)
132 }
133 #[inline]
135 pub const fn bottom(&self) -> LogicalLength {
136 LogicalLength::new(self.bottom as crate::Coord)
137 }
138 #[inline]
140 pub const fn left(&self) -> LogicalLength {
141 LogicalLength::new(self.left as crate::Coord)
142 }
143 #[inline]
145 pub const fn right(&self) -> LogicalLength {
146 LogicalLength::new(self.right as crate::Coord)
147 }
148}
149
150#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
152#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
153pub struct PhysicalEdges {
154 pub top: i32,
156 pub bottom: i32,
158 pub left: i32,
160 pub right: i32,
162}
163
164impl PhysicalEdges {
165 pub const fn new(top: i32, bottom: i32, left: i32, right: i32) -> Self {
168 Self { top, bottom, left, right }
169 }
170
171 #[inline]
174 pub const fn to_logical(&self, scale_factor: f32) -> LogicalEdges {
175 LogicalEdges::new(
176 self.top_to_logical(scale_factor).0 as f32,
177 self.bottom_to_logical(scale_factor).0 as f32,
178 self.left_to_logical(scale_factor).0 as f32,
179 self.right_to_logical(scale_factor).0 as f32,
180 )
181 }
182
183 #[inline]
186 pub const fn top_to_logical(&self, scale_factor: f32) -> LogicalLength {
187 LogicalLength::new((self.top as f32 / scale_factor) as crate::Coord)
188 }
189
190 #[inline]
193 pub const fn bottom_to_logical(&self, scale_factor: f32) -> LogicalLength {
194 LogicalLength::new((self.bottom as f32 / scale_factor) as crate::Coord)
195 }
196
197 #[inline]
198 pub const fn left_to_logical(&self, scale_factor: f32) -> LogicalLength {
201 LogicalLength::new((self.left as f32 / scale_factor) as crate::Coord)
202 }
203
204 #[inline]
207 pub const fn right_to_logical(&self, scale_factor: f32) -> LogicalLength {
208 LogicalLength::new((self.right as f32 / scale_factor) as crate::Coord)
209 }
210}