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    /// The typed horizontal range, unlike euclid's unitless [`euclid::Rect::x_range`].
73    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
93/// Convert from the api size to the internal size
94/// (This doesn't use the `From` trait because it would expose the conversion to euclid in the public API)
95pub 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/// Four distances from the edges of a rectangle represented in the coordinate space of logical pixels.
112#[derive(Debug, Default, Copy, Clone, PartialEq)]
113#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
114#[repr(C)]
115pub struct LogicalEdges {
116    /// The top distance in logical pixels.
117    pub top: f32,
118    /// The bottom distance in logical pixels.
119    pub bottom: f32,
120    /// The left distance in logical pixels.
121    pub left: f32,
122    /// The right distance in logical pixels.
123    pub right: f32,
124}
125
126impl LogicalEdges {
127    /// Construct a new logical edges struct from the given border values, that are assumed to be
128    /// in the logical coordinate space.
129    pub const fn new(top: f32, bottom: f32, left: f32, right: f32) -> Self {
130        Self { top, bottom, left, right }
131    }
132
133    /// Converts the top edge to logical pixels.
134    #[inline]
135    pub const fn top(&self) -> LogicalLength {
136        LogicalLength::new(self.top as crate::Coord)
137    }
138    /// Converts the bottom edge to logical pixels.
139    #[inline]
140    pub const fn bottom(&self) -> LogicalLength {
141        LogicalLength::new(self.bottom as crate::Coord)
142    }
143    /// Converts the left edge to logical pixels.
144    #[inline]
145    pub const fn left(&self) -> LogicalLength {
146        LogicalLength::new(self.left as crate::Coord)
147    }
148    /// Converts the right edge to logical pixels.
149    #[inline]
150    pub const fn right(&self) -> LogicalLength {
151        LogicalLength::new(self.right as crate::Coord)
152    }
153}
154
155/// Four distances from the edges of a rectangle represented in the coordinate space of physical pixels.
156#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
157#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
158pub struct PhysicalEdges {
159    /// The top edge in physical pixels.
160    pub top: i32,
161    /// The bottom edge in physical pixels.
162    pub bottom: i32,
163    /// The left edge in physical pixels.
164    pub left: i32,
165    /// The right edge in physical pixels.
166    pub right: i32,
167}
168
169impl PhysicalEdges {
170    /// Construct a new physical edges struct from the given border values, that are assumed to be
171    /// in the physical coordinate space.
172    pub const fn new(top: i32, bottom: i32, left: i32, right: i32) -> Self {
173        Self { top, bottom, left, right }
174    }
175
176    /// Convert a given logical edges to a physical edges by dividing the lengths by the
177    /// specified scale factor.
178    #[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    /// Convert the top logical edge to a physical edge by dividing the length by the
189    /// specified scale factor.
190    #[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    /// Convert the bottom logical edge to a physical edge by dividing the length by the
196    /// specified scale factor.
197    #[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    /// Convert the left logical edge to a physical edge by dividing the length by the
204    /// specified scale factor.
205    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    /// Convert the right logical edge to a physical edge by dividing the length by the
210    /// specified scale factor.
211    #[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}