#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::{Window, WindowId};
#[derive(Default)]
struct App {
window: Option<Window>,
}
impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if let Ok(window) = event_loop.create_window(Window::default_attributes()) {
window.set_title("Hello, World!");
self.window = Some(window);
} else {
eprintln!("Failed to create window");
}
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
match event {
WindowEvent::CloseRequested => {
println!("The close button was pressed; stopping");
event_loop.exit();
}
WindowEvent::RedrawRequested => {
if let Some(window) = self.window.as_ref() {
window.request_redraw()
}
}
_ => (),
}
}
}
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let event_loop = EventLoop::new()?;
event_loop.set_control_flow(ControlFlow::Poll);
event_loop.set_control_flow(ControlFlow::Wait);
let mut app = App::default();
event_loop.run_app(&mut app)?;
Ok(())
}