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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! General window stuff.
//!
//! Multiple structs to change the properties of a Window.
use crossbeam::atomic::AtomicCell;
use dpi::*;
use std::sync::Arc;
use vulkano::swapchain::Surface;
pub use winit::{
    dpi,
    window::{CursorGrabMode, CursorIcon, Icon, UserAttentionType, WindowLevel},
};
use winit::{
    error::ExternalError,
    window::{Fullscreen, WindowButtons},
};

use crate::utils::color_art_to_array;

/// A struct representing the window.
#[derive(Clone)]
pub struct Window {
    window: Arc<winit::window::Window>,
    clear_color: Arc<AtomicCell<[f32; 4]>>,
}

impl Window {
    pub fn new(surface: Arc<Surface>, clear_color: [f32; 4]) -> Self {
        Self {
            window: surface
                .object()
                .unwrap()
                .clone()
                .downcast::<winit::window::Window>()
                .unwrap(),
            clear_color: Arc::new(AtomicCell::new(clear_color)),
        }
    }

    /// Requests the window to be redrawn.
    #[inline]
    pub fn request_redraw(&self) {
        self.window.request_redraw();
    }

    /// Returns the inner size of the window.
    #[inline]
    pub fn inner_size(&self) -> PhysicalSize<u32> {
        self.window.inner_size()
    }

    /// Sets the size of the window. This unmaximizes the window in case it is.
    #[inline]
    pub fn set_inner_size(&self, size: impl Into<Size>) {
        self.window.set_inner_size(size);
    }

    /// Returns the outer size of the window.
    #[inline]
    pub fn outer_size(&self) -> PhysicalSize<u32> {
        self.window.outer_size()
    }

    /// Restricts the window to not go smaller than the given size.
    #[inline]
    pub fn set_min_inner_size(&self, size: Option<impl Into<Size>>) {
        self.window.set_min_inner_size(size);
    }

    /// Restricts the window to not go bigger than the given size.
    #[inline]
    pub fn set_max_inner_size(&self, size: Option<impl Into<Size>>) {
        self.window.set_max_inner_size(size);
    }

    /// Returns the increments in which the window gets resized.
    #[inline]
    pub fn resize_increments(&self) -> Option<PhysicalSize<u32>> {
        self.window.resize_increments()
    }

    /// Returns the increments in which the window gets resized.
    #[inline]
    pub fn set_resize_increments(&self, increments: Option<impl Into<Size>>) {
        self.window.set_resize_increments(increments);
    }

    /// Returns the title of the window.
    #[inline]
    pub fn title(&self) -> String {
        self.window.title()
    }

    /// Sets the title of the window.
    #[inline]
    pub fn set_title(&self, title: &str) {
        self.window.set_title(title)
    }

    /// Sets whether the window should have an alpha channel.
    #[inline]
    fn set_transparent(&self, transparent: bool) {
        self.window.set_transparent(transparent)
    }

    /// Sets whether the window should be visible.
    #[inline]
    pub fn set_visible(&self, visible: bool) {
        self.window.set_visible(visible)
    }

    /// Returns whether the window is visible.
    ///
    /// `None` means it can't be determined if the window is visible or not.
    #[inline]
    pub fn visible(&self) -> Option<bool> {
        self.window.is_visible()
    }

    /// Sets whether the window should be resizable.
    #[inline]
    pub fn set_resizable(&self, resizable: bool) {
        self.window.set_resizable(resizable)
    }

    /// Returns whether the window is resizable.
    #[inline]
    pub fn resizable(&self) -> bool {
        self.window.is_resizable()
    }

    /// Sets the enabled_buttons of the title bar.
    #[inline]
    pub fn set_enabled_buttons(&self, close: bool, minimize: bool, maximize: bool) {
        let mut buttons = WindowButtons::empty();
        if close {
            buttons.toggle(WindowButtons::CLOSE);
        };
        if minimize {
            buttons.toggle(WindowButtons::MINIMIZE);
        };
        if maximize {
            buttons.toggle(WindowButtons::MAXIMIZE);
        };
        self.window.set_enabled_buttons(buttons)
    }

    /// Returns the enabled buttons on the title bar.
    /// (bool, bool, bool) -> close, minimize, maximize buttons
    #[inline]
    pub fn enabled_buttons(&self) -> (bool, bool, bool) {
        let mut result = (false, false, false);
        let enabled = self.window.enabled_buttons();
        if enabled.contains(WindowButtons::CLOSE) {
            result.0 = true;
        };
        if enabled.contains(WindowButtons::MINIMIZE) {
            result.1 = true;
        };
        if enabled.contains(WindowButtons::MAXIMIZE) {
            result.2 = true;
        };
        result
    }

    /// Sets whether the window should be minimized.
    #[inline]
    pub fn set_minimized(&self, minimized: bool) {
        self.window.set_minimized(minimized)
    }

    /// Returns whether the window is minimized.
    ///
    /// `None` gets returned if it couldn'd be determined if the window is minimized.
    #[inline]
    pub fn minimized(&self) -> Option<bool> {
        self.window.is_minimized()
    }

    /// Sets whether the window should be maximized.
    #[inline]
    pub fn set_maximized(&self, maximized: bool) {
        self.window.set_maximized(maximized)
    }

    /// Returns whether the window is maximized.
    #[inline]
    pub fn maximized(&self) -> bool {
        self.window.is_maximized()
    }

    /// Sets whether the window should be in fullscreen.
    #[inline]
    pub fn set_fullscreen(&self, fullscreen: bool) {
        let fullscreen = if fullscreen {
            Some(Fullscreen::Borderless(None))
        } else {
            None
        };
        self.window.set_fullscreen(fullscreen)
    }

    /// Returns whether the window is fullscreen.
    #[inline]
    pub fn fullscreen(&self) -> bool {
        self.window.fullscreen().is_some()
    }

    /// Sets whether the window should have a title bar.
    #[inline]
    pub fn set_decorations(&self, decorations: bool) {
        self.window.set_decorations(decorations)
    }

    /// Returns whether the window has a title bar.
    #[inline]
    pub fn decorated(&self) -> bool {
        self.window.is_decorated()
    }

    /// Sets the window level.
    #[inline]
    pub fn set_window_level(&self, level: WindowLevel) {
        self.window.set_window_level(level)
    }

    /// Sets the window icon.
    #[inline]
    pub fn set_window_icon(&self, icon: Option<Icon>) {
        self.window.set_window_icon(icon);
    }

    /// Focuses the window.
    #[inline]
    pub fn focus(&self) {
        self.window.focus_window();
    }

    /// Returns true if the window is focused.
    #[inline]
    pub fn has_focus(&self) -> bool {
        self.window.has_focus()
    }

    /// Makes the window request for user attention with the given context.
    #[inline]
    pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) {
        self.window.request_user_attention(request_type);
    }

    /// Sets the cursor icon to be the given variant.
    #[inline]
    pub fn set_cursor_icon(&self, cursor: CursorIcon) {
        self.window.set_cursor_icon(cursor);
    }

    /// Makes the window grab the cursor.
    /// Some variants don't work on some platforms.
    #[inline]
    pub fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), ExternalError> {
        self.window.set_cursor_grab(mode)
    }

    /// Makes the cursor invisible mostly just within the confines of the window.
    #[inline]
    pub fn set_cursor_visible(&self, mode: CursorGrabMode) -> Result<(), ExternalError> {
        self.window.set_cursor_grab(mode)
    }

    /// Drags the window with the left mouse button until it's released.
    #[inline]
    pub fn drag_window(&self) -> Result<(), ExternalError> {
        self.window.drag_window()
    }

    /// Modifies whether the window catches cursor events.
    /// If `true`, the window will catch the cursor events. If `false`, events are passed through the window such that any other window behind it receives them. By default hittest is enabled.
    #[inline]
    pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> {
        self.window.set_cursor_hittest(hittest)
    }

    /// Sets the clear color of the window.
    pub fn set_clear_color(&self, color: color_art::Color) {
        let clear_color = [
            color.red() as f32 / 255.0,
            color.green() as f32 / 255.0,
            color.blue() as f32 / 255.0,
            color.alpha() as f32,
        ];
        self.set_transparent(clear_color[3] != 1.0);
        self.clear_color.store(clear_color);
    }

    pub fn clear_color(&self) -> [f32; 4] {
        self.clear_color.load()
    }
}

/// A builder describing the initial state of the window.
#[derive(Clone)]
#[must_use]
pub struct WindowBuilder {
    attributes: winit::window::WindowBuilder,
    pub(crate) clear_color: [f32; 4],
}

impl WindowBuilder {
    /// Creates a new window builder.
    pub fn new() -> Self {
        let attributes = winit::window::WindowBuilder::new().with_title("Game");
        Self {
            attributes,
            clear_color: [0.0, 0.0, 0.0, 1.0],
        }
    }

    /// Makes a new window builder using the one from the Winit crate.
    #[inline]
    pub fn from_winit_builder(builder: winit::window::WindowBuilder) -> Self {
        Self {
            attributes: builder,
            clear_color: [0.0, 0.0, 0.0, 1.0],
        }
    }

    /// Sets the inner size of the window.
    #[inline]
    pub fn inner_size(mut self, size: impl Into<Size>) -> Self {
        self.attributes = self.attributes.with_inner_size(size);
        self
    }

    /// Restricts the inner size of the window to not go past the given size.
    #[inline]
    pub fn max_inner_size(mut self, size: impl Into<Size>) -> Self {
        self.attributes = self.attributes.with_max_inner_size(size);
        self
    }

    /// Restricts the inner size of the window to not go below the given size.
    #[inline]
    pub fn min_inner_size(mut self, size: impl Into<Size>) -> Self {
        self.attributes = self.attributes.with_min_inner_size(size);
        self
    }

    /// Moves the window to the given position.
    ///
    /// Works on windows, mac and x11 but not wayland.
    #[inline]
    pub fn position(mut self, position: impl Into<Position>) -> Self {
        self.attributes = self.attributes.with_position(position);
        self
    }

    /// Makes the window resizable.
    #[inline]
    pub fn resizable(mut self, resizable: bool) -> Self {
        self.attributes = self.attributes.with_resizable(resizable);
        self
    }

    /// Enables the given buttons on the title bar.
    #[inline]
    pub fn enabled_buttons(mut self, close: bool, minimize: bool, maximize: bool) -> Self {
        let mut buttons = WindowButtons::empty();
        if close {
            buttons.toggle(WindowButtons::CLOSE);
        };
        if minimize {
            buttons.toggle(WindowButtons::MINIMIZE);
        };
        if maximize {
            buttons.toggle(WindowButtons::MAXIMIZE);
        };
        self.attributes = self.attributes.with_enabled_buttons(buttons);
        self
    }

    /// Sets the title of the window seen on the title bar.
    #[inline]
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.attributes = self.attributes.with_title(title);
        self
    }

    // Add more modes.
    /// Sets the window to borderless fullscreen on the current monitor.
    #[inline]
    pub fn fullscreen(mut self, fullscreen: bool) -> Self {
        let fullscreen = if fullscreen {
            Some(Fullscreen::Borderless(None))
        } else {
            None
        };
        self.attributes = self.attributes.with_fullscreen(fullscreen);
        self
    }

    /// Request that the window is maximized upon creation.
    #[inline]
    pub fn maximized(mut self, maximized: bool) -> Self {
        self.attributes = self.attributes.with_maximized(maximized);
        self
    }

    /// Sets the window to be visible upon creation.
    #[inline]
    pub fn visible(mut self, visible: bool) -> Self {
        self.attributes = self.attributes.with_visible(visible);
        self
    }

    /// Sets the clear color of the window.
    pub fn clear_color(mut self, color: color_art::Color) -> Self {
        let clear_color = color_art_to_array(color);
        self.attributes = self.attributes.with_transparent(clear_color[3] != 1.0);
        self.clear_color = clear_color;
        self
    }

    /// Gives the window a title bar and buttons.
    #[inline]
    pub fn decorations(mut self, decorations: bool) -> Self {
        self.attributes = self.attributes.with_decorations(decorations);
        self
    }

    /// The ordering of the window.
    #[inline]
    pub fn window_level(mut self, level: WindowLevel) -> Self {
        self.attributes = self.attributes.with_window_level(level);
        self
    }

    /// Sets the icon of the window application.
    #[inline]
    pub fn icon(mut self, icon: Option<Icon>) -> Self {
        self.attributes = self.attributes.with_window_icon(icon);
        self
    }

    /// Build window with resize increments hint.
    #[inline]
    pub fn resize_increments(mut self, increments: impl Into<Size>) -> Self {
        self.attributes = self.attributes.with_resize_increments(increments);
        self
    }

    /// Should the window be initially active?
    #[inline]
    pub fn active(mut self, active: bool) -> Self {
        self.attributes = self.attributes.with_active(active);
        self
    }
}

impl From<WindowBuilder> for winit::window::WindowBuilder {
    fn from(val: WindowBuilder) -> Self {
        val.attributes
    }
}

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