Skip to content
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
6 changes: 6 additions & 0 deletions .changes/new-window-main-thread.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": minor:changes
"tauri-runtime-wry": minor:changes
---

The new window handler passed to `on_new_window` no longer requires `Sync`, and runs on main thread on Windows, aligning with other platforms
113 changes: 51 additions & 62 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4000,13 +4000,9 @@ fn handle_user_message<T: UserEvent>(
}
}
Message::CreateWindow(window_id, handler) => match handler(event_loop) {
// wait for borrow_mut to be available - on Windows we might poll for the window to be inserted
Ok(webview) => loop {
if let Ok(mut windows) = windows.0.try_borrow_mut() {
windows.insert(window_id, webview);
break;
}
},
Ok(webview) => {
windows.0.borrow_mut().insert(window_id, webview);
}
Err(e) => {
log::error!("{e}");
}
Expand Down Expand Up @@ -4789,63 +4785,56 @@ You may have it installed on another user account, but it is not available for t
#[cfg(desktop)]
let context = context.clone();
webview_builder = webview_builder.with_new_window_req_handler(move |url, features| {
url
.parse()
.map(|url| {
let response = new_window_handler(
url,
tauri_runtime::webview::NewWindowFeatures::new(
features.size,
features.position,
tauri_runtime::webview::NewWindowOpener {
#[cfg(desktop)]
webview: features.opener.webview,
#[cfg(windows)]
environment: features.opener.environment,
#[cfg(target_os = "macos")]
target_configuration: features.opener.target_configuration,
},
),
);
match response {
tauri_runtime::webview::NewWindowResponse::Allow => wry::NewWindowResponse::Allow,
let Ok(url) = url.parse() else {
return wry::NewWindowResponse::Deny;
};
let response = new_window_handler(
url,
tauri_runtime::webview::NewWindowFeatures::new(
features.size,
features.position,
tauri_runtime::webview::NewWindowOpener {
#[cfg(desktop)]
tauri_runtime::webview::NewWindowResponse::Create { window_id } => {
let windows = &context.main_thread.windows.0;
let webview = loop {
if let Some(webview) = windows.try_borrow().ok().and_then(|windows| {
windows
.get(&window_id)
.map(|window| window.webviews.first().unwrap().clone())
}) {
break webview;
} else {
// on Windows the window is created async so we should wait for it to be available
std::thread::sleep(std::time::Duration::from_millis(50));
continue;
};
};

#[cfg(desktop)]
wry::NewWindowResponse::Create {
#[cfg(target_os = "macos")]
webview: wry::WebViewExtMacOS::webview(&*webview).as_super().into(),
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
webview: webview.webview(),
#[cfg(windows)]
webview: webview.webview(),
}
}
tauri_runtime::webview::NewWindowResponse::Deny => wry::NewWindowResponse::Deny,
webview: features.opener.webview,
#[cfg(windows)]
environment: features.opener.environment,
#[cfg(target_os = "macos")]
target_configuration: features.opener.target_configuration,
},
),
);
match response {
tauri_runtime::webview::NewWindowResponse::Allow => wry::NewWindowResponse::Allow,
#[cfg(desktop)]
tauri_runtime::webview::NewWindowResponse::Create { window_id } => {
let windows = &context.main_thread.windows.0;
let webview = windows
.borrow()
.get(&window_id)
.unwrap()
.webviews
.first()
.unwrap()
.clone();

#[cfg(desktop)]
wry::NewWindowResponse::Create {
#[cfg(target_os = "macos")]
webview: wry::WebViewExtMacOS::webview(&*webview).as_super().into(),
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
webview: webview.webview(),
#[cfg(windows)]
webview: webview.webview(),
}
})
.unwrap_or(wry::NewWindowResponse::Deny)
}
tauri_runtime::webview::NewWindowResponse::Deny => wry::NewWindowResponse::Deny,
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type WebResourceRequestHandler =

type NavigationHandler = dyn Fn(&Url) -> bool + Send;

type NewWindowHandler = dyn Fn(Url, NewWindowFeatures) -> NewWindowResponse + Send + Sync;
type NewWindowHandler = dyn Fn(Url, NewWindowFeatures) -> NewWindowResponse + Send;

type OnPageLoadHandler = dyn Fn(Url, PageLoadEvent) + Send;

Expand Down
9 changes: 2 additions & 7 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ use std::{
pub(crate) type WebResourceRequestHandler =
dyn Fn(http::Request<Vec<u8>>, &mut http::Response<Cow<'static, [u8]>>) + Send + Sync;
pub(crate) type NavigationHandler = dyn Fn(&Url) -> bool + Send;
pub(crate) type NewWindowHandler<R> =
dyn Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + Sync;
pub(crate) type NewWindowHandler<R> = dyn Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send;
pub(crate) type UriSchemeProtocolHandler =
Box<dyn Fn(&str, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync>;
pub(crate) type OnPageLoad<R> = dyn Fn(Webview<R>, PageLoadPayload<'_>) + Send + Sync + 'static;
Expand Down Expand Up @@ -581,12 +580,9 @@ tauri::Builder::default()
/// # Platform-specific
///
/// - **Android / iOS**: Not supported.
/// - **Windows**: The closure is executed on a separate thread to prevent a deadlock.
///
/// [window.open]: https://developer.mozilla.org/en-US/docs/Web/API/Window/open
pub fn on_new_window<
F: Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + Sync + 'static,
>(
pub fn on_new_window<F: Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + 'static>(
mut self,
f: F,
) -> Self {
Expand Down Expand Up @@ -724,7 +720,6 @@ tauri::Builder::default()
as Box<
dyn Fn(Url, NewWindowFeatures) -> tauri_runtime::webview::NewWindowResponse
+ Send
+ Sync
+ 'static,
>
});
Expand Down
5 changes: 1 addition & 4 deletions crates/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,9 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
/// # Platform-specific
///
/// - **Android / iOS**: Not supported.
/// - **Windows**: The closure is executed on a separate thread to prevent a deadlock.
///
/// [window.open]: https://developer.mozilla.org/en-US/docs/Web/API/Window/open
pub fn on_new_window<
F: Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + Sync + 'static,
>(
pub fn on_new_window<F: Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + 'static>(
mut self,
f: F,
) -> Self {
Expand Down
Loading