api: make EventLoopProxy wrap an opaque type
The proxy is intended to be Clone, thus use `Arc` for it internally and don't require backends for it to be `Clone`. Use `EventLoopProxyProvider` to hide the backend's proxy implementation details. Co-authored-by: daxpedda <daxpedda@gmail.com>
This commit is contained in:
parent
ae4c449670
commit
3a60cbaba5
24 changed files with 256 additions and 307 deletions
|
|
@ -18,7 +18,10 @@ use super::{
|
|||
use crate::application::ApplicationHandler;
|
||||
use crate::error::{EventLoopError, NotSupportedError, RequestError};
|
||||
use crate::event::{self, Ime, Modifiers, StartCause};
|
||||
use crate::event_loop::{self, ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents};
|
||||
use crate::event_loop::{
|
||||
self, ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
|
||||
EventLoopProxy as CoreEventLoopProxy, EventLoopProxyProvider,
|
||||
};
|
||||
use crate::keyboard::{
|
||||
Key, KeyCode, KeyLocation, ModifiersKeys, ModifiersState, NamedKey, NativeKey, NativeKeyCode,
|
||||
PhysicalKey,
|
||||
|
|
@ -286,8 +289,7 @@ impl EventLoop {
|
|||
let event_socket =
|
||||
Arc::new(RedoxSocket::event().map_err(|error| os_error!(format!("{error}")))?);
|
||||
|
||||
let wake_socket =
|
||||
Arc::new(TimeSocket::open().map_err(|error| os_error!(format!("{error}")))?);
|
||||
let wake_socket = TimeSocket::open().map_err(|error| os_error!(format!("{error}")))?;
|
||||
|
||||
event_socket
|
||||
.write(&syscall::Event {
|
||||
|
|
@ -306,8 +308,7 @@ impl EventLoop {
|
|||
redraws: Arc::new(Mutex::new(VecDeque::new())),
|
||||
destroys: Arc::new(Mutex::new(VecDeque::new())),
|
||||
event_socket,
|
||||
wake_socket,
|
||||
user_events_sender,
|
||||
event_loop_proxy: Arc::new(EventLoopProxy { wake_socket, user_events_sender }),
|
||||
},
|
||||
user_events_receiver,
|
||||
})
|
||||
|
|
@ -664,11 +665,11 @@ impl EventLoop {
|
|||
|
||||
pub struct EventLoopProxy {
|
||||
user_events_sender: mpsc::SyncSender<()>,
|
||||
wake_socket: Arc<TimeSocket>,
|
||||
pub(super) wake_socket: TimeSocket,
|
||||
}
|
||||
|
||||
impl EventLoopProxy {
|
||||
pub fn wake_up(&self) {
|
||||
impl EventLoopProxyProvider for EventLoopProxy {
|
||||
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() {
|
||||
|
|
@ -677,15 +678,6 @@ impl EventLoopProxy {
|
|||
}
|
||||
}
|
||||
|
||||
impl Clone for EventLoopProxy {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
user_events_sender: self.user_events_sender.clone(),
|
||||
wake_socket: self.wake_socket.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Unpin for EventLoopProxy {}
|
||||
|
||||
pub struct ActiveEventLoop {
|
||||
|
|
@ -695,18 +687,12 @@ pub struct ActiveEventLoop {
|
|||
pub(super) redraws: Arc<Mutex<VecDeque<WindowId>>>,
|
||||
pub(super) destroys: Arc<Mutex<VecDeque<WindowId>>>,
|
||||
pub(super) event_socket: Arc<RedoxSocket>,
|
||||
pub(super) wake_socket: Arc<TimeSocket>,
|
||||
user_events_sender: mpsc::SyncSender<()>,
|
||||
pub(super) event_loop_proxy: Arc<EventLoopProxy>,
|
||||
}
|
||||
|
||||
impl RootActiveEventLoop for ActiveEventLoop {
|
||||
fn create_proxy(&self) -> event_loop::EventLoopProxy {
|
||||
event_loop::EventLoopProxy {
|
||||
event_loop_proxy: EventLoopProxy {
|
||||
user_events_sender: self.user_events_sender.clone(),
|
||||
wake_socket: self.wake_socket.clone(),
|
||||
},
|
||||
}
|
||||
fn create_proxy(&self) -> CoreEventLoopProxy {
|
||||
CoreEventLoopProxy::new(self.event_loop_proxy.clone())
|
||||
}
|
||||
|
||||
fn create_window(
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use std::{fmt, str};
|
|||
|
||||
use smol_str::SmolStr;
|
||||
|
||||
pub(crate) use self::event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle};
|
||||
pub(crate) use self::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle};
|
||||
use crate::dpi::{PhysicalPosition, PhysicalSize};
|
||||
use crate::keyboard::Key;
|
||||
mod event_loop;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
use std::collections::VecDeque;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use super::{ActiveEventLoop, MonitorHandle, RedoxSocket, TimeSocket, WindowProperties};
|
||||
use super::event_loop::EventLoopProxy;
|
||||
use super::{ActiveEventLoop, MonitorHandle, RedoxSocket, WindowProperties};
|
||||
use crate::cursor::Cursor;
|
||||
use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
|
||||
use crate::error::{NotSupportedError, RequestError};
|
||||
|
|
@ -23,7 +24,7 @@ pub struct Window {
|
|||
window_socket: Arc<RedoxSocket>,
|
||||
redraws: Arc<Mutex<VecDeque<WindowId>>>,
|
||||
destroys: Arc<Mutex<VecDeque<WindowId>>>,
|
||||
wake_socket: Arc<TimeSocket>,
|
||||
event_loop_proxy: Arc<EventLoopProxy>,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
|
|
@ -113,13 +114,13 @@ impl Window {
|
|||
creates.push_back(window_socket.clone());
|
||||
}
|
||||
|
||||
el.wake_socket.wake().unwrap();
|
||||
el.event_loop_proxy.wake_socket.wake().unwrap();
|
||||
|
||||
Ok(Self {
|
||||
window_socket,
|
||||
redraws: el.redraws.clone(),
|
||||
destroys: el.destroys.clone(),
|
||||
wake_socket: el.wake_socket.clone(),
|
||||
event_loop_proxy: el.event_loop_proxy.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -184,7 +185,7 @@ impl CoreWindow for Window {
|
|||
if !redraws.contains(&window_id) {
|
||||
redraws.push_back(window_id);
|
||||
|
||||
self.wake_socket.wake().unwrap();
|
||||
self.event_loop_proxy.wake_socket.wake().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -475,6 +476,6 @@ impl Drop for Window {
|
|||
destroys.push_back(self.id());
|
||||
}
|
||||
|
||||
self.wake_socket.wake().unwrap();
|
||||
self.event_loop_proxy.wake_socket.wake().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue