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 std::sync::Arc;
30use winit::event_loop::EventLoopProxy;
31use winit::event_loop::{ControlFlow, EventLoop};
32
33#[derive(Default)]
34pub struct Config {
35    pub stylesheets: Vec<String>,
36    pub base_url: Option<String>,
37}
38
39/// Build an event loop for the application
40pub fn create_default_event_loop<Event>() -> EventLoop<Event> {
41    let mut ev_builder = EventLoop::<Event>::with_user_event();
42    #[cfg(target_os = "android")]
43    {
44        use winit::platform::android::EventLoopBuilderExtAndroid;
45        ev_builder.with_android_app(current_android_app());
46    }
47
48    let event_loop = ev_builder.build().unwrap();
49    event_loop.set_control_flow(ControlFlow::Wait);
50
51    event_loop
52}
53
54#[cfg(target_os = "android")]
55static ANDROID_APP: std::sync::OnceLock<android_activity::AndroidApp> = std::sync::OnceLock::new();
56
57#[cfg(target_os = "android")]
58#[cfg_attr(docsrs, doc(cfg(target_os = "android")))]
59/// Set the current [`AndroidApp`](android_activity::AndroidApp).
60pub fn set_android_app(app: android_activity::AndroidApp) {
61    ANDROID_APP.set(app).unwrap()
62}
63
64#[cfg(target_os = "android")]
65#[cfg_attr(docsrs, doc(cfg(target_os = "android")))]
66/// Get the current [`AndroidApp`](android_activity::AndroidApp).
67/// This will panic if the android activity has not been setup with [`set_android_app`].
68pub fn current_android_app() -> android_activity::AndroidApp {
69    ANDROID_APP.get().unwrap().clone()
70}
71
72/// A NetCallback that injects the fetched Resource into our winit event loop
73pub struct BlitzShellNetCallback(EventLoopProxy<BlitzShellEvent>);
74
75impl BlitzShellNetCallback {
76    pub fn new(proxy: EventLoopProxy<BlitzShellEvent>) -> Self {
77        Self(proxy)
78    }
79
80    pub fn shared(proxy: EventLoopProxy<BlitzShellEvent>) -> Arc<dyn NetCallback<Data = Resource>> {
81        Arc::new(Self(proxy))
82    }
83}
84impl NetCallback for BlitzShellNetCallback {
85    type Data = Resource;
86    fn call(&self, doc_id: usize, data: Self::Data) {
87        self.0
88            .send_event(BlitzShellEvent::ResourceLoad { doc_id, data })
89            .unwrap()
90    }
91}