Skip to content

Commit 146a794

Browse files
fix(core): sync windows metadata across all windows, closes #5571 (#5615)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent ff99a9b commit 146a794

File tree

7 files changed

+24
-18
lines changed

7 files changed

+24
-18
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": "patch"
3+
---
4+
5+
Sync `__TAURI_METADATA__.__windows` across all windows.

core/tauri-runtime-wry/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exclude = [ "CHANGELOG.md", "/target" ]
1313
readme = "README.md"
1414

1515
[dependencies]
16-
wry = { version = "0.23", default-features = false, features = [ "file-drop", "protocol" ] }
16+
wry = { git = "https://github.com/tauri-apps/wry", branch = "dev", default-features = false, features = [ "file-drop", "protocol" ] }
1717
tauri-runtime = { version = "0.12.1", path = "../tauri-runtime" }
1818
tauri-utils = { version = "1.2.1", path = "../tauri-utils" }
1919
uuid = { version = "1", features = [ "v4" ] }

core/tauri-runtime-wry/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub use wry::application::platform::macos::{
8181
};
8282

8383
use std::{
84+
borrow::Cow,
8485
cell::RefCell,
8586
collections::{
8687
hash_map::Entry::{Occupied, Vacant},
@@ -286,7 +287,7 @@ impl From<&WryRequest<Vec<u8>>> for HttpRequestWrapper {
286287
}
287288

288289
// response
289-
struct HttpResponseWrapper(WryResponse<Vec<u8>>);
290+
struct HttpResponseWrapper(WryResponse<Cow<'static, [u8]>>);
290291
impl From<HttpResponse> for HttpResponseWrapper {
291292
fn from(response: HttpResponse) -> Self {
292293
let (parts, body) = response.into_parts();
@@ -300,7 +301,7 @@ impl From<HttpResponse> for HttpResponseWrapper {
300301
res_builder = res_builder.header(name, val);
301302
}
302303

303-
let res = res_builder.body(body).unwrap();
304+
let res = res_builder.body(Cow::Owned(body)).unwrap();
304305
Self(res)
305306
}
306307
}

core/tauri/scripts/core.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
;(function () {
5+
; (function () {
66
function uid() {
77
return window.crypto.getRandomValues(new Uint32Array(1))[0]
88
}
@@ -151,15 +151,6 @@
151151
}
152152
})
153153

154-
listen('tauri://window-created', function (event) {
155-
if (event.payload) {
156-
var windowLabel = event.payload.label
157-
window.__TAURI_METADATA__.__windows.push({
158-
label: windowLabel
159-
})
160-
}
161-
})
162-
163154
let permissionSettable = false
164155
let permissionValue = 'default'
165156

core/tauri/src/event.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,7 @@ pub fn listen_js(
338338
windowLabel: {window_label},
339339
handler: {handler}
340340
}};
341-
if ({event} == 'tauri://window-created') {{
342-
eventListeners.splice(eventListeners.length - 1, 0, listener)
343-
}} else {{
344-
eventListeners.push(listener);
345-
}}
341+
eventListeners.push(listener);
346342
}})()
347343
",
348344
listeners = listeners_object_name,

core/tauri/src/manager.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,14 @@ impl<R: Runtime> WindowManager<R> {
13021302
.try_for_each(|window| window.emit_internal(event, source_window_label, payload.clone()))
13031303
}
13041304

1305+
pub fn eval_script_all<S: Into<String>>(&self, script: S) -> crate::Result<()> {
1306+
let script = script.into();
1307+
self
1308+
.windows_lock()
1309+
.values()
1310+
.try_for_each(|window| window.eval(&script))
1311+
}
1312+
13051313
pub fn labels(&self) -> HashSet<String> {
13061314
self.windows_lock().keys().cloned().collect()
13071315
}

core/tauri/src/window.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
252252
}
253253
.map(|window| self.manager.attach_window(self.app_handle.clone(), window))?;
254254

255+
self.manager.eval_script_all(format!(
256+
"window.__TAURI_METADATA__.__windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }})",
257+
window_labels_array = serde_json::to_string(&self.manager.labels())?,
258+
))?;
259+
255260
self.manager.emit_filter(
256261
"tauri://window-created",
257262
None,

0 commit comments

Comments
 (0)