| 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 { user_events_sender: self.user_events_sender.clone() } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | impl<T: 'static> EventLoopProxy<T> { |
| 21 | pub fn new(user_events_sender: Sender<T>) -> Self { |
| 22 | Self { user_events_sender } |
| 23 | } |
| 24 | |
| 25 | pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> { |
| 26 | self.user_events_sender.send(event).map_err(|SendError(error: T)| EventLoopClosed(error)) |
| 27 | } |
| 28 | } |
| 29 | |