Skip to main content

baseview/
dpi.rs

1use crate::WindowSize;
2pub use dpi::*;
3
4/// A size represented in the platform's native pixels.
5///
6/// This size is represented in physical pixels on Windows and Linux, and in logical pixels on macOS.
7#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
8pub struct NativeSize<P> {
9    pub width: P,
10    pub height: P,
11}
12
13#[cfg(target_os = "macos")]
14const NATIVE_IS_LOGICAL: bool = true;
15#[cfg(not(target_os = "macos"))]
16const NATIVE_IS_LOGICAL: bool = false;
17
18impl<P> NativeSize<P> {
19    #[inline]
20    pub const fn new(width: P, height: P) -> Self {
21        NativeSize { width, height }
22    }
23
24    /// This is `true` if the platform's native size is represented in logical pixels, and `false` otherwise.
25    ///
26    /// This is `true` on macOS, and `false` on Windows and Linux.
27    pub const IS_LOGICAL: bool = NATIVE_IS_LOGICAL;
28}
29
30impl<P: Pixel> NativeSize<P> {
31    #[inline]
32    pub fn from_size(size: Size, scale_factor: f64) -> Self {
33        #[cfg(target_os = "macos")]
34        {
35            let size = size.to_logical(scale_factor);
36            Self { width: size.width, height: size.height }
37        }
38
39        #[cfg(not(target_os = "macos"))]
40        {
41            let size = size.to_physical(scale_factor);
42            Self { width: size.width, height: size.height }
43        }
44    }
45
46    #[inline]
47    pub fn cast<X: Pixel>(&self) -> NativeSize<X> {
48        NativeSize { width: self.width.cast(), height: self.height.cast() }
49    }
50
51    #[inline]
52    pub fn to_physical(self, scale_factor: f64) -> PhysicalSize<P> {
53        #[cfg(target_os = "macos")]
54        {
55            let size = LogicalSize { width: self.width, height: self.height };
56            size.to_physical(scale_factor)
57        }
58        #[cfg(not(target_os = "macos"))]
59        {
60            let _ = scale_factor;
61            PhysicalSize { width: self.width, height: self.height }
62        }
63    }
64
65    #[inline]
66    pub fn to_logical(self, scale_factor: f64) -> LogicalSize<P> {
67        #[cfg(target_os = "macos")]
68        {
69            let _ = scale_factor;
70            LogicalSize { width: self.width, height: self.height }
71        }
72        #[cfg(not(target_os = "macos"))]
73        {
74            let size = PhysicalSize { width: self.width, height: self.height };
75            size.to_logical(scale_factor)
76        }
77    }
78}
79
80impl<P: Pixel> From<WindowSize> for NativeSize<P> {
81    #[inline]
82    fn from(size: WindowSize) -> Self {
83        #[cfg(target_os = "macos")]
84        {
85            let size = size.logical.cast();
86            Self { width: size.width, height: size.height }
87        }
88
89        #[cfg(not(target_os = "macos"))]
90        {
91            let size = size.physical.cast();
92            Self { width: size.width, height: size.height }
93        }
94    }
95}
96
97impl<P: Pixel> From<NativeSize<P>> for Size {
98    #[inline]
99    fn from(size: NativeSize<P>) -> Self {
100        #[cfg(target_os = "macos")]
101        {
102            Size::Logical(LogicalSize::new(size.width, size.height).cast())
103        }
104        #[cfg(not(target_os = "macos"))]
105        {
106            Size::Physical(PhysicalSize::new(size.width, size.height).cast())
107        }
108    }
109}