Skip to main content

blitz_shell/
event.rs

1use blitz_traits::navigation::NavigationOptions;
2use blitz_traits::net::NetWaker;
3use futures_util::task::ArcWake;
4use std::sync::mpsc::{Receiver, Sender, channel};
5use std::{any::Any, sync::Arc};
6use winit::{event_loop::EventLoopProxy, window::WindowId};
7
8#[cfg(feature = "accessibility")]
9use accesskit_xplat::WindowEvent as AccessKitEvent;
10
11#[derive(Debug, Clone)]
12pub enum BlitzShellEvent {
13    Poll {
14        window_id: WindowId,
15    },
16
17    /// The renderer for this window has finished its async initialization. The
18    /// embedder should call `View::complete_resume` to transition the view into
19    /// an active state.
20    ResumeReady {
21        window_id: WindowId,
22    },
23
24    RequestRedraw {
25        doc_id: usize,
26    },
27
28    /// An accessibility event from `accesskit`.
29    #[cfg(feature = "accessibility")]
30    Accessibility {
31        window_id: WindowId,
32        data: Arc<AccessKitEvent>,
33    },
34
35    /// An arbitary event from the Blitz embedder
36    Embedder(Arc<dyn Any + Send + Sync>),
37
38    /// Navigate to another URL (triggered by e.g. clicking a link)
39    Navigate(Box<NavigationOptions>),
40
41    /// Navigate to another URL (triggered by e.g. clicking a link)
42    NavigationLoad {
43        url: String,
44        contents: String,
45        retain_scroll_position: bool,
46        is_md: bool,
47    },
48
49    /// Delivered after the WASM resize-debounce window expires. Route to
50    /// `View::apply_pending_resize_if_settled`, which applies the pending
51    /// size iff motion has actually settled.
52    #[cfg(target_arch = "wasm32")]
53    ResizeSettleCheck {
54        window_id: WindowId,
55    },
56}
57impl BlitzShellEvent {
58    pub fn embedder_event<T: Any + Send + Sync>(value: T) -> Self {
59        let boxed = Arc::new(value) as Arc<dyn Any + Send + Sync>;
60        Self::Embedder(boxed)
61    }
62}
63
64#[derive(Clone)]
65pub struct BlitzShellProxy(Arc<BlitzShellProxyInner>);
66pub struct BlitzShellProxyInner {
67    winit_proxy: EventLoopProxy,
68    sender: Sender<BlitzShellEvent>,
69}
70
71impl BlitzShellProxy {
72    pub fn new(winit_proxy: EventLoopProxy) -> (Self, Receiver<BlitzShellEvent>) {
73        let (sender, receiver) = channel();
74        let proxy = Self(Arc::new(BlitzShellProxyInner {
75            winit_proxy,
76            sender,
77        }));
78        (proxy, receiver)
79    }
80
81    pub fn wake_up(&self) {
82        self.0.winit_proxy.wake_up();
83    }
84    pub fn send_event(&self, event: impl Into<BlitzShellEvent>) {
85        self.send_event_impl(event.into());
86    }
87    fn send_event_impl(&self, event: BlitzShellEvent) {
88        let _ = self.0.sender.send(event);
89        self.wake_up();
90    }
91}
92
93impl NetWaker for BlitzShellProxy {
94    fn wake(&self, client_id: usize) {
95        self.send_event_impl(BlitzShellEvent::RequestRedraw { doc_id: client_id })
96    }
97}
98
99/// Create a waker that will send a poll event to the event loop.
100///
101/// This lets the VirtualDom "come up for air" and process events while the main thread is blocked by the WebView.
102///
103/// All other IO lives in the Tokio runtime,
104pub fn create_waker(proxy: &BlitzShellProxy, id: WindowId) -> std::task::Waker {
105    struct DomHandle {
106        proxy: BlitzShellProxy,
107        id: WindowId,
108    }
109    impl ArcWake for DomHandle {
110        fn wake_by_ref(arc_self: &Arc<Self>) {
111            let event = BlitzShellEvent::Poll {
112                window_id: arc_self.id,
113            };
114            arc_self.proxy.send_event(event)
115        }
116    }
117
118    let proxy = proxy.clone();
119    futures_util::task::waker(Arc::new(DomHandle { id, proxy }))
120}