Skip to main content

i_slint_core/
lengths.rs

1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4use crate::BorderRadius;
5use crate::Coord;
6/// This type is used as a tagging type for use with [`euclid::Scale`] to convert
7/// between physical and logical pixels.
8pub struct PhysicalPx;
9
10/// This type is used as a tagging type for use with [`euclid::Scale`] to convert
11/// between physical and logical pixels.
12pub 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
88/// Convert from the api size to the internal size
89/// (This doesn't use the `From` trait because it would expose the conversion to euclid in the public API)
90pub 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/// Four distances from the edges of a rectangle represented in the coordinate space of logical pixels.
107#[derive(Debug, Default, Copy, Clone, PartialEq)]
108#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
109#[repr(C)]
110pub struct LogicalEdges {
111    /// The top distance in logical pixels.
112    pub top: f32,
113    /// The bottom distance in logical pixels.
114    pub bottom: f32,
115    /// The left distance in logical pixels.
116    pub left: f32,
117    /// The right distance in logical pixels.
118    pub right: f32,
119}
120
121impl LogicalEdges {
122    /// Construct a new logical edges struct from the given border values, that are assumed to be
123    /// in the logical coordinate space.
124    pub const fn new(top: f32, bottom: f32, left: f32, right: f32) -> Self {
125        Self { top, bottom, left, right }
126    }
127
128    /// Converts the top edge to logical pixels.
129    #[inline]
130    pub const fn top(&self) -> LogicalLength {
131        LogicalLength::new(self.top as crate::Coord)
132    }
133    /// Converts the bottom edge to logical pixels.
134    #[inline]
135    pub const fn bottom(&self) -> LogicalLength {
136        LogicalLength::new(self.bottom as crate::Coord)
137    }
138    /// Converts the left edge to logical pixels.
139    #[inline]
140    pub const fn left(&self) -> LogicalLength {
141        LogicalLength::new(self.left as crate::Coord)
142    }
143    /// Converts the right edge to logical pixels.
144    #[inline]
145    pub const fn right(&self) -> LogicalLength {
146        LogicalLength::new(self.right as crate::Coord)
147    }
148}
149
150/// Four distances from the edges of a rectangle represented in the coordinate space of physical pixels.
151#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
152#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
153pub struct PhysicalEdges {
154    /// The top edge in physical pixels.
155    pub top: i32,
156    /// The bottom edge in physical pixels.
157    pub bottom: i32,
158    /// The left edge in physical pixels.
159    pub left: i32,
160    /// The right edge in physical pixels.
161    pub right: i32,
162}
163
164impl PhysicalEdges {
165    /// Construct a new physical edges struct from the given border values, that are assumed to be
166    /// in the physical coordinate space.
167    pub const fn new(top: i32, bottom: i32, left: i32, right: i32) -> Self {
168        Self { top, bottom, left, right }
169    }
170
171    /// Convert a given logical edges to a physical edges by dividing the lengths by the
172    /// specified scale factor.
173    #[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    /// Convert the top logical edge to a physical edge by dividing the length by the
184    /// specified scale factor.
185    #[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    /// Convert the bottom logical edge to a physical edge by dividing the length by the
191    /// specified scale factor.
192    #[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    /// Convert the left logical edge to a physical edge by dividing the length by the
199    /// specified scale factor.
200    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    /// Convert the right logical edge to a physical edge by dividing the length by the
205    /// specified scale factor.
206    #[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}