Skip to main content

nice_plug_core/editor/
dpi.rs

1pub use dpi::*;
2
3/// A size represented in the platform's native pixels.
4///
5/// This size is represented in physical pixels on Windows and Linux, and in logical pixels on macOS.
6///
7/// (This is a re-implementation of
8/// [`baseview::dpi::NativeSize`](https://github.com/RustAudio/baseview/blob/e95471e23d111d64db2e86e3b33ba619984bb96e/src/dpi.rs#L8)
9/// to avoid directly depending on `baseview` until it is stabilized.)
10#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
11pub struct NativeSize<P> {
12    pub width: P,
13    pub height: P,
14}
15
16impl<P> NativeSize<P> {
17    #[inline]
18    pub const fn new(width: P, height: P) -> Self {
19        NativeSize { width, height }
20    }
21}
22
23impl<P: Pixel> NativeSize<P> {
24    #[inline]
25    pub fn from_size(size: Size, scale_factor: f64) -> Self {
26        #[cfg(target_os = "macos")]
27        {
28            let size = size.to_logical(scale_factor);
29            Self {
30                width: size.width,
31                height: size.height,
32            }
33        }
34
35        #[cfg(not(target_os = "macos"))]
36        {
37            let size = size.to_physical(scale_factor);
38            Self {
39                width: size.width,
40                height: size.height,
41            }
42        }
43    }
44
45    #[inline]
46    pub fn from_logical_or_physical(
47        logical_size: LogicalSize<f64>,
48        physical_size: PhysicalSize<u32>,
49    ) -> Self {
50        #[cfg(target_os = "macos")]
51        {
52            let _ = physical_size;
53
54            Self {
55                width: logical_size.width.cast(),
56                height: logical_size.height.cast(),
57            }
58        }
59
60        #[cfg(not(target_os = "macos"))]
61        {
62            let _ = logical_size;
63
64            Self {
65                width: physical_size.width.cast(),
66                height: physical_size.height.cast(),
67            }
68        }
69    }
70
71    #[inline]
72    pub fn cast<X: Pixel>(&self) -> NativeSize<X> {
73        NativeSize {
74            width: self.width.cast(),
75            height: self.height.cast(),
76        }
77    }
78
79    #[inline]
80    pub fn to_physical(self, scale_factor: f64) -> PhysicalSize<P> {
81        #[cfg(target_os = "macos")]
82        {
83            let size = LogicalSize {
84                width: self.width,
85                height: self.height,
86            };
87            size.to_physical(scale_factor)
88        }
89        #[cfg(not(target_os = "macos"))]
90        {
91            let _ = scale_factor;
92            PhysicalSize {
93                width: self.width,
94                height: self.height,
95            }
96        }
97    }
98
99    #[inline]
100    pub fn to_logical(self, scale_factor: f64) -> LogicalSize<P> {
101        #[cfg(target_os = "macos")]
102        {
103            let _ = scale_factor;
104            LogicalSize {
105                width: self.width,
106                height: self.height,
107            }
108        }
109        #[cfg(not(target_os = "macos"))]
110        {
111            let size = PhysicalSize {
112                width: self.width,
113                height: self.height,
114            };
115            size.to_logical(scale_factor)
116        }
117    }
118}
119
120impl<P: Pixel> From<NativeSize<P>> for Size {
121    #[inline]
122    fn from(size: NativeSize<P>) -> Self {
123        #[cfg(target_os = "macos")]
124        {
125            Size::Logical(LogicalSize::new(size.width, size.height).cast())
126        }
127        #[cfg(not(target_os = "macos"))]
128        {
129            Size::Physical(PhysicalSize::new(size.width, size.height).cast())
130        }
131    }
132}