|
@@ -49,6 +49,7 @@ use std::{
|
|
};
|
|
};
|
|
|
|
|
|
pub(crate) type WebResourceRequestHandler = dyn Fn(&HttpRequest, &mut HttpResponse) + Send + Sync;
|
|
pub(crate) type WebResourceRequestHandler = dyn Fn(&HttpRequest, &mut HttpResponse) + Send + Sync;
|
|
|
|
+pub(crate) type NavigationHandler = dyn Fn(Url) -> bool + Send;
|
|
|
|
|
|
#[derive(Clone, Serialize)]
|
|
#[derive(Clone, Serialize)]
|
|
struct WindowCreatedEvent {
|
|
struct WindowCreatedEvent {
|
|
@@ -109,6 +110,7 @@ pub struct WindowBuilder<'a, R: Runtime> {
|
|
pub(crate) window_builder: <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder,
|
|
pub(crate) window_builder: <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder,
|
|
pub(crate) webview_attributes: WebviewAttributes,
|
|
pub(crate) webview_attributes: WebviewAttributes,
|
|
web_resource_request_handler: Option<Box<WebResourceRequestHandler>>,
|
|
web_resource_request_handler: Option<Box<WebResourceRequestHandler>>,
|
|
|
|
+ navigation_handler: Option<Box<NavigationHandler>>,
|
|
}
|
|
}
|
|
|
|
|
|
impl<'a, R: Runtime> fmt::Debug for WindowBuilder<'a, R> {
|
|
impl<'a, R: Runtime> fmt::Debug for WindowBuilder<'a, R> {
|
|
@@ -182,6 +184,7 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
|
|
window_builder: <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder::new(),
|
|
window_builder: <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder::new(),
|
|
webview_attributes: WebviewAttributes::new(url),
|
|
webview_attributes: WebviewAttributes::new(url),
|
|
web_resource_request_handler: None,
|
|
web_resource_request_handler: None,
|
|
|
|
+ navigation_handler: None,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -230,6 +233,33 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Defines a closure to be executed when the webview navigates to a URL. Returning `false` cancels the navigation.
|
|
|
|
+ ///
|
|
|
|
+ /// # Examples
|
|
|
|
+ ///
|
|
|
|
+ /// ```rust,no_run
|
|
|
|
+ /// use tauri::{
|
|
|
|
+ /// utils::config::{Csp, CspDirectiveSources, WindowUrl},
|
|
|
|
+ /// http::header::HeaderValue,
|
|
|
|
+ /// window::WindowBuilder,
|
|
|
|
+ /// };
|
|
|
|
+ /// use std::collections::HashMap;
|
|
|
|
+ /// tauri::Builder::default()
|
|
|
|
+ /// .setup(|app| {
|
|
|
|
+ /// WindowBuilder::new(app, "core", WindowUrl::App("index.html".into()))
|
|
|
|
+ /// .on_navigation(|url| {
|
|
|
|
+ /// // allow the production URL or localhost on dev
|
|
|
|
+ /// url.scheme() == "tauri" || (cfg!(dev) && url.host_str() == Some("localhost"))
|
|
|
|
+ /// })
|
|
|
|
+ /// .build()?;
|
|
|
|
+ /// Ok(())
|
|
|
|
+ /// });
|
|
|
|
+ /// ```
|
|
|
|
+ pub fn on_navigation<F: Fn(Url) -> bool + Send + 'static>(mut self, f: F) -> Self {
|
|
|
|
+ self.navigation_handler.replace(Box::new(f));
|
|
|
|
+ self
|
|
|
|
+ }
|
|
|
|
+
|
|
/// Creates a new webview window.
|
|
/// Creates a new webview window.
|
|
pub fn build(mut self) -> crate::Result<Window<R>> {
|
|
pub fn build(mut self) -> crate::Result<Window<R>> {
|
|
let web_resource_request_handler = self.web_resource_request_handler.take();
|
|
let web_resource_request_handler = self.web_resource_request_handler.take();
|
|
@@ -239,12 +269,13 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
|
|
self.label.clone(),
|
|
self.label.clone(),
|
|
)?;
|
|
)?;
|
|
let labels = self.manager.labels().into_iter().collect::<Vec<_>>();
|
|
let labels = self.manager.labels().into_iter().collect::<Vec<_>>();
|
|
- let pending = self.manager.prepare_window(
|
|
|
|
|
|
+ let mut pending = self.manager.prepare_window(
|
|
self.app_handle.clone(),
|
|
self.app_handle.clone(),
|
|
pending,
|
|
pending,
|
|
&labels,
|
|
&labels,
|
|
web_resource_request_handler,
|
|
web_resource_request_handler,
|
|
)?;
|
|
)?;
|
|
|
|
+ pending.navigation_handler = self.navigation_handler.take();
|
|
let window = match &mut self.runtime {
|
|
let window = match &mut self.runtime {
|
|
RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending),
|
|
RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending),
|
|
RuntimeOrDispatch::RuntimeHandle(handle) => handle.create_window(pending),
|
|
RuntimeOrDispatch::RuntimeHandle(handle) => handle.create_window(pending),
|