From 351b9fab6f37d2bce3359ac25cabc397e4295cc7 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 12 Jan 2023 10:52:24 -0600 Subject: [PATCH] allow custom namespaces --- Cargo.toml | 1 - .../src/en/getting_started/hot_reload.md | 1 + packages/dioxus/src/lib.rs | 2 +- packages/hot-reload/README.md | 11 ++++++ packages/hot-reload/src/lib.rs | 39 +++++++++++++------ 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c00f8f948..e32940e18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,7 +44,6 @@ publish = false dioxus = { path = "./packages/dioxus" } dioxus-desktop = { path = "./packages/desktop", features = ["transparent"] } dioxus-ssr = { path = "./packages/ssr" } -dioxus-hot-reload = { path = "./packages/hot-reload" } dioxus-router = { path = "./packages/router" } dioxus-signals = { path = "./packages/signals" } fermi = { path = "./packages/fermi" } diff --git a/docs/guide/src/en/getting_started/hot_reload.md b/docs/guide/src/en/getting_started/hot_reload.md index bd0cb65ac..f2b12c983 100644 --- a/docs/guide/src/en/getting_started/hot_reload.md +++ b/docs/guide/src/en/getting_started/hot_reload.md @@ -26,6 +26,7 @@ dioxus serve --hot-reload # Desktop/Liveview/TUI For desktop, LiveView, and tui, you can place the hot reload macro at the top of your main function to enable hot reloading. +Hot reloading is automatically enabled on debug builds. ## Setup diff --git a/packages/dioxus/src/lib.rs b/packages/dioxus/src/lib.rs index fe181bf14..4a7d41ea6 100644 --- a/packages/dioxus/src/lib.rs +++ b/packages/dioxus/src/lib.rs @@ -33,5 +33,5 @@ pub mod prelude { pub use dioxus_elements::{prelude::*, GlobalAttributes, SvgAttributes}; #[cfg(feature = "hot-reload")] - pub use dioxus_hot_reload::hot_reload_init; + pub use dioxus_hot_reload::{self, hot_reload_init}; } diff --git a/packages/hot-reload/README.md b/packages/hot-reload/README.md index fed458361..534bbbc98 100644 --- a/packages/hot-reload/README.md +++ b/packages/hot-reload/README.md @@ -97,6 +97,17 @@ fn main(){ } ``` +If you are using a namespace other than html, you can implement the [HotReloadingContext](https://docs.rs/dioxus-rsx/latest/dioxus_rsx/trait.HotReloadingContext.html) trait to provide a mapping between the rust names of your elements/attributes and the resultsing strings. + +You can then provide the Context to the macro to make hot reloading work with your custom namespace: + +```rust +fn main(){ + hot_reload_init!(@MyNamespace /*more configeration*/); + // launch your application +} +``` + ## Implementing hot reloading for a custom renderer To add hot reloading support to your custom renderer you can use the connect function. This will connect to the dev server you just need to provide a way to transfer `Template`s to the `VirtualDom`. Once you implement this your users can use the hot_reload_init function just like any other render. diff --git a/packages/hot-reload/src/lib.rs b/packages/hot-reload/src/lib.rs index 36ba0eaf9..ef87d4d21 100644 --- a/packages/hot-reload/src/lib.rs +++ b/packages/hot-reload/src/lib.rs @@ -6,17 +6,26 @@ use std::{ }; use dioxus_core::Template; -use dioxus_html::HtmlCtx; -use dioxus_rsx::hot_reload::{FileMap, UpdateResult}; +use dioxus_rsx::{ + hot_reload::{FileMap, UpdateResult}, + HotReloadingContext, +}; use interprocess::local_socket::{LocalSocketListener, LocalSocketStream}; use notify::{RecommendedWatcher, RecursiveMode, Watcher}; +#[cfg(debug_assertions)] +pub use dioxus_html::HtmlCtx; + /// Initialize the hot reloading listener on the given path -pub fn init(root_path: &'static str, listening_paths: &'static [&'static str], log: bool) { +pub fn init( + root_path: &'static str, + listening_paths: &'static [&'static str], + log: bool, +) { if let Ok(crate_dir) = PathBuf::from_str(root_path) { let temp_file = std::env::temp_dir().join("@dioxusin"); let channels = Arc::new(Mutex::new(Vec::new())); - let file_map = Arc::new(Mutex::new(FileMap::::new(crate_dir.clone()))); + let file_map = Arc::new(Mutex::new(FileMap::::new(crate_dir.clone()))); if let Ok(local_socket_stream) = LocalSocketListener::bind(temp_file.as_path()) { // listen for connections std::thread::spawn({ @@ -159,25 +168,33 @@ pub fn connect(mut f: impl FnMut(Template<'static>) + Send + 'static) { /// If no paths are passed, it will listen on the src and examples folders. #[macro_export] macro_rules! hot_reload_init { - ($($t: ident)*) => { + ($(@ $ctx:ident)? $($t: ident)*) => { #[cfg(debug_assertions)] - dioxus_hot_reload::init(core::env!("CARGO_MANIFEST_DIR"), &["src", "examples"], hot_reload_init!(log: $($t)*)) + dioxus_hot_reload::init::(core::env!("CARGO_MANIFEST_DIR"), &["src", "examples"], hot_reload_init!(@log: $($t)*)) }; - ($($paths: literal),* $(,)? $($t: ident)*) => { + ($(@ $ctx:ident)? $($paths: literal),* $(,)? $($t: ident)*) => { #[cfg(debug_assertions)] - dioxus_hot_reload::init(core::env!("CARGO_MANIFEST_DIR"), &[$($paths),*], hot_reload_init!(log: $($t)*)) + dioxus_hot_reload::init::(core::env!("CARGO_MANIFEST_DIR"), &[$($paths),*], hot_reload_init!(@log: $($t)*)) }; - (log:) => { + (@log:) => { false }; - (log: enable logging) => { + (@log: enable logging) => { true }; - (log: disable logging) => { + (@log: disable logging) => { false }; + + (@ctx: $ctx: ident) => { + $ctx + }; + + (@ctx: ) => { + dioxus_hot_reload::HtmlCtx + }; }