native: open links in default webbrowser (#4623)
This commit is contained in:
1 parent
6cfe64209a
commit
ab19ad4624
6 files changed
+30
-2
No files matched your search
@@ -47,6 +47,7 @@ keyboard-types = { workspace = true }
|
||||
|
||||
# IO & Networking
|
||||
tokio = { workspace = true, features = ["rt-multi-thread"], optional = true }
|
||||
webbrowser = { workspace = true }
|
||||
|
||||
# Other dependencies
|
||||
tracing = { workspace = true, optional = true }
|
||||
|
||||
@@ -13,6 +13,7 @@ mod assets;
|
||||
mod contexts;
|
||||
mod dioxus_application;
|
||||
mod dioxus_renderer;
|
||||
mod link_handler;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use dioxus_native_dom::*;
|
||||
@@ -26,7 +27,10 @@ pub use dioxus_renderer::{use_wgpu, DioxusNativeWindowRenderer, Features, Limits
|
||||
|
||||
use blitz_shell::{create_default_event_loop, BlitzShellEvent, Config, WindowConfig};
|
||||
use dioxus_core::{ComponentFunction, Element, VirtualDom};
|
||||
use link_handler::DioxusNativeNavigationProvider;
|
||||
use std::any::Any;
|
||||
#[cfg(feature = "html")]
|
||||
use std::sync::Arc;
|
||||
use winit::window::WindowAttributes;
|
||||
|
||||
/// Launch an interactive HTML/CSS renderer driven by the Dioxus virtualdom
|
||||
@@ -125,16 +129,19 @@ pub fn launch_cfg_with_props<P: Clone + 'static, M: 'static>(
|
||||
let net_provider = None;
|
||||
|
||||
#[cfg(feature = "html")]
|
||||
let html_parser_provider = Some(std::sync::Arc::new(blitz_html::HtmlProvider) as _);
|
||||
let html_parser_provider = Some(Arc::new(blitz_html::HtmlProvider) as _);
|
||||
#[cfg(not(feature = "html"))]
|
||||
let html_parser_provider = None;
|
||||
|
||||
let navigation_provider = Some(Arc::new(DioxusNativeNavigationProvider) as _);
|
||||
|
||||
// Create document + window from the baked virtualdom
|
||||
let doc = DioxusDocument::new(
|
||||
vdom,
|
||||
DocumentConfig {
|
||||
net_provider,
|
||||
html_parser_provider,
|
||||
navigation_provider,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
use blitz_traits::{
|
||||
navigation::{NavigationOptions, NavigationProvider},
|
||||
net::Method,
|
||||
};
|
||||
|
||||
pub(crate) struct DioxusNativeNavigationProvider;
|
||||
|
||||
impl NavigationProvider for DioxusNativeNavigationProvider {
|
||||
fn navigate_to(&self, options: NavigationOptions) {
|
||||
if options.method == Method::GET
|
||||
&& matches!(options.url.scheme(), "http" | "https" | "mailto")
|
||||
{
|
||||
if let Err(err) = webbrowser::open(options.url.as_str()) {
|
||||
tracing::error!("Failed to open URL: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user