witer/window/
settings.rs

1use super::{
2  data::{CursorMode, Flow, Fullscreen, LogicalSize, Position, Size, Theme, Visibility},
3  Window,
4};
5use crate::error::WindowError;
6
7/// Optional onfiguration for the window to be built.
8#[derive(Debug, Clone)]
9pub struct WindowSettings {
10  pub flow: Flow,
11  pub theme: Theme,
12  pub visibility: Visibility,
13  pub decorations: Visibility,
14  pub resizeable: bool,
15  pub fullscreen: Option<Fullscreen>,
16  pub cursor_mode: CursorMode,
17  pub close_on_x: bool,
18}
19
20impl Default for WindowSettings {
21  fn default() -> Self {
22    let flow = Flow::default();
23    let theme = Theme::default();
24    let fullscreen = None;
25    let cursor_mode = CursorMode::default();
26    let visibility = Visibility::default();
27    let decorations = Visibility::default();
28    let resizeable = true;
29    let close_on_x = true;
30
31    Self {
32      flow,
33      theme,
34      visibility,
35      decorations,
36      close_on_x,
37      fullscreen,
38      resizeable,
39      cursor_mode,
40    }
41  }
42}
43
44impl WindowSettings {
45  pub fn with_flow(mut self, flow: Flow) -> Self {
46    self.flow = flow;
47    self
48  }
49
50  pub fn with_theme(mut self, theme: Theme) -> Self {
51    self.theme = theme;
52    self
53  }
54
55  pub fn with_visibility(mut self, visibility: Visibility) -> Self {
56    self.visibility = visibility;
57    self
58  }
59
60  pub fn with_decorations(mut self, visibility: Visibility) -> Self {
61    self.decorations = visibility;
62    self
63  }
64
65  pub fn with_fullscreen(mut self, fullscreen: Option<Fullscreen>) -> Self {
66    self.fullscreen = fullscreen;
67
68    self
69  }
70
71  pub fn with_cursor_mode(mut self, cursor_mode: CursorMode) -> Self {
72    self.cursor_mode = cursor_mode;
73    self
74  }
75
76  pub fn with_close_on_x(mut self, close_on_x: bool) -> Self {
77    self.close_on_x = close_on_x;
78    self
79  }
80
81  pub fn with_resizeable(mut self, resizeable: bool) -> Self {
82    self.resizeable = resizeable;
83    self
84  }
85}
86
87pub struct WindowBuilder {
88  title: String,
89  size: Size,
90  position: Option<Position>,
91  settings: WindowSettings,
92}
93
94impl Default for WindowBuilder {
95  fn default() -> Self {
96    Self {
97      title: "Window".into(),
98      size: LogicalSize::new(800.0, 500.0).into(),
99      position: None,
100      settings: WindowSettings::default(),
101    }
102  }
103}
104
105impl From<WindowSettings> for WindowBuilder {
106  fn from(settings: WindowSettings) -> Self {
107    Self {
108      settings,
109      ..Default::default()
110    }
111  }
112}
113
114impl WindowBuilder {
115  pub fn new() -> Self {
116    Self::default()
117  }
118
119  pub fn with_title(mut self, title: impl Into<String>) -> Self {
120    self.title = title.into();
121    self
122  }
123
124  /// Relative to the whole window frame, not just the client area
125  pub fn with_size(mut self, size: impl Into<Size>) -> Self {
126    self.size = size.into();
127    self
128  }
129
130  pub fn with_position(mut self, position: impl Into<Option<Position>>) -> Self {
131    self.position = position.into();
132    self
133  }
134
135  pub fn with_flow(mut self, flow: Flow) -> Self {
136    self.settings = self.settings.with_flow(flow);
137    self
138  }
139
140  pub fn with_theme(mut self, theme: Theme) -> Self {
141    self.settings = self.settings.with_theme(theme);
142    self
143  }
144
145  pub fn with_visibility(mut self, visibility: Visibility) -> Self {
146    self.settings = self.settings.with_visibility(visibility);
147    self
148  }
149
150  pub fn with_decorations(mut self, visibility: Visibility) -> Self {
151    self.settings = self.settings.with_decorations(visibility);
152    self
153  }
154
155  pub fn with_fullscreen(mut self, fullscreen: Option<Fullscreen>) -> Self {
156    self.settings = self.settings.with_fullscreen(fullscreen);
157
158    self
159  }
160
161  pub fn with_cursor_mode(mut self, cursor_mode: CursorMode) -> Self {
162    self.settings = self.settings.with_cursor_mode(cursor_mode);
163    self
164  }
165
166  pub fn with_close_on_x(mut self, close_on_x: bool) -> Self {
167    self.settings = self.settings.with_close_on_x(close_on_x);
168    self
169  }
170
171  pub fn with_resizeable(mut self, resizeable: bool) -> Self {
172    self.settings = self.settings.with_resizeable(resizeable);
173    self
174  }
175
176  pub fn build(self) -> Result<Window, WindowError> {
177    Window::new(self.title, self.size, self.position, self.settings)
178  }
179}