Skip to content

Commit

Permalink
Fix: closing of viewports
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 20, 2023
1 parent 7bfaf49 commit 422cd10
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
8 changes: 5 additions & 3 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,11 @@ impl EpiIntegration {

match event {
WindowEvent::CloseRequested => {
log::debug!("Received WindowEvent::CloseRequested");
self.close = app.on_close_event() && viewport_id == ViewportId::ROOT;
log::debug!("App::on_close_event returned {}", self.close);
log::debug!("Received WindowEvent::CloseRequested for viewport {viewport_id:?}");
if viewport_id == ViewportId::ROOT {
self.close = app.on_close_event();
log::debug!("App::on_close_event returned {}", self.close);
}
}
WindowEvent::Destroyed => {
log::debug!("Received WindowEvent::Destroyed");
Expand Down
13 changes: 12 additions & 1 deletion crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,9 +706,20 @@ impl GlowWinitRunning {
}

winit::event::WindowEvent::CloseRequested => {
log::debug!("Received WindowEvent::CloseRequested for viewport {viewport_id:?}");

if let Some(viewport_id) = viewport_id {
if let Some(viewport) = self.glutin.borrow_mut().viewports.get_mut(&viewport_id)
{
viewport.info.close_requested = true;
}
}

let is_root = viewport_id == Some(ViewportId::ROOT);
if is_root && self.integration.should_close() {
log::debug!("Received WindowEvent::CloseRequested");
log::debug!(
"Received WindowEvent::CloseRequested for main viewport - shutting down."
);
return EventResult::Exit;
}
}
Expand Down
19 changes: 16 additions & 3 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,22 @@ impl WgpuWinitRunning {
}
}

winit::event::WindowEvent::CloseRequested if integration.should_close() => {
log::debug!("Received WindowEvent::CloseRequested");
return EventResult::Exit;
winit::event::WindowEvent::CloseRequested => {
log::debug!("Received WindowEvent::CloseRequested for viewport {viewport_id:?}");

if let Some(viewport_id) = viewport_id {
if let Some(viewport) = shared.viewports.get_mut(&viewport_id) {
viewport.info.close_requested = true;
}
}

let is_root = viewport_id == Some(ViewportId::ROOT);
if is_root && integration.should_close() {
log::debug!(
"Received WindowEvent::CloseRequested for main viewport - shutting down."
);
return EventResult::Exit;
}
}

_ => {}
Expand Down
10 changes: 5 additions & 5 deletions examples/multiple_viewports/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::{
Arc,
};

use eframe::egui;
use eframe::egui::{self, ViewportId};

fn main() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
Expand Down Expand Up @@ -68,20 +68,20 @@ impl eframe::App for MyApp {
if ctx.input(|i| i.viewport().close_requested) {
// Tell parent viewport that we should not show next frame:
self.show_immediate_viewport = false;
ctx.request_repaint(); // make sure there is a next frame
ctx.request_repaint_of(ctx.parent_viewport_id()); // make sure we get closed
}
},
);
}

if self.show_deferred_viewport.load(Ordering::Relaxed) {
let show_deferred_viewport = self.show_deferred_viewport.clone();
ctx.show_viewport_immediate(
ctx.show_viewport_deferred(
egui::ViewportId::from_hash_of("deferred_viewport"),
egui::ViewportBuilder::default()
.with_title("Deferred Viewport")
.with_inner_size([200.0, 100.0]),
|ctx, class| {
move |ctx, class| {
assert!(
class == egui::ViewportClass::Deferred,
"This egui backend doesn't support multiple viewports"
Expand All @@ -93,7 +93,7 @@ impl eframe::App for MyApp {
if ctx.input(|i| i.viewport().close_requested) {
// Tell parent to close us.
show_deferred_viewport.store(false, Ordering::Relaxed);
ctx.request_repaint(); // make sure there is a next frame
ctx.request_repaint_of(ctx.parent_viewport_id()); // make sure we get closed
}
},
);
Expand Down

0 comments on commit 422cd10

Please sign in to comment.