Skip to content

Commit c33f6e6

Browse files
authored
fix(core): Announce new webviews and windows (#9211)
* fix(core): Announce new webviews and windows fixes #9200 fixes #8144 * fix js import in example * emit created events to all listeners. * remove duplicate event
1 parent e7cd973 commit c33f6e6

File tree

8 files changed

+40
-20
lines changed

8 files changed

+40
-20
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tauri-apps/api': 'patch:bug'
3+
---
4+
5+
Re-added the `TauriEvent.WINDOW_CREATED` (`tauri://window-created`) event.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
tauri: 'patch:bug'
3+
---
4+
5+
Fixed an issue preventing webview/window creation events to not be emitted. This also fixed the `getByLabel` and `getAll` JavaScript functions.

core/tauri/scripts/bundle.global.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/src/manager/webview.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,19 @@ impl<R: Runtime> WebviewManager<R> {
610610
.expect("failed to run on_webview_created hook");
611611
}
612612

613+
if let Ok(webview_labels_array) = serde_json::to_string(&webview.manager.webview.labels()) {
614+
let _ = webview.manager.webview.eval_script_all(format!(
615+
"window.__TAURI_INTERNALS__.metadata.webviews = {webview_labels_array}.map(function (label) {{ return {{ label: label }} }})",
616+
));
617+
}
618+
619+
let _ = webview.manager.emit(
620+
"tauri://webview-created",
621+
Some(crate::webview::CreatedEvent {
622+
label: webview.label().into(),
623+
}),
624+
);
625+
613626
webview
614627
}
615628

core/tauri/src/webview/mod.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub(crate) type OnPageLoad<R> = dyn Fn(Webview<R>, PageLoadPayload<'_>) + Send +
5454
pub(crate) type DownloadHandler<R> = dyn Fn(Webview<R>, DownloadEvent<'_>) -> bool + Send + Sync;
5555

5656
#[derive(Clone, Serialize)]
57-
struct CreatedEvent {
58-
label: String,
57+
pub(crate) struct CreatedEvent {
58+
pub(crate) label: String,
5959
}
6060

6161
/// Download event for the [`WebviewBuilder#method.on_download`] hook.
@@ -624,22 +624,6 @@ tauri::Builder::default()
624624
}
625625
.map(|webview| app_manager.webview.attach_webview(window.clone(), webview))?;
626626

627-
app_manager.webview.eval_script_all(format!(
628-
"window.__TAURI_INTERNALS__.metadata.windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }})",
629-
window_labels_array = serde_json::to_string(&app_manager.webview.labels())?,
630-
))?;
631-
632-
app_manager.emit_filter(
633-
"tauri://webview-created",
634-
Some(CreatedEvent {
635-
label: webview.label().into(),
636-
}),
637-
|s| match s {
638-
EventTarget::Webview { label } => label == webview.label(),
639-
_ => false,
640-
},
641-
)?;
642-
643627
Ok(webview)
644628
}
645629
}

core/tauri/src/window/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,18 @@ tauri::Builder::default()
425425
crate::vibrancy::set_window_effects(&window, Some(effects))?;
426426
}
427427

428+
app_manager.webview.eval_script_all(format!(
429+
"window.__TAURI_INTERNALS__.metadata.windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }})",
430+
window_labels_array = serde_json::to_string(&app_manager.window.labels())?,
431+
))?;
432+
433+
app_manager.emit(
434+
"tauri://window-created",
435+
Some(crate::webview::CreatedEvent {
436+
label: window.label().into(),
437+
}),
438+
)?;
439+
428440
Ok(window)
429441
}
430442
}

examples/multiwindow/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div id="response"></div>
1515

1616
<script>
17-
const WebviewWindow = window.__TAURI__.webview.WebviewWindow
17+
const WebviewWindow = window.__TAURI__.webviewWindow.WebviewWindow
1818
const appWindow = window.__TAURI__.window.getCurrent()
1919
const windowLabel = appWindow.label
2020
const windowLabelContainer = document.getElementById('window-label')

tooling/api/src/event.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ enum TauriEvent {
5555
WINDOW_BLUR = 'tauri://blur',
5656
WINDOW_SCALE_FACTOR_CHANGED = 'tauri://scale-change',
5757
WINDOW_THEME_CHANGED = 'tauri://theme-changed',
58+
WINDOW_CREATED = 'tauri://window-created',
5859
WEBVIEW_CREATED = 'tauri://webview-created',
5960
FILE_DROP = 'tauri://file-drop',
6061
FILE_DROP_HOVER = 'tauri://file-drop-hover',

0 commit comments

Comments
 (0)