Skip to main content

oxide_engine/
window.rs

1//! Window abstraction
2
3use std::sync::Arc;
4use winit::{
5    dpi::{PhysicalPosition, PhysicalSize},
6    event_loop::ActiveEventLoop,
7    window::{Window as WinitWindow, WindowId},
8};
9
10#[derive(Clone)]
11pub struct Window {
12    inner: Arc<WinitWindow>,
13}
14
15impl Window {
16    pub fn new(event_loop: &ActiveEventLoop, title: &str, width: u32, height: u32) -> Self {
17        let window = event_loop
18            .create_window(
19                WinitWindow::default_attributes()
20                    .with_title(title)
21                    .with_inner_size(PhysicalSize::new(width, height)),
22            )
23            .expect("Failed to create window");
24
25        Self {
26            inner: Arc::new(window),
27        }
28    }
29
30    pub fn id(&self) -> WindowId {
31        self.inner.id()
32    }
33
34    pub fn size(&self) -> PhysicalSize<u32> {
35        self.inner.inner_size()
36    }
37
38    pub fn scale_factor(&self) -> f64 {
39        self.inner.scale_factor()
40    }
41
42    pub fn set_title(&self, title: &str) {
43        self.inner.set_title(title);
44    }
45
46    pub fn set_cursor_visible(&self, visible: bool) {
47        self.inner.set_cursor_visible(visible);
48    }
49
50    pub fn set_cursor_position(
51        &self,
52        position: PhysicalPosition<f64>,
53    ) -> Result<(), winit::error::ExternalError> {
54        self.inner.set_cursor_position(position)
55    }
56
57    pub fn request_redraw(&self) {
58        self.inner.request_redraw();
59    }
60
61    pub fn winit_window(&self) -> &Arc<WinitWindow> {
62        &self.inner
63    }
64}
65
66impl std::ops::Deref for Window {
67    type Target = Arc<WinitWindow>;
68
69    fn deref(&self) -> &Self::Target {
70        &self.inner
71    }
72}