From f24a46b908badc2b2bdf549f5d05139422eacec9 Mon Sep 17 00:00:00 2001 From: Shirayama Kazatsuyu Date: Tue, 13 Dec 2022 21:59:23 +0900 Subject: [PATCH] Prevent crashes when minimizing on Windows This commit fixes #1424 --- crates/fj-window/src/event_loop_handler.rs | 46 +++++++++++++--------- crates/fj-window/src/run.rs | 1 + 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/crates/fj-window/src/event_loop_handler.rs b/crates/fj-window/src/event_loop_handler.rs index d22197c90..996676d2b 100644 --- a/crates/fj-window/src/event_loop_handler.rs +++ b/crates/fj-window/src/event_loop_handler.rs @@ -30,6 +30,7 @@ pub struct EventLoopHandler { /// context: /// pub new_size: Option, + pub stop_drawing: bool, } impl EventLoopHandler { @@ -164,28 +165,37 @@ impl EventLoopHandler { // Only do a screen resize once per frame. This protects against // spurious resize events that cause issues with the renderer. if let Some(size) = self.new_size.take() { - self.viewer.handle_screen_resize(size); + self.stop_drawing = size.width == 0 || size.height == 0; + if !self.stop_drawing { + self.viewer.handle_screen_resize(size); + } } - let pixels_per_point = - self.window.window().scale_factor() as f32; - - self.egui_winit_state.set_pixels_per_point(pixels_per_point); - let egui_input = - self.egui_winit_state.take_egui_input(self.window.window()); + if !self.stop_drawing { + let pixels_per_point = + self.window.window().scale_factor() as f32; - let gui_state = GuiState { - status: &self.status, - model_available: self.host.is_some(), - }; - let new_model_path = - self.viewer.draw(pixels_per_point, egui_input, gui_state); + self.egui_winit_state + .set_pixels_per_point(pixels_per_point); + let egui_input = self + .egui_winit_state + .take_egui_input(self.window.window()); - if let Some(model_path) = new_model_path { - let model = - Model::new(model_path, Parameters::empty()).unwrap(); - let new_host = Host::from_model(model)?; - self.host = Some(new_host); + let gui_state = GuiState { + status: &self.status, + model_available: self.host.is_some(), + }; + let new_model_path = self.viewer.draw( + pixels_per_point, + egui_input, + gui_state, + ); + if let Some(model_path) = new_model_path { + let model = Model::new(model_path, Parameters::empty()) + .unwrap(); + let new_host = Host::from_model(model)?; + self.host = Some(new_host); + } } } _ => {} diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 8b9999deb..35f57c194 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -44,6 +44,7 @@ pub fn run( status: StatusReport::new(), held_mouse_button: None, new_size: None, + stop_drawing: false, }; event_loop.run(move |event, _, control_flow| {