Skip to content

Commit 1dbd887

Browse files
authored
fix(core): emit tauri://window-created event for windows created on Rust (#3299)
1 parent 878b8b9 commit 1dbd887

3 files changed

Lines changed: 31 additions & 23 deletions

File tree

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+
Emit `tauri://window-created` event for windows created on the backend.

core/tauri/src/endpoints/window.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,6 @@ pub enum Cmd {
146146
},
147147
}
148148

149-
#[cfg(window_create)]
150-
#[derive(Clone, serde::Serialize)]
151-
struct WindowCreatedEvent {
152-
label: String,
153-
}
154-
155149
impl Cmd {
156150
#[module_command_handler(window_create, "window > create")]
157151
async fn create_webview<R: Runtime>(
@@ -162,14 +156,12 @@ impl Cmd {
162156
let label = options.label.clone();
163157
let url = options.url.clone();
164158

165-
window
166-
.create_window(label.clone(), url, |_, webview_attributes| {
167-
(
168-
<<R::Dispatcher as Dispatch>::WindowBuilder>::with_config(*options),
169-
webview_attributes,
170-
)
171-
})?
172-
.emit_others("tauri://window-created", Some(WindowCreatedEvent { label }))?;
159+
window.create_window(label, url, |_, webview_attributes| {
160+
(
161+
<<R::Dispatcher as Dispatch>::WindowBuilder>::with_config(*options),
162+
webview_attributes,
163+
)
164+
})?;
173165

174166
Ok(())
175167
}

core/tauri/src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,11 @@ pub(crate) mod sealed {
498498
Dispatch(R::Dispatcher),
499499
}
500500

501+
#[derive(Clone, serde::Serialize)]
502+
struct WindowCreatedEvent {
503+
label: String,
504+
}
505+
501506
/// Managed handle to the application runtime.
502507
pub trait ManagerBase<R: Runtime> {
503508
/// The manager behind the [`Managed`] item.
@@ -516,16 +521,22 @@ pub(crate) mod sealed {
516521
let pending = self
517522
.manager()
518523
.prepare_window(self.app_handle(), pending, &labels)?;
519-
match self.runtime() {
520-
RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending).map_err(Into::into),
521-
RuntimeOrDispatch::RuntimeHandle(handle) => {
522-
handle.create_window(pending).map_err(Into::into)
523-
}
524-
RuntimeOrDispatch::Dispatch(mut dispatcher) => {
525-
dispatcher.create_window(pending).map_err(Into::into)
526-
}
524+
let window = match self.runtime() {
525+
RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending),
526+
RuntimeOrDispatch::RuntimeHandle(handle) => handle.create_window(pending),
527+
RuntimeOrDispatch::Dispatch(mut dispatcher) => dispatcher.create_window(pending),
527528
}
528-
.map(|window| self.manager().attach_window(self.app_handle(), window))
529+
.map(|window| self.manager().attach_window(self.app_handle(), window))?;
530+
531+
self.manager().emit_filter(
532+
"tauri://window-created",
533+
Some(WindowCreatedEvent {
534+
label: window.label().into(),
535+
}),
536+
|w| w != &window,
537+
)?;
538+
539+
Ok(window)
529540
}
530541
}
531542
}

0 commit comments

Comments
 (0)