Skip to content

Commit

Permalink
Remove WINIT_EVENT_LOOP hack
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 12, 2023
1 parent 5892d05 commit de6c250
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserEvent>> = RefCell::new(std::ptr::null());
}

// ----------------------------------------------------------------------------

/// The custom even `eframe` uses with the [`winit`] event loop.
#[derive(Debug)]
pub enum UserEvent {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -334,8 +325,6 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, 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");
Expand Down Expand Up @@ -1084,12 +1073,20 @@ mod glow_integration {
let painter = Rc::downgrade(&painter);
let beginning = integration.beginning;

let event_loop: *const EventLoopWindowTarget<UserEvent> = 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,
Expand Down Expand Up @@ -1120,6 +1117,7 @@ mod glow_integration {
#[inline(always)]
#[allow(clippy::too_many_arguments)]
fn render_immediate_viewport(
event_loop: &EventLoopWindowTarget<UserEvent>,
egui_ctx: &egui::Context,
mut viewport_builder: ViewportBuilder,
id_pair: ViewportIdPair,
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -2157,10 +2148,18 @@ mod wgpu_integration {
let shared = Rc::downgrade(&shared);
let beginning = integration.beginning;

let event_loop: *const EventLoopWindowTarget<UserEvent> = 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,
Expand All @@ -2187,6 +2186,7 @@ mod wgpu_integration {
#[inline(always)]
#[allow(clippy::too_many_arguments)]
fn render_immediate_viewport(
event_loop: &EventLoopWindowTarget<UserEvent>,
egui_ctx: &egui::Context,
mut viewport_builder: ViewportBuilder,
id_pair: ViewportIdPair,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit de6c250

Please sign in to comment.