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

@ -272,16 +272,18 @@ impl EventState {
}
}
pub struct EventLoop<T> {
pub struct EventLoop {
windows: Vec<(Arc<RedoxSocket>, EventState)>,
window_target: event_loop::ActiveEventLoop,
user_events_sender: mpsc::Sender<T>,
user_events_receiver: mpsc::Receiver<T>,
user_events_sender: mpsc::SyncSender<()>,
user_events_receiver: mpsc::Receiver<()>,
}
impl<T: 'static> EventLoop<T> {
impl EventLoop {
pub(crate) fn new(_: &PlatformSpecificEventLoopAttributes) -> Result<Self, EventLoopError> {
let (user_events_sender, user_events_receiver) = mpsc::channel();
// NOTE: Create a channel which can hold only one event to automatically _squash_ user
// events.
let (user_events_sender, user_events_receiver) = mpsc::sync_channel(1);
let event_socket = Arc::new(
RedoxSocket::event()
@ -323,7 +325,7 @@ impl<T: 'static> EventLoop<T> {
})
}
fn process_event<A: ApplicationHandler<T>>(
fn process_event<A: ApplicationHandler>(
window_id: WindowId,
event_option: EventOption,
event_state: &mut EventState,
@ -500,7 +502,7 @@ impl<T: 'static> EventLoop<T> {
}
}
pub fn run_app<A: ApplicationHandler<T>>(mut self, app: &mut A) -> Result<(), EventLoopError> {
pub fn run_app<A: ApplicationHandler>(mut self, app: &mut A) -> Result<(), EventLoopError> {
let mut start_cause = StartCause::Init;
loop {
app.new_events(&self.window_target, start_cause);
@ -594,8 +596,8 @@ impl<T: 'static> EventLoop<T> {
i += 1;
}
while let Ok(event) = self.user_events_receiver.try_recv() {
app.user_event(&self.window_target, event);
while self.user_events_receiver.try_recv().is_ok() {
app.proxy_wake_up(&self.window_target);
}
// To avoid deadlocks the redraws lock is not held during event processing.
@ -683,7 +685,7 @@ impl<T: 'static> EventLoop<T> {
&self.window_target
}
pub fn create_proxy(&self) -> EventLoopProxy<T> {
pub fn create_proxy(&self) -> EventLoopProxy {
EventLoopProxy {
user_events_sender: self.user_events_sender.clone(),
wake_socket: self.window_target.p.wake_socket.clone(),
@ -691,24 +693,22 @@ impl<T: 'static> EventLoop<T> {
}
}
pub struct EventLoopProxy<T: 'static> {
user_events_sender: mpsc::Sender<T>,
pub struct EventLoopProxy {
user_events_sender: mpsc::SyncSender<()>,
wake_socket: Arc<TimeSocket>,
}
impl<T> EventLoopProxy<T> {
pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopClosed<T>> {
self.user_events_sender
.send(event)
.map_err(|mpsc::SendError(x)| event_loop::EventLoopClosed(x))?;
self.wake_socket.wake().unwrap();
Ok(())
impl EventLoopProxy {
pub fn wake_up(&self) {
// When we fail to send the event it means that we haven't woken up to read the previous
// event.
if self.user_events_sender.try_send(()).is_ok() {
self.wake_socket.wake().unwrap();
}
}
}
impl<T> Clone for EventLoopProxy<T> {
impl Clone for EventLoopProxy {
fn clone(&self) -> Self {
Self {
user_events_sender: self.user_events_sender.clone(),
@ -717,7 +717,7 @@ impl<T> Clone for EventLoopProxy<T> {
}
}
impl<T> Unpin for EventLoopProxy<T> {}
impl Unpin for EventLoopProxy {}
pub struct ActiveEventLoop {
control_flow: Cell<ControlFlow>,