mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-11-24 12:14:05 +03:00
feat: expose WebviewWinowBuilder::on_download
(#9922)
* feat: expose `WebviewWinowBuilder::on_download` closes #9921 * fix tests
This commit is contained in:
parent
3cca5c2be8
commit
ddaabda365
5
.changes/webview-builder-on-download.md
Normal file
5
.changes/webview-builder-on-download.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri": "patch:feat"
|
||||
---
|
||||
|
||||
Add `WebviewWindowBuilder::on_download`.
|
@ -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<R>> {
|
||||
window_builder: WindowBuilder<'a, R, M>,
|
||||
@ -263,6 +265,54 @@ 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 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 } => {
|
||||
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<F: Fn(Webview<R>, 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.
|
||||
|
Loading…
Reference in New Issue
Block a user