use crate::error::GarasuError;
use winit::event_loop::EventLoop;
use winit::window::{Window, WindowAttributes};
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct WindowConfig {
pub width: u32,
pub height: u32,
pub title: String,
pub transparent: bool,
pub decorations: bool,
}
impl Default for WindowConfig {
fn default() -> Self {
Self {
width: 800,
height: 600,
title: "garasu".to_owned(),
transparent: false,
decorations: true,
}
}
}
pub struct AppWindow;
impl AppWindow {
pub fn event_loop() -> Result<EventLoop<()>, GarasuError> {
EventLoop::new().map_err(|e| GarasuError::Window(e.to_string()))
}
pub fn create_from_config(
event_loop: &winit::event_loop::ActiveEventLoop,
config: &WindowConfig,
) -> Result<Window, GarasuError> {
let attrs = WindowAttributes::default()
.with_title(&config.title)
.with_inner_size(winit::dpi::PhysicalSize::new(config.width, config.height))
.with_transparent(config.transparent)
.with_decorations(config.decorations);
event_loop
.create_window(attrs)
.map_err(|e| GarasuError::Window(e.to_string()))
}
pub fn create(
event_loop: &winit::event_loop::ActiveEventLoop,
title: &str,
width: u32,
height: u32,
) -> Result<Window, GarasuError> {
let attrs = WindowAttributes::default()
.with_title(title)
.with_inner_size(winit::dpi::PhysicalSize::new(width, height));
event_loop
.create_window(attrs)
.map_err(|e| GarasuError::Window(e.to_string()))
}
}