Skip to content

Commit

Permalink
refactor(window): block main thread when creating a new window (#4298)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jun 8, 2022
1 parent d703d27 commit 69ae6f1
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 60 deletions.
5 changes: 5 additions & 0 deletions .changes/refactor-window-creation.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

**Breaking change:** Revert the window creation to be blocking in the main thread. This ensures the window is created before using other methods, but has an issue on Windows where the program deadlocks when creating a window in a Tauri command if it is not `async`. The documentation now states that commands must be `async` in other to prevent it until the issue is fixed in Webview2.
11 changes: 5 additions & 6 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -225,16 +225,15 @@ impl<T: UserEvent> Context<T> {

self.prepare_window(window_id);

// we cannot use `send_user_message` here, see https://github.com/tauri-apps/wry/issues/583
self
.proxy
.send_event(Message::CreateWebview(
send_user_message(
self,
Message::CreateWebview(
window_id,
Box::new(move |event_loop, web_context| {
create_webview(window_id, event_loop, web_context, context, pending)
}),
))
.map_err(|_| Error::FailedToSendMessage)?;
),
)?;

let dispatcher = WryDispatcher {
window_id,
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-runtime/src/lib.rs
Expand Up @@ -81,7 +81,7 @@ impl SystemTray {
}

/// Type of user attention requested on a window.
#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(tag = "type")]
pub enum UserAttentionType {
/// ## Platform-specific
Expand Down
92 changes: 46 additions & 46 deletions core/tauri-utils/src/config.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions core/tauri/src/api/file/extract.rs
Expand Up @@ -38,7 +38,7 @@ impl<R: Read + Seek> ArchiveReader<R> {
}

/// The supported archive formats.
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ArchiveFormat {
/// Tar archive.
Expand All @@ -48,7 +48,7 @@ pub enum ArchiveFormat {
}

/// The supported compression types.
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Compression {
/// Gz compression (e.g. `.tar.gz` archives)
Expand Down
6 changes: 3 additions & 3 deletions core/tauri/src/api/process/command.rs
Expand Up @@ -275,7 +275,7 @@ impl Command {
Ok(status) => {
let _l = guard.write().unwrap();
commands().lock().unwrap().remove(&child_.id());
let _ = block_on_task(async move {
block_on_task(async move {
tx.send(CommandEvent::Terminated(TerminatedPayload {
code: status.code(),
#[cfg(windows)]
Expand All @@ -284,11 +284,11 @@ impl Command {
signal: status.signal(),
}))
.await
});
})
}
Err(e) => {
let _l = guard.write().unwrap();
let _ = block_on_task(async move { tx.send(CommandEvent::Error(e.to_string())).await });
block_on_task(async move { tx.send(CommandEvent::Error(e.to_string())).await })
}
};
});
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/endpoints/dialog.rs
Expand Up @@ -269,7 +269,7 @@ fn set_default_path(
if parent.components().count() > 0 {
dialog_builder = dialog_builder.set_directory(parent);
}
dialog_builder = dialog_builder.set_file_name(&file_name.to_string_lossy().to_string());
dialog_builder = dialog_builder.set_file_name(&file_name.to_string_lossy());
} else {
dialog_builder = dialog_builder.set_directory(default_path);
}
Expand Down
48 changes: 47 additions & 1 deletion core/tauri/src/window.rs
Expand Up @@ -120,6 +120,52 @@ impl<'a, R: Runtime> fmt::Debug for WindowBuilder<'a, R> {

impl<'a, R: Runtime> WindowBuilder<'a, R> {
/// Initializes a webview window builder with the given window label and URL to load on the webview.
///
/// # Examples
///
/// - Create a window in the setup hook:
///
/// ```
/// tauri::Builder::default()
/// .setup(|app| {
/// let window = tauri::WindowBuilder::new(app, "label", tauri::WindowUrl::App("index.html".into()))
/// .build()?;
/// Ok(())
/// });
/// ```
///
/// - Create a window in a separate thread:
///
/// ```
/// tauri::Builder::default()
/// .setup(|app| {
/// let handle = app.handle();
/// std::thread::spawn(move || {
/// let window = tauri::WindowBuilder::new(&handle, "label", tauri::WindowUrl::App("index.html".into()))
/// .build()
/// .unwrap();
/// });
/// Ok(())
/// });
/// ```
///
/// - Create a window in a command:
///
/// ```
/// #[tauri::command]
/// async fn create_window(app: tauri::AppHandle) {
/// let window = tauri::WindowBuilder::new(&app, "label", tauri::WindowUrl::External("https://tauri.studio/".parse().unwrap()))
/// .build()
/// .unwrap();
/// }
/// ```
///
/// # Known issues
///
/// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue].
/// You should use `async` commands when creating windows.
///
/// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583
pub fn new<M: Manager<R>, L: Into<String>>(manager: &'a M, label: L, url: WindowUrl) -> Self {
let runtime = manager.runtime();
let app_handle = manager.app_handle();
Expand Down Expand Up @@ -600,7 +646,7 @@ impl Window<crate::Wry> {
/// use webkit2gtk::traits::WebViewExt;
/// webview.inner().set_zoom_level(4.);
/// }
///
///
/// #[cfg(windows)]
/// unsafe {
/// // see https://docs.rs/webview2-com/latest/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html
Expand Down

0 comments on commit 69ae6f1

Please sign in to comment.