diff --git a/crates/eframe/src/epi.rs b/crates/eframe/src/epi.rs index a971097a..ef01e338 100644 --- a/crates/eframe/src/epi.rs +++ b/crates/eframe/src/epi.rs @@ -400,6 +400,10 @@ pub struct NativeOptions { /// [`with_android_app`]: winit::platform::android::EventLoopBuilderExtAndroid::with_android_app #[cfg(target_os = "android")] pub android_app: Option, + + /// Per-viewport window and raw-input callbacks, see [`crate::ViewportHooks`]. + #[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))] + pub viewport_hooks: Option>, } #[cfg(not(target_arch = "wasm32"))] @@ -425,6 +429,9 @@ impl Clone for NativeOptions { #[cfg(target_os = "android")] android_app: self.android_app.clone(), + #[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))] + viewport_hooks: self.viewport_hooks.clone(), + ..*self } } @@ -468,6 +475,9 @@ impl Default for NativeOptions { #[cfg(target_os = "android")] android_app: None, + + #[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))] + viewport_hooks: None, } } } diff --git a/crates/eframe/src/lib.rs b/crates/eframe/src/lib.rs index 55caffdd..10e0abda 100644 --- a/crates/eframe/src/lib.rs +++ b/crates/eframe/src/lib.rs @@ -197,6 +197,10 @@ pub use native::macos::WindowChromeMetrics; #[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))] pub use native::run::EframeWinitApplication; +#[cfg(not(target_arch = "wasm32"))] +#[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))] +pub use native::viewport_hooks::ViewportHooks; + #[cfg(not(any(target_arch = "wasm32", target_os = "ios")))] #[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))] pub use native::run::EframePumpStatus; @@ -310,6 +314,7 @@ pub fn run_native_ext( app_creator: AppCreator<'_>, ) -> Result { let renderer = init_native(app_name, &mut native_options); + native::viewport_hooks::install(native_options.viewport_hooks.clone()); match renderer { #[cfg(feature = "glow")] diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index b8122563..55167524 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -359,6 +359,7 @@ impl EpiIntegration { raw_input.time = Some(self.beginning.elapsed().as_secs_f64()); + super::viewport_hooks::with(|hooks| hooks.raw_input(&self.egui_ctx, &mut raw_input)); app.raw_input_hook(&self.egui_ctx, &mut raw_input); raw_input diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index 82a150aa..873039b5 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -1173,6 +1173,7 @@ impl GlutinWindowContext { let mut viewport_info = ViewportInfo::default(); if let Some(window) = &window { viewport_from_window.insert(window.id(), ViewportId::ROOT); + super::viewport_hooks::with(|hooks| hooks.window_created(ViewportId::ROOT, window)); window_from_viewport.insert(ViewportId::ROOT, window.id()); egui_winit::update_viewport_info(&mut viewport_info, egui_ctx, window, true); @@ -1374,7 +1375,10 @@ impl GlutinWindowContext { self.current_gl_context = Some(current_gl_context); } - self.viewport_from_window.insert(window.id(), viewport_id); + // This runs on every frame; report a window only when it is new for this viewport. + if self.viewport_from_window.insert(window.id(), viewport_id) != Some(viewport_id) { + super::viewport_hooks::with(|hooks| hooks.window_created(viewport_id, &window)); + } self.window_from_viewport.insert(viewport_id, window.id()); Ok(()) @@ -1442,8 +1446,13 @@ impl GlutinWindowContext { viewport_output: &OrderedViewportIdMap, ) { // GC old viewports - self.viewports - .retain(|id, _| viewport_output.contains_key(id)); + self.viewports.retain(|id, _| { + let keep = viewport_output.contains_key(id); + if !keep { + super::viewport_hooks::with(|hooks| hooks.window_destroyed(*id)); + } + keep + }); self.viewport_from_window .retain(|_, id| viewport_output.contains_key(id)); self.window_from_viewport @@ -1658,6 +1667,7 @@ fn render_immediate_viewport( .map(|(id, viewport)| (*id, viewport.info.clone())) .collect(); raw_input.time = Some(beginning.elapsed().as_secs_f64()); + super::viewport_hooks::with(|hooks| hooks.raw_input(egui_ctx, &mut raw_input)); raw_input }; diff --git a/crates/eframe/src/native/mod.rs b/crates/eframe/src/native/mod.rs index 771964ae..aa75505b 100644 --- a/crates/eframe/src/native/mod.rs +++ b/crates/eframe/src/native/mod.rs @@ -3,6 +3,8 @@ mod epi_integration; mod event_loop_context; pub mod run; +pub mod viewport_hooks; + #[cfg(target_os = "macos")] pub(crate) mod macos; diff --git a/crates/eframe/src/native/viewport_hooks.rs b/crates/eframe/src/native/viewport_hooks.rs new file mode 100644 index 00000000..bc1c8e4a --- /dev/null +++ b/crates/eframe/src/native/viewport_hooks.rs @@ -0,0 +1,38 @@ +//! Per-viewport native window and raw-input hooks. +//! +//! eframe exposes the root window's handles through [`crate::CreationContext`] only, and runs +//! [`crate::App::raw_input_hook`] for the root and deferred viewports but not for immediate ones. +//! Integrations that need every viewport's native window (for example to match Wayland surfaces +//! named by a protocol the windowing layer doesn't implement) or that must add input to every +//! viewport before egui processes it can set [`crate::NativeOptions::viewport_hooks`]. + +use std::sync::{Arc, PoisonError, RwLock}; + +/// Callbacks eframe makes for every viewport. All methods have empty defaults. +/// +/// They run on the event loop thread, inside eframe's frame handling: keep them short and don't +/// call back into eframe or block. +pub trait ViewportHooks: Send + Sync { + /// eframe created the native window of `viewport` (root, deferred or immediate). + fn window_created(&self, _viewport: egui::ViewportId, _window: &winit::window::Window) {} + + /// eframe dropped `viewport`; its window is about to be (or has been) destroyed. + fn window_destroyed(&self, _viewport: egui::ViewportId) {} + + /// The raw input of one viewport (`raw_input.viewport_id`), before egui processes it. Runs for + /// root, deferred and immediate viewports, before [`crate::App::raw_input_hook`]. + fn raw_input(&self, _ctx: &egui::Context, _raw_input: &mut egui::RawInput) {} +} + +static HOOKS: RwLock>> = RwLock::new(None); + +pub(crate) fn install(hooks: Option>) { + *HOOKS.write().unwrap_or_else(PoisonError::into_inner) = hooks; +} + +pub(crate) fn with(f: impl FnOnce(&dyn ViewportHooks)) { + let hooks = HOOKS.read().unwrap_or_else(PoisonError::into_inner).clone(); + if let Some(hooks) = hooks { + f(&*hooks); + } +} diff --git a/crates/eframe/src/native/wgpu_integration.rs b/crates/eframe/src/native/wgpu_integration.rs index 4a94a5d4..a56a57c3 100644 --- a/crates/eframe/src/native/wgpu_integration.rs +++ b/crates/eframe/src/native/wgpu_integration.rs @@ -335,6 +335,7 @@ impl<'app> WgpuWinitApp<'app> { let mut viewport_from_window = HashMap::default(); viewport_from_window.insert(window.id(), ViewportId::ROOT); + super::viewport_hooks::with(|hooks| hooks.window_created(ViewportId::ROOT, &window)); let mut viewports = Viewports::default(); viewports.insert( @@ -861,7 +862,13 @@ impl WgpuWinitRunning<'_> { ); // Prune dead viewports: - viewports.retain(|id, _| active_viewports_ids.contains(id)); + viewports.retain(|id, _| { + let keep = active_viewports_ids.contains(id); + if !keep { + super::viewport_hooks::with(|hooks| hooks.window_destroyed(*id)); + } + keep + }); viewport_from_window.retain(|_, id| active_viewports_ids.contains(id)); painter.gc_viewports(&active_viewports_ids); @@ -1067,6 +1074,7 @@ impl Viewport { match egui_winit::create_window(egui_ctx, event_loop, &self.builder) { Ok(window) => { windows_id.insert(window.id(), viewport_id); + super::viewport_hooks::with(|hooks| hooks.window_created(viewport_id, &window)); let window = Arc::new(window); @@ -1183,6 +1191,8 @@ fn render_immediate_viewport( .map(|(id, viewport)| (*id, viewport.info.clone())) .collect(); input.time = Some(beginning.elapsed().as_secs_f64()); + let egui_ctx = egui_ctx.clone(); + super::viewport_hooks::with(|hooks| hooks.raw_input(&egui_ctx, &mut input)); input }; @@ -1265,7 +1275,13 @@ pub(crate) fn remove_viewports_not_in( let active_viewports_ids: ViewportIdSet = viewport_output.keys().copied().collect(); // Prune dead viewports: - viewports.retain(|id, _| active_viewports_ids.contains(id)); + viewports.retain(|id, _| { + let keep = active_viewports_ids.contains(id); + if !keep { + super::viewport_hooks::with(|hooks| hooks.window_destroyed(*id)); + } + keep + }); viewport_from_window.retain(|_, id| active_viewports_ids.contains(id)); painter.gc_viewports(&active_viewports_ids); }