From 0c0938c7df6ab57051957ffe4c06a2506b95b5bb Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sat, 6 Jul 2024 17:29:55 +0200 Subject: [PATCH] feat: Allow custom Tokio Runtimes --- Cargo.toml | 2 ++ crates/freya/Cargo.toml | 4 +++- crates/freya/src/launch.rs | 9 +++++++++ crates/renderer/src/renderer.rs | 7 ------- examples/custom_tokio_rt.rs | 17 +++++++++++++++++ 5 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 examples/custom_tokio_rt.rs diff --git a/Cargo.toml b/Cargo.toml index d416f658e..e8cb3a964 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ log = ["freya/log"] devtools = ["freya/devtools"] use_camera = ["freya/use_camera"] hot-reload = ["freya/hot-reload"] +skia = ["freya/skia"] +custom-tokio-rt = ["freya/custom-tokio-rt"] [patch.crates-io] # dioxus = { git = "https://github.com/DioxusLabs/dioxus", rev = "7beacdf9c76ae5412d3c2bcd55f7c5d87f486a0f" } diff --git a/crates/freya/Cargo.toml b/crates/freya/Cargo.toml index c3e257604..fa2cf2070 100644 --- a/crates/freya/Cargo.toml +++ b/crates/freya/Cargo.toml @@ -16,12 +16,14 @@ features = ["freya-engine/mocked-engine"] no-default-features = true [features] +custom-tokio-rt = [] +skia = ["freya-engine/skia-engine"] hot-reload = ["freya-renderer/hot-reload", "dioxus/hot-reload"] log = ["dep:tracing", "dep:tracing-subscriber"] devtools = ["dep:freya-devtools"] use_camera = ["freya-hooks/use_camera"] mocked-engine-development = ["freya-engine/mocked-engine"] # This is just for the CI -default = ["freya-engine/skia-engine"] +default = ["skia"] [dependencies] freya-devtools = { workspace = true, optional = true } diff --git a/crates/freya/src/launch.rs b/crates/freya/src/launch.rs index 1a744b938..b3a3906f9 100644 --- a/crates/freya/src/launch.rs +++ b/crates/freya/src/launch.rs @@ -223,6 +223,15 @@ pub fn launch_cfg(app: AppComponent, config: LaunchConfig (vdom, None, None) } }; + #[cfg(not(feature = "custom-tokio-rt"))] + { + let rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(); + let _guard = rt.enter(); + } + DesktopRenderer::launch(vdom, sdom, config, devtools, hovered_node); } diff --git a/crates/renderer/src/renderer.rs b/crates/renderer/src/renderer.rs index 4fe7cfc21..97462bdba 100644 --- a/crates/renderer/src/renderer.rs +++ b/crates/renderer/src/renderer.rs @@ -80,13 +80,6 @@ impl<'a, State: Clone + 'static> DesktopRenderer<'a, State> { devtools: Option, hovered_node: HoveredNode, ) { - let rt = tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .unwrap(); - - let _guard = rt.enter(); - let event_loop = EventLoop::::with_user_event() .build() .expect("Failed to create event loop."); diff --git a/examples/custom_tokio_rt.rs b/examples/custom_tokio_rt.rs new file mode 100644 index 000000000..7517240a9 --- /dev/null +++ b/examples/custom_tokio_rt.rs @@ -0,0 +1,17 @@ +#![cfg_attr( + all(not(debug_assertions), target_os = "windows"), + windows_subsystem = "windows" +)] + +use freya::prelude::*; + +#[cfg(not(feature = "custom-tokio-rt"))] +fn main() { + panic!("Run this example without the `custom-tokio-rt` feature."); +} + +#[cfg(feature = "custom-tokio-rt")] +#[tokio::main] +async fn main() { + launch_with_title(|| rsx!(label { "Hello, World!" }), "Custom Tokio Runtime") +}