Browse Source

fix(core): `Window` must be `Send + Sync` on Windows, closes #2078 (#2093)

Lucas Fernandes Nogueira 4 years ago
parent
commit
fe32afcc93

+ 7 - 0
.changes/window-send-sync.md

@@ -0,0 +1,7 @@
+---
+"tauri": patch
+"tauri-runtime-wry": patch
+"tauri-runtime": patch
+---
+
+`Window` is now `Send + Sync` on Windows.

+ 3 - 3
core/tauri-runtime-wry/src/lib.rs

@@ -594,7 +594,7 @@ impl From<FileDropEventWrapper> for FileDropEvent {
 }
 
 #[cfg(windows)]
-struct Hwnd(*mut std::ffi::c_void);
+struct Hwnd(HWND);
 #[cfg(windows)]
 unsafe impl Send for Hwnd {}
 
@@ -823,7 +823,7 @@ impl Dispatch for WryDispatcher {
   }
 
   #[cfg(windows)]
-  fn hwnd(&self) -> Result<*mut std::ffi::c_void> {
+  fn hwnd(&self) -> Result<HWND> {
     Ok(dispatcher_getter!(self, WindowMessage::Hwnd).0)
   }
 
@@ -1594,7 +1594,7 @@ fn handle_event_loop(
             #[cfg(windows)]
             WindowMessage::Hwnd(tx) => {
               use wry::application::platform::windows::WindowExtWindows;
-              tx.send(Hwnd(window.hwnd())).unwrap()
+              tx.send(Hwnd(window.hwnd() as HWND)).unwrap()
             }
             // Setters
             WindowMessage::Center(tx) => {

+ 3 - 1
core/tauri-runtime/src/lib.rs

@@ -11,6 +11,8 @@ use std::{fmt::Debug, hash::Hash, path::PathBuf};
 use serde::{Deserialize, Serialize};
 use tauri_utils::assets::Assets;
 use uuid::Uuid;
+#[cfg(windows)]
+use winapi::shared::windef::HWND;
 
 /// Create window and system tray menus.
 #[cfg(any(feature = "menu", feature = "system-tray"))]
@@ -413,7 +415,7 @@ pub trait Dispatch: Clone + Send + Sized + 'static {
 
   /// Returns the native handle that is used by this window.
   #[cfg(windows)]
-  fn hwnd(&self) -> crate::Result<*mut std::ffi::c_void>;
+  fn hwnd(&self) -> crate::Result<HWND>;
 
   // SETTERS
 

+ 6 - 1
core/tauri/src/window.rs

@@ -503,7 +503,12 @@ impl<P: Params> Window<P> {
   /// You can spawn a task to use the API using the [`async_runtime`](crate::async_runtime) to prevent the panic.
   #[cfg(windows)]
   pub fn hwnd(&self) -> crate::Result<*mut std::ffi::c_void> {
-    self.window.dispatcher.hwnd().map_err(Into::into)
+    self
+      .window
+      .dispatcher
+      .hwnd()
+      .map(|hwnd| hwnd as *mut _)
+      .map_err(Into::into)
   }
 
   // Setters