Skip to content

Commit

Permalink
Simplify the launch builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff committed Sep 16, 2024
1 parent 983fcfc commit d4afc24
Show file tree
Hide file tree
Showing 21 changed files with 175 additions and 388 deletions.
2 changes: 1 addition & 1 deletion examples/dynamic_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use dioxus::prelude::*;
const STYLE: &str = asset!("./examples/assets/custom_assets.css");

fn main() {
launch_desktop(app);
LaunchBuilder::desktop().launch(app);
}

fn app() -> Element {
Expand Down
2 changes: 1 addition & 1 deletion examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use dioxus::prelude::*;

fn main() {
launch_desktop(app);
LaunchBuilder::desktop().launch(app);
}

fn app() -> Element {
Expand Down
2 changes: 1 addition & 1 deletion examples/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dioxus::prelude::*;
use std::rc::Rc;

fn main() {
launch_desktop(app);
LaunchBuilder::desktop().launch(app);
}

fn app() -> Element {
Expand Down
2 changes: 1 addition & 1 deletion examples/shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use dioxus::desktop::use_global_shortcut;
use dioxus::prelude::*;

fn main() {
launch_desktop(app);
LaunchBuilder::desktop().launch(app);
}

fn app() -> Element {
Expand Down
2 changes: 1 addition & 1 deletion examples/video_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
// For the sake of this example, we will download the video file if it doesn't exist
ensure_video_is_loaded();

launch_desktop(app);
LaunchBuilder::desktop().launch(app);
}

fn app() -> Element {
Expand Down
2 changes: 1 addition & 1 deletion examples/window_zoom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use dioxus::prelude::*;

fn main() {
launch_desktop(app);
LaunchBuilder::desktop().launch(app);
}

fn app() -> Element {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/launch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! This module contains utilities renderers use to integrate with the launch function.

/// A marker trait for platform configs. We use this marker to
/// make sure that the user doesn't accidentally pass in a config
/// builder instead of the config
pub trait LaunchConfig: 'static {}

impl LaunchConfig for () {}
1 change: 1 addition & 0 deletions packages/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod events;
mod fragment;
mod generational_box;
mod global_context;
pub mod launch;
mod mutations;
mod nodes;
mod properties;
Expand Down
3 changes: 3 additions & 0 deletions packages/desktop/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use dioxus_core::launch::LaunchConfig;
use std::borrow::Cow;
use std::path::PathBuf;
use tao::window::{Icon, WindowBuilder};
Expand Down Expand Up @@ -51,6 +52,8 @@ pub struct Config {
pub(crate) last_window_close_behavior: WindowCloseBehaviour,
}

impl LaunchConfig for Config {}

pub(crate) type WryProtocol = (
String,
Box<dyn Fn(HttpRequest<Vec<u8>>) -> HttpResponse<Cow<'static, [u8]>> + 'static>,
Expand Down
11 changes: 8 additions & 3 deletions packages/desktop/src/launch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use crate::Config;
use crate::Config;
use crate::{
app::App,
ipc::{IpcMethod, UserWindowEvent},
Expand Down Expand Up @@ -85,14 +85,19 @@ pub fn launch_virtual_dom(virtual_dom: VirtualDom, desktop_config: Config) -> !
/// Launches the WebView and runs the event loop, with configuration and root props.
pub fn launch(
root: fn() -> Element,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any>>>,
platform_config: Config,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
platform_config: Vec<Box<dyn Any>>,
) -> ! {
let mut virtual_dom = VirtualDom::new(root);

for context in contexts {
virtual_dom.insert_any_root_context(context());
}

let platform_config = *platform_config
.into_iter()
.find_map(|cfg| cfg.downcast::<Config>().ok())
.unwrap_or_default();

launch_virtual_dom(virtual_dom, platform_config)
}
Loading

0 comments on commit d4afc24

Please sign in to comment.