Skip to content

Commit

Permalink
Prevent crashes when minimizing on Windows
Browse files Browse the repository at this point in the history
This commit fixes hannobraun#1424
  • Loading branch information
kazatsuyu authored and hannobraun committed Dec 13, 2022
1 parent 3e8b54a commit f24a46b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
46 changes: 28 additions & 18 deletions crates/fj-window/src/event_loop_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct EventLoopHandler {
/// context:
/// <https://github.com/rust-windowing/winit/issues/2094>
pub new_size: Option<ScreenSize>,
pub stop_drawing: bool,
}

impl EventLoopHandler {
Expand Down Expand Up @@ -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);
}
}
}
_ => {}
Expand Down
1 change: 1 addition & 0 deletions crates/fj-window/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down

0 comments on commit f24a46b

Please sign in to comment.