This commit is contained in:
Tony 2026-02-06 14:33:41 +11:00 committed by GitHub
commit 95c775dc3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 57 additions and 76 deletions

5
Cargo.lock generated
View File

@ -10916,9 +10916,8 @@ checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51"
[[package]]
name = "wry"
version = "0.54.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e456eeaf7f09413fdc16799782879b2b9f1d264dfdbce4cf7e924df0ef36afb9"
version = "0.54.1"
source = "git+https://github.com/Legend-Master/wry.git?branch=new-window-handler-no-send#d76ecad82587b52dd09ff2bf0b75e032f8d06895"
dependencies = [
"base64 0.22.1",
"block2 0.6.0",

View File

@ -72,3 +72,4 @@ schemars_derive = { git = 'https://github.com/tauri-apps/schemars.git', branch =
tauri = { path = "./crates/tauri" }
tauri-plugin = { path = "./crates/tauri-plugin" }
tauri-utils = { path = "./crates/tauri-utils" }
wry = { git = "https://github.com/Legend-Master/wry.git", branch = "new-window-handler-no-send" }

View File

@ -3932,13 +3932,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}");
}
@ -4716,63 +4712,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;
};
};
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(),
}
}
tauri_runtime::webview::NewWindowResponse::Deny => wry::NewWindowResponse::Deny,
#[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,
}
});
}

View File

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

View File

@ -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;
@ -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 {
@ -724,7 +720,6 @@ tauri::Builder::default()
as Box<
dyn Fn(Url, NewWindowFeatures) -> tauri_runtime::webview::NewWindowResponse
+ Send
+ Sync
+ 'static,
>
});

View File

@ -314,12 +314,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 {