Skip to content

Commit f5109e0

Browse files
authored
fix(api): window label null instead of actual value, closes #3295 (#3332)
1 parent 65ad5b5 commit f5109e0

File tree

7 files changed

+44
-38
lines changed

7 files changed

+44
-38
lines changed

.changes/fix-window-label-api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Fixes `window.label` property returning null instead of the actual label.
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+
**Breaking change:** Move `__currentWindow` and `__windows` values from `window.__TAURI__` to `window.__TAURI_METADATA__`.

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/scripts/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
handler: window.__TAURI__.transformCallback(function (event) {
159159
if (event.payload) {
160160
var windowLabel = event.payload.label
161-
window.__TAURI__.__windows.push({
161+
window.__TAURI_METADATA__.__windows.push({
162162
label: windowLabel
163163
})
164164
}

core/tauri/src/manager.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -424,20 +424,20 @@ impl<R: Runtime> WindowManager<R> {
424424

425425
webview_attributes = webview_attributes
426426
.initialization_script(&self.inner.invoke_initialization_script)
427-
.initialization_script(&self.initialization_script(&ipc_init,&pattern_init,&plugin_init, is_init_global)?)
428427
.initialization_script(&format!(
429428
r#"
430-
if (!window.__TAURI__) {{
431-
Object.defineProperty(window, '__TAURI__', {{
432-
value: {{}}
433-
}})
434-
}}
435-
window.__TAURI__.__windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }});
436-
window.__TAURI__.__currentWindow = {{ label: {current_window_label} }}
429+
Object.defineProperty(window, '__TAURI_METADATA__', {{
430+
value: {{
431+
__windows: {window_labels_array}.map(function (label) {{ return {{ label: label }} }}),
432+
__currentWindow: {{ label: {current_window_label} }}
433+
}}
434+
}})
437435
"#,
438436
window_labels_array = serde_json::to_string(pending_labels)?,
439437
current_window_label = serde_json::to_string(&label)?,
440-
));
438+
))
439+
.initialization_script(&self.initialization_script(&ipc_init,&pattern_init,&plugin_init, is_init_global)?)
440+
;
441441

442442
#[cfg(feature = "isolation")]
443443
if let Pattern::Isolation { schema, .. } = self.pattern() {
@@ -1164,7 +1164,7 @@ fn on_window_event<R: Runtime>(
11641164
let label = window.label();
11651165
for window in manager.inner.windows.lock().unwrap().values() {
11661166
window.eval(&format!(
1167-
r#"window.__TAURI__.__windows = window.__TAURI__.__windows.filter(w => w.label !== "{}");"#,
1167+
r#"window.__TAURI_METADATA__.__windows = window.__TAURI_METADATA__.__windows.filter(w => w.label !== "{}");"#,
11681168
label
11691169
))?;
11701170
}

examples/api/dist/assets/index.js

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/api/src/window.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ interface WindowDef {
164164
/** @ignore */
165165
declare global {
166166
interface Window {
167-
__TAURI__: {
167+
__TAURI_METADATA__: {
168168
__windows: WindowDef[]
169169
__currentWindow: WindowDef
170170
}
@@ -193,7 +193,7 @@ enum UserAttentionType {
193193
* @return The current WebviewWindow.
194194
*/
195195
function getCurrent(): WebviewWindow {
196-
return new WebviewWindow(window.__TAURI__.__currentWindow.label, {
196+
return new WebviewWindow(window.__TAURI_METADATA__.__currentWindow.label, {
197197
// @ts-expect-error
198198
skip: true
199199
})
@@ -205,7 +205,7 @@ function getCurrent(): WebviewWindow {
205205
* @return The list of WebviewWindow.
206206
*/
207207
function getAll(): WebviewWindow[] {
208-
return window.__TAURI__.__windows.map(
208+
return window.__TAURI_METADATA__.__windows.map(
209209
(w) =>
210210
new WebviewWindow(w.label, {
211211
// @ts-expect-error
@@ -228,12 +228,8 @@ class WebviewWindowHandle {
228228
/** Local event listeners. */
229229
listeners: { [key: string]: Array<EventCallback<any>> }
230230

231-
constructor(label: WindowLabel | null | undefined) {
232-
try {
233-
this.label = label ?? window.__TAURI__.__currentWindow.label
234-
} catch {
235-
this.label = ''
236-
}
231+
constructor(label: WindowLabel) {
232+
this.label = label
237233
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
238234
this.listeners = Object.create(null)
239235
}
@@ -1100,10 +1096,7 @@ class WebviewWindow extends WindowManager {
11001096
* * @param label The webview window label. It must be alphanumeric.
11011097
* @returns The WebviewWindow instance to communicate with the webview.
11021098
*/
1103-
constructor(
1104-
label: WindowLabel | null | undefined,
1105-
options: WindowOptions = {}
1106-
) {
1099+
constructor(label: WindowLabel, options: WindowOptions = {}) {
11071100
super(label)
11081101
// @ts-expect-error
11091102
if (!options?.skip) {
@@ -1140,10 +1133,13 @@ class WebviewWindow extends WindowManager {
11401133
}
11411134

11421135
/** The WebviewWindow for the current window. */
1143-
const appWindow = new WebviewWindow(null, {
1144-
// @ts-expect-error
1145-
skip: true
1146-
})
1136+
const appWindow = new WebviewWindow(
1137+
window.__TAURI_METADATA__.__currentWindow.label,
1138+
{
1139+
// @ts-expect-error
1140+
skip: true
1141+
}
1142+
)
11471143

11481144
/** Configuration for the window to create. */
11491145
interface WindowOptions {

0 commit comments

Comments
 (0)