浏览代码

fix(core): sync windows metadata across all windows, closes #5571 (#5615)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Amr Bashir 2 年之前
父节点
当前提交
146a794cb6

+ 5 - 0
.changes/core-sync-windows-metadata.md

@@ -0,0 +1,5 @@
+---
+"tauri": "patch"
+---
+
+Sync `__TAURI_METADATA__.__windows` across all windows.

+ 1 - 1
core/tauri-runtime-wry/Cargo.toml

@@ -13,7 +13,7 @@ exclude = [ "CHANGELOG.md", "/target" ]
 readme = "README.md"
 
 [dependencies]
-wry = { version = "0.23", default-features = false, features = [ "file-drop", "protocol" ] }
+wry = { git = "https://github.com/tauri-apps/wry", branch = "dev", default-features = false, features = [ "file-drop", "protocol" ] }
 tauri-runtime = { version = "0.12.1", path = "../tauri-runtime" }
 tauri-utils = { version = "1.2.1", path = "../tauri-utils" }
 uuid = { version = "1", features = [ "v4" ] }

+ 3 - 2
core/tauri-runtime-wry/src/lib.rs

@@ -81,6 +81,7 @@ pub use wry::application::platform::macos::{
 };
 
 use std::{
+  borrow::Cow,
   cell::RefCell,
   collections::{
     hash_map::Entry::{Occupied, Vacant},
@@ -286,7 +287,7 @@ impl From<&WryRequest<Vec<u8>>> for HttpRequestWrapper {
 }
 
 // response
-struct HttpResponseWrapper(WryResponse<Vec<u8>>);
+struct HttpResponseWrapper(WryResponse<Cow<'static, [u8]>>);
 impl From<HttpResponse> for HttpResponseWrapper {
   fn from(response: HttpResponse) -> Self {
     let (parts, body) = response.into_parts();
@@ -300,7 +301,7 @@ impl From<HttpResponse> for HttpResponseWrapper {
       res_builder = res_builder.header(name, val);
     }
 
-    let res = res_builder.body(body).unwrap();
+    let res = res_builder.body(Cow::Owned(body)).unwrap();
     Self(res)
   }
 }

+ 1 - 10
core/tauri/scripts/core.js

@@ -2,7 +2,7 @@
 // SPDX-License-Identifier: Apache-2.0
 // SPDX-License-Identifier: MIT
 
-;(function () {
+; (function () {
   function uid() {
     return window.crypto.getRandomValues(new Uint32Array(1))[0]
   }
@@ -151,15 +151,6 @@
     }
   })
 
-  listen('tauri://window-created', function (event) {
-    if (event.payload) {
-      var windowLabel = event.payload.label
-      window.__TAURI_METADATA__.__windows.push({
-        label: windowLabel
-      })
-    }
-  })
-
   let permissionSettable = false
   let permissionValue = 'default'
 

+ 1 - 5
core/tauri/src/event.rs

@@ -338,11 +338,7 @@ pub fn listen_js(
         windowLabel: {window_label},
         handler: {handler}
       }};
-      if ({event} == 'tauri://window-created') {{
-        eventListeners.splice(eventListeners.length - 1, 0, listener)
-      }} else {{
-        eventListeners.push(listener);
-      }}
+      eventListeners.push(listener);
     }})()
   ",
     listeners = listeners_object_name,

+ 8 - 0
core/tauri/src/manager.rs

@@ -1302,6 +1302,14 @@ impl<R: Runtime> WindowManager<R> {
       .try_for_each(|window| window.emit_internal(event, source_window_label, payload.clone()))
   }
 
+  pub fn eval_script_all<S: Into<String>>(&self, script: S) -> crate::Result<()> {
+    let script = script.into();
+    self
+      .windows_lock()
+      .values()
+      .try_for_each(|window| window.eval(&script))
+  }
+
   pub fn labels(&self) -> HashSet<String> {
     self.windows_lock().keys().cloned().collect()
   }

+ 5 - 0
core/tauri/src/window.rs

@@ -252,6 +252,11 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
     }
     .map(|window| self.manager.attach_window(self.app_handle.clone(), window))?;
 
+    self.manager.eval_script_all(format!(
+      "window.__TAURI_METADATA__.__windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }})",
+      window_labels_array = serde_json::to_string(&self.manager.labels())?,
+    ))?;
+
     self.manager.emit_filter(
       "tauri://window-created",
       None,