diff --git a/.changes/emit-window-created-backend.md b/.changes/emit-window-created-backend.md new file mode 100644 index 000000000..f03e855d8 --- /dev/null +++ b/.changes/emit-window-created-backend.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Emit `tauri://window-created` event for windows created on the backend. diff --git a/core/tauri/src/endpoints/window.rs b/core/tauri/src/endpoints/window.rs index d069c0635..1f33963e9 100644 --- a/core/tauri/src/endpoints/window.rs +++ b/core/tauri/src/endpoints/window.rs @@ -146,12 +146,6 @@ pub enum Cmd { }, } -#[cfg(window_create)] -#[derive(Clone, serde::Serialize)] -struct WindowCreatedEvent { - label: String, -} - impl Cmd { #[module_command_handler(window_create, "window > create")] async fn create_webview( @@ -162,14 +156,12 @@ impl Cmd { let label = options.label.clone(); let url = options.url.clone(); - window - .create_window(label.clone(), url, |_, webview_attributes| { - ( - <::WindowBuilder>::with_config(*options), - webview_attributes, - ) - })? - .emit_others("tauri://window-created", Some(WindowCreatedEvent { label }))?; + window.create_window(label, url, |_, webview_attributes| { + ( + <::WindowBuilder>::with_config(*options), + webview_attributes, + ) + })?; Ok(()) } diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index 105468a65..cdec89a3e 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -498,6 +498,11 @@ pub(crate) mod sealed { Dispatch(R::Dispatcher), } + #[derive(Clone, serde::Serialize)] + struct WindowCreatedEvent { + label: String, + } + /// Managed handle to the application runtime. pub trait ManagerBase { /// The manager behind the [`Managed`] item. @@ -516,16 +521,22 @@ pub(crate) mod sealed { let pending = self .manager() .prepare_window(self.app_handle(), pending, &labels)?; - match self.runtime() { - RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending).map_err(Into::into), - RuntimeOrDispatch::RuntimeHandle(handle) => { - handle.create_window(pending).map_err(Into::into) - } - RuntimeOrDispatch::Dispatch(mut dispatcher) => { - dispatcher.create_window(pending).map_err(Into::into) - } + let window = match self.runtime() { + RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending), + RuntimeOrDispatch::RuntimeHandle(handle) => handle.create_window(pending), + RuntimeOrDispatch::Dispatch(mut dispatcher) => dispatcher.create_window(pending), } - .map(|window| self.manager().attach_window(self.app_handle(), window)) + .map(|window| self.manager().attach_window(self.app_handle(), window))?; + + self.manager().emit_filter( + "tauri://window-created", + Some(WindowCreatedEvent { + label: window.label().into(), + }), + |w| w != &window, + )?; + + Ok(window) } } }