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 fn x_length_range(&self) -> core::ops::Range<Self::LengthType>;
74}
75
76impl<T: Copy + core::ops::Add<Output = T>, U> RectLengths for euclid::Rect<T, U> {
77 type LengthType = euclid::Length<T, U>;
78 type SizeType = euclid::Size2D<T, U>;
79 fn size_length(&self) -> Self::SizeType {
80 euclid::Size2D::new(self.size.width, self.size.height)
81 }
82 fn width_length(&self) -> Self::LengthType {
83 self.size_length().width_length()
84 }
85 fn height_length(&self) -> Self::LengthType {
86 self.size_length().height_length()
87 }
88 fn x_length_range(&self) -> core::ops::Range<Self::LengthType> {
89 self.origin.x_length()..self.origin.x_length() + self.width_length()
90 }
91}
92
93pub fn logical_size_from_api(size: crate::api::LogicalSize) -> LogicalSize {
96 size.to_euclid()
97}
98
99pub fn logical_point_from_api(position: crate::api::LogicalPosition) -> LogicalPoint {
100 position.to_euclid()
101}
102
103pub fn logical_position_to_api(pos: LogicalPoint) -> crate::api::LogicalPosition {
104 crate::api::LogicalPosition::from_euclid(pos)
105}
106
107pub fn logical_size_to_api(size: LogicalSize) -> crate::api::LogicalSize {
108 crate::api::LogicalSize::from_euclid(size)
109}
110
111#[derive(Debug, Default, Copy, Clone, PartialEq)]
113#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
114#[repr(C)]
115pub struct LogicalEdges {
116 pub top: f32,
118 pub bottom: f32,
120 pub left: f32,
122 pub right: f32,
124}
125
126impl LogicalEdges {
127 pub const fn new(top: f32, bottom: f32, left: f32, right: f32) -> Self {
130 Self { top, bottom, left, right }
131 }
132
133 #[inline]
135 pub const fn top(&self) -> LogicalLength {
136 LogicalLength::new(self.top as crate::Coord)
137 }
138 #[inline]
140 pub const fn bottom(&self) -> LogicalLength {
141 LogicalLength::new(self.bottom as crate::Coord)
142 }
143 #[inline]
145 pub const fn left(&self) -> LogicalLength {
146 LogicalLength::new(self.left as crate::Coord)
147 }
148 #[inline]
150 pub const fn right(&self) -> LogicalLength {
151 LogicalLength::new(self.right as crate::Coord)
152 }
153}
154
155#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
157#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
158pub struct PhysicalEdges {
159 pub top: i32,
161 pub bottom: i32,
163 pub left: i32,
165 pub right: i32,
167}
168
169impl PhysicalEdges {
170 pub const fn new(top: i32, bottom: i32, left: i32, right: i32) -> Self {
173 Self { top, bottom, left, right }
174 }
175
176 #[inline]
179 pub const fn to_logical(&self, scale_factor: f32) -> LogicalEdges {
180 LogicalEdges::new(
181 self.top_to_logical(scale_factor).0 as f32,
182 self.bottom_to_logical(scale_factor).0 as f32,
183 self.left_to_logical(scale_factor).0 as f32,
184 self.right_to_logical(scale_factor).0 as f32,
185 )
186 }
187
188 #[inline]
191 pub const fn top_to_logical(&self, scale_factor: f32) -> LogicalLength {
192 LogicalLength::new((self.top as f32 / scale_factor) as crate::Coord)
193 }
194
195 #[inline]
198 pub const fn bottom_to_logical(&self, scale_factor: f32) -> LogicalLength {
199 LogicalLength::new((self.bottom as f32 / scale_factor) as crate::Coord)
200 }
201
202 #[inline]
203 pub const fn left_to_logical(&self, scale_factor: f32) -> LogicalLength {
206 LogicalLength::new((self.left as f32 / scale_factor) as crate::Coord)
207 }
208
209 #[inline]
212 pub const fn right_to_logical(&self, scale_factor: f32) -> LogicalLength {
213 LogicalLength::new((self.right as f32 / scale_factor) as crate::Coord)
214 }
215}