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 | |
4 | use crate::BorderRadius; |
5 | use crate::Coord; |
6 | /// This type is used as a tagging type for use with [`euclid::Scale`] to convert |
7 | /// between physical and logical pixels. |
8 | pub 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. |
12 | pub struct LogicalPx; |
13 | pub type LogicalLength = euclid::Length<Coord, LogicalPx>; |
14 | pub type LogicalRect = euclid::Rect<Coord, LogicalPx>; |
15 | pub type LogicalPoint = euclid::Point2D<Coord, LogicalPx>; |
16 | pub type LogicalSize = euclid::Size2D<Coord, LogicalPx>; |
17 | pub type LogicalVector = euclid::Vector2D<Coord, LogicalPx>; |
18 | pub type LogicalBorderRadius = BorderRadius<Coord, LogicalPx>; |
19 | pub type ItemTransform = euclid::Transform2D<f32, LogicalPx, LogicalPx>; |
20 | |
21 | pub type ScaleFactor = euclid::Scale<f32, LogicalPx, PhysicalPx>; |
22 | |
23 | pub trait SizeLengths { |
24 | type LengthType; |
25 | fn width_length(&self) -> Self::LengthType; |
26 | fn height_length(&self) -> Self::LengthType; |
27 | } |
28 | |
29 | impl<T: Copy, U> SizeLengths for euclid::Size2D<T, U> { |
30 | type LengthType = euclid::Length<T, U>; |
31 | fn width_length(&self) -> Self::LengthType { |
32 | euclid::Length::new(self.width) |
33 | } |
34 | fn height_length(&self) -> Self::LengthType { |
35 | euclid::Length::new(self.height) |
36 | } |
37 | } |
38 | |
39 | pub trait PointLengths { |
40 | type LengthType; |
41 | fn x_length(&self) -> Self::LengthType; |
42 | fn y_length(&self) -> Self::LengthType; |
43 | } |
44 | |
45 | impl<T: Copy, U> PointLengths for euclid::Point2D<T, U> { |
46 | type LengthType = euclid::Length<T, U>; |
47 | fn x_length(&self) -> Self::LengthType { |
48 | euclid::Length::new(self.x) |
49 | } |
50 | fn y_length(&self) -> Self::LengthType { |
51 | euclid::Length::new(self.y) |
52 | } |
53 | } |
54 | |
55 | impl<T: Copy, U> PointLengths for euclid::Vector2D<T, U> { |
56 | type LengthType = euclid::Length<T, U>; |
57 | fn x_length(&self) -> Self::LengthType { |
58 | euclid::Length::new(self.x) |
59 | } |
60 | fn y_length(&self) -> Self::LengthType { |
61 | euclid::Length::new(self.y) |
62 | } |
63 | } |
64 | |
65 | pub trait RectLengths { |
66 | type SizeType; |
67 | type LengthType; |
68 | fn size_length(&self) -> Self::SizeType; |
69 | fn width_length(&self) -> Self::LengthType; |
70 | fn height_length(&self) -> Self::LengthType; |
71 | } |
72 | |
73 | impl<T: Copy, U> RectLengths for euclid::Rect<T, U> { |
74 | type LengthType = euclid::Length<T, U>; |
75 | type SizeType = euclid::Size2D<T, U>; |
76 | fn size_length(&self) -> Self::SizeType { |
77 | euclid::Size2D::new(self.size.width, self.size.height) |
78 | } |
79 | fn width_length(&self) -> Self::LengthType { |
80 | self.size_length().width_length() |
81 | } |
82 | fn height_length(&self) -> Self::LengthType { |
83 | self.size_length().height_length() |
84 | } |
85 | } |
86 | |
87 | /// Convert from the api size to the internal size |
88 | /// (This doesn't use the `From` trait because it would expose the conversion to euclid in the public API) |
89 | pub fn logical_size_from_api(size: crate::api::LogicalSize) -> LogicalSize { |
90 | size.to_euclid() |
91 | } |
92 | |
93 | pub fn logical_point_from_api(position: crate::api::LogicalPosition) -> LogicalPoint { |
94 | position.to_euclid() |
95 | } |
96 | |
97 | pub fn logical_position_to_api(pos: LogicalPoint) -> crate::api::LogicalPosition { |
98 | crate::api::LogicalPosition::from_euclid(pos) |
99 | } |
100 | |
101 | pub fn logical_size_to_api(size: LogicalSize) -> crate::api::LogicalSize { |
102 | crate::api::LogicalSize::from_euclid(size) |
103 | } |
104 | |