From a239d2ba6ac7f1f3d09de16c022ce8ca52cf0f63 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Mon, 24 Jan 2022 20:24:50 -0500 Subject: [PATCH 1/2] docs: fix web doc example and use &mut for builders everywhere --- packages/desktop/src/lib.rs | 2 +- packages/web/src/lib.rs | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index bc9a84e42..93c175cc6 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -149,7 +149,7 @@ pub fn launch_with_props( props: P, builder: impl FnOnce(&mut DesktopConfig) -> &mut DesktopConfig, ) { - let mut cfg = DesktopConfig::new(); + let mut cfg = DesktopConfig::default(); builder(&mut cfg); let event_loop = EventLoop::with_user_event(); diff --git a/packages/web/src/lib.rs b/packages/web/src/lib.rs index e1e2af491..f4266a7c8 100644 --- a/packages/web/src/lib.rs +++ b/packages/web/src/lib.rs @@ -92,6 +92,29 @@ pub fn launch(root_component: Component) { launch_with_props(root_component, (), |c| c); } +/// Launch your app and run the event loop, with configuration. +/// +/// This function will start your web app on the main web thread. +/// +/// You can configure the WebView window with a configuration closure +/// +/// ```rust +/// use dioxus::prelude::*; +/// +/// fn main() { +/// dioxus_web::launch_with_props(App, |config| config.pre_render(true)); +/// } +/// +/// fn app(cx: Scope) -> Element { +/// cx.render(rsx!{ +/// h1 {"hello world!"} +/// }) +/// } +/// ``` +pub fn launch_cfg(root: Component, config_builder: impl FnOnce(&mut WebConfig) -> &mut WebConfig) { + launch_with_props(root, (), config_builder) +} + /// Launches the VirtualDOM from the specified component function and props. /// /// This method will block the thread with `spawn_local` @@ -100,7 +123,11 @@ pub fn launch(root_component: Component) { /// /// ```rust, ignore /// fn main() { -/// dioxus_web::launch_with_props(App, RootProps { name: String::from("joe") }); +/// dioxus_web::launch_with_props( +/// App, +/// RootProps { name: String::from("joe") }, +/// |config| config +/// ); /// } /// /// #[derive(ParitalEq, Props)] @@ -112,19 +139,19 @@ pub fn launch(root_component: Component) { /// rsx!(cx, div {"hello {cx.props.name}"}) /// } /// ``` -pub fn launch_with_props( +pub fn launch_with_props( root_component: Component, root_properties: T, - configuration_builder: F, + configuration_builder: impl FnOnce(&mut WebConfig) -> &mut WebConfig, ) where T: Send + 'static, - F: FnOnce(WebConfig) -> WebConfig, { if cfg!(feature = "panic_hook") { console_error_panic_hook::set_once(); } - let config = configuration_builder(WebConfig::default()); + let mut config = WebConfig::default(); + configuration_builder(&mut config); wasm_bindgen_futures::spawn_local(run_with_props(root_component, root_properties, config)); } From ae676d9d816fcbf88d110157124443a5b3aef9eb Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Mon, 24 Jan 2022 20:25:32 -0500 Subject: [PATCH 2/2] fix: webconfig should take &mut self --- packages/web/src/cfg.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/web/src/cfg.rs b/packages/web/src/cfg.rs index cc5d18bf7..591868c34 100644 --- a/packages/web/src/cfg.rs +++ b/packages/web/src/cfg.rs @@ -30,7 +30,7 @@ impl WebConfig { /// work and suspended nodes. /// /// Dioxus will load up all the elements with the `dio_el` data attribute into memory when the page is loaded. - pub fn hydrate(mut self, f: bool) -> Self { + pub fn hydrate(&mut self, f: bool) -> &mut Self { self.hydrate = f; self } @@ -38,7 +38,7 @@ impl WebConfig { /// Set the name of the element that Dioxus will use as the root. /// /// This is akint to calling React.render() on the element with the specified name. - pub fn rootname(mut self, name: impl Into) -> Self { + pub fn rootname(&mut self, name: impl Into) -> &mut Self { self.rootname = name.into(); self } @@ -46,7 +46,7 @@ impl WebConfig { /// Set the name of the element that Dioxus will use as the root. /// /// This is akint to calling React.render() on the element with the specified name. - pub fn with_string_cache(mut self, cache: Vec) -> Self { + pub fn with_string_cache(&mut self, cache: Vec) -> &mut Self { self.cached_strings = cache; self }