chore: remove deprecated winit method (#34638)

* Add ApplicationHandler stub

Signed-off-by: Wu Wayne <yuweiwu@pm.me>

* Implement ApplicationHandler

Signed-off-by: Wu Wayne <yuweiwu@pm.me>

* Abstract common methods

Signed-off-by: Wu Wayne <yuweiwu@pm.me>

* Impliment headless mode

Signed-off-by: Wu Yuwei <yuweiwu@pm.me>

* Apply suggestions

Signed-off-by: Wu Yuwei <yuweiwu@pm.me>

---------

Signed-off-by: Wu Wayne <yuweiwu@pm.me>
Signed-off-by: Wu Yuwei <yuweiwu@pm.me>
This commit is contained in:
Ngo Iok Ui (Wu Yu Wei) 2024-12-19 14:21:09 +09:00 committed by GitHub
parent fcf996196b
commit a0ca34d9e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 392 additions and 367 deletions

View file

@ -11,11 +11,12 @@ use std::time;
use log::warn;
use servo::embedder_traits::EventLoopWaker;
use winit::error::EventLoopError;
use winit::event::{Event, StartCause};
use winit::event_loop::{ActiveEventLoop, EventLoop as WinitEventLoop};
#[cfg(target_os = "macos")]
use winit::platform::macos::{ActivationPolicy, EventLoopBuilderExtMacOS};
use super::app::App;
/// Another process or thread has kicked the OS event loop with EventLoopWaker.
#[derive(Debug)]
pub struct WakerEvent;
@ -86,40 +87,25 @@ impl EventsLoop {
},
}
}
pub fn run_forever<F>(self, mut callback: F)
where
F: 'static + FnMut(Event<WakerEvent>, &mut ControlFlow),
{
pub fn run_app(self, app: &mut App) {
match self.0 {
EventLoop::Winit(events_loop) => {
let events_loop = events_loop.expect("Can't run an unavailable event loop.");
#[allow(deprecated)]
events_loop
.run(move |e, window_target| {
let mut control_flow = ControlFlow::default();
let _guard = EventLoopGuard::new(window_target);
callback(e, &mut control_flow);
control_flow.apply_to(window_target);
})
.run_app(app)
.expect("Failed while running events loop");
},
EventLoop::Headless(ref data) => {
let (flag, condvar) = &**data;
let mut event = Event::NewEvents(StartCause::Init);
app.init();
loop {
self.sleep(flag, condvar);
let mut control_flow = ControlFlow::Poll;
callback(event, &mut control_flow);
event = Event::<WakerEvent>::UserEvent(WakerEvent);
if control_flow != ControlFlow::Poll {
*flag.lock().unwrap() = false;
}
if control_flow == ControlFlow::Exit {
if app.handle_events_with_headless() {
break;
}
if !app.is_animating() {
*flag.lock().unwrap() = false;
}
}
},
}
@ -140,63 +126,6 @@ impl EventsLoop {
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ControlFlow {
Poll,
#[default]
Wait,
WaitUntil(std::time::Instant),
/// winit removed their ControlFlow::Exit variant in 0.29.2
Exit,
}
impl ControlFlow {
fn apply_to(self, window_target: &ActiveEventLoop) {
match self {
ControlFlow::Poll => {
window_target.set_control_flow(winit::event_loop::ControlFlow::Poll)
},
ControlFlow::Wait => {
window_target.set_control_flow(winit::event_loop::ControlFlow::Wait)
},
ControlFlow::WaitUntil(instant) => {
window_target.set_control_flow(winit::event_loop::ControlFlow::WaitUntil(instant))
},
ControlFlow::Exit => window_target.exit(),
}
}
pub fn set_poll(&mut self) {
*self = ControlFlow::Poll;
}
pub fn set_wait(&mut self) {
*self = ControlFlow::Wait;
}
#[allow(unused)]
pub fn set_wait_until(&mut self, instant: std::time::Instant) {
*self = ControlFlow::WaitUntil(instant);
}
pub fn set_exit(&mut self) {
*self = ControlFlow::Exit;
}
}
impl From<winit::event_loop::ControlFlow> for ControlFlow {
fn from(cf: winit::event_loop::ControlFlow) -> Self {
match cf {
winit::event_loop::ControlFlow::Poll => Self::Poll,
winit::event_loop::ControlFlow::Wait => Self::Wait,
winit::event_loop::ControlFlow::WaitUntil(instant) => Self::WaitUntil(instant),
}
}
}
struct HeadedEventLoopWaker {
proxy: Arc<Mutex<winit::event_loop::EventLoopProxy<WakerEvent>>>,
}
@ -241,10 +170,10 @@ thread_local! {
static CURRENT_EVENT_LOOP: Cell<Option<*const ActiveEventLoop>> = const { Cell::new(None) };
}
struct EventLoopGuard;
pub struct EventLoopGuard;
impl EventLoopGuard {
fn new(event_loop: &ActiveEventLoop) -> Self {
pub fn new(event_loop: &ActiveEventLoop) -> Self {
CURRENT_EVENT_LOOP.with(|cell| {
assert!(
cell.get().is_none(),