arin_protocol/geom.rs
1//! Logical geometry.
2//!
3//! Everything here is in logical points and is meaningless without the [`DisplayId`] it
4//! travels with. Retina screenshots come back at 2x while the overlay draws in points,
5//! and mixed-DPI multi-monitor setups make implicit conversion unrecoverable. Clients
6//! working from a screenshot divide by the scale reported in an ack before sending.
7
8use serde::{Deserialize, Serialize};
9
10/// Identifies a display. Stable for the lifetime of a session.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct DisplayId(pub u32);
14
15impl DisplayId {
16 /// What a client uses when the user did not name a display.
17 ///
18 /// Not part of the wire contract, and the daemon never substitutes it: every
19 /// positioned message still carries an explicit `display_id`, because a mixed-DPI
20 /// setup makes an implicit display unrecoverable. This is only what a client fills in
21 /// on the way out, so it lives here rather than being spelled `1` in each of them.
22 ///
23 /// The id macOS gives the main display, which is `1` in practice rather than by
24 /// guarantee. `arin displays` lists what a machine actually has.
25 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/// A point in logical coordinates, relative to the origin of its display.
35#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
36pub struct LogicalPoint {
37 /// Horizontal offset in logical points.
38 pub x: f64,
39 /// Vertical offset in logical points.
40 pub y: f64,
41}
42
43impl LogicalPoint {
44 /// Construct a point.
45 pub const fn new(x: f64, y: f64) -> Self {
46 Self { x, y }
47 }
48
49 /// Whether both components are finite.
50 pub fn is_finite(&self) -> bool {
51 self.x.is_finite() && self.y.is_finite()
52 }
53}
54
55/// A rectangle in logical coordinates.
56///
57/// Serialized as `[x, y, width, height]`, not as an object.
58#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
59#[serde(from = "[f64; 4]", into = "[f64; 4]")]
60pub struct LogicalRect {
61 /// Left edge.
62 pub x: f64,
63 /// Top edge.
64 pub y: f64,
65 /// Extent along x. Must be positive.
66 pub width: f64,
67 /// Extent along y. Must be positive.
68 pub height: f64,
69}
70
71impl LogicalRect {
72 /// Construct a rectangle.
73 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 /// Whether every component is finite and the extents are positive.
83 ///
84 /// Zero-area rects are rejected: they render as nothing, so they are always a bug in
85 /// the caller rather than an intentional annotation.
86 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 /// The centre of the rectangle.
96 pub fn center(&self) -> LogicalPoint {
97 LogicalPoint::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
98 }
99
100 /// This rectangle if it is drawable, `None` if it is not.
101 ///
102 /// For the callers that treat a region as an optional refinement rather than the
103 /// answer. Something derived from a measurement, a model's guess, or arithmetic on
104 /// either can come out degenerate, and dropping it is better than propagating a rect
105 /// that renders as nothing.
106 pub fn into_valid(self) -> Option<Self> {
107 self.is_valid().then_some(self)
108 }
109}
110
111impl From<[f64; 4]> for LogicalRect {
112 fn from([x, y, width, height]: [f64; 4]) -> Self {
113 Self::new(x, y, width, height)
114 }
115}
116
117impl From<LogicalRect> for [f64; 4] {
118 fn from(r: LogicalRect) -> Self {
119 [r.x, r.y, r.width, r.height]
120 }
121}
122
123/// Display metadata, returned on acks so clients can convert screenshot pixels.
124#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
125pub struct DisplayInfo {
126 /// The display this describes.
127 pub id: DisplayId,
128 /// Backing scale factor. 2.0 on a Retina panel.
129 pub scale: f64,
130 /// Display size in logical points, as `[width, height]`.
131 pub logical_size: [f64; 2],
132}
133
134#[cfg(test)]
135mod tests {
136 use super::*;
137
138 #[test]
139 fn rects_are_arrays_on_the_wire() {
140 let rect = LogicalRect::new(100.0, 200.0, 340.0, 90.0);
141 assert_eq!(
142 serde_json::to_string(&rect).unwrap(),
143 "[100.0,200.0,340.0,90.0]"
144 );
145 assert_eq!(
146 serde_json::from_str::<LogicalRect>("[100,200,340,90]").unwrap(),
147 rect
148 );
149 }
150
151 #[test]
152 fn zero_area_rects_are_invalid() {
153 assert!(LogicalRect::new(0.0, 0.0, 10.0, 10.0).is_valid());
154 assert!(!LogicalRect::new(0.0, 0.0, 0.0, 10.0).is_valid());
155 assert!(!LogicalRect::new(0.0, 0.0, 10.0, -1.0).is_valid());
156 assert!(!LogicalRect::new(f64::NAN, 0.0, 10.0, 10.0).is_valid());
157 }
158
159 #[test]
160 fn center_is_the_midpoint() {
161 let rect = LogicalRect::new(100.0, 200.0, 340.0, 90.0);
162 assert_eq!(rect.center(), LogicalPoint::new(270.0, 245.0));
163 }
164}