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 ItemTransform = euclid::Transform2D<f32, LogicalPx, LogicalPx>;
20
21pub type ScaleFactor = euclid::Scale<f32, LogicalPx, PhysicalPx>;
22
23pub trait SizeLengths {
24 type LengthType;
25 fn width_length(&self) -> Self::LengthType;
26 fn height_length(&self) -> Self::LengthType;
27}
28
29impl<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
39pub trait PointLengths {
40 type LengthType;
41 fn x_length(&self) -> Self::LengthType;
42 fn y_length(&self) -> Self::LengthType;
43}
44
45impl<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
55impl<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
65pub 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
73impl<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)
89pub fn logical_size_from_api(size: crate::api::LogicalSize) -> LogicalSize {
90 size.to_euclid()
91}
92
93pub fn logical_point_from_api(position: crate::api::LogicalPosition) -> LogicalPoint {
94 position.to_euclid()
95}
96
97pub fn logical_position_to_api(pos: LogicalPoint) -> crate::api::LogicalPosition {
98 crate::api::LogicalPosition::from_euclid(pos)
99}
100
101pub fn logical_size_to_api(size: LogicalSize) -> crate::api::LogicalSize {
102 crate::api::LogicalSize::from_euclid(size)
103}
104