1 | //! An event loop proxy. |
2 | |
3 | use std::sync::mpsc::SendError; |
4 | |
5 | use sctk::reexports::calloop::channel::Sender; |
6 | |
7 | use crate::event_loop::EventLoopClosed; |
8 | |
9 | /// A handle that can be sent across the threads and used to wake up the `EventLoop`. |
10 | pub struct EventLoopProxy<T: 'static> { |
11 | user_events_sender: Sender<T>, |
12 | } |
13 | |
14 | impl<T: 'static> Clone for EventLoopProxy<T> { |
15 | fn clone(&self) -> Self { |
16 | EventLoopProxy { |
17 | user_events_sender: self.user_events_sender.clone(), |
18 | } |
19 | } |
20 | } |
21 | |
22 | impl<T: 'static> EventLoopProxy<T> { |
23 | pub fn new(user_events_sender: Sender<T>) -> Self { |
24 | Self { user_events_sender } |
25 | } |
26 | |
27 | pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> { |
28 | self.user_events_sender |
29 | .send(event) |
30 | .map_err(|SendError(error: T)| EventLoopClosed(error)) |
31 | } |
32 | } |
33 | |