소스 검색

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
Fabian-Lars 1 년 전
부모
커밋
c33f6e6cf3

+ 5 - 0
.changes/api-readd-window-created-event.md

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

+ 5 - 0
.changes/core-emit-created-events.md

@@ -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.

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
core/tauri/scripts/bundle.global.js


+ 13 - 0
core/tauri/src/manager/webview.rs

@@ -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
   }
 

+ 2 - 18
core/tauri/src/webview/mod.rs

@@ -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)
   }
 }

+ 12 - 0
core/tauri/src/window/mod.rs

@@ -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)
   }
 }

+ 1 - 1
examples/multiwindow/index.html

@@ -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')

+ 1 - 0
tooling/api/src/event.ts

@@ -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',

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.