#![cfg_attr(docsrs, feature(doc_cfg))]
mod assets;
mod config;
mod contexts;
mod dioxus_application;
mod dioxus_renderer;
mod link_handler;
#[cfg(feature = "prelude")]
pub mod prelude;
#[doc(inline)]
pub use dioxus_native_dom::*;
pub use anyrender_vello::{CustomPaintCtx, CustomPaintSource, DeviceHandle, TextureHandle};
use assets::DioxusNativeNetProvider;
pub use dioxus_application::{DioxusNativeApplication, DioxusNativeEvent};
pub use dioxus_renderer::{DioxusNativeWindowRenderer, Features, Limits};
#[cfg(not(all(target_os = "ios", target_abi = "sim")))]
pub use dioxus_renderer::use_wgpu;
pub use config::Config;
pub use winit::dpi::{LogicalSize, PhysicalSize};
pub use winit::window::WindowAttributes;
use blitz_shell::{create_default_event_loop, BlitzShellEvent, WindowConfig};
use dioxus_core::{ComponentFunction, Element, VirtualDom};
use link_handler::DioxusNativeNavigationProvider;
use std::any::Any;
use std::sync::Arc;
pub fn launch(app: fn() -> Element) {
launch_cfg(app, vec![], vec![])
}
pub fn launch_cfg(
app: fn() -> Element,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
cfg: Vec<Box<dyn Any>>,
) {
launch_cfg_with_props(app, (), contexts, cfg)
}
pub fn launch_cfg_with_props<P: Clone + 'static, M: 'static>(
app: impl ComponentFunction<P, M>,
props: P,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
configs: Vec<Box<dyn Any>>,
) {
macro_rules! try_read_config {
($input:ident, $store:ident, $kind:ty) => {
match $input.downcast::<$kind>() {
Ok(value) => {
$store = Some(*value);
continue;
}
Err(cfg) => cfg,
}
};
}
let mut features = None;
let mut limits = None;
let mut window_attributes = None;
let mut config = None;
for mut cfg in configs {
cfg = try_read_config!(cfg, features, Features);
cfg = try_read_config!(cfg, limits, Limits);
cfg = try_read_config!(cfg, window_attributes, WindowAttributes);
cfg = try_read_config!(cfg, config, Config);
let _ = cfg;
}
let mut config = config.unwrap_or_default();
if let Some(window_attributes) = window_attributes {
config.window_attributes = window_attributes;
}
let event_loop = create_default_event_loop::<BlitzShellEvent>();
#[cfg(feature = "net")]
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
#[cfg(feature = "net")]
let _guard = rt.enter();
#[cfg(all(
feature = "hot-reload",
debug_assertions,
not(target_os = "android"),
not(target_os = "ios")
))]
{
let proxy = event_loop.create_proxy();
dioxus_devtools::connect(move |event| {
let dxn_event = DioxusNativeEvent::DevserverEvent(event);
let _ = proxy.send_event(BlitzShellEvent::embedder_event(dxn_event));
})
}
let mut vdom = VirtualDom::new_with_props(app, props);
for context in contexts {
vdom.insert_any_root_context(context());
}
let net_provider = Some(DioxusNativeNetProvider::shared(event_loop.create_proxy()));
#[cfg(feature = "html")]
let html_parser_provider = Some(Arc::new(blitz_html::HtmlProvider) as _);
#[cfg(not(feature = "html"))]
let html_parser_provider = None;
let navigation_provider = Some(Arc::new(DioxusNativeNavigationProvider) as _);
let doc = DioxusDocument::new(
vdom,
DocumentConfig {
net_provider,
html_parser_provider,
navigation_provider,
..Default::default()
},
);
#[cfg(not(all(target_os = "ios", target_abi = "sim")))]
let renderer = DioxusNativeWindowRenderer::with_features_and_limits(features, limits);
#[cfg(all(target_os = "ios", target_abi = "sim"))]
let renderer = DioxusNativeWindowRenderer::new();
let config = WindowConfig::with_attributes(
Box::new(doc) as _,
renderer.clone(),
config.window_attributes,
);
let mut application = DioxusNativeApplication::new(event_loop.create_proxy(), config);
event_loop.run_app(&mut application).unwrap();
}