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

View File

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