Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): emit tauri://window-created event for windows created on Rust #3299

Merged
merged 3 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/emit-window-created-backend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Emit `tauri://window-created` event for windows created on the backend.
20 changes: 6 additions & 14 deletions core/tauri/src/endpoints/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: Runtime>(
Expand All @@ -162,14 +156,12 @@ impl Cmd {
let label = options.label.clone();
let url = options.url.clone();

window
.create_window(label.clone(), url, |_, webview_attributes| {
(
<<R::Dispatcher as Dispatch>::WindowBuilder>::with_config(*options),
webview_attributes,
)
})?
.emit_others("tauri://window-created", Some(WindowCreatedEvent { label }))?;
window.create_window(label, url, |_, webview_attributes| {
(
<<R::Dispatcher as Dispatch>::WindowBuilder>::with_config(*options),
webview_attributes,
)
})?;

Ok(())
}
Expand Down
29 changes: 20 additions & 9 deletions core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: Runtime> {
/// The manager behind the [`Managed`] item.
Expand All @@ -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)
}
}
}
Expand Down