Skip to content

Commit

Permalink
fix(api): window label null instead of actual value, closes #3295 (#3332
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lucasfernog committed Feb 5, 2022
1 parent 65ad5b5 commit f5109e0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-window-label-api.md
@@ -0,0 +1,5 @@
---
"api": patch
---

Fixes `window.label` property returning null instead of the actual label.
5 changes: 5 additions & 0 deletions .changes/refactor-window-metadata.md
@@ -0,0 +1,5 @@
---
"tauri": "patch"
---

**Breaking change:** Move `__currentWindow` and `__windows` values from `window.__TAURI__` to `window.__TAURI_METADATA__`.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/tauri/scripts/core.js
Expand Up @@ -158,7 +158,7 @@
handler: window.__TAURI__.transformCallback(function (event) {
if (event.payload) {
var windowLabel = event.payload.label
window.__TAURI__.__windows.push({
window.__TAURI_METADATA__.__windows.push({
label: windowLabel
})
}
Expand Down
20 changes: 10 additions & 10 deletions core/tauri/src/manager.rs
Expand Up @@ -424,20 +424,20 @@ impl<R: Runtime> WindowManager<R> {

webview_attributes = webview_attributes
.initialization_script(&self.inner.invoke_initialization_script)
.initialization_script(&self.initialization_script(&ipc_init,&pattern_init,&plugin_init, is_init_global)?)
.initialization_script(&format!(
r#"
if (!window.__TAURI__) {{
Object.defineProperty(window, '__TAURI__', {{
value: {{}}
}})
}}
window.__TAURI__.__windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }});
window.__TAURI__.__currentWindow = {{ label: {current_window_label} }}
Object.defineProperty(window, '__TAURI_METADATA__', {{
value: {{
__windows: {window_labels_array}.map(function (label) {{ return {{ label: label }} }}),
__currentWindow: {{ label: {current_window_label} }}
}}
}})
"#,
window_labels_array = serde_json::to_string(pending_labels)?,
current_window_label = serde_json::to_string(&label)?,
));
))
.initialization_script(&self.initialization_script(&ipc_init,&pattern_init,&plugin_init, is_init_global)?)
;

#[cfg(feature = "isolation")]
if let Pattern::Isolation { schema, .. } = self.pattern() {
Expand Down Expand Up @@ -1164,7 +1164,7 @@ fn on_window_event<R: Runtime>(
let label = window.label();
for window in manager.inner.windows.lock().unwrap().values() {
window.eval(&format!(
r#"window.__TAURI__.__windows = window.__TAURI__.__windows.filter(w => w.label !== "{}");"#,
r#"window.__TAURI_METADATA__.__windows = window.__TAURI_METADATA__.__windows.filter(w => w.label !== "{}");"#,
label
))?;
}
Expand Down
18 changes: 9 additions & 9 deletions examples/api/dist/assets/index.js

Large diffs are not rendered by default.

30 changes: 13 additions & 17 deletions tooling/api/src/window.ts
Expand Up @@ -164,7 +164,7 @@ interface WindowDef {
/** @ignore */
declare global {
interface Window {
__TAURI__: {
__TAURI_METADATA__: {
__windows: WindowDef[]
__currentWindow: WindowDef
}
Expand Down Expand Up @@ -193,7 +193,7 @@ enum UserAttentionType {
* @return The current WebviewWindow.
*/
function getCurrent(): WebviewWindow {
return new WebviewWindow(window.__TAURI__.__currentWindow.label, {
return new WebviewWindow(window.__TAURI_METADATA__.__currentWindow.label, {
// @ts-expect-error
skip: true
})
Expand All @@ -205,7 +205,7 @@ function getCurrent(): WebviewWindow {
* @return The list of WebviewWindow.
*/
function getAll(): WebviewWindow[] {
return window.__TAURI__.__windows.map(
return window.__TAURI_METADATA__.__windows.map(
(w) =>
new WebviewWindow(w.label, {
// @ts-expect-error
Expand All @@ -228,12 +228,8 @@ class WebviewWindowHandle {
/** Local event listeners. */
listeners: { [key: string]: Array<EventCallback<any>> }

constructor(label: WindowLabel | null | undefined) {
try {
this.label = label ?? window.__TAURI__.__currentWindow.label
} catch {
this.label = ''
}
constructor(label: WindowLabel) {
this.label = label
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.listeners = Object.create(null)
}
Expand Down Expand Up @@ -1100,10 +1096,7 @@ class WebviewWindow extends WindowManager {
* * @param label The webview window label. It must be alphanumeric.
* @returns The WebviewWindow instance to communicate with the webview.
*/
constructor(
label: WindowLabel | null | undefined,
options: WindowOptions = {}
) {
constructor(label: WindowLabel, options: WindowOptions = {}) {
super(label)
// @ts-expect-error
if (!options?.skip) {
Expand Down Expand Up @@ -1140,10 +1133,13 @@ class WebviewWindow extends WindowManager {
}

/** The WebviewWindow for the current window. */
const appWindow = new WebviewWindow(null, {
// @ts-expect-error
skip: true
})
const appWindow = new WebviewWindow(
window.__TAURI_METADATA__.__currentWindow.label,
{
// @ts-expect-error
skip: true
}
)

/** Configuration for the window to create. */
interface WindowOptions {
Expand Down

0 comments on commit f5109e0

Please sign in to comment.