From 7261a14368eeef040aee3350bb39183558d18bf0 Mon Sep 17 00:00:00 2001 From: Sean Wang <126865849+WSH032@users.noreply.github.com> Date: Sat, 16 Aug 2025 11:01:53 +0800 Subject: [PATCH] feat: impl `AsRef` and `on_webview_event` for `WebviewWindow` (#14012) --- .changes/WebviewWindow-on_webview_event.md | 11 +++++++++++ crates/tauri/src/webview/mod.rs | 2 +- crates/tauri/src/webview/webview_window.rs | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 .changes/WebviewWindow-on_webview_event.md diff --git a/.changes/WebviewWindow-on_webview_event.md b/.changes/WebviewWindow-on_webview_event.md new file mode 100644 index 000000000..c7333b01b --- /dev/null +++ b/.changes/WebviewWindow-on_webview_event.md @@ -0,0 +1,11 @@ +--- +tauri: minor:enhance +--- + +- Implemented `Webview::on_webview_event` for `WebviewWindow` as well +- Implemented `AsRef>` for `WebviewWindow` + + This can be considered a *BREAKING CHANGE* in very specific cases: + Typically, this means you are relying on implicit type inference, such as `let webview: _ = WebviewWindow.as_ref()`. + To resolve this, you should explicitly specify the type, for example `let webview: &Window = WebviewWindow.as_ref()` + or `let webview: _ = AsRef::>::as_ref(&WebviewWindow)`. diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index 4f7a17a0e..1a2eddaac 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -1121,7 +1121,7 @@ impl Webview { self.use_https_scheme } - /// Registers a window event listener. + /// Registers a webview event listener. pub fn on_webview_event(&self, f: F) { self .webview diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index b8ba7eebc..cdd0984b5 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -34,7 +34,7 @@ use crate::{ ipc::{CommandArg, CommandItem, InvokeError, OwnedInvokeResponder}, manager::AppManager, sealed::{ManagerBase, RuntimeOrDispatch}, - webview::{Cookie, PageLoadPayload, WebviewBuilder}, + webview::{Cookie, PageLoadPayload, WebviewBuilder, WebviewEvent}, window::WindowBuilder, AppHandle, Event, EventId, Manager, Runtime, Webview, WindowEvent, }; @@ -1193,6 +1193,12 @@ impl AsRef> for WebviewWindow { } } +impl AsRef> for WebviewWindow { + fn as_ref(&self) -> &Window { + &self.window + } +} + impl Clone for WebviewWindow { fn clone(&self) -> Self { Self { @@ -1204,7 +1210,7 @@ impl Clone for WebviewWindow { impl Eq for WebviewWindow {} impl PartialEq for WebviewWindow { - /// Only use the [`Window`]'s label to compare equality. + /// Only use the [`Webview`]'s label to compare equality. fn eq(&self, other: &Self) -> bool { self.webview.eq(&other.webview) } @@ -1269,6 +1275,11 @@ impl WebviewWindow { self.window.on_window_event(f); } + /// Registers a webview event listener. + pub fn on_webview_event(&self, f: F) { + self.webview.on_webview_event(f); + } + /// Resolves the given command scope for this webview on the currently loaded URL. /// /// If the command is not allowed, returns None.