Skip to main content

baseview/
window_info.rs

1/// The info about the window
2#[derive(Debug, Copy, Clone)]
3pub struct WindowInfo {
4    logical_size: Size,
5    physical_size: PhySize,
6    scale: f64,
7    scale_recip: f64,
8}
9
10impl WindowInfo {
11    pub fn from_logical_size(logical_size: Size, scale: f64) -> Self {
12        let scale_recip = if scale == 1.0 { 1.0 } else { 1.0 / scale };
13
14        let physical_size = PhySize {
15            width: (logical_size.width * scale).round() as u32,
16            height: (logical_size.height * scale).round() as u32,
17        };
18
19        Self { logical_size, physical_size, scale, scale_recip }
20    }
21
22    pub fn from_physical_size(physical_size: PhySize, scale: f64) -> Self {
23        let scale_recip = if scale == 1.0 { 1.0 } else { 1.0 / scale };
24
25        let logical_size = Size {
26            width: f64::from(physical_size.width) * scale_recip,
27            height: f64::from(physical_size.height) * scale_recip,
28        };
29
30        Self { logical_size, physical_size, scale, scale_recip }
31    }
32
33    /// The logical size of the window
34    pub fn logical_size(&self) -> Size {
35        self.logical_size
36    }
37
38    /// The physical size of the window
39    pub fn physical_size(&self) -> PhySize {
40        self.physical_size
41    }
42
43    /// The scale factor of the window
44    pub fn scale(&self) -> f64 {
45        self.scale
46    }
47
48    /// The reciprocal of the scale factor of the window
49    pub fn scale_recip(&self) -> f64 {
50        self.scale_recip
51    }
52}
53
54/// A point in logical coordinates
55#[derive(Debug, Copy, Clone, PartialEq)]
56pub struct Point {
57    pub x: f64,
58    pub y: f64,
59}
60
61impl Point {
62    /// Create a new point in logical coordinates
63    pub fn new(x: f64, y: f64) -> Self {
64        Self { x, y }
65    }
66
67    /// Convert to actual physical coordinates
68    #[inline]
69    pub fn to_physical(&self, window_info: &WindowInfo) -> PhyPoint {
70        PhyPoint {
71            x: (self.x * window_info.scale()).round() as i32,
72            y: (self.y * window_info.scale()).round() as i32,
73        }
74    }
75}
76
77/// A point in actual physical coordinates
78#[derive(Debug, Copy, Clone, PartialEq)]
79pub struct PhyPoint {
80    pub x: i32,
81    pub y: i32,
82}
83
84impl PhyPoint {
85    /// Create a new point in actual physical coordinates
86    pub fn new(x: i32, y: i32) -> Self {
87        Self { x, y }
88    }
89
90    /// Convert to logical coordinates
91    #[inline]
92    pub fn to_logical(&self, window_info: &WindowInfo) -> Point {
93        Point {
94            x: f64::from(self.x) * window_info.scale_recip(),
95            y: f64::from(self.y) * window_info.scale_recip(),
96        }
97    }
98}
99
100/// A size in logical coordinates
101#[derive(Debug, Copy, Clone, PartialEq)]
102pub struct Size {
103    pub width: f64,
104    pub height: f64,
105}
106
107impl Size {
108    /// Create a new size in logical coordinates
109    pub fn new(width: f64, height: f64) -> Self {
110        Self { width, height }
111    }
112
113    /// Convert to actual physical size
114    #[inline]
115    pub fn to_physical(&self, window_info: &WindowInfo) -> PhySize {
116        PhySize {
117            width: (self.width * window_info.scale()).round() as u32,
118            height: (self.height * window_info.scale()).round() as u32,
119        }
120    }
121}
122
123/// An actual size in physical coordinates
124#[derive(Debug, Copy, Clone, PartialEq)]
125pub struct PhySize {
126    pub width: u32,
127    pub height: u32,
128}
129
130impl PhySize {
131    /// Create a new size in actual physical coordinates
132    pub fn new(width: u32, height: u32) -> Self {
133        Self { width, height }
134    }
135
136    /// Convert to logical size
137    #[inline]
138    pub fn to_logical(&self, window_info: &WindowInfo) -> Size {
139        Size {
140            width: f64::from(self.width) * window_info.scale_recip(),
141            height: f64::from(self.height) * window_info.scale_recip(),
142        }
143    }
144}