winio_primitive/
monitor.rs

1use crate::{Point, Rect, Size};
2
3/// Represents the geometry of a monitor.
4#[derive(Debug, Clone, PartialEq)]
5pub struct Monitor {
6    region: Rect,
7    client: Rect,
8    dpi: Size,
9}
10
11impl Monitor {
12    /// Create a monitor geometry.
13    pub fn new(region: Rect, client: Rect, dpi: Size) -> Self {
14        Self {
15            region,
16            client,
17            dpi,
18        }
19    }
20
21    /// The physical region.
22    pub fn region(&self) -> Rect {
23        self.region
24    }
25
26    /// The client region.
27    pub fn client(&self) -> Rect {
28        self.client
29    }
30
31    /// Dpi of the monitor, 1.0 if no scale. You should take it into
32    /// consideration when setting the location of windows.
33    /// See [`Monitor::region_scaled`] & [`Monitor::client_scaled`].
34    pub fn dpi(&self) -> Size {
35        self.dpi
36    }
37
38    /// Scaled physical region.
39    pub fn region_scaled(&self) -> Rect {
40        div_rect(self.region, self.dpi)
41    }
42
43    /// Scaled client region.
44    pub fn client_scaled(&self) -> Rect {
45        div_rect(self.client, self.dpi)
46    }
47}
48
49#[inline]
50fn div_rect(r: Rect, s: Size) -> Rect {
51    Rect::new(div_point(r.origin, s), div_size(r.size, s))
52}
53
54#[inline]
55fn div_point(p: Point, s: Size) -> Point {
56    Point::new(p.x / s.width, p.y / s.height)
57}
58
59#[inline]
60fn div_size(s1: Size, s2: Size) -> Size {
61    Size::new(s1.width / s2.width, s1.height / s2.height)
62}