Skip to content

Commit

Permalink
Remove T from EventLoopTargetWindow (rust-windowing#3081)
Browse files Browse the repository at this point in the history
Co-authored-by: nerditation <[email protected]>
  • Loading branch information
2 people authored and kchibisov committed Oct 17, 2023
1 parent 604dfe3 commit b9ba73d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 48 deletions.
35 changes: 31 additions & 4 deletions src/platform_impl/web/event_loop/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::marker::PhantomData;
use std::sync::mpsc::{self, Receiver, Sender};

use crate::error::EventLoopError;
use crate::event::Event;
Expand All @@ -16,18 +17,23 @@ pub use window_target::EventLoopWindowTarget;

pub struct EventLoop<T: 'static> {
elw: RootEventLoopWindowTarget<T>,
user_event_sender: Sender<T>,
user_event_receiver: Receiver<T>,
}

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct PlatformSpecificEventLoopAttributes {}

impl<T> EventLoop<T> {
pub(crate) fn new(_: &PlatformSpecificEventLoopAttributes) -> Result<Self, EventLoopError> {
let (user_event_sender, user_event_receiver) = mpsc::channel();
Ok(EventLoop {
elw: RootEventLoopWindowTarget {
p: EventLoopWindowTarget::new(),
_marker: PhantomData,
},
user_event_sender,
user_event_receiver,
})
}

Expand All @@ -41,8 +47,18 @@ impl<T> EventLoop<T> {
};

// SAFETY: Don't use `move` to make sure we leak the `event_handler` and `target`.
let handler: Box<dyn FnMut(_, _)> =
Box::new(|event, flow| event_handler(event, &target, flow));
let handler: Box<dyn FnMut(Event<()>, _)> = Box::new(|event, flow| {
let event = match event.map_nonuser_event() {
Ok(event) => event,
Err(Event::UserEvent(())) => Event::UserEvent(
self.user_event_receiver
.try_recv()
.expect("handler woken up without user event"),
),
Err(_) => unreachable!(),
};
event_handler(event, &target, flow)
});
// SAFETY: The `transmute` is necessary because `run()` requires `'static`. This is safe
// because this function will never return and all resources not cleaned up by the point we
// `throw` will leak, making this actually `'static`.
Expand All @@ -68,13 +84,24 @@ impl<T> EventLoop<T> {
};

self.elw.p.run(
Box::new(move |event, flow| event_handler(event, &target, flow)),
Box::new(move |event, flow| {
let event = match event.map_nonuser_event() {
Ok(event) => event,
Err(Event::UserEvent(())) => Event::UserEvent(
self.user_event_receiver
.try_recv()
.expect("handler woken up without user event"),
),
Err(_) => unreachable!(),
};
event_handler(event, &target, flow)
}),
true,
);
}

pub fn create_proxy(&self) -> EventLoopProxy<T> {
self.elw.p.proxy()
EventLoopProxy::new(self.elw.p.runner.clone(), self.user_event_sender.clone())
}

pub fn window_target(&self) -> &RootEventLoopWindowTarget<T> {
Expand Down
13 changes: 10 additions & 3 deletions src/platform_impl/web/event_loop/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
use std::sync::mpsc::Sender;

use super::runner;
use crate::event::Event;
use crate::event_loop::EventLoopClosed;
use crate::platform_impl::platform::r#async::Channel;

pub struct EventLoopProxy<T: 'static> {
runner: Channel<runner::Shared<T>, T>,
// used to wake the event loop handler, not to actually pass data
runner: Channel<runner::Shared, ()>,
sender: Sender<T>,
}

impl<T: 'static> EventLoopProxy<T> {
pub fn new(runner: runner::Shared<T>) -> Self {
pub fn new(runner: runner::Shared, sender: Sender<T>) -> Self {
Self {
runner: Channel::new(runner, |runner, event| {
runner.send_event(Event::UserEvent(event))
})
.unwrap(),
sender,
}
}

pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
self.runner.send(event);
self.sender.send(event).unwrap();
self.runner.send(());
Ok(())
}
}
Expand All @@ -27,6 +33,7 @@ impl<T: 'static> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
Self {
runner: self.runner.clone(),
sender: self.sender.clone(),
}
}
}
59 changes: 28 additions & 31 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ use wasm_bindgen::prelude::Closure;
use web_sys::{Document, KeyboardEvent, PageTransitionEvent, PointerEvent, WheelEvent};
use web_time::{Duration, Instant};

pub struct Shared<T: 'static>(Rc<Execution<T>>);
pub struct Shared(Rc<Execution>);

pub(super) type EventHandler<T> = dyn FnMut(Event<T>, &mut ControlFlow);
pub(super) type EventHandler = dyn FnMut(Event<()>, &mut ControlFlow);

impl<T> Clone for Shared<T> {
impl Clone for Shared {
fn clone(&self) -> Self {
Shared(self.0.clone())
}
}

type OnEventHandle<T> = RefCell<Option<EventListenerHandle<dyn FnMut(T)>>>;

pub struct Execution<T: 'static> {
runner: RefCell<RunnerEnum<T>>,
pub struct Execution {
runner: RefCell<RunnerEnum>,
suspended: Cell<bool>,
event_loop_recreation: Cell<bool>,
events: RefCell<VecDeque<EventWrapper<T>>>,
events: RefCell<VecDeque<EventWrapper>>,
id: RefCell<u32>,
window: web_sys::Window,
document: Document,
Expand All @@ -57,33 +57,33 @@ pub struct Execution<T: 'static> {
on_touch_end: OnEventHandle<web_sys::Event>,
}

enum RunnerEnum<T: 'static> {
enum RunnerEnum {
/// The `EventLoop` is created but not being run.
Pending,
/// The `EventLoop` is being run.
Running(Runner<T>),
Running(Runner),
/// The `EventLoop` is exited after being started with `EventLoop::run`. Since
/// `EventLoop::run` takes ownership of the `EventLoop`, we can be certain
/// that this event loop will never be run again.
Destroyed,
}

impl<T: 'static> RunnerEnum<T> {
fn maybe_runner(&self) -> Option<&Runner<T>> {
impl RunnerEnum {
fn maybe_runner(&self) -> Option<&Runner> {
match self {
RunnerEnum::Running(runner) => Some(runner),
_ => None,
}
}
}

struct Runner<T: 'static> {
struct Runner {
state: State,
event_handler: Box<EventHandler<T>>,
event_handler: Box<EventHandler>,
}

impl<T: 'static> Runner<T> {
pub fn new(event_handler: Box<EventHandler<T>>) -> Self {
impl Runner {
pub fn new(event_handler: Box<EventHandler>) -> Self {
Runner {
state: State::Init,
event_handler,
Expand All @@ -110,8 +110,8 @@ impl<T: 'static> Runner<T> {

fn handle_single_event(
&mut self,
runner: &Shared<T>,
event: impl Into<EventWrapper<T>>,
runner: &Shared,
event: impl Into<EventWrapper>,
control: &mut ControlFlow,
) {
let is_closed = matches!(*control, ControlFlow::ExitWithCode(_));
Expand Down Expand Up @@ -141,7 +141,7 @@ impl<T: 'static> Runner<T> {
}
}

impl<T: 'static> Shared<T> {
impl Shared {
pub fn new() -> Self {
#[allow(clippy::disallowed_methods)]
let window = web_sys::window().expect("only callable from inside the `Window`");
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<T: 'static> Shared<T> {
// 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 RootEventLoopWindowTarget reference
pub fn set_listener(&self, event_handler: Box<EventHandler<T>>) {
pub fn set_listener(&self, event_handler: Box<EventHandler>) {
{
let mut runner = self.0.runner.borrow_mut();
assert!(matches!(*runner, RunnerEnum::Pending));
Expand Down Expand Up @@ -457,7 +457,7 @@ impl<T: 'static> Shared<T> {

pub fn request_redraw(&self, id: WindowId) {
self.0.redraw_pending.borrow_mut().insert(id);
self.send_events::<EventWrapper<T>>(iter::empty());
self.send_events::<EventWrapper>(iter::empty());
}

pub fn init(&self) {
Expand Down Expand Up @@ -485,17 +485,14 @@ impl<T: 'static> Shared<T> {
// Add an event to the event loop runner, from the user or an event handler
//
// It will determine if the event should be immediately sent to the user or buffered for later
pub(crate) fn send_event<E: Into<EventWrapper<T>>>(&self, event: E) {
pub(crate) fn send_event<E: Into<EventWrapper>>(&self, event: E) {
self.send_events(iter::once(event));
}

// Add a series of events to the event loop runner
//
// It will determine if the event should be immediately sent to the user or buffered for later
pub(crate) fn send_events<E: Into<EventWrapper<T>>>(
&self,
events: impl IntoIterator<Item = E>,
) {
pub(crate) fn send_events<E: Into<EventWrapper>>(&self, events: impl IntoIterator<Item = E>) {
// If the event loop is closed, it should discard any new events
if self.is_closed() {
return;
Expand Down Expand Up @@ -573,7 +570,7 @@ impl<T: 'static> Shared<T> {
// cleared
//
// This will also process any events that have been queued or that are queued during processing
fn run_until_cleared<E: Into<EventWrapper<T>>>(&self, events: impl Iterator<Item = E>) {
fn run_until_cleared<E: Into<EventWrapper>>(&self, events: impl Iterator<Item = E>) {
let mut control = self.current_control_flow();
for event in events {
self.handle_event(event.into(), &mut control);
Expand Down Expand Up @@ -613,7 +610,7 @@ impl<T: 'static> Shared<T> {
// handle_event takes in events and either queues them or applies a callback
//
// It should only ever be called from `run_until_cleared`.
fn handle_event(&self, event: impl Into<EventWrapper<T>>, control: &mut ControlFlow) {
fn handle_event(&self, event: impl Into<EventWrapper>, control: &mut ControlFlow) {
if self.is_closed() {
*control = ControlFlow::Exit;
}
Expand Down Expand Up @@ -721,7 +718,7 @@ impl<T: 'static> Shared<T> {
// * The `register_redraw_request` closure.
// * The `destroy_fn` closure.
if self.0.event_loop_recreation.get() {
crate::event_loop::EventLoopBuilder::<T>::allow_event_loop_recreation();
crate::event_loop::EventLoopBuilder::<()>::allow_event_loop_recreation();
}
}

Expand Down Expand Up @@ -779,17 +776,17 @@ impl<T: 'static> Shared<T> {
}
}

pub(crate) enum EventWrapper<T: 'static> {
Event(Event<T>),
pub(crate) enum EventWrapper {
Event(Event<()>),
ScaleChange {
canvas: Weak<RefCell<backend::Canvas>>,
size: PhysicalSize<u32>,
scale: f64,
},
}

impl<T> From<Event<T>> for EventWrapper<T> {
fn from(value: Event<T>) -> Self {
impl From<Event<()>> for EventWrapper {
fn from(value: Event<()>) -> Self {
Self::Event(value)
}
}
13 changes: 6 additions & 7 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::cell::{Cell, RefCell};
use std::clone::Clone;
use std::collections::{vec_deque::IntoIter as VecDequeIter, VecDeque};
use std::iter;
use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::atomic::Ordering;

Expand All @@ -12,7 +13,6 @@ use super::{
super::{monitor::MonitorHandle, KeyEventExtra},
backend,
device::DeviceId,
proxy::EventLoopProxy,
runner,
window::WindowId,
};
Expand Down Expand Up @@ -43,15 +43,17 @@ impl Clone for ModifiersShared {
}

pub struct EventLoopWindowTarget<T: 'static> {
pub(crate) runner: runner::Shared<T>,
pub(crate) runner: runner::Shared,
modifiers: ModifiersShared,
_marker: PhantomData<T>,
}

impl<T> Clone for EventLoopWindowTarget<T> {
fn clone(&self) -> Self {
Self {
runner: self.runner.clone(),
modifiers: self.modifiers.clone(),
_marker: PhantomData,
}
}
}
Expand All @@ -61,14 +63,11 @@ impl<T> EventLoopWindowTarget<T> {
Self {
runner: runner::Shared::new(),
modifiers: ModifiersShared::default(),
_marker: PhantomData,
}
}

pub fn proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy::new(self.runner.clone())
}

pub fn run(&self, event_handler: Box<runner::EventHandler<T>>, event_loop_recreation: bool) {
pub fn run(&self, event_handler: Box<runner::EventHandler>, event_loop_recreation: bool) {
self.runner.event_loop_recreation(event_loop_recreation);
self.runner.set_listener(event_handler);
}
Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,10 @@ impl Canvas {
self.animation_frame_handler.request();
}

pub(crate) fn handle_scale_change<T: 'static>(
pub(crate) fn handle_scale_change(
&self,
runner: &super::super::event_loop::runner::Shared<T>,
event_handler: impl FnOnce(crate::event::Event<T>),
runner: &super::super::event_loop::runner::Shared,
event_handler: impl FnOnce(crate::event::Event<()>),
current_size: PhysicalSize<u32>,
scale: f64,
) {
Expand Down

0 comments on commit b9ba73d

Please sign in to comment.