Skip to content

Commit d34497e

Browse files
refactor(runtime-wry): remove RefCell hack (#14862)
* refactor(runtime-wry): remove RefCell hack * Remove `Sync` requirement on `on_new_window` * Merge branch 'dev' into remove-ref-cell-hack * Add change file
1 parent 4017a7e commit d34497e

5 files changed

Lines changed: 61 additions & 74 deletions

File tree

.changes/new-window-main-thread.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": minor:changes
3+
"tauri-runtime-wry": minor:changes
4+
---
5+
6+
The new window handler passed to `on_new_window` no longer requires `Sync`, and runs on main thread on Windows, aligning with other platforms

crates/tauri-runtime-wry/src/lib.rs

Lines changed: 51 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,13 +4000,9 @@ fn handle_user_message<T: UserEvent>(
40004000
}
40014001
}
40024002
Message::CreateWindow(window_id, handler) => match handler(event_loop) {
4003-
// wait for borrow_mut to be available - on Windows we might poll for the window to be inserted
4004-
Ok(webview) => loop {
4005-
if let Ok(mut windows) = windows.0.try_borrow_mut() {
4006-
windows.insert(window_id, webview);
4007-
break;
4008-
}
4009-
},
4003+
Ok(webview) => {
4004+
windows.0.borrow_mut().insert(window_id, webview);
4005+
}
40104006
Err(e) => {
40114007
log::error!("{e}");
40124008
}
@@ -4789,63 +4785,56 @@ You may have it installed on another user account, but it is not available for t
47894785
#[cfg(desktop)]
47904786
let context = context.clone();
47914787
webview_builder = webview_builder.with_new_window_req_handler(move |url, features| {
4792-
url
4793-
.parse()
4794-
.map(|url| {
4795-
let response = new_window_handler(
4796-
url,
4797-
tauri_runtime::webview::NewWindowFeatures::new(
4798-
features.size,
4799-
features.position,
4800-
tauri_runtime::webview::NewWindowOpener {
4801-
#[cfg(desktop)]
4802-
webview: features.opener.webview,
4803-
#[cfg(windows)]
4804-
environment: features.opener.environment,
4805-
#[cfg(target_os = "macos")]
4806-
target_configuration: features.opener.target_configuration,
4807-
},
4808-
),
4809-
);
4810-
match response {
4811-
tauri_runtime::webview::NewWindowResponse::Allow => wry::NewWindowResponse::Allow,
4788+
let Ok(url) = url.parse() else {
4789+
return wry::NewWindowResponse::Deny;
4790+
};
4791+
let response = new_window_handler(
4792+
url,
4793+
tauri_runtime::webview::NewWindowFeatures::new(
4794+
features.size,
4795+
features.position,
4796+
tauri_runtime::webview::NewWindowOpener {
48124797
#[cfg(desktop)]
4813-
tauri_runtime::webview::NewWindowResponse::Create { window_id } => {
4814-
let windows = &context.main_thread.windows.0;
4815-
let webview = loop {
4816-
if let Some(webview) = windows.try_borrow().ok().and_then(|windows| {
4817-
windows
4818-
.get(&window_id)
4819-
.map(|window| window.webviews.first().unwrap().clone())
4820-
}) {
4821-
break webview;
4822-
} else {
4823-
// on Windows the window is created async so we should wait for it to be available
4824-
std::thread::sleep(std::time::Duration::from_millis(50));
4825-
continue;
4826-
};
4827-
};
4828-
4829-
#[cfg(desktop)]
4830-
wry::NewWindowResponse::Create {
4831-
#[cfg(target_os = "macos")]
4832-
webview: wry::WebViewExtMacOS::webview(&*webview).as_super().into(),
4833-
#[cfg(any(
4834-
target_os = "linux",
4835-
target_os = "dragonfly",
4836-
target_os = "freebsd",
4837-
target_os = "netbsd",
4838-
target_os = "openbsd",
4839-
))]
4840-
webview: webview.webview(),
4841-
#[cfg(windows)]
4842-
webview: webview.webview(),
4843-
}
4844-
}
4845-
tauri_runtime::webview::NewWindowResponse::Deny => wry::NewWindowResponse::Deny,
4798+
webview: features.opener.webview,
4799+
#[cfg(windows)]
4800+
environment: features.opener.environment,
4801+
#[cfg(target_os = "macos")]
4802+
target_configuration: features.opener.target_configuration,
4803+
},
4804+
),
4805+
);
4806+
match response {
4807+
tauri_runtime::webview::NewWindowResponse::Allow => wry::NewWindowResponse::Allow,
4808+
#[cfg(desktop)]
4809+
tauri_runtime::webview::NewWindowResponse::Create { window_id } => {
4810+
let windows = &context.main_thread.windows.0;
4811+
let webview = windows
4812+
.borrow()
4813+
.get(&window_id)
4814+
.unwrap()
4815+
.webviews
4816+
.first()
4817+
.unwrap()
4818+
.clone();
4819+
4820+
#[cfg(desktop)]
4821+
wry::NewWindowResponse::Create {
4822+
#[cfg(target_os = "macos")]
4823+
webview: wry::WebViewExtMacOS::webview(&*webview).as_super().into(),
4824+
#[cfg(any(
4825+
target_os = "linux",
4826+
target_os = "dragonfly",
4827+
target_os = "freebsd",
4828+
target_os = "netbsd",
4829+
target_os = "openbsd",
4830+
))]
4831+
webview: webview.webview(),
4832+
#[cfg(windows)]
4833+
webview: webview.webview(),
48464834
}
4847-
})
4848-
.unwrap_or(wry::NewWindowResponse::Deny)
4835+
}
4836+
tauri_runtime::webview::NewWindowResponse::Deny => wry::NewWindowResponse::Deny,
4837+
}
48494838
});
48504839
}
48514840

crates/tauri-runtime/src/webview.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type WebResourceRequestHandler =
3333

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

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

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

crates/tauri/src/webview/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ use std::{
5858
pub(crate) type WebResourceRequestHandler =
5959
dyn Fn(http::Request<Vec<u8>>, &mut http::Response<Cow<'static, [u8]>>) + Send + Sync;
6060
pub(crate) type NavigationHandler = dyn Fn(&Url) -> bool + Send;
61-
pub(crate) type NewWindowHandler<R> =
62-
dyn Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + Sync;
61+
pub(crate) type NewWindowHandler<R> = dyn Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send;
6362
pub(crate) type UriSchemeProtocolHandler =
6463
Box<dyn Fn(&str, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync>;
6564
pub(crate) type OnPageLoad<R> = dyn Fn(Webview<R>, PageLoadPayload<'_>) + Send + Sync + 'static;
@@ -581,12 +580,9 @@ tauri::Builder::default()
581580
/// # Platform-specific
582581
///
583582
/// - **Android / iOS**: Not supported.
584-
/// - **Windows**: The closure is executed on a separate thread to prevent a deadlock.
585583
///
586584
/// [window.open]: https://developer.mozilla.org/en-US/docs/Web/API/Window/open
587-
pub fn on_new_window<
588-
F: Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + Sync + 'static,
589-
>(
585+
pub fn on_new_window<F: Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + 'static>(
590586
mut self,
591587
f: F,
592588
) -> Self {
@@ -724,7 +720,6 @@ tauri::Builder::default()
724720
as Box<
725721
dyn Fn(Url, NewWindowFeatures) -> tauri_runtime::webview::NewWindowResponse
726722
+ Send
727-
+ Sync
728723
+ 'static,
729724
>
730725
});

crates/tauri/src/webview/webview_window.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,9 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
310310
/// # Platform-specific
311311
///
312312
/// - **Android / iOS**: Not supported.
313-
/// - **Windows**: The closure is executed on a separate thread to prevent a deadlock.
314313
///
315314
/// [window.open]: https://developer.mozilla.org/en-US/docs/Web/API/Window/open
316-
pub fn on_new_window<
317-
F: Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + Sync + 'static,
318-
>(
315+
pub fn on_new_window<F: Fn(Url, NewWindowFeatures) -> NewWindowResponse<R> + Send + 'static>(
319316
mut self,
320317
f: F,
321318
) -> Self {

0 commit comments

Comments
 (0)