From 6251645acfe2df2da726f38a09373d7370bfcc86 Mon Sep 17 00:00:00 2001 From: Fabian-Lars Date: Wed, 3 Apr 2024 17:18:13 +0200 Subject: [PATCH] fix(core): Update webview metadata on window close (#9360) * fix(core): Update webview metadata on window close * make it multiwebview friendlier * support webview.close() too -> THIS IS STILL MISSING AN EVENT LIKE tauri://destroyed !!! --- .changes/fix-metadata-on-close.md | 5 +++++ core/tauri/src/manager/mod.rs | 6 ++++++ core/tauri/src/manager/window.rs | 11 +++++------ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 .changes/fix-metadata-on-close.md diff --git a/.changes/fix-metadata-on-close.md b/.changes/fix-metadata-on-close.md new file mode 100644 index 000000000..7208e19ef --- /dev/null +++ b/.changes/fix-metadata-on-close.md @@ -0,0 +1,5 @@ +--- +"tauri": patch:bug +--- + +Fixes an issue causing `getAll()` to list webviews that were already destroyed. diff --git a/core/tauri/src/manager/mod.rs b/core/tauri/src/manager/mod.rs index f3f7a72b9..283656826 100644 --- a/core/tauri/src/manager/mod.rs +++ b/core/tauri/src/manager/mod.rs @@ -544,6 +544,12 @@ impl AppManager { pub(crate) fn on_webview_close(&self, label: &str) { self.webview.webviews_lock().remove(label); + + if let Ok(webview_labels_array) = serde_json::to_string(&self.webview.labels()) { + let _ = self.webview.eval_script_all(format!( + r#"(function () {{ const metadata = window.__TAURI_INTERNALS__.metadata; if (metadata != null) {{ metadata.webviews = {webview_labels_array}.map(function (label) {{ return {{ label: label }} }}) }} }})()"#, + )); + } } pub fn windows(&self) -> HashMap> { diff --git a/core/tauri/src/manager/window.rs b/core/tauri/src/manager/window.rs index c2ce19d0e..17c8e3ea7 100644 --- a/core/tauri/src/manager/window.rs +++ b/core/tauri/src/manager/window.rs @@ -167,12 +167,11 @@ fn on_window_event(window: &Window, event: &WindowEvent) -> crate WindowEvent::Destroyed => { window.emit_to_window(WINDOW_DESTROYED_EVENT, ())?; let label = window.label(); - let webviews_map = window.manager().webview.webviews_lock(); - let webviews = webviews_map.values(); - for webview in webviews { - webview.eval(&format!( - r#"(function () {{ const metadata = window.__TAURI_INTERNALS__.metadata; if (metadata != null) {{ metadata.windows = window.__TAURI_INTERNALS__.metadata.windows.filter(w => w.label !== "{label}"); }} }})()"#, - ))?; + + if let Ok(webview_labels_array) = serde_json::to_string(&window.manager().webview.labels()) { + let _ = window.manager().webview.eval_script_all(format!( + r#"(function () {{ const metadata = window.__TAURI_INTERNALS__.metadata; if (metadata != null) {{ metadata.windows = window.__TAURI_INTERNALS__.metadata.windows.filter(w => w.label !== "{label}"); metadata.webviews = {webview_labels_array}.map(function (label) {{ return {{ label: label }} }}) }} }})()"#, + )); } } WindowEvent::Focused(focused) => window.emit_to_window(