Skip to content

Commit

Permalink
fix(core): sync windows metadata across all windows, closes #5571 (#5615
Browse files Browse the repository at this point in the history
)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog authored Dec 27, 2022
1 parent ff99a9b commit 146a794
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changes/core-sync-windows-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": "patch"
---

Sync `__TAURI_METADATA__.__windows` across all windows.
2 changes: 1 addition & 1 deletion core/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
Expand Down
5 changes: 3 additions & 2 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub use wry::application::platform::macos::{
};

use std::{
borrow::Cow,
cell::RefCell,
collections::{
hash_map::Entry::{Occupied, Vacant},
Expand Down Expand Up @@ -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();
Expand All @@ -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)
}
}
Expand Down
11 changes: 1 addition & 10 deletions core/tauri/scripts/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down Expand Up @@ -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'

Expand Down
6 changes: 1 addition & 5 deletions core/tauri/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
5 changes: 5 additions & 0 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 146a794

Please sign in to comment.