workflow_egui/runtime/
events.rs

1use crate::imports::*;
2
3pub type ApplicationEventsChannel = crate::runtime::channel::Channel<RuntimeEvent>;
4
5#[derive(Clone, Debug)]
6pub struct ApplicationEvent(Arc<dyn Any + Send + Sync>);
7
8impl ApplicationEvent {
9    pub fn new<T>(event: T) -> Self
10    where
11        T: Any + Send + Sync + 'static,
12    {
13        ApplicationEvent(Arc::new(event))
14    }
15
16    #[allow(clippy::should_implement_trait)]
17    pub fn as_ref<T>(&self) -> &T
18    where
19        T: Any,
20    {
21        self.0.downcast_ref::<T>().unwrap()
22    }
23
24    // pub fn into<T>(self) -> T
25    // where
26    //     T: Any + Send + Sync,
27    // {
28    //     let this = self
29    //         .0
30    //         .downcast::<T>()
31    //         .expect("unknown application event type");
32    //     // Arc::into_inner(this).expect("multiple references to application event type")
33    //     Arc::unwrap_or_clone(this).expect("multiple references to application event type")
34    // }
35
36    pub fn as_arc<T>(self) -> Arc<T>
37    where
38        T: Any + Send + Sync,
39    {
40        self.0
41            .downcast::<T>()
42            .expect("unknown application event type")
43    }
44}
45
46#[derive(Clone, Debug)]
47pub enum RuntimeEvent {
48    Error(String),
49    Exit,
50    VisibilityState(VisibilityState),
51    Application(ApplicationEvent),
52}
53
54impl RuntimeEvent {
55    pub fn new<T>(event: T) -> Self
56    where
57        T: Any + Send + Sync + 'static,
58    {
59        RuntimeEvent::Application(ApplicationEvent::new(event))
60    }
61}