From 572a8fed88db70518042c81e555d6ee586007d6d Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 30 May 2024 20:02:51 +0300 Subject: [PATCH 1/2] feat: expose `WebviewWinowBuilder::on_download` closes #9921 --- .changes/webview-builder-on-download.md | 5 +++ core/tauri/src/webview/webview_window.rs | 48 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 .changes/webview-builder-on-download.md diff --git a/.changes/webview-builder-on-download.md b/.changes/webview-builder-on-download.md new file mode 100644 index 000000000000..b84649bcc066 --- /dev/null +++ b/.changes/webview-builder-on-download.md @@ -0,0 +1,5 @@ +--- +"tauri": "patch:feat" +--- + +Add `WebviewWindowBuilder::on_download`. diff --git a/core/tauri/src/webview/webview_window.rs b/core/tauri/src/webview/webview_window.rs index e96c73b24962..a8bc5ccb1370 100644 --- a/core/tauri/src/webview/webview_window.rs +++ b/core/tauri/src/webview/webview_window.rs @@ -44,6 +44,8 @@ use tauri_macros::default_runtime; #[cfg(windows)] use windows::Win32::Foundation::HWND; +use super::DownloadEvent; + /// A builder for [`WebviewWindow`], a window that hosts a single webview. pub struct WebviewWindowBuilder<'a, R: Runtime, M: Manager> { window_builder: WindowBuilder<'a, R, M>, @@ -263,6 +265,52 @@ tauri::Builder::default() self } + /// Set a download event handler to be notified when a download is requested or finished. + /// + /// Returning `false` prevents the download from happening on a [`DownloadEvent::Requested`] event. + /// + /// # Examples + /// + #[cfg_attr( + feature = "unstable", + doc = r####" +```rust,no_run +use tauri::{ + utils::config::{Csp, CspDirectiveSources, WebviewUrl}, + webview::{DownloadEvent, WebviewWindowBuilder}, +}; + +tauri::Builder::default() + .setup(|app| { + let webview_window = WebviewWindowBuilder::new("core", WebviewUrl::App("index.html".into())) + .on_download(|webview, event| { + match event { + DownloadEvent::Requested { url, destination } => { + println!("downloading {}", url); + *destination = "/home/tauri/target/path".into(); + } + DownloadEvent::Finished { url, path, success } => { + println!("downloaded {} to {:?}, success: {}", url, path, success); + } + _ => (), + } + // let the download start + true + }).build()?; + + Ok(()) + }); +``` + "#### + )] + pub fn on_download, DownloadEvent<'_>) -> bool + Send + Sync + 'static>( + mut self, + f: F, + ) -> Self { + self.webview_builder.download_handler.replace(Arc::new(f)); + self + } + /// Defines a closure to be executed when a page load event is triggered. /// The event can be either [`tauri_runtime::webview::PageLoadEvent::Started`] if the page has started loading /// or [`tauri_runtime::webview::PageLoadEvent::Finished`] when the page finishes loading. From 22e2d386a4cd11cc446b8c129ec0490bff4de8e5 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 30 May 2024 21:49:46 +0300 Subject: [PATCH 2/2] fix tests --- core/tauri/src/webview/webview_window.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/tauri/src/webview/webview_window.rs b/core/tauri/src/webview/webview_window.rs index a8bc5ccb1370..6e718b1c890b 100644 --- a/core/tauri/src/webview/webview_window.rs +++ b/core/tauri/src/webview/webview_window.rs @@ -282,7 +282,8 @@ use tauri::{ tauri::Builder::default() .setup(|app| { - let webview_window = WebviewWindowBuilder::new("core", WebviewUrl::App("index.html".into())) + let handle = app.handle(); + let webview_window = WebviewWindowBuilder::new(handle, "core", WebviewUrl::App("index.html".into())) .on_download(|webview, event| { match event { DownloadEvent::Requested { url, destination } => { @@ -296,7 +297,8 @@ tauri::Builder::default() } // let the download start true - }).build()?; + }) + .build()?; Ok(()) });