1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! Monitor related stuff!!
macro_rules! dpi_vec2_impl {
($($t_ident:ident ($m1:ident, $m2:ident) $name:literal),* $(,)?) => {
$(
// Type definition
document!(
concat!("Represents an unscaled logical or physical ", $name, "."),
#[derive(Copy, Clone)]
pub enum $t_ident {
#[doc = "Logical"] #[doc = $name] #[doc = "that is scalable to monitor DPI."]
Logical(f64, f64),
#[doc = "Physical"] #[doc = $name] #[doc = "in absolute values regardless of DPI."]
Physical(u32, u32),
}
);
// Implementations
impl $t_ident {
document!(
concat!(
"Constructs a logical ", $name, " from a given ",
stringify!($m1), " and ", stringify!($m2), "."
),
pub const fn logical($m1: f64, $m2: f64) -> Self {
Self::Logical($m1, $m2)
}
);
document!(
concat!(
"Constructs a physical ", $name, " from a given ",
stringify!($m1), " and ", stringify!($m2), "."
),
pub const fn physical($m1: u32, $m2: u32) -> Self {
Self::Physical($m1, $m2)
}
);
document!(
concat!(
"Gets `self` as a logical ", stringify!($m1), " and ", stringify!($m2),
", upscaled with the given factor.\n\n",
"If `self` is already logical, no upscaling is done."
),
#[inline]
pub fn get_logical(self, scale: f64) -> (f64, f64) {
// NOTE: `const fn` doesn't have floating point arithmetic yet.
match self {
Self::Logical($m1, $m2) => ($m1, $m2),
Self::Physical($m1, $m2) => ($m1 as f64 * scale, $m2 as f64 * scale),
}
}
);
document!(
concat!(
"Gets `self` as a physical ", stringify!($m1), " and ", stringify!($m2),
", downscaled with the given factor.\n\n",
"If `self` is already physical, no downscaling is done."
),
#[inline]
pub fn get_physical(self, scale: f64) -> (u32, u32) {
// NOTE: `const fn` doesn't have floating point arithmetic yet.
match self {
Self::Logical($m1, $m2) => (($m1 / scale) as u32, ($m2 / scale) as u32),
Self::Physical($m1, $m2) => ($m1, $m2),
}
}
);
document!(
concat!(
"Converts `self` to a logical ", $name,
", upscaled with the given factor.\n\n",
"If `self` is already logical, no upscaling is done."
),
#[inline]
pub fn to_logical(self, scale: f64) -> Self {
// NOTE: `const fn` doesn't have floating point arithmetic yet.
let ($m1, $m2) = self.get_logical(scale);
Self::Logical($m1, $m2)
}
);
document!(
concat!(
"Converts `self` to a physical ", $name,
", downscaled with the given factor.\n\n",
"If `self` is already physical, no downscaling is done."
),
#[inline]
pub fn to_physical(self, scale: f64) -> Self {
// NOTE: `const fn` doesn't have floating point arithmetic yet.
let ($m1, $m2) = self.get_physical(scale);
Self::Physical($m1, $m2)
}
);
// Private implementations
pub(crate) fn scale_if_logical(self, scale: f64) -> (f64, f64) {
match self {
Self::Logical($m1, $m2) => ($m1 * scale, $m2 * scale),
Self::Physical($m1, $m2) => ($m1 as f64, $m2 as f64),
}
}
}
)*
};
}
// This is where the magic happens.
dpi_vec2_impl! {
Point(x, y) "point",
Size(width, height) "size",
}