Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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/wait-async-window-creation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-runtime-wry": patch:bug
---

`create_window` and `create_webview` should wait for the window creation to complete before returning even if it's off main thread, it should also return the error if it failed
26 changes: 20 additions & 6 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ impl<T: UserEvent> Context<T> {
})
.unwrap_or((None, false));

let (tx, rx) = channel();
send_user_message(
self,
Message::CreateWindow(
Expand All @@ -331,8 +332,11 @@ impl<T: UserEvent> Context<T> {
after_window_creation,
)
}),
tx,
),
)?;
rx.recv()
.map_err(|_| crate::Error::FailedToReceiveMessage)??;

let dispatcher = WryWindowDispatcher {
window_id,
Expand Down Expand Up @@ -375,6 +379,7 @@ impl<T: UserEvent> Context<T> {
let window_id_wrapper = Arc::new(Mutex::new(window_id));
let window_id_wrapper_ = window_id_wrapper.clone();

let (tx, rx) = channel();
send_user_message(
self,
Message::CreateWebview(
Expand All @@ -390,8 +395,11 @@ impl<T: UserEvent> Context<T> {
options.focused_webview,
)
}),
tx,
),
)?;
rx.recv()
.map_err(|_| crate::Error::FailedToReceiveMessage)??;

let dispatcher = WryWebviewDispatcher {
window_id: window_id_wrapper,
Expand Down Expand Up @@ -1566,8 +1574,8 @@ pub enum Message<T: 'static> {
Window(WindowId, WindowMessage),
Webview(WindowId, WebviewId, WebviewMessage),
EventLoopWindowTarget(EventLoopWindowTargetMessage),
CreateWebview(WindowId, CreateWebviewClosure),
CreateWindow(WindowId, CreateWindowClosure<T>),
CreateWebview(WindowId, CreateWebviewClosure, Sender<Result<()>>),
CreateWindow(WindowId, CreateWindowClosure<T>, Sender<Result<()>>),
CreateRawWindow(
WindowId,
Box<dyn FnOnce() -> (String, TaoWindowBuilder) + Send>,
Expand Down Expand Up @@ -4058,7 +4066,7 @@ fn handle_user_message<T: UserEvent>(
}
}
}
Message::CreateWebview(window_id, handler) => {
Message::CreateWebview(window_id, handler, sender) => {
let window = windows
.0
.borrow()
Expand All @@ -4073,19 +4081,25 @@ fn handle_user_message<T: UserEvent>(
w.has_children.store(true, Ordering::Relaxed);
w
});
// SAFETY: The caller calls blocking `rx.recv()` so the receiver will never be dropped before this
sender.send(Ok(())).unwrap();
}
Err(e) => {
log::error!("{e}");
// SAFETY: The caller calls blocking `rx.recv()` so the receiver will never be dropped before this
sender.send(Err(e)).unwrap();
}
}
}
}
Message::CreateWindow(window_id, handler) => match handler(event_loop) {
Message::CreateWindow(window_id, handler, sender) => match handler(event_loop) {
Ok(webview) => {
windows.0.borrow_mut().insert(window_id, webview);
// SAFETY: The caller calls blocking `rx.recv()` so the receiver will never be dropped before this
sender.send(Ok(())).unwrap();
}
Err(e) => {
log::error!("{e}");
// SAFETY: The caller calls blocking `rx.recv()` so the receiver will never be dropped before this
sender.send(Err(e)).unwrap();
}
},
Message::CreateRawWindow(window_id, handler, sender) => {
Expand Down
Loading