extern crate relaunch;
use winit::{
application::ApplicationHandler,
dpi::{LogicalSize, Size},
event::{StartCause, WindowEvent},
event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
window::{Window, WindowAttributes, WindowId},
};
struct App {
window: Option<Window>,
}
impl App {
fn new() -> Self {
App { window: None }
}
}
impl ApplicationHandler for App {
fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: winit::event::StartCause) {
if cause == StartCause::Init {
self.window = Some(
event_loop
.create_window(
WindowAttributes::default()
.with_title("re-winit")
.with_inner_size(Size::Logical(LogicalSize::new(800.0, 600.0))),
)
.expect("Failed to create window"),
);
event_loop.set_control_flow(ControlFlow::Wait);
}
}
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let _ = event_loop;
let redraw_required = false;
if redraw_required {
if let Some(window) = &self.window {
window.request_redraw();
}
}
}
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
window_id: WindowId,
event: WindowEvent,
) {
let _ = window_id; match event {
WindowEvent::RedrawRequested => {
}
WindowEvent::CloseRequested => {
event_loop.exit();
}
_ => (),
}
}
}
fn main() {
let event_loop = EventLoop::new().expect("Failed to create event loop");
match relaunch::Trampoline::new("re-winit", "com.github.maaku.relauncher.winit")
.bundle(relaunch::InstallDir::Temp)
{
Err(error) => {
println!("Application relaunch failed: {}", error);
std::process::exit(1);
}
Ok(app) => {
println!(
"Application relaunched successfully from {}",
app.bundle_path.to_str().unwrap()
);
}
};
assert!(relaunch::Trampoline::is_bundled());
let mut app = App::new();
if let Err(err) = event_loop.run_app(&mut app) {
eprintln!("Event loop terminated with error: {}", err);
};
}