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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use super::{
  data::{CursorMode, Flow, Fullscreen, LogicalSize, Position, Size, Theme, Visibility},
  Window,
};
use crate::error::WindowError;

/// Optional onfiguration for the window to be built.
#[derive(Debug, Clone)]
pub struct WindowSettings {
  pub flow: Flow,
  pub theme: Theme,
  pub visibility: Visibility,
  pub decorations: Visibility,
  pub resizeable: bool,
  pub fullscreen: Option<Fullscreen>,
  pub cursor_mode: CursorMode,
  pub close_on_x: bool,
}

impl Default for WindowSettings {
  fn default() -> Self {
    let flow = Flow::default();
    let theme = Theme::default();
    let fullscreen = None;
    let cursor_mode = CursorMode::default();
    let visibility = Visibility::default();
    let decorations = Visibility::default();
    let resizeable = true;
    let close_on_x = true;

    Self {
      flow,
      theme,
      visibility,
      decorations,
      close_on_x,
      fullscreen,
      resizeable,
      cursor_mode,
    }
  }
}

impl WindowSettings {
  pub fn with_flow(mut self, flow: Flow) -> Self {
    self.flow = flow;
    self
  }

  pub fn with_theme(mut self, theme: Theme) -> Self {
    self.theme = theme;
    self
  }

  pub fn with_visibility(mut self, visibility: Visibility) -> Self {
    self.visibility = visibility;
    self
  }

  pub fn with_decorations(mut self, visibility: Visibility) -> Self {
    self.decorations = visibility;
    self
  }

  pub fn with_fullscreen(mut self, fullscreen: Option<Fullscreen>) -> Self {
    self.fullscreen = fullscreen;

    self
  }

  pub fn with_cursor_mode(mut self, cursor_mode: CursorMode) -> Self {
    self.cursor_mode = cursor_mode;
    self
  }

  pub fn with_close_on_x(mut self, close_on_x: bool) -> Self {
    self.close_on_x = close_on_x;
    self
  }

  pub fn with_resizeable(mut self, resizeable: bool) -> Self {
    self.resizeable = resizeable;
    self
  }
}

pub struct WindowBuilder {
  title: String,
  size: Size,
  position: Option<Position>,
  settings: WindowSettings,
}

impl Default for WindowBuilder {
  fn default() -> Self {
    Self {
      title: "Window".into(),
      size: LogicalSize::new(800.0, 500.0).into(),
      position: None,
      settings: WindowSettings::default(),
    }
  }
}

impl From<WindowSettings> for WindowBuilder {
  fn from(settings: WindowSettings) -> Self {
    Self {
      settings,
      ..Default::default()
    }
  }
}

impl WindowBuilder {
  pub fn new() -> Self {
    Self::default()
  }

  pub fn with_title(mut self, title: impl Into<String>) -> Self {
    self.title = title.into();
    self
  }

  /// Relative to the whole window frame, not just the client area
  pub fn with_size(mut self, size: impl Into<Size>) -> Self {
    self.size = size.into();
    self
  }

  pub fn with_position(mut self, position: impl Into<Option<Position>>) -> Self {
    self.position = position.into();
    self
  }

  pub fn with_flow(mut self, flow: Flow) -> Self {
    self.settings = self.settings.with_flow(flow);
    self
  }

  pub fn with_theme(mut self, theme: Theme) -> Self {
    self.settings = self.settings.with_theme(theme);
    self
  }

  pub fn with_visibility(mut self, visibility: Visibility) -> Self {
    self.settings = self.settings.with_visibility(visibility);
    self
  }

  pub fn with_decorations(mut self, visibility: Visibility) -> Self {
    self.settings = self.settings.with_decorations(visibility);
    self
  }

  pub fn with_fullscreen(mut self, fullscreen: Option<Fullscreen>) -> Self {
    self.settings = self.settings.with_fullscreen(fullscreen);

    self
  }

  pub fn with_cursor_mode(mut self, cursor_mode: CursorMode) -> Self {
    self.settings = self.settings.with_cursor_mode(cursor_mode);
    self
  }

  pub fn with_close_on_x(mut self, close_on_x: bool) -> Self {
    self.settings = self.settings.with_close_on_x(close_on_x);
    self
  }

  pub fn with_resizeable(mut self, resizeable: bool) -> Self {
    self.settings = self.settings.with_resizeable(resizeable);
    self
  }

  pub fn build(self) -> Result<Window, WindowError> {
    Window::new(self.title, self.size, self.position, self.settings)
  }
}