-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update to winit 0.29 #3606
Update to winit 0.29 #3606
Changes from 4 commits
c93defd
1a07d9d
60a1792
b65e24d
5b1b091
c6f0704
4491fae
5e326fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
use std::{cell::RefCell, rc::Rc, sync::Arc, time::Instant}; | ||
|
||
use glutin::{ | ||
context::NotCurrentGlContext, | ||
display::GetGlDisplay, | ||
prelude::{GlDisplay, NotCurrentGlContextSurfaceAccessor, PossiblyCurrentGlContext}, | ||
prelude::{GlDisplay, PossiblyCurrentGlContext}, | ||
surface::GlSurface, | ||
}; | ||
use raw_window_handle::{HasRawDisplayHandle as _, HasRawWindowHandle as _}; | ||
|
@@ -402,7 +403,7 @@ impl WinitApp for GlowWinitApp { | |
fn on_event( | ||
&mut self, | ||
event_loop: &EventLoopWindowTarget<UserEvent>, | ||
event: &winit::event::Event<'_, UserEvent>, | ||
event: &winit::event::Event<UserEvent>, | ||
) -> Result<EventResult> { | ||
crate::profile_function!(winit_integration::short_event_description(event)); | ||
|
||
|
@@ -434,15 +435,6 @@ impl WinitApp for GlowWinitApp { | |
EventResult::Wait | ||
} | ||
|
||
winit::event::Event::MainEventsCleared => { | ||
if let Some(running) = &self.running { | ||
if let Err(err) = running.glutin.borrow_mut().on_resume(event_loop) { | ||
log::warn!("on_resume failed {err}"); | ||
} | ||
} | ||
EventResult::Wait | ||
} | ||
|
||
winit::event::Event::WindowEvent { event, window_id } => { | ||
if let Some(running) = &mut self.running { | ||
running.on_window_event(*window_id, event) | ||
|
@@ -664,7 +656,7 @@ impl GlowWinitRunning { | |
fn on_window_event( | ||
&mut self, | ||
window_id: WindowId, | ||
event: &winit::event::WindowEvent<'_>, | ||
event: &winit::event::WindowEvent, | ||
) -> EventResult { | ||
crate::profile_function!(egui_winit::short_window_event_description(event)); | ||
|
||
|
@@ -703,10 +695,9 @@ impl GlowWinitRunning { | |
} | ||
} | ||
|
||
winit::event::WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { | ||
if let Some(viewport_id) = viewport_id { | ||
winit::event::WindowEvent::ScaleFactorChanged { .. } => { | ||
if viewport_id.is_some() { | ||
repaint_asap = true; | ||
glutin.resize(viewport_id, **new_inner_size); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In winit 0.29 |
||
} | ||
} | ||
|
||
|
@@ -821,7 +812,7 @@ impl GlutinWindowContext { | |
// Create GL display. This may probably create a window too on most platforms. Definitely on `MS windows`. Never on Android. | ||
let display_builder = glutin_winit::DisplayBuilder::new() | ||
// we might want to expose this option to users in the future. maybe using an env var or using native_options. | ||
.with_preference(glutin_winit::ApiPrefence::FallbackEgl) // https://github.com/emilk/egui/issues/2520#issuecomment-1367841150 | ||
.with_preference(glutin_winit::ApiPreference::FallbackEgl) // https://github.com/emilk/egui/issues/2520#issuecomment-1367841150 | ||
.with_window_builder(Some(create_winit_window_builder( | ||
egui_ctx, | ||
event_loop, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ fn create_event_loop(native_options: &mut epi::NativeOptions) -> EventLoop<UserE | |
let mut builder = create_event_loop_builder(native_options); | ||
|
||
crate::profile_scope!("EventLoopBuilder::build"); | ||
builder.build() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Add error handling. |
||
builder.build().unwrap() | ||
} | ||
|
||
/// Access a thread-local event loop. | ||
|
@@ -67,7 +67,7 @@ fn run_and_return( | |
event_loop: &mut EventLoop<UserEvent>, | ||
mut winit_app: impl WinitApp, | ||
) -> Result<()> { | ||
use winit::{event_loop::ControlFlow, platform::run_return::EventLoopExtRunReturn as _}; | ||
use winit::{event_loop::ControlFlow, platform::run_on_demand::EventLoopExtRunOnDemand}; | ||
|
||
log::debug!("Entering the winit event loop (run_return)…"); | ||
|
||
|
@@ -76,20 +76,22 @@ fn run_and_return( | |
|
||
let mut returned_result = Ok(()); | ||
|
||
event_loop.run_return(|event, event_loop, control_flow| { | ||
let _ = event_loop.run_on_demand(|event, event_loop_window_target| { | ||
emilk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
crate::profile_scope!("winit_event", short_event_description(&event)); | ||
|
||
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), | ||
winit::event::Event::LoopExiting => { | ||
// On Mac, Cmd-Q we get here and then `run_on_demand` doesn't return (despite its name), | ||
// so we need to save state now: | ||
log::debug!("Received Event::LoopDestroyed - saving app state…"); | ||
log::debug!("Received Event::LoopExiting - saving app state…"); | ||
winit_app.save_and_destroy(); | ||
*control_flow = ControlFlow::Exit; | ||
return; | ||
} | ||
|
||
winit::event::Event::RedrawRequested(window_id) => { | ||
winit::event::Event::WindowEvent { | ||
event: winit::event::WindowEvent::RedrawRequested, | ||
window_id, | ||
} => { | ||
windows_next_repaint_times.remove(window_id); | ||
winit_app.run_ui_and_paint(*window_id) | ||
} | ||
|
@@ -120,7 +122,7 @@ fn run_and_return( | |
EventResult::Wait | ||
} | ||
|
||
event => match winit_app.on_event(event_loop, event) { | ||
event => match winit_app.on_event(event_loop_window_target, event) { | ||
Ok(event_result) => event_result, | ||
Err(err) => { | ||
log::error!("Exiting because of error: {err} during event {event:?}"); | ||
|
@@ -132,7 +134,7 @@ fn run_and_return( | |
|
||
match event_result { | ||
EventResult::Wait => { | ||
control_flow.set_wait(); | ||
event_loop_window_target.set_control_flow(ControlFlow::Wait); | ||
} | ||
EventResult::RepaintNow(window_id) => { | ||
log::trace!("Repaint caused by {}", short_event_description(&event)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cane some of this be simplified or removed for winit 0.29? |
||
|
@@ -160,7 +162,7 @@ fn run_and_return( | |
EventResult::Exit => { | ||
log::debug!("Asking to exit event loop…"); | ||
winit_app.save_and_destroy(); | ||
*control_flow = ControlFlow::Exit; | ||
event_loop_window_target.exit(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this |
||
return; | ||
} | ||
} | ||
|
@@ -171,15 +173,18 @@ fn run_and_return( | |
use winit::event::Event; | ||
if matches!( | ||
event, | ||
Event::RedrawEventsCleared | Event::RedrawRequested(_) | Event::Resumed | ||
Event::WindowEvent { | ||
event: winit::event::WindowEvent::RedrawRequested, | ||
.. | ||
} | Event::Resumed | ||
) { | ||
windows_next_repaint_times.retain(|window_id, repaint_time| { | ||
if Instant::now() < *repaint_time { | ||
return true; | ||
}; | ||
|
||
next_repaint_time = None; | ||
control_flow.set_poll(); | ||
event_loop_window_target.set_control_flow(ControlFlow::Poll); | ||
|
||
if let Some(window) = winit_app.window(*window_id) { | ||
log::trace!("request_redraw for {window_id:?}"); | ||
|
@@ -196,7 +201,7 @@ fn run_and_return( | |
if time_until_next < std::time::Duration::from_secs(10_000) { | ||
log::trace!("WaitUntil {time_until_next:?}"); | ||
} | ||
control_flow.set_wait_until(next_repaint_time); | ||
event_loop_window_target.set_control_flow(ControlFlow::WaitUntil(next_repaint_time)); | ||
}; | ||
}); | ||
|
||
|
@@ -220,21 +225,25 @@ fn run_and_return( | |
} | ||
|
||
fn run_and_exit(event_loop: EventLoop<UserEvent>, mut winit_app: impl WinitApp + 'static) -> ! { | ||
use winit::event_loop::ControlFlow; | ||
log::debug!("Entering the winit event loop (run)…"); | ||
|
||
// When to repaint what window | ||
let mut windows_next_repaint_times = HashMap::default(); | ||
|
||
event_loop.run(move |event, event_loop, control_flow| { | ||
let result = event_loop.run(move |event, event_loop_window_target| { | ||
fornwall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
crate::profile_scope!("winit_event", short_event_description(&event)); | ||
|
||
let event_result = match &event { | ||
winit::event::Event::LoopDestroyed => { | ||
log::debug!("Received Event::LoopDestroyed"); | ||
winit::event::Event::LoopExiting => { | ||
log::debug!("Received Event::LoopExiting"); | ||
EventResult::Exit | ||
} | ||
|
||
winit::event::Event::RedrawRequested(window_id) => { | ||
winit::event::Event::WindowEvent { | ||
event: winit::event::WindowEvent::RedrawRequested, | ||
window_id, | ||
} => { | ||
windows_next_repaint_times.remove(window_id); | ||
winit_app.run_ui_and_paint(*window_id) | ||
} | ||
|
@@ -264,7 +273,7 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, mut winit_app: impl WinitApp + | |
EventResult::Wait | ||
} | ||
|
||
event => match winit_app.on_event(event_loop, event) { | ||
event => match winit_app.on_event(event_loop_window_target, event) { | ||
Ok(event_result) => event_result, | ||
Err(err) => { | ||
panic!("eframe encountered a fatal error: {err} during event {event:?}"); | ||
|
@@ -274,7 +283,7 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, mut winit_app: impl WinitApp + | |
|
||
match event_result { | ||
EventResult::Wait => { | ||
control_flow.set_wait(); | ||
event_loop_window_target.set_control_flow(ControlFlow::Wait); | ||
} | ||
EventResult::RepaintNow(window_id) => { | ||
log::trace!("Repaint caused by {}", short_event_description(&event)); | ||
|
@@ -314,15 +323,18 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, mut winit_app: impl WinitApp + | |
use winit::event::Event; | ||
if matches!( | ||
event, | ||
Event::RedrawEventsCleared | Event::RedrawRequested(_) | Event::Resumed | ||
Event::WindowEvent { | ||
event: winit::event::WindowEvent::RedrawRequested, | ||
.. | ||
} | Event::Resumed | ||
) { | ||
windows_next_repaint_times.retain(|window_id, repaint_time| { | ||
if Instant::now() < *repaint_time { | ||
return true; | ||
} | ||
|
||
next_repaint_time = None; | ||
control_flow.set_poll(); | ||
event_loop_window_target.set_control_flow(ControlFlow::Poll); | ||
|
||
if let Some(window) = winit_app.window(*window_id) { | ||
log::trace!("request_redraw for {window_id:?}"); | ||
|
@@ -350,9 +362,16 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, mut winit_app: impl WinitApp + | |
.map(|window| window.request_redraw()) | ||
}); | ||
|
||
control_flow.set_wait_until(next_repaint_time); | ||
event_loop_window_target.set_control_flow(ControlFlow::WaitUntil(next_repaint_time)); | ||
}; | ||
}) | ||
}); | ||
|
||
std::process::exit(if let Err(e) = result { | ||
log::warn!("Error from event loop: {e}"); | ||
1 | ||
} else { | ||
0 | ||
}); | ||
emilk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to remove this, as
winit::event::Event::Resumed
is handled above.Let's make sure to test on Android - there is a eframe using example in
android-activity
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're working on
glow_integration
, it's probably wise to remove this stale comment entirely:egui/crates/eframe/src/native/glow_integration.rs
Lines 36 to 46 in e037489
The issue linked there has long been closed and fixed. Windows/Surfaces are only associated with context when/while it's made current, and it looks like
eframe
supports Android now with a properResumed
/Suspended
cycle?