Skip to content

Commit

Permalink
fix(core): Update webview metadata on window close (#9360)
Browse files Browse the repository at this point in the history
* 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 !!!
  • Loading branch information
FabianLars committed Apr 3, 2024
1 parent 4c2e747 commit 6251645
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .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.
6 changes: 6 additions & 0 deletions core/tauri/src/manager/mod.rs
Expand Up @@ -544,6 +544,12 @@ impl<R: Runtime> AppManager<R> {

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<String, Window<R>> {
Expand Down
11 changes: 5 additions & 6 deletions core/tauri/src/manager/window.rs
Expand Up @@ -167,12 +167,11 @@ fn on_window_event<R: Runtime>(window: &Window<R>, 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(
Expand Down

0 comments on commit 6251645

Please sign in to comment.