diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 59c93219281..bd8212ec4b6 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -30,13 +30,6 @@ pub const IS_DESKTOP: bool = cfg!(any( // ---------------------------------------------------------------------------- -thread_local! { - /// This makes [`Context::show_viewport_immediate`] to have a native window in the same frame! - pub static WINIT_EVENT_LOOP: RefCell<*const EventLoopWindowTarget> = RefCell::new(std::ptr::null()); -} - -// ---------------------------------------------------------------------------- - /// The custom even `eframe` uses with the [`winit`] event loop. #[derive(Debug)] pub enum UserEvent { @@ -176,8 +169,6 @@ fn run_and_return( event_loop.run_return(|event, event_loop, control_flow| { crate::profile_scope!("winit_event", short_event_description(&event)); - WINIT_EVENT_LOOP.with(|row_event_loop| row_event_loop.replace(event_loop)); - let event_result = match &event { winit::event::Event::LoopDestroyed => { // On Mac, Cmd-Q we get here and then `run_return` doesn't return (despite its name), @@ -334,8 +325,6 @@ fn run_and_exit(event_loop: EventLoop, mut winit_app: impl WinitApp + event_loop.run(move |event, event_loop, control_flow| { crate::profile_scope!("winit_event", short_event_description(&event)); - WINIT_EVENT_LOOP.with(|row_event_loop| row_event_loop.replace(event_loop)); - let event_result = match &event { winit::event::Event::LoopDestroyed => { log::debug!("Received Event::LoopDestroyed"); @@ -1084,12 +1073,20 @@ mod glow_integration { let painter = Rc::downgrade(&painter); let beginning = integration.beginning; + let event_loop: *const EventLoopWindowTarget = event_loop; + integration.egui_ctx.set_immediate_viewport_renderer( move |egui_ctx, viewport_builder, id_pair, viewport_ui_cb| { if let (Some(glutin), Some(gl), Some(painter)) = (glutin.upgrade(), gl.upgrade(), painter.upgrade()) { + // SAFETY: the event loop lives longer than + // the Rc:s we just upgraded above. + #[allow(unsafe_code)] + let event_loop = unsafe { event_loop.as_ref().unwrap() }; + Self::render_immediate_viewport( + event_loop, egui_ctx, viewport_builder, id_pair, @@ -1120,6 +1117,7 @@ mod glow_integration { #[inline(always)] #[allow(clippy::too_many_arguments)] fn render_immediate_viewport( + event_loop: &EventLoopWindowTarget, egui_ctx: &egui::Context, mut viewport_builder: ViewportBuilder, id_pair: ViewportIdPair, @@ -1166,13 +1164,6 @@ mod glow_integration { .or_insert(viewport_builder); } - #[allow(unsafe_code)] - let event_loop = unsafe { - WINIT_EVENT_LOOP.with(|event_loop| { - event_loop.borrow().as_ref().expect("No winit event loop") - }) - }; - glutin .init_viewport(id_pair.this, event_loop) .expect("Cannot init window on egui::Context::show_viewport_immediate"); @@ -2157,10 +2148,18 @@ mod wgpu_integration { let shared = Rc::downgrade(&shared); let beginning = integration.beginning; + let event_loop: *const EventLoopWindowTarget = event_loop; + integration.egui_ctx.set_immediate_viewport_renderer( move |egui_ctx, viewport_builder, id_pair, viewport_ui_cb| { if let Some(shared) = shared.upgrade() { + // SAFETY: the event loop lives longer than + // the Rc:s we just upgraded above. + #[allow(unsafe_code)] + let event_loop = unsafe { event_loop.as_ref().unwrap() }; + Self::render_immediate_viewport( + event_loop, egui_ctx, viewport_builder, id_pair, @@ -2187,6 +2186,7 @@ mod wgpu_integration { #[inline(always)] #[allow(clippy::too_many_arguments)] fn render_immediate_viewport( + event_loop: &EventLoopWindowTarget, egui_ctx: &egui::Context, mut viewport_builder: ViewportBuilder, id_pair: ViewportIdPair, @@ -2229,13 +2229,6 @@ mod wgpu_integration { .entry(id_pair.this) .or_insert(viewport_builder.clone()); - #[allow(unsafe_code)] - let event_loop = unsafe { - WINIT_EVENT_LOOP.with(|event_loop| { - event_loop.borrow().as_ref().expect("No winit event loop") - }) - }; - Self::init_window( id_pair.this, &viewport_builder,