diff --git a/crates/eframe/src/epi.rs b/crates/eframe/src/epi.rs index bc19951bf..ee737d611 100644 --- a/crates/eframe/src/epi.rs +++ b/crates/eframe/src/epi.rs @@ -83,6 +83,10 @@ pub struct CreationContext<'s> { #[cfg(feature = "wgpu_no_default_features")] pub wgpu_render_state: Option, + /// The root [`winit::window::Window`]. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) window: Option>, + /// Raw platform window handle #[cfg(not(target_arch = "wasm32"))] pub(crate) raw_window_handle: Result, @@ -125,11 +129,21 @@ impl CreationContext<'_> { #[cfg(feature = "wgpu_no_default_features")] wgpu_render_state: None, #[cfg(not(target_arch = "wasm32"))] + window: None, + #[cfg(not(target_arch = "wasm32"))] raw_window_handle: Err(HandleError::NotSupported), #[cfg(not(target_arch = "wasm32"))] raw_display_handle: Err(HandleError::NotSupported), } } + + /// Access to the root [`winit::window::Window`]. + /// + /// `None` for headless (tests etc). + #[cfg(not(target_arch = "wasm32"))] + pub fn winit_window(&self) -> Option<&std::sync::Arc> { + self.window.as_ref() + } } // ---------------------------------------------------------------------------- @@ -699,6 +713,10 @@ pub struct Frame { #[doc(hidden)] pub wgpu_render_state: Option, + /// The current [`winit::window::Window`] (i.e. the one the active viewport is rendered to). + #[cfg(not(target_arch = "wasm32"))] + pub(crate) window: Option>, + /// Raw platform window handle #[cfg(not(target_arch = "wasm32"))] pub(crate) raw_window_handle: Result, @@ -744,6 +762,8 @@ impl Frame { raw_display_handle: Err(HandleError::NotSupported), #[cfg(not(target_arch = "wasm32"))] raw_window_handle: Err(HandleError::NotSupported), + #[cfg(not(target_arch = "wasm32"))] + window: None, storage: None, #[cfg(feature = "wgpu_no_default_features")] wgpu_render_state: None, @@ -773,6 +793,14 @@ impl Frame { self.storage.as_deref_mut() } + /// Access to the current [`winit::window::Window`] (i.e. the one the active viewport is rendered to). + /// + /// `None` for headless (tests etc). + #[cfg(not(target_arch = "wasm32"))] + pub fn winit_window(&self) -> Option<&std::sync::Arc> { + self.window.as_ref() + } + /// A reference to the underlying [`glow`] (OpenGL) context. /// /// This can be used, for instance, to: diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 5b22eb08c..7f938a00a 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -2,7 +2,7 @@ use web_time::Instant; -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; use winit::event_loop::ActiveEventLoop; use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _}; @@ -171,7 +171,7 @@ impl EpiIntegration { #[allow(clippy::allow_attributes, clippy::too_many_arguments)] pub fn new( egui_ctx: egui::Context, - window: &winit::window::Window, + window: &Arc, app_name: &str, native_options: &crate::NativeOptions, storage: Option>, @@ -192,6 +192,7 @@ impl EpiIntegration { glow_register_native_texture, #[cfg(feature = "wgpu_no_default_features")] wgpu_render_state, + window: Some(Arc::clone(window)), raw_display_handle: window.display_handle().map(|h| h.as_raw()), raw_window_handle: window.window_handle().map(|h| h.as_raw()), }; diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index 37e3faa69..af1b68f9f 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -305,6 +305,7 @@ impl<'app> GlowWinitApp<'app> { get_proc_address: Some(Arc::new(get_proc_address)), #[cfg(feature = "wgpu_no_default_features")] wgpu_render_state: None, + window: Some(Arc::clone(&window)), raw_display_handle: window.display_handle().map(|h| h.as_raw()), raw_window_handle: window.window_handle().map(|h| h.as_raw()), }; diff --git a/crates/eframe/src/native/wgpu_integration.rs b/crates/eframe/src/native/wgpu_integration.rs index 6d300d513..bbd4979ea 100644 --- a/crates/eframe/src/native/wgpu_integration.rs +++ b/crates/eframe/src/native/wgpu_integration.rs @@ -294,6 +294,7 @@ impl<'app> WgpuWinitApp<'app> { #[cfg(feature = "glow")] get_proc_address: None, wgpu_render_state, + window: Some(Arc::clone(&window)), raw_display_handle: window.display_handle().map(|h| h.as_raw()), raw_window_handle: window.window_handle().map(|h| h.as_raw()), };