Skip to main content

zenthra_platform/
window.rs

1use std::sync::Arc;
2use zenthra_render::gpu::GpuContext;
3
4pub struct Window {
5    pub gpu: GpuContext,
6    pub winit_window: Arc<winit::window::Window>,
7    pub title: String,
8}
9
10impl Window {
11    pub async fn new(
12        event_loop: &winit::event_loop::ActiveEventLoop,
13        title: &str,
14        width: u32,
15        height: u32,
16        decorations: bool,
17        transparent: bool,
18        blur: bool,
19    ) -> Self {
20        let attrs = winit::window::Window::default_attributes()
21            .with_title(title)
22            .with_inner_size(winit::dpi::LogicalSize::new(width, height))
23            .with_decorations(decorations)
24            .with_transparent(transparent)
25            .with_blur(blur);
26
27        let winit_window = Arc::new(event_loop.create_window(attrs).unwrap());
28        let gpu = GpuContext::new(winit_window.clone()).await;
29
30        Self {
31            gpu,
32            winit_window,
33            title: title.to_string(),
34        }
35    }
36
37    pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
38        let sf = self.winit_window.scale_factor();
39        self.gpu.resize(new_size, sf);
40    }
41
42    pub fn request_redraw(&self) {
43        self.winit_window.request_redraw();
44    }
45
46    pub fn scale_factor(&self) -> f64 {
47        self.gpu.scale_factor
48    }
49
50    pub fn width(&self) -> u32 {
51        self.gpu.size.width
52    }
53    pub fn height(&self) -> u32 {
54        self.gpu.size.height
55    }
56}