Browse Source

fix(core): dialog parent window on macOS, closes #3312 (#3753)

Lucas Fernandes Nogueira 3 years ago
parent
commit
bf89a05fcf
3 changed files with 155 additions and 75 deletions
  1. 5 0
      .changes/dialog-parent-macos.md
  2. 65 10
      core/tauri/src/api/dialog.rs
  3. 85 65
      examples/api/src-tauri/Cargo.lock

+ 5 - 0
.changes/dialog-parent-macos.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Use asynchronous file dialog on macOS and Windows to properly set the parent window.

+ 65 - 10
core/tauri/src/api/dialog.rs

@@ -33,24 +33,52 @@ macro_rules! run_dialog {
   }};
 }
 
+#[cfg(not(target_os = "linux"))]
+macro_rules! run_file_dialog {
+  ($e:expr, $h: ident) => {{
+    std::thread::spawn(move || {
+      let response = crate::async_runtime::block_on($e);
+      $h(response);
+    });
+  }};
+}
+
+#[cfg(target_os = "linux")]
+macro_rules! run_file_dialog {
+  ($e:expr, $h: ident) => {{
+    std::thread::spawn(move || {
+      let context = glib::MainContext::default();
+      context.invoke_with_priority(glib::PRIORITY_HIGH, move || {
+        let response = $e;
+        $h(response);
+      });
+    });
+  }};
+}
+
 macro_rules! run_dialog_sync {
   ($e:expr) => {{
     let (tx, rx) = sync_channel(0);
     let cb = move |response| {
       tx.send(response).unwrap();
     };
-    run_dialog!($e, cb);
+    run_file_dialog!($e, cb);
     rx.recv().unwrap()
   }};
 }
 
 macro_rules! file_dialog_builder {
   () => {
+    #[cfg(target_os = "linux")]
+    type FileDialog = rfd::FileDialog;
+    #[cfg(not(target_os = "linux"))]
+    type FileDialog = rfd::AsyncFileDialog;
+
     /// The file dialog builder.
     ///
     /// Constructs file picker dialogs that can select single/multiple files or directories.
     #[derive(Debug, Default)]
-    pub struct FileDialogBuilder(rfd::FileDialog);
+    pub struct FileDialogBuilder(FileDialog);
 
     impl FileDialogBuilder {
       /// Gets the default file dialog builder.
@@ -127,7 +155,11 @@ pub mod blocking {
     /// }
     /// ```
     pub fn pick_file(self) -> Option<PathBuf> {
-      run_dialog_sync!(self.0.pick_file())
+      #[allow(clippy::let_and_return)]
+      let response = run_dialog_sync!(self.0.pick_file());
+      #[cfg(not(target_os = "linux"))]
+      let response = response.map(|p| p.path().to_path_buf());
+      response
     }
 
     /// Shows the dialog to select multiple files.
@@ -146,7 +178,12 @@ pub mod blocking {
     /// }
     /// ```
     pub fn pick_files(self) -> Option<Vec<PathBuf>> {
-      run_dialog_sync!(self.0.pick_files())
+      #[allow(clippy::let_and_return)]
+      let response = run_dialog_sync!(self.0.pick_files());
+      #[cfg(not(target_os = "linux"))]
+      let response =
+        response.map(|paths| paths.into_iter().map(|p| p.path().to_path_buf()).collect());
+      response
     }
 
     /// Shows the dialog to select a single folder.
@@ -165,7 +202,11 @@ pub mod blocking {
     /// }
     /// ```
     pub fn pick_folder(self) -> Option<PathBuf> {
-      run_dialog_sync!(self.0.pick_folder())
+      #[allow(clippy::let_and_return)]
+      let response = run_dialog_sync!(self.0.pick_folder());
+      #[cfg(not(target_os = "linux"))]
+      let response = response.map(|p| p.path().to_path_buf());
+      response
     }
 
     /// Shows the dialog to save a file.
@@ -184,7 +225,11 @@ pub mod blocking {
     /// }
     /// ```
     pub fn save_file(self) -> Option<PathBuf> {
-      run_dialog_sync!(self.0.save_file())
+      #[allow(clippy::let_and_return)]
+      let response = run_dialog_sync!(self.0.save_file());
+      #[cfg(not(target_os = "linux"))]
+      let response = response.map(|p| p.path().to_path_buf());
+      response
     }
   }
 
@@ -305,7 +350,9 @@ mod nonblocking {
     ///   })
     /// ```
     pub fn pick_file<F: FnOnce(Option<PathBuf>) + Send + 'static>(self, f: F) {
-      run_dialog!(self.0.pick_file(), f)
+      #[cfg(not(target_os = "linux"))]
+      let f = |path: Option<rfd::FileHandle>| f(path.map(|p| p.path().to_path_buf()));
+      run_file_dialog!(self.0.pick_file(), f)
     }
 
     /// Shows the dialog to select multiple files.
@@ -327,7 +374,11 @@ mod nonblocking {
     ///   })
     /// ```
     pub fn pick_files<F: FnOnce(Option<Vec<PathBuf>>) + Send + 'static>(self, f: F) {
-      run_dialog!(self.0.pick_files(), f)
+      #[cfg(not(target_os = "linux"))]
+      let f = |paths: Option<Vec<rfd::FileHandle>>| {
+        f(paths.map(|list| list.into_iter().map(|p| p.path().to_path_buf()).collect()))
+      };
+      run_file_dialog!(self.0.pick_files(), f)
     }
 
     /// Shows the dialog to select a single folder.
@@ -349,7 +400,9 @@ mod nonblocking {
     ///   })
     /// ```
     pub fn pick_folder<F: FnOnce(Option<PathBuf>) + Send + 'static>(self, f: F) {
-      run_dialog!(self.0.pick_folder(), f)
+      #[cfg(not(target_os = "linux"))]
+      let f = |path: Option<rfd::FileHandle>| f(path.map(|p| p.path().to_path_buf()));
+      run_file_dialog!(self.0.pick_folder(), f)
     }
 
     /// Shows the dialog to save a file.
@@ -372,7 +425,9 @@ mod nonblocking {
     ///   })
     /// ```
     pub fn save_file<F: FnOnce(Option<PathBuf>) + Send + 'static>(self, f: F) {
-      run_dialog!(self.0.save_file(), f)
+      #[cfg(not(target_os = "linux"))]
+      let f = |path: Option<rfd::FileHandle>| f(path.map(|p| p.path().to_path_buf()));
+      run_file_dialog!(self.0.save_file(), f)
     }
   }
 

+ 85 - 65
examples/api/src-tauri/Cargo.lock

@@ -101,20 +101,6 @@ version = "0.7.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
 
-[[package]]
-name = "ashpd"
-version = "0.2.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "098dee97729c0164b39a8a7de9c20e4b0eb9cd57f87c8bb465224587b44b1683"
-dependencies = [
- "enumflags2",
- "futures",
- "rand 0.8.5",
- "serde",
- "serde_repr",
- "zbus",
-]
-
 [[package]]
 name = "async-broadcast"
 version = "0.3.4"
@@ -407,9 +393,9 @@ dependencies = [
 
 [[package]]
 name = "cfb"
-version = "0.4.0"
+version = "0.6.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ca453e8624711b2f0f4eb47076a318feda166252a827ee25d067b43de83dcba0"
+checksum = "74f89d248799e3f15f91b70917f65381062a01bb8e222700ea0e5a7ff9785f9c"
 dependencies = [
  "byteorder",
  "uuid",
@@ -790,12 +776,11 @@ dependencies = [
 
 [[package]]
 name = "deflate"
-version = "0.8.6"
+version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174"
+checksum = "c86f7e25f518f4b81808a2cf1c50996a61f5c2eb394b2393bd87f2a4780a432f"
 dependencies = [
  "adler32",
- "byteorder",
 ]
 
 [[package]]
@@ -1573,9 +1558,9 @@ dependencies = [
 
 [[package]]
 name = "infer"
-version = "0.4.0"
+version = "0.7.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f92b41dab759f9e8427c03f519c344a14655490b8db548dac1e57a75b3258391"
+checksum = "20b2b533137b9cad970793453d4f921c2e91312a6d88b1085c07bc15fc51bb3b"
 dependencies = [
  "cfb",
 ]
@@ -1836,21 +1821,21 @@ checksum = "95ccf091884470c4b3a80ad6daadbb2e7736611d631cbf0c9e603bb7dbcfdfd9"
 
 [[package]]
 name = "miniz_oxide"
-version = "0.3.7"
+version = "0.4.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435"
+checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b"
 dependencies = [
- "adler32",
+ "adler",
+ "autocfg",
 ]
 
 [[package]]
 name = "miniz_oxide"
-version = "0.4.4"
+version = "0.5.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b"
+checksum = "d2b29bd4bc3f33391105ebee3589c19197c4271e3e5a9ec9bfe8127eeff8f082"
 dependencies = [
  "adler",
- "autocfg",
 ]
 
 [[package]]
@@ -2065,9 +2050,9 @@ dependencies = [
 
 [[package]]
 name = "once_cell"
-version = "1.9.0"
+version = "1.10.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
+checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
 
 [[package]]
 name = "opaque-debug"
@@ -2365,14 +2350,14 @@ dependencies = [
 
 [[package]]
 name = "png"
-version = "0.16.8"
+version = "0.17.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6"
+checksum = "dc38c0ad57efb786dd57b9864e5b18bae478c00c824dc55a38bbc9da95dde3ba"
 dependencies = [
  "bitflags",
  "crc32fast",
- "deflate 0.8.6",
- "miniz_oxide 0.3.7",
+ "deflate 1.0.0",
+ "miniz_oxide 0.5.1",
 ]
 
 [[package]]
@@ -2388,12 +2373,6 @@ dependencies = [
  "winapi",
 ]
 
-[[package]]
-name = "pollster"
-version = "0.2.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5da3b0203fd7ee5720aa0b5e790b591aa5d3f41c3ed2c34a3a393382198af2f7"
-
 [[package]]
 name = "polyval"
 version = "0.5.3"
@@ -2638,9 +2617,9 @@ dependencies = [
 
 [[package]]
 name = "regex"
-version = "1.5.4"
+version = "1.5.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
+checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
 dependencies = [
  "aho-corasick",
  "memchr",
@@ -2673,11 +2652,10 @@ dependencies = [
 
 [[package]]
 name = "rfd"
-version = "0.7.0"
+version = "0.8.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2aaf1d71ccd44689f7c2c72da1117fd8db71f72a76fe9b5c5dbb17ab903007e0"
+checksum = "e7ca9214be1b6d296d4d539a31e795e556cdb43e60cbf0b77003be5b01075c13"
 dependencies = [
- "ashpd",
  "block",
  "dispatch",
  "glib-sys 0.15.6",
@@ -2689,12 +2667,11 @@ dependencies = [
  "objc",
  "objc-foundation",
  "objc_id",
- "pollster",
  "raw-window-handle",
  "wasm-bindgen",
  "wasm-bindgen-futures",
  "web-sys",
- "windows 0.30.0",
+ "windows 0.33.0",
 ]
 
 [[package]]
@@ -3280,7 +3257,7 @@ dependencies = [
 
 [[package]]
 name = "tauri"
-version = "1.0.0-rc.3"
+version = "1.0.0-rc.4"
 dependencies = [
  "anyhow",
  "attohttpc",
@@ -3309,7 +3286,7 @@ dependencies = [
  "os_info",
  "os_pipe",
  "percent-encoding",
- "png 0.16.8",
+ "png 0.17.5",
  "rand 0.8.5",
  "raw-window-handle",
  "regex",
@@ -3337,7 +3314,7 @@ dependencies = [
 
 [[package]]
 name = "tauri-build"
-version = "1.0.0-rc.3"
+version = "1.0.0-rc.4"
 dependencies = [
  "anyhow",
  "cargo_toml",
@@ -3349,12 +3326,12 @@ dependencies = [
 
 [[package]]
 name = "tauri-codegen"
-version = "1.0.0-rc.2"
+version = "1.0.0-rc.3"
 dependencies = [
  "base64",
  "blake3",
  "ico",
- "png 0.16.8",
+ "png 0.17.5",
  "proc-macro2",
  "quote",
  "regex",
@@ -3370,7 +3347,7 @@ dependencies = [
 
 [[package]]
 name = "tauri-macros"
-version = "1.0.0-rc.2"
+version = "1.0.0-rc.3"
 dependencies = [
  "heck 0.4.0",
  "proc-macro2",
@@ -3382,7 +3359,7 @@ dependencies = [
 
 [[package]]
 name = "tauri-runtime"
-version = "0.3.2"
+version = "0.3.3"
 dependencies = [
  "gtk",
  "http",
@@ -3399,9 +3376,10 @@ dependencies = [
 
 [[package]]
 name = "tauri-runtime-wry"
-version = "0.3.2"
+version = "0.3.3"
 dependencies = [
  "gtk",
+ "rand 0.8.5",
  "tauri-runtime",
  "tauri-utils",
  "uuid",
@@ -3412,7 +3390,7 @@ dependencies = [
 
 [[package]]
 name = "tauri-utils"
-version = "1.0.0-rc.2"
+version = "1.0.0-rc.3"
 dependencies = [
  "aes-gcm",
  "ctor",
@@ -3993,13 +3971,26 @@ version = "0.30.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b749ebd2304aa012c5992d11a25d07b406bdbe5f79d371cb7a918ce501a19eb0"
 dependencies = [
- "windows_aarch64_msvc",
+ "windows_aarch64_msvc 0.30.0",
  "windows_i686_gnu 0.30.0",
  "windows_i686_msvc 0.30.0",
  "windows_x86_64_gnu 0.30.0",
  "windows_x86_64_msvc 0.30.0",
 ]
 
+[[package]]
+name = "windows"
+version = "0.33.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0128fa8e65e0616e45033d68dc0b7fbd521080b7844e5cad3a4a4d201c4b2bd2"
+dependencies = [
+ "windows_aarch64_msvc 0.33.0",
+ "windows_i686_gnu 0.33.0",
+ "windows_i686_msvc 0.33.0",
+ "windows_x86_64_gnu 0.33.0",
+ "windows_x86_64_msvc 0.33.0",
+]
+
 [[package]]
 name = "windows-bindgen"
 version = "0.30.0"
@@ -4016,6 +4007,12 @@ version = "0.30.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "29277a4435d642f775f63c7d1faeb927adba532886ce0287bd985bffb16b6bca"
 
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.33.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cd761fd3eb9ab8cc1ed81e56e567f02dd82c4c837e48ac3b2181b9ffc5060807"
+
 [[package]]
 name = "windows_gen"
 version = "0.30.0"
@@ -4038,6 +4035,12 @@ version = "0.30.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "1145e1989da93956c68d1864f32fb97c8f561a8f89a5125f6a2b7ea75524e4b8"
 
+[[package]]
+name = "windows_i686_gnu"
+version = "0.33.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cab0cf703a96bab2dc0c02c0fa748491294bf9b7feb27e1f4f96340f208ada0e"
+
 [[package]]
 name = "windows_i686_msvc"
 version = "0.24.0"
@@ -4050,6 +4053,12 @@ version = "0.30.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d4a09e3a0d4753b73019db171c1339cd4362c8c44baf1bcea336235e955954a6"
 
+[[package]]
+name = "windows_i686_msvc"
+version = "0.33.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8cfdbe89cc9ad7ce618ba34abc34bbb6c36d99e96cae2245b7943cd75ee773d0"
+
 [[package]]
 name = "windows_macros"
 version = "0.30.0"
@@ -4086,6 +4095,12 @@ version = "0.30.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8ca64fcb0220d58db4c119e050e7af03c69e6f4f415ef69ec1773d9aab422d5a"
 
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.33.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4dd9b0c0e9ece7bb22e84d70d01b71c6d6248b81a3c60d11869451b4cb24784"
+
 [[package]]
 name = "windows_x86_64_msvc"
 version = "0.24.0"
@@ -4098,6 +4113,12 @@ version = "0.30.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "08cabc9f0066848fef4bc6a1c1668e6efce38b661d2aeec75d18d8617eebb5f1"
 
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.33.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ff1e4aa646495048ec7f3ffddc411e1d829c026a2ec62b39da15c1055e406eaa"
+
 [[package]]
 name = "winres"
 version = "0.1.12"
@@ -4247,29 +4268,28 @@ dependencies = [
 
 [[package]]
 name = "zip"
-version = "0.5.13"
+version = "0.6.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "93ab48844d61251bb3835145c521d88aa4031d7139e8485990f60ca911fa0815"
+checksum = "e6fa4aa90e99fb8d701bda16fb040d8ed2f9c7176fb44de750e880a74b580315"
 dependencies = [
  "byteorder",
  "crc32fast",
- "thiserror",
 ]
 
 [[package]]
 name = "zstd"
-version = "0.10.0+zstd.1.5.2"
+version = "0.11.1+zstd.1.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3b1365becbe415f3f0fcd024e2f7b45bacfb5bdd055f0dc113571394114e7bdd"
+checksum = "77a16b8414fde0414e90c612eba70985577451c4c504b99885ebed24762cb81a"
 dependencies = [
  "zstd-safe",
 ]
 
 [[package]]
 name = "zstd-safe"
-version = "4.1.4+zstd.1.5.2"
+version = "5.0.1+zstd.1.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2f7cd17c9af1a4d6c24beb1cc54b17e2ef7b593dc92f19e9d9acad8b182bbaee"
+checksum = "7c12659121420dd6365c5c3de4901f97145b79651fb1d25814020ed2ed0585ae"
 dependencies = [
  "libc",
  "zstd-sys",
@@ -4277,9 +4297,9 @@ dependencies = [
 
 [[package]]
 name = "zstd-sys"
-version = "1.6.3+zstd.1.5.2"
+version = "2.0.1+zstd.1.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fc49afa5c8d634e75761feda8c592051e7eeb4683ba827211eb0d731d3402ea8"
+checksum = "9fd07cbbc53846d9145dbffdf6dd09a7a0aa52be46741825f5c97bdd4f73f12b"
 dependencies = [
  "cc",
  "libc",