1use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct DisplayId(pub u32);
14
15impl DisplayId {
16 pub const DEFAULT: Self = Self(1);
26}
27
28impl std::fmt::Display for DisplayId {
29 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 write!(f, "{}", self.0)
31 }
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
36pub struct LogicalPoint {
37 pub x: f64,
39 pub y: f64,
41}
42
43impl LogicalPoint {
44 pub const fn new(x: f64, y: f64) -> Self {
46 Self { x, y }
47 }
48
49 pub fn is_finite(&self) -> bool {
51 self.x.is_finite() && self.y.is_finite()
52 }
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
59#[serde(from = "[f64; 4]", into = "[f64; 4]")]
60pub struct LogicalRect {
61 pub x: f64,
63 pub y: f64,
65 pub width: f64,
67 pub height: f64,
69}
70
71impl LogicalRect {
72 pub const fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
74 Self {
75 x,
76 y,
77 width,
78 height,
79 }
80 }
81
82 pub fn is_valid(&self) -> bool {
87 self.x.is_finite()
88 && self.y.is_finite()
89 && self.width.is_finite()
90 && self.height.is_finite()
91 && self.width > 0.0
92 && self.height > 0.0
93 }
94
95 pub fn center(&self) -> LogicalPoint {
97 LogicalPoint::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
98 }
99
100 pub fn into_valid(self) -> Option<Self> {
107 self.is_valid().then_some(self)
108 }
109
110 pub fn containing(points: &[LogicalPoint]) -> Self {
116 let Some(first) = points.first() else {
117 return Self::new(0.0, 0.0, 1.0, 1.0);
118 };
119 let (mut min_x, mut min_y) = (first.x, first.y);
120 let (mut max_x, mut max_y) = (first.x, first.y);
121 for point in points {
122 min_x = min_x.min(point.x);
123 min_y = min_y.min(point.y);
124 max_x = max_x.max(point.x);
125 max_y = max_y.max(point.y);
126 }
127 Self::new(
128 min_x,
129 min_y,
130 (max_x - min_x).max(1.0),
131 (max_y - min_y).max(1.0),
132 )
133 }
134}
135
136impl From<[f64; 4]> for LogicalRect {
137 fn from([x, y, width, height]: [f64; 4]) -> Self {
138 Self::new(x, y, width, height)
139 }
140}
141
142impl From<LogicalRect> for [f64; 4] {
143 fn from(r: LogicalRect) -> Self {
144 [r.x, r.y, r.width, r.height]
145 }
146}
147
148#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
150pub struct DisplayInfo {
151 pub id: DisplayId,
153 pub scale: f64,
155 pub logical_size: [f64; 2],
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162
163 #[test]
164 fn a_bounding_rect_covers_every_point() {
165 let rect = LogicalRect::containing(&[
166 LogicalPoint::new(40.0, 90.0),
167 LogicalPoint::new(10.0, 200.0),
168 LogicalPoint::new(75.0, 55.0),
169 ]);
170 assert_eq!(rect, LogicalRect::new(10.0, 55.0, 65.0, 145.0));
171 assert!(rect.is_valid());
172 }
173
174 #[test]
176 fn a_bounding_rect_is_always_drawable() {
177 let flat = LogicalRect::containing(&[
178 LogicalPoint::new(10.0, 40.0),
179 LogicalPoint::new(90.0, 40.0),
180 ]);
181 assert!(flat.is_valid(), "a straight path came back with no height");
182 assert!(LogicalRect::containing(&[]).is_valid());
183 }
184
185 #[test]
186 fn rects_are_arrays_on_the_wire() {
187 let rect = LogicalRect::new(100.0, 200.0, 340.0, 90.0);
188 assert_eq!(
189 serde_json::to_string(&rect).unwrap(),
190 "[100.0,200.0,340.0,90.0]"
191 );
192 assert_eq!(
193 serde_json::from_str::<LogicalRect>("[100,200,340,90]").unwrap(),
194 rect
195 );
196 }
197
198 #[test]
199 fn zero_area_rects_are_invalid() {
200 assert!(LogicalRect::new(0.0, 0.0, 10.0, 10.0).is_valid());
201 assert!(!LogicalRect::new(0.0, 0.0, 0.0, 10.0).is_valid());
202 assert!(!LogicalRect::new(0.0, 0.0, 10.0, -1.0).is_valid());
203 assert!(!LogicalRect::new(f64::NAN, 0.0, 10.0, 10.0).is_valid());
204 }
205
206 #[test]
207 fn center_is_the_midpoint() {
208 let rect = LogicalRect::new(100.0, 200.0, 340.0, 90.0);
209 assert_eq!(rect.center(), LogicalPoint::new(270.0, 245.0));
210 }
211}