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
This commit is contained in:
Fabian-Lars 2024-03-28 01:23:19 +01:00 committed by GitHub
parent e7cd973123
commit c33f6e6cf3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 40 additions and 20 deletions

View File

@ -0,0 +1,5 @@
---
'@tauri-apps/api': 'patch:bug'
---
Re-added the `TauriEvent.WINDOW_CREATED` (`tauri://window-created`) event.

View File

@ -0,0 +1,5 @@
---
tauri: 'patch:bug'
---
Fixed an issue preventing webview/window creation events to not be emitted. This also fixed the `getByLabel` and `getAll` JavaScript functions.

File diff suppressed because one or more lines are too long

View File

@ -610,6 +610,19 @@ impl<R: Runtime> WebviewManager<R> {
.expect("failed to run on_webview_created hook");
}
if let Ok(webview_labels_array) = serde_json::to_string(&webview.manager.webview.labels()) {
let _ = webview.manager.webview.eval_script_all(format!(
"window.__TAURI_INTERNALS__.metadata.webviews = {webview_labels_array}.map(function (label) {{ return {{ label: label }} }})",
));
}
let _ = webview.manager.emit(
"tauri://webview-created",
Some(crate::webview::CreatedEvent {
label: webview.label().into(),
}),
);
webview
}

View File

@ -54,8 +54,8 @@ pub(crate) type OnPageLoad<R> = dyn Fn(Webview<R>, PageLoadPayload<'_>) + Send +
pub(crate) type DownloadHandler<R> = dyn Fn(Webview<R>, DownloadEvent<'_>) -> bool + Send + Sync;
#[derive(Clone, Serialize)]
struct CreatedEvent {
label: String,
pub(crate) struct CreatedEvent {
pub(crate) label: String,
}
/// Download event for the [`WebviewBuilder#method.on_download`] hook.
@ -624,22 +624,6 @@ tauri::Builder::default()
}
.map(|webview| app_manager.webview.attach_webview(window.clone(), webview))?;
app_manager.webview.eval_script_all(format!(
"window.__TAURI_INTERNALS__.metadata.windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }})",
window_labels_array = serde_json::to_string(&app_manager.webview.labels())?,
))?;
app_manager.emit_filter(
"tauri://webview-created",
Some(CreatedEvent {
label: webview.label().into(),
}),
|s| match s {
EventTarget::Webview { label } => label == webview.label(),
_ => false,
},
)?;
Ok(webview)
}
}

View File

@ -425,6 +425,18 @@ tauri::Builder::default()
crate::vibrancy::set_window_effects(&window, Some(effects))?;
}
app_manager.webview.eval_script_all(format!(
"window.__TAURI_INTERNALS__.metadata.windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }})",
window_labels_array = serde_json::to_string(&app_manager.window.labels())?,
))?;
app_manager.emit(
"tauri://window-created",
Some(crate::webview::CreatedEvent {
label: window.label().into(),
}),
)?;
Ok(window)
}
}

View File

@ -14,7 +14,7 @@
<div id="response"></div>
<script>
const WebviewWindow = window.__TAURI__.webview.WebviewWindow
const WebviewWindow = window.__TAURI__.webviewWindow.WebviewWindow
const appWindow = window.__TAURI__.window.getCurrent()
const windowLabel = appWindow.label
const windowLabelContainer = document.getElementById('window-label')

View File

@ -55,6 +55,7 @@ enum TauriEvent {
WINDOW_BLUR = 'tauri://blur',
WINDOW_SCALE_FACTOR_CHANGED = 'tauri://scale-change',
WINDOW_THEME_CHANGED = 'tauri://theme-changed',
WINDOW_CREATED = 'tauri://window-created',
WEBVIEW_CREATED = 'tauri://webview-created',
FILE_DROP = 'tauri://file-drop',
FILE_DROP_HOVER = 'tauri://file-drop-hover',