bevy_persistent_windows/
components.rs

1//! Components.
2
3use crate::prelude::*;
4
5/// Window state.
6#[derive(Clone, Component, Debug, Deserialize, PartialEq, Resource, Serialize)]
7#[serde(default)]
8pub struct WindowState {
9    /// Mode of the window.
10    pub mode: WindowMode,
11
12    /// Resolution of the window.
13    /// (`None` means pick the best resolution)
14    pub resolution: Option<(u32, u32)>,
15
16    /// Position of the window.
17    /// (`None` means centered)
18    pub position: Option<(i32, i32)>,
19
20    /// Scale of the window.
21    /// (`None` means pick the best scale)
22    pub scale: Option<f64>,
23
24    /// Whether the window scale should be set automatically at the beginning of the application.
25    pub(crate) auto_scaled: bool,
26
27    #[serde(skip)]
28    pub(crate) sync: bool,
29}
30
31impl WindowState {
32    /// Creates a borderless fullscreen state.
33    pub fn borderless_fullscreen() -> WindowState {
34        WindowState {
35            mode: WindowMode::BorderlessFullscreen(MonitorSelection::Primary),
36            resolution: None,
37            position: None,
38            scale: None,
39            auto_scaled: true,
40            sync: true,
41        }
42    }
43
44    /// Creates a fullscreen state.
45    pub fn fullscreen() -> WindowState {
46        WindowState {
47            mode: WindowMode::Fullscreen(MonitorSelection::Primary, VideoModeSelection::Current),
48            resolution: None,
49            position: None,
50            scale: None,
51            auto_scaled: true,
52            sync: true,
53        }
54    }
55
56    /// Creates a windowed state with given resolution.
57    pub fn windowed(width: u32, height: u32) -> WindowState {
58        WindowState {
59            mode: WindowMode::Windowed,
60            resolution: Some((width, height)),
61            position: None,
62            scale: None,
63            auto_scaled: true,
64            sync: true,
65        }
66    }
67}
68
69impl WindowState {
70    /// Adds position information to the state.
71    pub fn at(mut self, x: i32, y: i32) -> WindowState {
72        if self.mode == WindowMode::Windowed {
73            self.position = Some((x, y));
74        }
75        self
76    }
77
78    /// Adds scale information to the state.
79    pub fn scaled(mut self, scale: f64) -> WindowState {
80        self.scale = Some(scale);
81        self
82    }
83}
84
85impl Default for WindowState {
86    fn default() -> WindowState {
87        WindowState {
88            mode: WindowMode::BorderlessFullscreen(MonitorSelection::Primary),
89            resolution: None,
90            position: None,
91            scale: None,
92            auto_scaled: false,
93            sync: true,
94        }
95    }
96}