@@ -44,6 +44,8 @@ use tauri_macros::default_runtime;
44
44
#[ cfg( windows) ]
45
45
use windows:: Win32 :: Foundation :: HWND ;
46
46
47
+ use super :: DownloadEvent ;
48
+
47
49
/// A builder for [`WebviewWindow`], a window that hosts a single webview.
48
50
pub struct WebviewWindowBuilder < ' a , R : Runtime , M : Manager < R > > {
49
51
window_builder : WindowBuilder < ' a , R , M > ,
@@ -263,6 +265,54 @@ tauri::Builder::default()
263
265
self
264
266
}
265
267
268
+ /// Set a download event handler to be notified when a download is requested or finished.
269
+ ///
270
+ /// Returning `false` prevents the download from happening on a [`DownloadEvent::Requested`] event.
271
+ ///
272
+ /// # Examples
273
+ ///
274
+ #[ cfg_attr(
275
+ feature = "unstable" ,
276
+ doc = r####"
277
+ ```rust,no_run
278
+ use tauri::{
279
+ utils::config::{Csp, CspDirectiveSources, WebviewUrl},
280
+ webview::{DownloadEvent, WebviewWindowBuilder},
281
+ };
282
+
283
+ tauri::Builder::default()
284
+ .setup(|app| {
285
+ let handle = app.handle();
286
+ let webview_window = WebviewWindowBuilder::new(handle, "core", WebviewUrl::App("index.html".into()))
287
+ .on_download(|webview, event| {
288
+ match event {
289
+ DownloadEvent::Requested { url, destination } => {
290
+ println!("downloading {}", url);
291
+ *destination = "/home/tauri/target/path".into();
292
+ }
293
+ DownloadEvent::Finished { url, path, success } => {
294
+ println!("downloaded {} to {:?}, success: {}", url, path, success);
295
+ }
296
+ _ => (),
297
+ }
298
+ // let the download start
299
+ true
300
+ })
301
+ .build()?;
302
+
303
+ Ok(())
304
+ });
305
+ ```
306
+ "####
307
+ ) ]
308
+ pub fn on_download < F : Fn ( Webview < R > , DownloadEvent < ' _ > ) -> bool + Send + Sync + ' static > (
309
+ mut self ,
310
+ f : F ,
311
+ ) -> Self {
312
+ self . webview_builder . download_handler . replace ( Arc :: new ( f) ) ;
313
+ self
314
+ }
315
+
266
316
/// Defines a closure to be executed when a page load event is triggered.
267
317
/// The event can be either [`tauri_runtime::webview::PageLoadEvent::Started`] if the page has started loading
268
318
/// or [`tauri_runtime::webview::PageLoadEvent::Finished`] when the page finishes loading.
0 commit comments