1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Components.

use crate::prelude::*;

/// Window state.
#[derive(Clone, Component, Debug, Deserialize, Eq, PartialEq, Resource, Serialize)]
#[serde(default)]
pub struct WindowState {
    /// Mode of the window.
    pub mode: WindowMode,

    /// Name of the monitor that the window is in.
    /// (`None` means pick the best monitor)
    pub monitor: Option<String>,

    /// Resolution of the window.
    /// (`None` means pick the best resolution)
    pub resolution: Option<(u32, u32)>,

    /// Position of the window.
    /// (`None` means centered)
    pub position: Option<(i32, i32)>,

    #[serde(skip)]
    pub(crate) sync: bool,
}

impl WindowState {
    /// Creates a fullscreen state.
    pub fn fullscreen() -> WindowState {
        WindowState {
            mode: WindowMode::Fullscreen,
            monitor: None,
            resolution: None,
            position: None,
            sync: true,
        }
    }

    /// Creates a windowed state with given resolution.
    pub fn windowed(width: u32, height: u32) -> WindowState {
        WindowState {
            mode: WindowMode::Windowed,
            monitor: None,
            resolution: Some((width, height)),
            position: None,
            sync: true,
        }
    }
}

impl WindowState {
    /// Adds position information to the state.
    pub fn at(mut self, x: i32, y: i32) -> WindowState {
        if self.mode == WindowMode::Windowed {
            self.position = Some((x, y));
        }
        self
    }
}

impl Default for WindowState {
    fn default() -> WindowState {
        WindowState::fullscreen()
    }
}