diff --git a/tauri/src/app.rs b/tauri/src/app.rs
index 515dac7cc..eae94940a 100644
--- a/tauri/src/app.rs
+++ b/tauri/src/app.rs
@@ -1,5 +1,5 @@
use futures::future::BoxFuture;
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use tauri_api::{config::Config, private::AsTauriContext};
@@ -23,7 +23,9 @@ pub use webview_manager::{WebviewDispatcher, WebviewManager};
type InvokeHandler = dyn Fn(WebviewManager, String, JsonValue) -> BoxFuture<'static, crate::Result>
+ Send
+ Sync;
-type Setup = dyn Fn(WebviewManager) -> BoxFuture<'static, ()> + Send + Sync;
+type ManagerHook = dyn Fn(WebviewManager) -> BoxFuture<'static, ()> + Send + Sync;
+type PageLoadHook =
+ dyn Fn(WebviewManager, PageLoadPayload) -> BoxFuture<'static, ()> + Send + Sync;
/// `App` runtime information.
pub struct Context {
@@ -63,12 +65,27 @@ impl From for InvokeResponse {
}
}
+/// The payload for the "page_load" hook.
+#[derive(Debug, Clone, Deserialize)]
+pub struct PageLoadPayload {
+ url: String,
+}
+
+impl PageLoadPayload {
+ /// The page URL.
+ pub fn url(&self) -> &str {
+ &self.url
+ }
+}
+
/// The application runner.
pub struct App {
/// The JS message handler.
invoke_handler: Option>>,
- /// The setup callback, invoked when the webview is ready.
- setup: Option>>,
+ /// The page load hook, invoked when the webview performs a navigation.
+ on_page_load: Option>>,
+ /// The setup hook, invoked when the webviews have been created.
+ setup: Option>>,
/// The context the App was created with
pub(crate) context: Context,
pub(crate) dispatchers: Arc>>>,
@@ -118,10 +135,22 @@ impl App {
}
}
- /// Runs the setup callback if defined.
- pub(crate) async fn run_setup(&self, dispatcher: &WebviewManager) {
+ /// Runs the setup hook if defined.
+ pub(crate) async fn run_setup(&self, dispatcher: WebviewManager) {
if let Some(ref setup) = self.setup {
- let fut = setup(dispatcher.clone());
+ let fut = setup(dispatcher);
+ fut.await;
+ }
+ }
+
+ /// Runs the on page load hook if defined.
+ pub(crate) async fn run_on_page_load(
+ &self,
+ dispatcher: &WebviewManager,
+ payload: PageLoadPayload,
+ ) {
+ if let Some(ref on_page_load) = self.on_page_load {
+ let fut = on_page_load(dispatcher.clone(), payload);
fut.await;
}
}
@@ -197,8 +226,10 @@ where
{
/// The JS message handler.
invoke_handler: Option>>,
- /// The setup callback, invoked when the webview is ready.
- setup: Option>>,
+ /// The setup hook.
+ setup: Option>>,
+ /// Page load hook.
+ on_page_load: Option>>,
config: PhantomData,
/// The webview dispatchers.
dispatchers: Arc>>>,
@@ -212,6 +243,7 @@ impl AppBuilder {
Self {
invoke_handler: None,
setup: None,
+ on_page_load: None,
config: Default::default(),
dispatchers: Default::default(),
webviews: Default::default(),
@@ -232,7 +264,7 @@ impl AppBuilder {
self
}
- /// Defines the setup callback.
+ /// Defines the setup hook.
pub fn setup<
T: futures::Future