From 422cd10dd9ffe625052c12dcd8117e75becd351b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 20 Nov 2023 13:02:51 +0100 Subject: [PATCH] Fix: closing of viewports --- crates/eframe/src/native/epi_integration.rs | 8 +++++--- crates/eframe/src/native/glow_integration.rs | 13 ++++++++++++- crates/eframe/src/native/wgpu_integration.rs | 19 ++++++++++++++++--- examples/multiple_viewports/src/main.rs | 10 +++++----- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 4333431ca7c..f9e9d815aa0 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -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"); diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index 1a059ca2a24..7f5985e86f1 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -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; } } diff --git a/crates/eframe/src/native/wgpu_integration.rs b/crates/eframe/src/native/wgpu_integration.rs index c0f0ba5f9e5..c5fd393afb1 100644 --- a/crates/eframe/src/native/wgpu_integration.rs +++ b/crates/eframe/src/native/wgpu_integration.rs @@ -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; + } } _ => {} diff --git a/examples/multiple_viewports/src/main.rs b/examples/multiple_viewports/src/main.rs index e6238030a39..ce82e281b74 100644 --- a/examples/multiple_viewports/src/main.rs +++ b/examples/multiple_viewports/src/main.rs @@ -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`). @@ -68,7 +68,7 @@ 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 } }, ); @@ -76,12 +76,12 @@ impl eframe::App for MyApp { 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" @@ -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 } }, );