blitz_shell/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2
3//! A native renderer for Dioxus.
4//!
5//! ## Feature flags
6//!  - `default`: Enables the features listed below.
7//!  - `accessibility`: Enables [`accesskit`] accessibility support.
8//!  - `hot-reload`: Enables hot-reloading of Dioxus RSX.
9//!  - `menu`: Enables the [`muda`] menubar.
10//!  - `tracing`: Enables tracing support.
11
12mod application;
13mod convert_events;
14mod event;
15mod window;
16
17#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
18mod menu;
19
20#[cfg(feature = "accessibility")]
21mod accessibility;
22
23pub use crate::application::BlitzApplication;
24pub use crate::event::BlitzShellEvent;
25pub use crate::window::{View, WindowConfig};
26
27use blitz_dom::net::Resource;
28use blitz_traits::net::NetCallback;
29use blitz_traits::shell::ShellProvider;
30use std::sync::Arc;
31use winit::event_loop::{ControlFlow, EventLoop, EventLoopProxy};
32use winit::window::{CursorIcon, Window};
33
34#[derive(Default)]
35pub struct Config {
36    pub stylesheets: Vec<String>,
37    pub base_url: Option<String>,
38}
39
40/// Build an event loop for the application
41pub fn create_default_event_loop<Event>() -> EventLoop<Event> {
42    let mut ev_builder = EventLoop::<Event>::with_user_event();
43    #[cfg(target_os = "android")]
44    {
45        use winit::platform::android::EventLoopBuilderExtAndroid;
46        ev_builder.with_android_app(current_android_app());
47    }
48
49    let event_loop = ev_builder.build().unwrap();
50    event_loop.set_control_flow(ControlFlow::Wait);
51
52    event_loop
53}
54
55#[cfg(target_os = "android")]
56static ANDROID_APP: std::sync::OnceLock<android_activity::AndroidApp> = std::sync::OnceLock::new();
57
58#[cfg(target_os = "android")]
59#[cfg_attr(docsrs, doc(cfg(target_os = "android")))]
60/// Set the current [`AndroidApp`](android_activity::AndroidApp).
61pub fn set_android_app(app: android_activity::AndroidApp) {
62    ANDROID_APP.set(app).unwrap()
63}
64
65#[cfg(target_os = "android")]
66#[cfg_attr(docsrs, doc(cfg(target_os = "android")))]
67/// Get the current [`AndroidApp`](android_activity::AndroidApp).
68/// This will panic if the android activity has not been setup with [`set_android_app`].
69pub fn current_android_app() -> android_activity::AndroidApp {
70    ANDROID_APP.get().unwrap().clone()
71}
72
73/// A NetCallback that injects the fetched Resource into our winit event loop
74pub struct BlitzShellNetCallback(EventLoopProxy<BlitzShellEvent>);
75
76impl BlitzShellNetCallback {
77    pub fn new(proxy: EventLoopProxy<BlitzShellEvent>) -> Self {
78        Self(proxy)
79    }
80
81    pub fn shared(proxy: EventLoopProxy<BlitzShellEvent>) -> Arc<dyn NetCallback<Resource>> {
82        Arc::new(Self(proxy))
83    }
84}
85impl NetCallback<Resource> for BlitzShellNetCallback {
86    fn call(&self, doc_id: usize, result: Result<Resource, Option<String>>) {
87        // TODO: handle error case
88        if let Ok(data) = result {
89            self.0
90                .send_event(BlitzShellEvent::ResourceLoad { doc_id, data })
91                .unwrap()
92        }
93    }
94}
95
96pub struct BlitzShellProvider {
97    window: Arc<Window>,
98}
99impl BlitzShellProvider {
100    pub fn new(window: Arc<Window>) -> Self {
101        Self { window }
102    }
103}
104
105impl ShellProvider for BlitzShellProvider {
106    fn request_redraw(&self) {
107        self.window.request_redraw();
108    }
109    fn set_cursor(&self, icon: CursorIcon) {
110        self.window.set_cursor(icon);
111    }
112}