#![allow(clippy::new_without_default)]
#![allow(unused)]
use dioxus_config_macro::*;
use dioxus_core::{Element, LaunchConfig};
use std::any::Any;
use crate::prelude::*;
pub fn launch(app: fn() -> Element) {
#[allow(deprecated)]
LaunchBuilder::new().launch(app)
}
#[must_use]
pub struct LaunchBuilder {
platform: KnownPlatform,
contexts: Vec<ContextFn>,
configs: Vec<Box<dyn Any>>,
}
pub type LaunchFn = fn(fn() -> Element, Vec<ContextFn>, Vec<Box<dyn Any>>);
pub type ContextFn = Box<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>;
enum KnownPlatform {
Web,
Desktop,
Mobile,
Server,
Liveview,
Native,
Other(LaunchFn),
}
#[allow(clippy::redundant_closure)] impl LaunchBuilder {
#[cfg_attr(
all(not(any(
docsrs,
feature = "third-party-renderer",
feature = "liveview",
feature = "desktop",
feature = "mobile",
feature = "web",
feature = "fullstack",
))),
deprecated(
note = "No renderer is enabled. You must enable a renderer feature on the dioxus crate before calling the launch function.\nAdd `web`, `desktop`, `mobile`, or `fullstack` to the `features` of dioxus field in your Cargo.toml.\n# Example\n```toml\n# ...\n[dependencies]\ndioxus = { version = \"0.5.0\", features = [\"web\"] }\n# ...\n```"
)
)]
pub fn new() -> LaunchBuilder {
let platform = if cfg!(feature = "native") {
KnownPlatform::Native
} else if cfg!(feature = "desktop") {
KnownPlatform::Desktop
} else if cfg!(feature = "mobile") {
KnownPlatform::Mobile
} else if cfg!(feature = "web") {
KnownPlatform::Web
} else if cfg!(feature = "server") {
KnownPlatform::Server
} else if cfg!(feature = "liveview") {
KnownPlatform::Liveview
} else {
panic!("No platform feature enabled. Please enable one of the following features: liveview, desktop, mobile, web, tui, fullstack to use the launch API.")
};
LaunchBuilder {
platform,
contexts: Vec::new(),
configs: Vec::new(),
}
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
pub fn web() -> LaunchBuilder {
LaunchBuilder {
platform: KnownPlatform::Web,
contexts: Vec::new(),
configs: Vec::new(),
}
}
#[cfg(feature = "desktop")]
#[cfg_attr(docsrs, doc(cfg(feature = "desktop")))]
pub fn desktop() -> LaunchBuilder {
LaunchBuilder {
platform: KnownPlatform::Desktop,
contexts: Vec::new(),
configs: Vec::new(),
}
}
#[cfg(all(feature = "fullstack", feature = "server"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "fullstack", feature = "server"))))]
pub fn server() -> LaunchBuilder {
LaunchBuilder {
platform: KnownPlatform::Server,
contexts: Vec::new(),
configs: Vec::new(),
}
}
#[cfg(feature = "mobile")]
#[cfg_attr(docsrs, doc(cfg(feature = "mobile")))]
pub fn mobile() -> LaunchBuilder {
LaunchBuilder {
platform: KnownPlatform::Mobile,
contexts: Vec::new(),
configs: Vec::new(),
}
}
pub fn custom(launch_fn: LaunchFn) -> LaunchBuilder {
LaunchBuilder {
platform: KnownPlatform::Other(launch_fn),
contexts: vec![],
configs: Vec::new(),
}
}
pub fn with_context_provider(
mut self,
state: impl Fn() -> Box<dyn Any> + Send + Sync + 'static,
) -> Self {
self.contexts.push(Box::new(state));
self
}
pub fn with_context(mut self, state: impl Any + Clone + Send + Sync + 'static) -> Self {
self.contexts
.push(Box::new(move || Box::new(state.clone())));
self
}
pub fn with_cfg(mut self, config: impl LaunchConfig) -> Self {
self.configs.push(Box::new(config));
self
}
#[allow(clippy::diverging_sub_expression)]
pub fn launch(self, app: fn() -> Element) {
let Self {
platform,
contexts,
configs,
} = self;
#[cfg(feature = "logger")]
dioxus_logger::initialize_default();
#[cfg(feature = "fullstack")]
{
use dioxus_fullstack::{get_server_url, set_server_url};
#[cfg(any(feature = "desktop", feature = "mobile", feature = "native"))]
if get_server_url().is_empty() {
let serverurl = format!(
"http://{}:{}",
std::env::var("DIOXUS_DEVSERVER_IP")
.unwrap_or_else(|_| "127.0.0.1".to_string()),
std::env::var("DIOXUS_DEVSERVER_PORT").unwrap_or_else(|_| "8080".to_string())
)
.leak();
set_server_url(serverurl);
}
#[cfg(feature = "web")]
if let Some(base_path) = dioxus_cli_config::base_path() {
let base_path = base_path.trim_matches('/');
set_server_url(format!("{}/{}", get_server_url(), base_path).leak());
}
}
#[cfg(feature = "native")]
if matches!(platform, KnownPlatform::Native) {
return dioxus_native::launch_cfg(app, contexts, configs);
}
#[cfg(feature = "mobile")]
if matches!(platform, KnownPlatform::Mobile) {
return dioxus_desktop::launch::launch(app, contexts, configs);
}
#[cfg(feature = "desktop")]
if matches!(platform, KnownPlatform::Desktop) {
return dioxus_desktop::launch::launch(app, contexts, configs);
}
#[cfg(feature = "server")]
if matches!(platform, KnownPlatform::Server) {
return dioxus_server::launch_cfg(app, contexts, configs);
}
#[cfg(feature = "web")]
if matches!(platform, KnownPlatform::Web) {
return dioxus_web::launch::launch(app, contexts, configs);
}
#[cfg(feature = "liveview")]
if matches!(platform, KnownPlatform::Liveview) {
return dioxus_liveview::launch::launch(app, contexts, configs);
}
if let KnownPlatform::Other(launch_fn) = platform {
return launch_fn(app, contexts, configs);
}
if cfg!(feature = "third-party-renderer") {
panic!("No first party renderer feature enabled. It looks like you are trying to use a third party renderer. You will need to use the launch function from the third party renderer crate.");
}
panic!("No platform feature enabled. Please enable one of the following features: liveview, desktop, mobile, web, tui, fullstack to use the launch API.")
}
}