remove some generics from LaunchBuilder

This commit is contained in:
Evan Almloff committed 2024-01-17 20:15:16 -06:00
1 parent 16e2c3f2f2
commit 25ad3da659
14 files changed
+142 -304

No files matched your search

+59 -108
View File
@@ -5,83 +5,46 @@ use std::any::Any;
use crate::prelude::*;
use dioxus_core::VProps;
use dioxus_core::{prelude::*, AnyProps};
use dioxus_core::{BoxedContext, CrossPlatformConfig, PlatformBuilder};
#[cfg(feature = "fullstack")]
macro_rules! impl_launch_builder {
($($inner:item)*) => {
impl<F, Props: Clone + Send + Sync + 'static, M: Send + Sync + 'static>
LaunchBuilder<VProps<F, Props, M>>
where F: ComponentFunction<Props, M> + Send + Sync + 'static
{
$($inner)*
}
};
}
#[cfg(not(feature = "fullstack"))]
macro_rules! impl_launch_builder {
($($inner:item)*) => {
impl<F, Props: Clone + 'static, M: 'static>
LaunchBuilder<VProps<F, Props, M>>
where F: ComponentFunction<Props, M> + 'static
{
$($inner)*
}
};
}
/// A builder for a fullstack app.
pub struct LaunchBuilder<P: AnyProps, Platform: PlatformBuilder<P> = CurrentPlatform> {
cross_platform_config: CrossPlatformConfig<P>,
platform_config: Option<<Platform as PlatformBuilder<P>>::Config>,
pub struct LaunchBuilder {
cross_platform_config: fn() -> Element,
#[cfg(feature = "fullstack")]
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send>>,
#[cfg(not(feature = "fullstack"))]
contexts: Vec<Box<dyn Fn() -> Box<dyn Any>>>,
platform_config: Option<current_platform::Config>,
}
// Fullstack platform builder
impl_launch_builder! {
impl LaunchBuilder {
/// Create a new builder for your application. This will create a launch configuration for the current platform based on the features enabled on the `dioxus` crate.
pub fn new(component: F) -> Self
where
Props: Default,
{
pub fn new(component: fn() -> Element) -> Self {
Self {
cross_platform_config: CrossPlatformConfig::new(VProps::new(
component,
|_, _| true,
Default::default(),
"root",
)),
cross_platform_config: component,
contexts: Vec::new(),
platform_config: None,
}
}
/// Create a new builder for your application with some root props. This will create a launch configuration for the current platform based on the features enabled on the `dioxus` crate.
pub fn new_with_props(component: F, props: Props) -> Self {
Self {
cross_platform_config: CrossPlatformConfig::new(VProps::new(
component,
|_, _| true,
props,
"root",
)),
platform_config: None,
}
#[cfg(feature = "fullstack")]
/// Inject state into the root component's context.
pub fn context(mut self, state: impl Any + Clone + Send + 'static) -> Self {
self.contexts
.push(Box::new(move || Box::new(state.clone())));
self
}
}
impl<P: AnyProps, Platform: PlatformBuilder<P>> LaunchBuilder<P, Platform> {
// /// Inject state into the root component's context.
// pub fn context(mut self, state: impl Any + Clone + 'static) -> Self {
// self.cross_platform_config
// .push_context(BoxedContext::new(state));
// self
// }
#[cfg(not(feature = "fullstack"))]
/// Inject state into the root component's context.
pub fn context(mut self, state: impl Any + Clone + 'static) -> Self {
self.contexts
.push(Box::new(move || Box::new(state.clone())));
self
}
/// Provide a platform-specific config to the builder.
pub fn cfg(
mut self,
config: impl Into<Option<<Platform as PlatformBuilder<P>>::Config>>,
) -> Self {
pub fn cfg(mut self, config: impl Into<Option<current_platform::Config>>) -> Self {
if let Some(config) = config.into() {
self.platform_config = Some(config);
}
@@ -91,99 +54,87 @@ impl<P: AnyProps, Platform: PlatformBuilder<P>> LaunchBuilder<P, Platform> {
#[allow(clippy::unit_arg)]
/// Launch the app.
pub fn launch(self) {
Platform::launch(
current_platform::launch(
self.cross_platform_config,
Default::default(),
self.platform_config.unwrap_or_default(),
);
}
}
#[cfg(feature = "web")]
impl<P: AnyProps> LaunchBuilder<P, dioxus_web::WebPlatform> {
impl LaunchBuilder {
/// Launch your web application.
pub fn launch_web(self) {
dioxus_web::WebPlatform::launch(
dioxus_web::launch::launch(
self.cross_platform_config,
self.platform_config.unwrap_or_default(),
Default::default(),
Default::default(),
);
}
}
#[cfg(feature = "desktop")]
impl<P: AnyProps> LaunchBuilder<P, dioxus_desktop::DesktopPlatform> {
impl LaunchBuilder {
/// Launch your desktop application.
pub fn launch_desktop(self) {
dioxus_desktop::DesktopPlatform::launch(
dioxus_desktop::launch::launch(
self.cross_platform_config,
self.platform_config.unwrap_or_default(),
Default::default(),
Default::default(),
);
}
}
#[cfg(feature = "fullstack")]
impl<P: AnyProps + Clone + Send + Sync> LaunchBuilder<P, dioxus_fullstack::FullstackPlatform> {
impl LaunchBuilder {
/// Launch your fullstack application.
pub fn launch_fullstack(self) {
dioxus_fullstack::FullstackPlatform::launch(
dioxus_fullstack::launch::launch(
self.cross_platform_config,
self.platform_config.unwrap_or_default(),
Default::default(),
Default::default(),
);
}
}
#[cfg(feature = "fullstack")]
type CurrentPlatform = dioxus_fullstack::FullstackPlatform;
#[cfg(all(feature = "desktop", not(feature = "fullstack")))]
type CurrentPlatform = dioxus_desktop::DesktopPlatform;
#[cfg(all(feature = "web", not(any(feature = "desktop", feature = "fullstack"))))]
type CurrentPlatform = dioxus_web::WebPlatform;
#[cfg(not(any(feature = "desktop", feature = "web", feature = "fullstack")))]
type CurrentPlatform = ();
#[cfg(feature = "fullstack")]
/// Launch your application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch<Props, Marker>(component: impl ComponentFunction<Props, Marker> + Send + Sync)
where
Props: Default + Send + Sync + Clone + 'static,
Marker: Send + Sync + 'static,
{
LaunchBuilder::new(component).launch()
mod current_platform {
#[cfg(all(feature = "desktop", not(feature = "fullstack")))]
pub use dioxus_desktop::launch::*;
#[cfg(feature = "fullstack")]
pub use dioxus_fullstack::launch::*;
#[cfg(all(feature = "web", not(any(feature = "desktop", feature = "fullstack"))))]
pub use dioxus_web::launch::*;
#[cfg(not(any(feature = "desktop", feature = "web", feature = "fullstack")))]
type Config = ();
#[cfg(not(any(feature = "desktop", feature = "web", feature = "fullstack")))]
pub fn launch(
root: fn() -> Element,
contexts: Vec<Box<dyn CloneAny + Send + Sync>>,
platform_config: Config,
) {
}
}
#[cfg(not(feature = "fullstack"))]
/// Launch your application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch<Props, Marker: 'static>(component: impl ComponentFunction<Props, Marker>)
where
Props: Default + Clone + 'static,
{
pub fn launch(component: fn() -> Element) {
LaunchBuilder::new(component).launch()
}
#[cfg(all(feature = "web", not(feature = "fullstack")))]
/// Launch your web application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch_web<Props, Marker: 'static>(component: impl ComponentFunction<Props, Marker>)
where
Props: Default + Clone + 'static,
{
pub fn launch_web(component: fn() -> Element) {
LaunchBuilder::new(component).launch_web()
}
#[cfg(all(feature = "desktop", not(feature = "fullstack")))]
/// Launch your desktop application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch_desktop<Props, Marker: 'static>(component: impl ComponentFunction<Props, Marker>)
where
Props: Default + Clone + 'static,
{
pub fn launch_desktop(component: fn() -> Element) {
LaunchBuilder::new(component).launch_desktop()
}
#[cfg(feature = "fullstack")]
/// Launch your fullstack application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch_fullstack<Props, Marker>(
component: impl ComponentFunction<Props, Marker> + Send + Sync,
) where
Props: Default + Send + Sync + Clone + 'static,
Marker: Send + Sync + 'static,
{
pub fn launch_fullstack(component: fn() -> Element) {
LaunchBuilder::new(component).launch_fullstack()
}