handle window close event

This commit is contained in:
Dustin Carlino 2018-12-17 11:32:52 -08:00
parent 806a139788
commit b3dea902de
2 changed files with 15 additions and 6 deletions

View File

@ -128,10 +128,7 @@ impl<S: UIState> GUI<RenderingHints> for UI<S> {
// Can do this at any time.
if input.action_chosen("quit") {
self.save_editor_state();
self.cs.save();
info!("Saved color_scheme");
//cpuprofiler::PROFILER.lock().unwrap().stop().unwrap();
self.before_quit();
process::exit(0);
}
@ -180,6 +177,13 @@ impl<S: UIState> GUI<RenderingHints> for UI<S> {
self.state.dump_before_abort();
self.save_editor_state();
}
fn before_quit(&self) {
self.save_editor_state();
self.cs.save();
info!("Saved color_scheme");
//cpuprofiler::PROFILER.lock().unwrap().stop().unwrap();
}
}
impl<S: UIState> UI<S> {

View File

@ -4,7 +4,7 @@ use glutin_window::GlutinWindow;
use opengl_graphics::{GlGraphics, OpenGL};
use piston::event_loop::{EventLoop, EventSettings, Events};
use piston::window::{Window, WindowSettings};
use std::panic;
use std::{panic, process};
pub trait GUI<T> {
// Called once
@ -16,6 +16,8 @@ pub trait GUI<T> {
fn draw(&self, g: &mut GfxCtx, data: &T);
// Will be called if event or draw panics.
fn dump_before_abort(&self) {}
// Only before a normal exit, like window close
fn before_quit(&self) {}
}
#[derive(Clone, Copy, PartialEq)]
@ -41,7 +43,7 @@ pub fn run<T, G: GUI<T>>(mut gui: G, window_title: &str, initial_width: u32, ini
let mut top_menu = G::top_menu(gui.get_mut_canvas());
let mut last_data: Option<T> = None;
while let Some(ev) = events.next(&mut window) {
use piston::input::RenderEvent;
use piston::input::{CloseEvent, RenderEvent};
if let Some(args) = ev.render_args() {
// If the very first event is render, then just wait.
if let Some(ref data) = last_data {
@ -66,6 +68,9 @@ pub fn run<T, G: GUI<T>>(mut gui: G, window_title: &str, initial_width: u32, ini
}
});
}
} else if ev.close_args().is_some() {
gui.before_quit();
process::exit(0);
} else {
// Skip some events.
use piston::input::{