2023-08-13 23:20:09 +04:00
|
|
|
use std::marker::PhantomData;
|
2023-09-03 02:26:53 +02:00
|
|
|
use std::sync::mpsc::{self, Receiver, Sender};
|
2023-08-13 23:20:09 +04:00
|
|
|
|
|
|
|
|
use crate::error::EventLoopError;
|
|
|
|
|
use crate::event::Event;
|
|
|
|
|
use crate::event_loop::{ControlFlow, EventLoopWindowTarget as RootEventLoopWindowTarget};
|
|
|
|
|
|
|
|
|
|
use super::{backend, device, window};
|
|
|
|
|
|
2019-06-25 03:15:34 +02:00
|
|
|
mod proxy;
|
2023-06-14 09:43:53 +02:00
|
|
|
pub(crate) mod runner;
|
2019-06-25 03:15:34 +02:00
|
|
|
mod state;
|
|
|
|
|
mod window_target;
|
|
|
|
|
|
2023-08-13 23:20:09 +04:00
|
|
|
pub use proxy::EventLoopProxy;
|
|
|
|
|
pub use window_target::EventLoopWindowTarget;
|
2019-06-25 03:15:34 +02:00
|
|
|
|
|
|
|
|
pub struct EventLoop<T: 'static> {
|
2022-03-18 14:09:39 +01:00
|
|
|
elw: RootEventLoopWindowTarget<T>,
|
2023-09-03 02:26:53 +02:00
|
|
|
user_event_sender: Sender<T>,
|
|
|
|
|
user_event_receiver: Receiver<T>,
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-10 13:43:33 +03:00
|
|
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2022-02-16 22:09:03 +01:00
|
|
|
pub(crate) struct PlatformSpecificEventLoopAttributes {}
|
|
|
|
|
|
2019-06-25 03:15:34 +02:00
|
|
|
impl<T> EventLoop<T> {
|
2023-08-13 23:20:09 +04:00
|
|
|
pub(crate) fn new(_: &PlatformSpecificEventLoopAttributes) -> Result<Self, EventLoopError> {
|
2023-09-03 02:26:53 +02:00
|
|
|
let (user_event_sender, user_event_receiver) = mpsc::channel();
|
2023-08-13 23:20:09 +04:00
|
|
|
Ok(EventLoop {
|
2022-03-18 14:09:39 +01:00
|
|
|
elw: RootEventLoopWindowTarget {
|
|
|
|
|
p: EventLoopWindowTarget::new(),
|
2019-06-25 03:15:34 +02:00
|
|
|
_marker: PhantomData,
|
|
|
|
|
},
|
2023-09-03 02:26:53 +02:00
|
|
|
user_event_sender,
|
|
|
|
|
user_event_receiver,
|
2023-08-13 23:20:09 +04:00
|
|
|
})
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-06 01:56:56 +04:00
|
|
|
pub fn run<F>(self, mut event_handler: F) -> !
|
2022-07-14 01:17:18 +10:00
|
|
|
where
|
2023-08-06 01:56:56 +04:00
|
|
|
F: FnMut(Event<T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
|
2022-07-14 01:17:18 +10:00
|
|
|
{
|
2023-08-06 01:56:56 +04:00
|
|
|
let target = RootEventLoopWindowTarget {
|
|
|
|
|
p: self.elw.p.clone(),
|
|
|
|
|
_marker: PhantomData,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// SAFETY: Don't use `move` to make sure we leak the `event_handler` and `target`.
|
2023-09-03 02:26:53 +02:00
|
|
|
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)
|
|
|
|
|
});
|
2023-08-06 01:56:56 +04:00
|
|
|
// 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`.
|
|
|
|
|
let handler = unsafe { std::mem::transmute(handler) };
|
|
|
|
|
self.elw.p.run(handler, false);
|
2022-07-14 01:17:18 +10:00
|
|
|
|
|
|
|
|
// Throw an exception to break out of Rust execution and use unreachable to tell the
|
|
|
|
|
// compiler this function won't return, giving it a return type of '!'
|
|
|
|
|
backend::throw(
|
|
|
|
|
"Using exceptions for control flow, don't mind me. This isn't actually an error!",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
unreachable!();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-06 01:56:56 +04:00
|
|
|
pub fn spawn<F>(self, mut event_handler: F)
|
2019-06-25 03:15:34 +02:00
|
|
|
where
|
2023-07-31 00:39:01 +04:00
|
|
|
F: 'static + FnMut(Event<T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
|
2019-06-25 03:15:34 +02:00
|
|
|
{
|
2022-03-18 14:09:39 +01:00
|
|
|
let target = RootEventLoopWindowTarget {
|
2019-06-25 03:15:34 +02:00
|
|
|
p: self.elw.p.clone(),
|
|
|
|
|
_marker: PhantomData,
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-25 12:03:22 +02:00
|
|
|
self.elw.p.run(
|
2023-09-03 02:26:53 +02:00
|
|
|
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)
|
|
|
|
|
}),
|
2023-08-06 01:56:56 +04:00
|
|
|
true,
|
2023-06-25 12:03:22 +02:00
|
|
|
);
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
pub fn create_proxy(&self) -> EventLoopProxy<T> {
|
2023-09-03 02:26:53 +02:00
|
|
|
EventLoopProxy::new(self.elw.p.runner.clone(), self.user_event_sender.clone())
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
pub fn window_target(&self) -> &RootEventLoopWindowTarget<T> {
|
2019-06-25 03:15:34 +02:00
|
|
|
&self.elw
|
|
|
|
|
}
|
|
|
|
|
}
|