#![cfg_attr(docsrs, feature(doc_cfg))] //! A native renderer for Dioxus. //! //! ## Feature flags //! - `default`: Enables the features listed below. //! - `accessibility`: Enables [`accesskit`] accessibility support. //! - `hot-reload`: Enables hot-reloading of Dioxus RSX. //! - `menu`: Enables the [`muda`] menubar. //! - `tracing`: Enables tracing support. mod assets; mod contexts; mod dioxus_application; mod dioxus_document; mod event; mod event_handler; mod mutation_writer; pub use dioxus_application::DioxusNativeApplication; pub use dioxus_document::DioxusDocument; pub use event::DioxusNativeEvent; use blitz_shell::{create_default_event_loop, BlitzShellEvent, Config, WindowConfig}; use dioxus_core::{ComponentFunction, Element, VirtualDom}; use std::any::Any; type NodeId = usize; /// Launch an interactive HTML/CSS renderer driven by the Dioxus virtualdom pub fn launch(app: fn() -> Element) { launch_cfg(app, vec![], vec![]) } pub fn launch_cfg( app: fn() -> Element, contexts: Vec Box + Send + Sync>>, cfg: Vec>, ) { launch_cfg_with_props(app, (), contexts, cfg) } // todo: props shouldn't have the clone bound - should try and match dioxus-desktop behavior pub fn launch_cfg_with_props( app: impl ComponentFunction, props: P, contexts: Vec Box + Send + Sync>>, _cfg: Vec>, ) { let _cfg = _cfg .into_iter() .find_map(|cfg| cfg.downcast::().ok()) .unwrap_or_default(); let event_loop = create_default_event_loop::(); // Turn on the runtime and enter it let rt = tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap(); let _guard = rt.enter(); // Setup hot-reloading if enabled. #[cfg(all( feature = "hot-reload", debug_assertions, not(target_os = "android"), not(target_os = "ios") ))] { use crate::event::DioxusNativeEvent; if let Some(endpoint) = dioxus_cli_config::devserver_ws_endpoint() { let proxy = event_loop.create_proxy(); dioxus_devtools::connect(endpoint, move |event| { let dxn_event = DioxusNativeEvent::DevserverEvent(event); let _ = proxy.send_event(BlitzShellEvent::embedder_event(dxn_event)); }) } } // Spin up the virtualdom // We're going to need to hit it with a special waker // Note that we are delaying the initialization of window-specific contexts (net provider, document, etc) let mut vdom = VirtualDom::new_with_props(app, props); // Add contexts for context in contexts { vdom.insert_any_root_context(context()); } // Create application let mut application = DioxusNativeApplication::new(event_loop.create_proxy(), vdom); // Run event loop event_loop.run_app(&mut application).unwrap(); }