2023-03-04 05:37:11 +01:00
|
|
|
use crate::futures::futures::{
|
2025-02-21 01:22:56 +01:00
|
|
|
Future, Sink, StreamExt,
|
2020-02-04 03:28:47 +01:00
|
|
|
channel::mpsc,
|
2024-04-17 15:54:12 +02:00
|
|
|
select,
|
2020-01-19 10:17:08 +01:00
|
|
|
task::{Context, Poll},
|
|
|
|
|
};
|
2025-10-24 17:23:40 +02:00
|
|
|
use crate::graphics::shell;
|
2024-06-14 01:47:39 +02:00
|
|
|
use crate::runtime::Action;
|
2025-10-24 17:23:40 +02:00
|
|
|
use crate::runtime::window;
|
2020-01-19 10:17:08 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
2024-04-16 21:50:28 +02:00
|
|
|
/// An event loop proxy with backpressure that implements `Sink`.
|
2020-05-21 00:37:47 +02:00
|
|
|
#[derive(Debug)]
|
2024-06-14 01:47:39 +02:00
|
|
|
pub struct Proxy<T: 'static> {
|
|
|
|
|
raw: winit::event_loop::EventLoopProxy<Action<T>>,
|
|
|
|
|
sender: mpsc::Sender<Action<T>>,
|
2024-04-16 21:50:28 +02:00
|
|
|
notifier: mpsc::Sender<usize>,
|
2020-01-19 10:17:08 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-14 01:47:39 +02:00
|
|
|
impl<T: 'static> Clone for Proxy<T> {
|
2020-01-19 10:17:08 +01:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
raw: self.raw.clone(),
|
2024-04-16 21:50:28 +02:00
|
|
|
sender: self.sender.clone(),
|
|
|
|
|
notifier: self.notifier.clone(),
|
2020-01-19 10:17:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 01:47:39 +02:00
|
|
|
impl<T: 'static> Proxy<T> {
|
2024-04-16 21:50:28 +02:00
|
|
|
const MAX_SIZE: usize = 100;
|
|
|
|
|
|
2020-05-28 02:49:32 +02:00
|
|
|
/// Creates a new [`Proxy`] from an `EventLoopProxy`.
|
2024-04-16 21:50:28 +02:00
|
|
|
pub fn new(
|
2024-06-14 01:47:39 +02:00
|
|
|
raw: winit::event_loop::EventLoopProxy<Action<T>>,
|
2024-04-16 21:50:28 +02:00
|
|
|
) -> (Self, impl Future<Output = ()>) {
|
2024-04-17 15:54:12 +02:00
|
|
|
let (notifier, mut processed) = mpsc::channel(Self::MAX_SIZE);
|
|
|
|
|
let (sender, mut receiver) = mpsc::channel(Self::MAX_SIZE);
|
2024-04-16 21:50:28 +02:00
|
|
|
let proxy = raw.clone();
|
|
|
|
|
|
|
|
|
|
let worker = async move {
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
if count < Self::MAX_SIZE {
|
2024-04-17 15:54:12 +02:00
|
|
|
select! {
|
|
|
|
|
message = receiver.select_next_some() => {
|
2024-04-16 21:50:28 +02:00
|
|
|
let _ = proxy.send_event(message);
|
|
|
|
|
count += 1;
|
2024-04-17 15:54:12 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
amount = processed.select_next_some() => {
|
|
|
|
|
count = count.saturating_sub(amount);
|
2024-04-16 21:50:28 +02:00
|
|
|
}
|
2024-04-17 15:54:12 +02:00
|
|
|
complete => break,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
select! {
|
|
|
|
|
amount = processed.select_next_some() => {
|
2024-04-16 21:50:28 +02:00
|
|
|
count = count.saturating_sub(amount);
|
|
|
|
|
}
|
2024-04-17 15:54:12 +02:00
|
|
|
complete => break,
|
2024-04-16 21:50:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
Self {
|
|
|
|
|
raw,
|
|
|
|
|
sender,
|
|
|
|
|
notifier,
|
|
|
|
|
},
|
|
|
|
|
worker,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 01:47:39 +02:00
|
|
|
/// Sends a value to the event loop.
|
2024-04-16 21:50:28 +02:00
|
|
|
///
|
|
|
|
|
/// Note: This skips the backpressure mechanism with an unbounded
|
|
|
|
|
/// channel. Use sparingly!
|
2025-08-29 09:07:19 +02:00
|
|
|
pub fn send(&self, value: T) {
|
2024-06-19 01:53:40 +02:00
|
|
|
self.send_action(Action::Output(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sends an action to the event loop.
|
|
|
|
|
///
|
|
|
|
|
/// Note: This skips the backpressure mechanism with an unbounded
|
|
|
|
|
/// channel. Use sparingly!
|
2025-08-29 08:39:44 +02:00
|
|
|
pub fn send_action(&self, action: Action<T>) {
|
2025-06-06 22:58:59 +02:00
|
|
|
let _ = self.raw.send_event(action);
|
2024-04-16 21:50:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Frees an amount of slots for additional messages to be queued in
|
|
|
|
|
/// this [`Proxy`].
|
|
|
|
|
pub fn free_slots(&mut self, amount: usize) {
|
|
|
|
|
let _ = self.notifier.start_send(amount);
|
2020-01-19 10:17:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 01:47:39 +02:00
|
|
|
impl<T: 'static> Sink<Action<T>> for Proxy<T> {
|
2020-02-04 03:28:47 +01:00
|
|
|
type Error = mpsc::SendError;
|
2020-01-19 10:17:08 +01:00
|
|
|
|
|
|
|
|
fn poll_ready(
|
2024-04-16 21:50:28 +02:00
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
|
cx: &mut Context<'_>,
|
2020-01-19 10:17:08 +01:00
|
|
|
) -> Poll<Result<(), Self::Error>> {
|
2024-04-16 21:50:28 +02:00
|
|
|
self.sender.poll_ready(cx)
|
2020-01-19 10:17:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn start_send(
|
2024-04-16 21:50:28 +02:00
|
|
|
mut self: Pin<&mut Self>,
|
2024-06-14 01:47:39 +02:00
|
|
|
action: Action<T>,
|
2020-01-19 10:17:08 +01:00
|
|
|
) -> Result<(), Self::Error> {
|
2024-06-14 01:47:39 +02:00
|
|
|
self.sender.start_send(action)
|
2020-01-19 10:17:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn poll_flush(
|
2024-04-16 21:50:28 +02:00
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
|
cx: &mut Context<'_>,
|
2020-01-19 10:17:08 +01:00
|
|
|
) -> Poll<Result<(), Self::Error>> {
|
2024-04-16 21:50:28 +02:00
|
|
|
match self.sender.poll_ready(cx) {
|
|
|
|
|
Poll::Ready(Err(ref e)) if e.is_disconnected() => {
|
|
|
|
|
// If the receiver disconnected, we consider the sink to be flushed.
|
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
|
}
|
|
|
|
|
x => x,
|
|
|
|
|
}
|
2020-01-19 10:17:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn poll_close(
|
2024-04-16 21:50:28 +02:00
|
|
|
mut self: Pin<&mut Self>,
|
2020-01-19 10:17:08 +01:00
|
|
|
_cx: &mut Context<'_>,
|
|
|
|
|
) -> Poll<Result<(), Self::Error>> {
|
2024-04-16 21:50:28 +02:00
|
|
|
self.sender.disconnect();
|
2020-01-19 10:17:08 +01:00
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-24 17:23:40 +02:00
|
|
|
|
|
|
|
|
impl<T> shell::Notifier for Proxy<T>
|
|
|
|
|
where
|
|
|
|
|
T: Send,
|
|
|
|
|
{
|
|
|
|
|
fn request_redraw(&self) {
|
|
|
|
|
self.send_action(Action::Window(window::Action::RedrawAll));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn invalidate_layout(&self) {
|
|
|
|
|
self.send_action(Action::Window(window::Action::RelayoutAll));
|
|
|
|
|
}
|
|
|
|
|
}
|