event_loop: remove generic user event

Let the users wake up the event loop and then they could poll their
user sources.

Co-authored-by: Mads Marquart <mads@marquart.dk>
Co-authored-by: daxpedda <daxpedda@gmail.com>
This commit is contained in:
Kirill Chibisov 2024-06-24 13:04:55 +03:00 committed by GitHub
parent 7d1287958f
commit ecb887e5c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
44 changed files with 675 additions and 966 deletions

View file

@ -18,7 +18,6 @@ use js_sys::Function;
use std::cell::{Cell, RefCell};
use std::collections::{HashSet, VecDeque};
use std::iter;
use std::num::NonZeroUsize;
use std::ops::Deref;
use std::rc::{Rc, Weak};
use wasm_bindgen::prelude::{wasm_bindgen, Closure};
@ -28,7 +27,7 @@ use web_time::{Duration, Instant};
pub struct Shared(Rc<Execution>);
pub(super) type EventHandler = dyn FnMut(Event<()>);
pub(super) type EventHandler = dyn FnMut(Event);
impl Clone for Shared {
fn clone(&self) -> Self {
@ -137,13 +136,12 @@ impl Shared {
let document = window.document().expect("Failed to obtain document");
Shared(Rc::<Execution>::new_cyclic(|weak| {
let proxy_spawner =
WakerSpawner::new(main_thread, weak.clone(), |runner, count, local| {
if let Some(runner) = runner.upgrade() {
Shared(runner).send_user_events(count, local)
}
})
.expect("`EventLoop` has to be created in the main thread");
let proxy_spawner = WakerSpawner::new(main_thread, weak.clone(), |runner, local| {
if let Some(runner) = runner.upgrade() {
Shared(runner).send_proxy_wake_up(local);
}
})
.expect("`EventLoop` has to be created in the main thread");
Execution {
main_thread,
@ -203,7 +201,7 @@ impl Shared {
// Set the event callback to use for the event loop runner
// This the event callback is a fairly thin layer over the user-provided callback that closes
// over a RootActiveEventLoop reference
pub fn set_listener(&self, event_handler: Box<EventHandler>) {
pub(crate) fn set_listener(&self, event_handler: Box<EventHandler>) {
{
let mut runner = self.0.runner.borrow_mut();
assert!(matches!(*runner, RunnerEnum::Pending));
@ -466,11 +464,11 @@ impl Shared {
self.send_events(iter::once(event));
}
// Add a series of user events to the event loop runner
// Add a user event to the event loop runner.
//
// This will schedule the event loop to wake up instead of waking it up immediately if its not
// running.
pub(crate) fn send_user_events(&self, count: NonZeroUsize, local: bool) {
pub(crate) fn send_proxy_wake_up(&self, local: bool) {
// If the event loop is closed, it should discard any new events
if self.is_closed() {
return;
@ -492,9 +490,7 @@ impl Shared {
let this = Rc::downgrade(&self.0);
move || {
if let Some(shared) = this.upgrade() {
Shared(shared).send_events(
iter::repeat(Event::UserEvent(())).take(count.get()),
)
Shared(shared).send_event(Event::UserWakeUp)
}
}
})
@ -505,7 +501,7 @@ impl Shared {
}
}
self.send_events(iter::repeat(Event::UserEvent(())).take(count.get()))
self.send_event(Event::UserWakeUp);
}
// Add a series of events to the event loop runner
@ -648,8 +644,10 @@ impl Shared {
// Pre-fetch `UserEvent`s to avoid having to wait until the next event loop cycle.
events.extend(
iter::repeat(Event::UserEvent(()))
.take(self.0.proxy_spawner.fetch())
self.0
.proxy_spawner
.take()
.then_some(Event::UserWakeUp)
.map(EventWrapper::from),
);
@ -737,7 +735,7 @@ impl Shared {
// * The `register_redraw_request` closure.
// * The `destroy_fn` closure.
if self.0.event_loop_recreation.get() {
crate::event_loop::EventLoopBuilder::<()>::allow_event_loop_recreation();
crate::event_loop::EventLoopBuilder::allow_event_loop_recreation();
}
}
@ -817,12 +815,12 @@ impl Shared {
}
pub(crate) enum EventWrapper {
Event(Event<()>),
Event(Event),
ScaleChange { canvas: Weak<RefCell<backend::Canvas>>, size: PhysicalSize<u32>, scale: f64 },
}
impl From<Event<()>> for EventWrapper {
fn from(value: Event<()>) -> Self {
impl From<Event> for EventWrapper {
fn from(value: Event) -> Self {
Self::Event(value)
}
}