basalt 0.21.0

A window/ui framework built upon vulkan.
Documentation
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
use std::any::Any;
use std::collections::HashMap;
use std::sync::atomic::{self, AtomicBool};
use std::sync::{Arc, Weak};
use std::time::Duration;

use parking_lot::Mutex;
use raw_window_handle::{
    HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
use vulkano::format::Format as VkFormat;
use vulkano::swapchain::{
    ColorSpace as VkColorSpace, FullScreenExclusive, PresentMode, Surface, SurfaceCapabilities,
    SurfaceInfo, Win32Monitor,
};
use winit::dpi::PhysicalSize;
use winit::window::{CursorGrabMode, Window as WinitWindow, WindowId as WinitWindowId};

use crate::input::{
    Char, InputEvent, InputHookCtrl, InputHookID, InputHookTarget, KeyCombo, LocalCursorState,
    LocalKeyState, WindowState,
};
use crate::interface::{Bin, BinID};
use crate::render::{RendererMetricsLevel, RendererPerfMetrics, VSync, MSAA};
use crate::window::monitor::{FullScreenBehavior, FullScreenError, Monitor};
use crate::window::{WindowEvent, WindowID, WindowManager, WindowType};
use crate::Basalt;

/// Object that represents a window.
///
/// This object is generally passed around as it allows accessing mosts things within the crate.
pub struct Window {
    id: WindowID,
    inner: Arc<WinitWindow>,
    basalt: Arc<Basalt>,
    wm: Arc<WindowManager>,
    surface: Arc<Surface>,
    window_type: WindowType,
    state: Mutex<State>,
    close_requested: AtomicBool,
}

struct State {
    cursor_captured: bool,
    ignore_dpi: bool,
    dpi_scale: f32,
    interface_scale: f32,
    msaa: MSAA,
    vsync: VSync,
    metrics: RendererPerfMetrics,
    metrics_level: RendererMetricsLevel,
    on_metrics_update: Vec<Box<dyn FnMut(WindowID, RendererPerfMetrics) + Send + Sync + 'static>>,
    associated_bins: HashMap<BinID, Weak<Bin>>,
    attached_input_hooks: Vec<InputHookID>,
    keep_alive_objects: Vec<Box<dyn Any + Send + Sync + 'static>>,
}

impl std::fmt::Debug for Window {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Window")
            .field("id", &self.id)
            .field("inner", &self.inner)
            .field("surface", &self.surface)
            .field("window_type", &self.window_type)
            .finish()
    }
}

impl PartialEq<Window> for Window {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id && Arc::ptr_eq(&self.basalt, &other.basalt)
    }
}

impl Window {
    pub(crate) fn new(
        basalt: Arc<Basalt>,
        wm: Arc<WindowManager>,
        id: WindowID,
        winit: Arc<WinitWindow>,
    ) -> Result<Arc<Self>, String> {
        // NOTE: Although it may seem the winit window doesn't need to be in an Arc. This allows
        //       vulkano to keep the window alive longer than the surface. It may be possible to
        //       pass the basalt window instead, but that'd likey mean keeping surface in a mutex.

        let surface = Surface::from_window(basalt.instance(), winit.clone())
            .map_err(|e| format!("Failed to create surface: {}", e))?;

        let window_type = match winit.raw_window_handle() {
            RawWindowHandle::AndroidNdk(_) => WindowType::Android,
            RawWindowHandle::AppKit(_) => WindowType::Macos,
            RawWindowHandle::UiKit(_) => WindowType::Ios,
            RawWindowHandle::Wayland(_) => WindowType::Wayland,
            RawWindowHandle::Win32(_) => WindowType::Windows,
            RawWindowHandle::Xcb(_) => WindowType::Xcb,
            RawWindowHandle::Xlib(_) => WindowType::Xlib,
            _ => unimplemented!(),
        };

        let (ignore_dpi, dpi_scale) = match basalt.config.window_ignore_dpi {
            true => (true, 1.0),
            false => (false, winit.scale_factor() as f32),
        };

        let state = State {
            cursor_captured: false,
            ignore_dpi,
            dpi_scale,
            msaa: basalt.config.render_default_msaa,
            vsync: basalt.config.render_default_vsync,
            metrics: RendererPerfMetrics::default(),
            metrics_level: RendererMetricsLevel::None,
            on_metrics_update: Vec::new(),
            interface_scale: basalt.config.window_default_scale,
            associated_bins: HashMap::new(),
            attached_input_hooks: Vec::new(),
            keep_alive_objects: Vec::new(),
        };

        Ok(Arc::new(Self {
            id,
            inner: winit,
            basalt,
            wm,
            surface,
            window_type,
            state: Mutex::new(state),
            close_requested: AtomicBool::new(false),
        }))
    }

    pub(crate) fn winit_id(&self) -> WinitWindowId {
        self.inner.id()
    }

    pub(crate) fn associate_bin(&self, bin: Arc<Bin>) {
        self.state
            .lock()
            .associated_bins
            .insert(bin.id(), Arc::downgrade(&bin));

        self.wm
            .send_window_event(self.id, WindowEvent::AssociateBin(bin));
    }

    pub(crate) fn dissociate_bin(&self, bin_id: BinID) {
        self.state.lock().associated_bins.remove(&bin_id);
        self.wm
            .send_window_event(self.id, WindowEvent::DissociateBin(bin_id));
    }

    pub(crate) fn update_bin(&self, bin_id: BinID) {
        self.wm
            .send_window_event(self.id, WindowEvent::UpdateBin(bin_id));
    }

    pub(crate) fn update_bin_batch(&self, bin_ids: Vec<BinID>) {
        self.wm
            .send_window_event(self.id, WindowEvent::UpdateBinBatch(bin_ids));
    }

    /// The window id of this window.
    pub fn id(&self) -> WindowID {
        self.id
    }

    /// Obtain a copy of `Arc<Basalt>`
    pub fn basalt(&self) -> Arc<Basalt> {
        self.basalt.clone()
    }

    /// Obtain a reference of `Arc<Basalt>`
    pub fn basalt_ref(&self) -> &Arc<Basalt> {
        &self.basalt
    }

    /// Obtain a copy of `Arc<Surface>`
    pub fn surface(&self) -> Arc<Surface> {
        self.surface.clone()
    }

    /// Obtain a reference of `Arc<Surface>`
    pub fn surface_ref(&self) -> &Arc<Surface> {
        &self.surface
    }

    /// Obtain a copy of `Arc<WindowManager>`
    pub fn window_manager(&self) -> Arc<WindowManager> {
        self.wm.clone()
    }

    /// Obtain a reference of `Arc<WindowManager>`
    pub fn window_manager_ref(&self) -> &Arc<WindowManager> {
        &self.wm
    }

    /// Create a new `Bin` associated with this window.
    pub fn new_bin(self: &Arc<Self>) -> Arc<Bin> {
        let bin = self.basalt.interface_ref().new_bin();
        bin.associate_window(self);
        bin
    }

    /// Create new `Bin`'s associated with this window.
    pub fn new_bins(self: &Arc<Self>, count: usize) -> Vec<Arc<Bin>> {
        let bins = self.basalt.interface_ref().new_bins(count);

        for bin in &bins {
            bin.associate_window(self);
        }

        bins
    }

    /// Retrieve a list of `Bin`'s associated to this window.
    pub fn associated_bins(&self) -> Vec<Arc<Bin>> {
        self.state
            .lock()
            .associated_bins
            .values()
            .filter_map(|wk| wk.upgrade())
            .collect()
    }

    /// Retrieve a list of `BinID`'s associated to this window.
    pub fn associated_bin_ids(&self) -> Vec<BinID> {
        self.state.lock().associated_bins.keys().copied().collect()
    }

    /// Hides and captures cursor.
    pub fn capture_cursor(&self) {
        let mut state = self.state.lock();
        state.cursor_captured = true;

        self.inner.set_cursor_visible(false);
        self.inner
            .set_cursor_grab(CursorGrabMode::Confined)
            .unwrap();
        self.basalt
            .input_ref()
            .send_event(InputEvent::CursorCapture {
                win: self.id,
                captured: true,
            });
    }

    /// Shows and releases cursor.
    pub fn release_cursor(&self) {
        let mut state = self.state.lock();
        state.cursor_captured = false;

        self.inner.set_cursor_visible(true);
        self.inner.set_cursor_grab(CursorGrabMode::None).unwrap();

        self.basalt
            .input_ref()
            .send_event(InputEvent::CursorCapture {
                win: self.id,
                captured: false,
            });
    }

    /// Checks if cursor is currently captured.
    pub fn cursor_captured(&self) -> bool {
        self.state.lock().cursor_captured
    }

    /// Return a list of active monitors on the system.
    pub fn monitors(&self) -> Vec<Monitor> {
        let current_op = self.inner.current_monitor();
        let primary_op = self.inner.primary_monitor();

        self.inner
            .available_monitors()
            .filter_map(|winit_monitor| {
                let is_current = match current_op.as_ref() {
                    Some(current) => *current == winit_monitor,
                    None => false,
                };

                let is_primary = match primary_op.as_ref() {
                    Some(primary) => *primary == winit_monitor,
                    None => false,
                };

                let mut monitor = Monitor::from_winit(winit_monitor)?;
                monitor.is_current = is_current;
                monitor.is_primary = is_primary;
                Some(monitor)
            })
            .collect()
    }

    /// Return the primary monitor if the implementation is able to determine it.
    pub fn primary_monitor(&self) -> Option<Monitor> {
        self.inner.primary_monitor().and_then(|winit_monitor| {
            let is_current = match self.inner.current_monitor() {
                Some(current) => current == winit_monitor,
                None => false,
            };

            let mut monitor = Monitor::from_winit(winit_monitor)?;
            monitor.is_primary = true;
            monitor.is_current = is_current;
            Some(monitor)
        })
    }

    /// Return the current monitor if the implementation is able to determine it.
    pub fn current_monitor(&self) -> Option<Monitor> {
        self.inner.current_monitor().and_then(|winit_monitor| {
            let is_primary = match self.inner.primary_monitor() {
                Some(primary) => primary == winit_monitor,
                None => false,
            };

            let mut monitor = Monitor::from_winit(winit_monitor)?;
            monitor.is_current = true;
            monitor.is_primary = is_primary;
            Some(monitor)
        })
    }

    /// Enable fullscreen with the provided behavior.
    ///
    /// If `fallback_borderless` is set to `true` and am exclusive behavior is used when it isn't
    /// supported am equivalent borderless behavior will be used.
    pub fn enable_fullscreen(
        &self,
        fallback_borderless: bool,
        behavior: FullScreenBehavior,
    ) -> Result<(), FullScreenError> {
        let winit_fullscreen = behavior.determine_winit_fullscreen(
            fallback_borderless,
            self.basalt
                .device_ref()
                .enabled_extensions()
                .ext_full_screen_exclusive,
            self.current_monitor(),
            self.primary_monitor(),
            self.monitors(),
        )?;

        self.inner.set_fullscreen(Some(winit_fullscreen));
        self.wm
            .send_window_event(self.id, WindowEvent::EnabledFullscreen);
        Ok(())
    }

    /// Disable fullscreen.
    ///
    /// ***Note:** This is a no-op if this window isn't fullscreen.*
    pub fn disable_fullscreen(&self) {
        if self.inner.fullscreen().is_some() {
            self.inner.set_fullscreen(None);
            self.wm
                .send_window_event(self.id, WindowEvent::DisabledFullscreen);
        }
    }

    /// Toggle fullscreen mode. Uses `FullScreenBehavior::Auto`.
    pub fn toggle_fullscreen(&self) -> Result<(), FullScreenError> {
        if self.is_fullscreen() {
            self.disable_fullscreen();
            Ok(())
        } else {
            self.enable_fullscreen(true, Default::default())
        }
    }

    /// Check if the window is fullscreen.
    pub fn is_fullscreen(&self) -> bool {
        self.inner.fullscreen().is_some()
    }

    /// Request the monitor to resize to the given dimensions.
    ///
    /// ***Note:** Returns `false` if the platform doesn't support resize.*
    pub fn request_resize(&self, width: u32, height: u32) -> bool {
        // TODO: Should this take into account dpi scaling and interface scaling?

        let request_size = PhysicalSize::new(width, height);
        let pre_request_size = self.inner.inner_size();

        match self.inner.request_inner_size(request_size) {
            Some(physical_size) => {
                if physical_size == pre_request_size {
                    // Platform doesn't support resize.
                    return false;
                }

                if physical_size == request_size {
                    // If the size is the same as the one that was requested, then the platform
                    // resized the window immediately. In this case, the resize event may not get
                    // sent out per winit docs.

                    self.wm.send_window_event(
                        self.id,
                        WindowEvent::Resized {
                            width,
                            height,
                        },
                    );
                }

                true
            },
            None => {
                // The resize request was sent and a subsequent resize event will be sent.
                true
            },
        }
    }

    /// Return the dimensions of the client area of this window.
    pub fn inner_dimensions(&self) -> [u32; 2] {
        self.inner.inner_size().into()
    }

    /// Return the `WindowType` of this window.
    pub fn window_type(&self) -> WindowType {
        self.window_type
    }

    /// DPI scaling used on this window.
    pub fn dpi_scale(&self) -> f32 {
        self.state.lock().dpi_scale
    }

    /// Check if this window is ignoring dpi scaling.
    ///
    /// ***Note:** This is configured upon basalt's creation via its options.*
    pub fn ignoring_dpi(&self) -> bool {
        self.state.lock().ignore_dpi
    }

    pub(crate) fn set_dpi_scale(&self, scale: f32) {
        let mut state = self.state.lock();

        if state.ignore_dpi {
            state.dpi_scale = 1.0;
        } else {
            state.dpi_scale = scale;
        }

        self.wm.send_window_event(
            self.id,
            WindowEvent::ScaleChanged(state.interface_scale * state.dpi_scale),
        );
    }

    /// Current interface scale. This does not include dpi scaling.
    pub fn current_interface_scale(&self) -> f32 {
        self.state.lock().interface_scale
    }

    /// Current effective interface scale. This includes dpi scaling.
    pub fn effective_interface_scale(&self) -> f32 {
        let state = self.state.lock();
        state.interface_scale * state.dpi_scale
    }

    /// Set the scale of the interface. This does not include dpi scaling.
    pub fn set_interface_scale(&self, set_scale: f32) {
        let mut state = self.state.lock();
        state.interface_scale = set_scale;

        self.wm.send_window_event(
            self.id,
            WindowEvent::ScaleChanged(state.interface_scale * state.dpi_scale),
        );
    }

    /// Set the scale of the interface. This includes dpi scaling.
    pub fn set_effective_interface_scale(&self, set_scale: f32) {
        let mut state = self.state.lock();
        state.interface_scale = set_scale / state.dpi_scale;

        self.wm.send_window_event(
            self.id,
            WindowEvent::ScaleChanged(state.interface_scale * state.dpi_scale),
        );
    }

    /// Get the current MSAA used for rendering.
    pub fn renderer_msaa(&self) -> MSAA {
        self.state.lock().msaa
    }

    /// Set the current MSAA used for rendering.
    pub fn set_renderer_msaa(&self, msaa: MSAA) {
        self.state.lock().msaa = msaa;

        self.wm
            .send_window_event(self.id, WindowEvent::SetMSAA(msaa));
    }

    /// Increase the current MSAA used for rendering returning the new value.
    pub fn incr_renderer_msaa(&self) -> MSAA {
        let mut state = self.state.lock();

        let msaa = match state.msaa {
            MSAA::X1 => MSAA::X2,
            MSAA::X2 => MSAA::X4,
            MSAA::X4 => MSAA::X8,
            MSAA::X8 => return MSAA::X8,
        };

        self.wm
            .send_window_event(self.id, WindowEvent::SetMSAA(msaa));

        state.msaa = msaa;
        msaa
    }

    /// Decrease the current MSAA used for rendering returning the new value.
    pub fn decr_renderer_msaa(&self) -> MSAA {
        let mut state = self.state.lock();

        let msaa = match state.msaa {
            MSAA::X1 => return MSAA::X1,
            MSAA::X2 => MSAA::X1,
            MSAA::X4 => MSAA::X2,
            MSAA::X8 => MSAA::X4,
        };

        self.wm
            .send_window_event(self.id, WindowEvent::SetMSAA(msaa));

        state.msaa = msaa;
        msaa
    }

    /// Get the current VSync used for rendering.
    pub fn renderer_vsync(&self) -> VSync {
        self.state.lock().vsync
    }

    /// Set the current VSync used for rendering.
    pub fn set_renderer_vsync(&self, vsync: VSync) {
        self.state.lock().vsync = vsync;

        self.wm
            .send_window_event(self.id, WindowEvent::SetVSync(vsync));
    }

    /// Toggle the current VSync used returning the new value.
    pub fn toggle_renderer_vsync(&self) -> VSync {
        let mut state = self.state.lock();

        let vsync = match state.vsync {
            VSync::Enable => VSync::Disable,
            VSync::Disable => VSync::Enable,
        };

        self.wm
            .send_window_event(self.id, WindowEvent::SetVSync(vsync));

        state.vsync = vsync;
        vsync
    }

    /// Get the current renderer metrics level used.
    pub fn renderer_metrics_level(&self) -> RendererMetricsLevel {
        self.state.lock().metrics_level
    }

    /// Set the current renderer metrics level used.
    pub fn set_renderer_metrics_level(&self, level: RendererMetricsLevel) {
        self.state.lock().metrics_level = level;
        self.wm
            .send_window_event(self.id, WindowEvent::SetMetrics(level));
    }

    /// Cycle between renderer metrics level returning the new current level.
    pub fn cycle_renderer_metrics_level(&self) -> RendererMetricsLevel {
        let mut state = self.state.lock();

        state.metrics_level = match state.metrics_level {
            RendererMetricsLevel::None => RendererMetricsLevel::Basic,
            RendererMetricsLevel::Basic => RendererMetricsLevel::Extended,
            RendererMetricsLevel::Extended => RendererMetricsLevel::Full,
            RendererMetricsLevel::Full => RendererMetricsLevel::None,
        };

        self.wm
            .send_window_event(self.id, WindowEvent::SetMetrics(state.metrics_level));

        state.metrics_level
    }

    /// Retrieve the current renderer metrics.
    ///
    /// ***Note:** If renderer metrics are disabled, this value will not be updated.*
    pub fn renderer_metrics(&self) -> RendererPerfMetrics {
        self.state.lock().metrics.clone()
    }

    /// When the renderer metrics are updated call the provided method.
    ///
    /// ***Note:** This method will be kept for the lifetime of the window.*
    pub fn on_renderer_metrics<F: FnMut(WindowID, RendererPerfMetrics) + Send + Sync + 'static>(
        &self,
        method: F,
    ) {
        self.state.lock().on_metrics_update.push(Box::new(method));
    }

    pub(crate) fn set_renderer_metrics(&self, metrics: RendererPerfMetrics) {
        let mut state = self.state.lock();

        for method in state.on_metrics_update.iter_mut() {
            method(self.id, metrics.clone());
        }

        state.metrics = metrics;
    }

    /// Keep objects alive for the lifetime of the window.
    pub fn keep_alive<O, T>(&self, objects: O)
    where
        O: IntoIterator<Item = T>,
        T: Any + Send + Sync + 'static,
    {
        for object in objects {
            self.state.lock().keep_alive_objects.push(Box::new(object));
        }
    }

    /// Request the window to close.
    ///
    /// ***Note:** This will not result in the window closing immeditely. Instead, this will remove any
    /// strong references basalt may have to this window allowing it to be dropped. It is also on
    /// the user to remove their strong references to the window to allow it drop. When the window
    /// drops it will be closed.*
    pub fn close(&self) {
        self.close_requested.store(true, atomic::Ordering::SeqCst);
        self.wm.send_window_event(self.id, WindowEvent::Closed);
    }

    /// Check if a close has been requested.
    pub fn close_requested(&self) -> bool {
        self.close_requested.load(atomic::Ordering::SeqCst)
    }

    /// Return the `Win32Monitor` used if present.
    pub(crate) fn win32_monitor(&self) -> Option<Win32Monitor> {
        #[cfg(target_os = "windows")]
        unsafe {
            use winit::platform::windows::MonitorHandleExtWindows;

            self.inner
                .current_monitor()
                .map(|m| Win32Monitor::new(m.hmonitor() as *const std::ffi::c_void))
        }

        #[cfg(not(target_os = "windows"))]
        {
            None
        }
    }

    pub(crate) fn surface_capabilities(&self, fse: FullScreenExclusive) -> SurfaceCapabilities {
        self.basalt
            .physical_device_ref()
            .surface_capabilities(
                &self.surface,
                match fse {
                    FullScreenExclusive::ApplicationControlled => {
                        SurfaceInfo {
                            full_screen_exclusive: FullScreenExclusive::ApplicationControlled,
                            win32_monitor: self.win32_monitor(),
                            ..SurfaceInfo::default()
                        }
                    },
                    fse => {
                        SurfaceInfo {
                            full_screen_exclusive: fse,
                            ..SurfaceInfo::default()
                        }
                    },
                },
            )
            .unwrap()
    }

    pub(crate) fn surface_formats(
        &self,
        fse: FullScreenExclusive,
    ) -> Vec<(VkFormat, VkColorSpace)> {
        self.basalt
            .physical_device_ref()
            .surface_formats(
                &self.surface,
                match fse {
                    FullScreenExclusive::ApplicationControlled => {
                        SurfaceInfo {
                            full_screen_exclusive: FullScreenExclusive::ApplicationControlled,
                            win32_monitor: self.win32_monitor(),
                            ..SurfaceInfo::default()
                        }
                    },
                    fse => {
                        SurfaceInfo {
                            full_screen_exclusive: fse,
                            ..SurfaceInfo::default()
                        }
                    },
                },
            )
            .unwrap()
    }

    pub(crate) fn surface_present_modes(&self, fse: FullScreenExclusive) -> Vec<PresentMode> {
        self.basalt
            .physical_device_ref()
            .surface_present_modes(
                &self.surface,
                match fse {
                    FullScreenExclusive::ApplicationControlled => {
                        SurfaceInfo {
                            full_screen_exclusive: FullScreenExclusive::ApplicationControlled,
                            win32_monitor: self.win32_monitor(),
                            ..SurfaceInfo::default()
                        }
                    },
                    fse => {
                        SurfaceInfo {
                            full_screen_exclusive: fse,
                            ..SurfaceInfo::default()
                        }
                    },
                },
            )
            .unwrap()
            .collect()
    }

    pub(crate) fn surface_current_extent(&self, fse: FullScreenExclusive) -> [u32; 2] {
        self.surface_capabilities(fse)
            .current_extent
            .unwrap_or_else(|| self.inner_dimensions())
    }

    /// Attach an input hook to this window. When the window closes, this hook will be
    /// automatically removed from `Input`.
    ///
    /// ***Note**: If a hook's target is a window this behavior already occurs without needing to
    /// call this method.*
    pub fn attach_input_hook(&self, hook: InputHookID) {
        self.state.lock().attached_input_hooks.push(hook);
    }

    pub fn on_press<C: KeyCombo, F>(self: &Arc<Self>, combo: C, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &WindowState, &LocalKeyState) -> InputHookCtrl + Send + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_press()
            .keys(combo)
            .call(method)
            .finish()
            .unwrap()
    }

    pub fn on_release<C: KeyCombo, F>(self: &Arc<Self>, combo: C, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &WindowState, &LocalKeyState) -> InputHookCtrl + Send + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_release()
            .keys(combo)
            .call(method)
            .finish()
            .unwrap()
    }

    pub fn on_hold<C: KeyCombo, F>(self: &Arc<Self>, combo: C, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &LocalKeyState, Option<Duration>) -> InputHookCtrl
            + Send
            + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_hold()
            .keys(combo)
            .call(method)
            .finish()
            .unwrap()
    }

    pub fn on_character<F>(self: &Arc<Self>, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &WindowState, Char) -> InputHookCtrl + Send + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_character()
            .call(method)
            .finish()
            .unwrap()
    }

    pub fn on_enter<F>(self: &Arc<Self>, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &WindowState) -> InputHookCtrl + Send + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_enter()
            .call(method)
            .finish()
            .unwrap()
    }

    pub fn on_leave<F>(self: &Arc<Self>, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &WindowState) -> InputHookCtrl + Send + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_leave()
            .call(method)
            .finish()
            .unwrap()
    }

    pub fn on_focus<F>(self: &Arc<Self>, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &WindowState) -> InputHookCtrl + Send + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_focus()
            .call(method)
            .finish()
            .unwrap()
    }

    pub fn on_focus_lost<F>(self: &Arc<Self>, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &WindowState) -> InputHookCtrl + Send + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_focus_lost()
            .call(method)
            .finish()
            .unwrap()
    }

    pub fn on_scroll<F>(self: &Arc<Self>, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &WindowState, f32, f32) -> InputHookCtrl + Send + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_scroll()
            .call(method)
            .finish()
            .unwrap()
    }

    pub fn on_cursor<F>(self: &Arc<Self>, method: F) -> InputHookID
    where
        F: FnMut(InputHookTarget, &WindowState, &LocalCursorState) -> InputHookCtrl
            + Send
            + 'static,
    {
        self.basalt()
            .input_ref()
            .hook()
            .window(self)
            .on_cursor()
            .call(method)
            .finish()
            .unwrap()
    }
}

impl Drop for Window {
    fn drop(&mut self) {
        for hook_id in self.state.lock().attached_input_hooks.drain(..) {
            self.basalt.input_ref().remove_hook(hook_id);
        }
    }
}

unsafe impl HasRawWindowHandle for Window {
    fn raw_window_handle(&self) -> RawWindowHandle {
        self.inner.raw_window_handle()
    }
}

unsafe impl HasRawDisplayHandle for Window {
    fn raw_display_handle(&self) -> RawDisplayHandle {
        self.inner.raw_display_handle()
    }
}