Skip to main content

baseview/
window_open_options.rs

1use crate::Size;
2
3#[cfg(feature = "opengl")]
4use crate::gl::GlConfig;
5
6/// The dpi scaling policy of the window
7#[derive(Default, Debug, Clone, Copy, PartialEq)]
8pub enum WindowScalePolicy {
9    /// Use the system's dpi scale factor
10    #[default]
11    SystemScaleFactor,
12    /// Use the given dpi scale factor (e.g. `1.0` = 96 dpi)
13    ScaleFactor(f64),
14}
15
16/// The options for opening a new window
17#[derive(Debug, Clone, PartialEq)]
18pub struct WindowOpenOptions {
19    pub title: String,
20
21    /// The logical size of the window
22    ///
23    /// These dimensions will be scaled by the scaling policy specified in `scale`. Mouse
24    /// position will be passed back as logical coordinates.
25    pub size: Size,
26
27    /// The dpi scaling policy
28    pub scale: WindowScalePolicy,
29
30    /// If provided, then an OpenGL context will be created for this window. You'll be able to
31    /// access this context through [crate::Window::gl_context].
32    ///
33    /// By default, this is set to `None`.
34    #[cfg(feature = "opengl")]
35    pub gl_config: Option<GlConfig>,
36}
37
38impl WindowOpenOptions {
39    #[inline]
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    #[inline]
45    pub fn with_title(mut self, title: impl Into<String>) -> Self {
46        self.title = title.into();
47        self
48    }
49
50    #[inline]
51    pub fn with_size(mut self, width: f64, height: f64) -> Self {
52        self.size = Size::new(width, height);
53        self
54    }
55
56    #[inline]
57    pub fn with_scale_policy(mut self, scale: WindowScalePolicy) -> Self {
58        self.scale = scale;
59        self
60    }
61
62    #[cfg(feature = "opengl")]
63    #[inline]
64    pub fn with_gl_config(mut self, gl_config: impl Into<Option<GlConfig>>) -> Self {
65        self.gl_config = gl_config.into();
66        self
67    }
68}
69
70impl Default for WindowOpenOptions {
71    fn default() -> Self {
72        Self {
73            title: String::from("baseview window"),
74            size: Size { width: 500.0, height: 400.0 },
75            scale: WindowScalePolicy::default(),
76            #[cfg(feature = "opengl")]
77            gl_config: None,
78        }
79    }
80}