diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 3d7277044e0..a8f0b851e59 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -1,14 +1,15 @@ //! Note that this file contains two similar paths - one for [`glow`], one for [`wgpu`]. //! When making changes to one you often also want to apply it to the other. -use std::{sync::Arc, time::Instant}; +use std::{cell::RefCell, sync::Arc, time::Instant}; -use egui::{epaint::ahash::HashMap, mutex::RwLock, ViewportBuilder, ViewportId}; use raw_window_handle::{HasRawDisplayHandle as _, HasRawWindowHandle as _}; use winit::event_loop::{ ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy, EventLoopWindowTarget, }; +use egui::{epaint::ahash::HashMap, mutex::RwLock, ViewportBuilder, ViewportId}; + #[cfg(feature = "accesskit")] use egui_winit::accesskit_winit; use egui_winit::winit; @@ -31,7 +32,7 @@ pub const IS_DESKTOP: bool = cfg!(any( thread_local! { /// This makes `Context::create_viewport_sync` to have a native window in the same frame! - pub static WINIT_EVENT_LOOP: RwLock<*const EventLoopWindowTarget> = RwLock::new(std::ptr::null()); + pub static WINIT_EVENT_LOOP: RefCell<*const EventLoopWindowTarget> = RefCell::new(std::ptr::null()); } // ---------------------------------------------------------------------------- @@ -146,7 +147,6 @@ fn with_event_loop( mut native_options: epi::NativeOptions, f: impl FnOnce(&mut EventLoop, NativeOptions) -> R, ) -> R { - use std::cell::RefCell; thread_local!(static EVENT_LOOP: RefCell>> = RefCell::new(None)); EVENT_LOOP.with(|event_loop| { @@ -176,7 +176,7 @@ 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.write() = event_loop); + WINIT_EVENT_LOOP.with(|row_event_loop| row_event_loop.replace(event_loop)); let event_result = match &event { winit::event::Event::LoopDestroyed => { @@ -334,7 +334,7 @@ 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.write() = event_loop); + WINIT_EVENT_LOOP.with(|row_event_loop| row_event_loop.replace(event_loop)); let event_result = match &event { winit::event::Event::LoopDestroyed => { @@ -1146,8 +1146,9 @@ mod glow_integration { let event_loop; #[allow(unsafe_code)] unsafe { - event_loop = - WINIT_EVENT_LOOP.with(|event_loop| event_loop.read().as_ref().unwrap()); + event_loop = WINIT_EVENT_LOOP.with(|event_loop| { + event_loop.borrow().as_ref().expect("No winit event loop") + }); } glutin .write() @@ -1796,23 +1797,16 @@ mod glow_integration { ) -> Result<()> { #[cfg(not(target_os = "ios"))] if native_options.run_and_return { - with_event_loop(native_options, |event_loop, native_options| { + return with_event_loop(native_options, |event_loop, native_options| { let glow_eframe = GlowWinitApp::new(event_loop, app_name, native_options, app_creator); run_and_return(event_loop, glow_eframe) - }) - } else { - let event_loop = create_event_loop(&mut native_options); - let glow_eframe = GlowWinitApp::new(&event_loop, app_name, native_options, app_creator); - run_and_exit(event_loop, glow_eframe); + }); } - #[cfg(target_os = "ios")] - { - let event_loop = create_event_loop(&mut native_options); - let glow_eframe = GlowWinitApp::new(&event_loop, app_name, native_options, app_creator); - run_and_exit(event_loop, glow_eframe); - } + let event_loop = create_event_loop(&mut native_options); + let glow_eframe = GlowWinitApp::new(&event_loop, app_name, native_options, app_creator); + run_and_exit(event_loop, glow_eframe); } } @@ -2175,8 +2169,9 @@ mod wgpu_integration { #[allow(unsafe_code)] unsafe { - event_loop = - WINIT_EVENT_LOOP.with(|event_loop| event_loop.read().as_ref().unwrap()); + event_loop = WINIT_EVENT_LOOP.with(|event_loop| { + event_loop.borrow().as_ref().expect("No winit event loop") + }); } Self::init_window( @@ -2681,23 +2676,16 @@ mod wgpu_integration { ) -> Result<()> { #[cfg(not(target_os = "ios"))] if native_options.run_and_return { - with_event_loop(native_options, |event_loop, native_options| { + return with_event_loop(native_options, |event_loop, native_options| { let wgpu_eframe = WgpuWinitApp::new(event_loop, app_name, native_options, app_creator); run_and_return(event_loop, wgpu_eframe) - }) - } else { - let event_loop = create_event_loop(&mut native_options); - let wgpu_eframe = WgpuWinitApp::new(&event_loop, app_name, native_options, app_creator); - run_and_exit(event_loop, wgpu_eframe); + }); } - #[cfg(target_os = "ios")] - { - let event_loop = create_event_loop(&mut native_options); - let wgpu_eframe = WgpuWinitApp::new(&event_loop, app_name, native_options, app_creator); - run_and_exit(event_loop, wgpu_eframe); - } + let event_loop = create_event_loop(&mut native_options); + let wgpu_eframe = WgpuWinitApp::new(&event_loop, app_name, native_options, app_creator); + run_and_exit(event_loop, wgpu_eframe); } } diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 10b932513c5..5af7927bcd5 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -1,7 +1,7 @@ #![warn(missing_docs)] // Let's keep `Context` well-documented. -use std::borrow::Cow; use std::sync::Arc; +use std::{borrow::Cow, cell::RefCell}; use crate::load::Bytes; use crate::load::SizedTexture; @@ -139,7 +139,7 @@ impl Repaint { // ---------------------------------------------------------------------------- thread_local! { - static EGUI_RENDER_SYNC: RwLock>> = RwLock::new(None); + static EGUI_RENDER_SYNC: RefCell>> = Default::default(); } // ---------------------------------------------------------------------------- @@ -2550,7 +2550,7 @@ impl Context { ) { let callback = Box::new(callback); EGUI_RENDER_SYNC.with(|render_sync| { - *render_sync.write() = Some(callback); + render_sync.replace(Some(callback)); }); } @@ -2670,7 +2670,7 @@ impl Context { { let out = &mut out; EGUI_RENDER_SYNC.with(|render_sync|{ - let render_sync = render_sync.read(); + let render_sync = render_sync.borrow(); let render_sync = render_sync.as_ref().expect("No EGUI_RENDER_SYNC callback on this thread, if you try to use Context::create_viewport_sync you cannot do that in other thread! If that is not the issue your egui intrecration is invalid or do not support sync viewports!"); render_sync( self,