From 16fc0f2ee34fdaef54d9ec88bbd2d6f802c90a4d Mon Sep 17 00:00:00 2001 From: Fabian-Lars Date: Wed, 6 Mar 2024 18:38:51 +0100 Subject: [PATCH] fix(dialog): Remove Linux specific code for rfd 0.14 compatibility (#1033) fixes #956 --- .changes/dialog-linux-freeze.md | 5 +++ plugins/dialog/src/desktop.rs | 58 +++++++-------------------------- 2 files changed, 16 insertions(+), 47 deletions(-) create mode 100644 .changes/dialog-linux-freeze.md diff --git a/.changes/dialog-linux-freeze.md b/.changes/dialog-linux-freeze.md new file mode 100644 index 00000000..5b9be9f9 --- /dev/null +++ b/.changes/dialog-linux-freeze.md @@ -0,0 +1,5 @@ +--- +"dialog": "patch" +--- + +Fixed an issue where dialogs would not spawn but instead freeze the whole app. diff --git a/plugins/dialog/src/desktop.rs b/plugins/dialog/src/desktop.rs index 7843f3b5..5d98952d 100644 --- a/plugins/dialog/src/desktop.rs +++ b/plugins/dialog/src/desktop.rs @@ -11,6 +11,7 @@ use std::path::PathBuf; use raw_window_handle::{HasWindowHandle, RawWindowHandle}; +use rfd::{AsyncFileDialog, AsyncMessageDialog}; use serde::de::DeserializeOwned; use tauri::{plugin::PluginApi, AppHandle, Runtime}; @@ -18,16 +19,6 @@ use crate::{models::*, FileDialogBuilder, MessageDialogBuilder}; const OK: &str = "Ok"; -#[cfg(target_os = "linux")] -type FileDialog = rfd::FileDialog; -#[cfg(not(target_os = "linux"))] -type FileDialog = rfd::AsyncFileDialog; - -#[cfg(target_os = "linux")] -type MessageDialog = rfd::MessageDialog; -#[cfg(not(target_os = "linux"))] -type MessageDialog = rfd::AsyncMessageDialog; - pub fn init( app: &AppHandle, _api: PluginApi, @@ -51,40 +42,18 @@ impl Dialog { } } -#[cfg(not(target_os = "linux"))] macro_rules! run_dialog { ($e:expr, $h: expr) => {{ std::thread::spawn(move || $h(tauri::async_runtime::block_on($e))); }}; } -#[cfg(target_os = "linux")] -macro_rules! run_dialog { - ($e:expr, $h: ident) => {{ - std::thread::spawn(move || { - let context = glib::MainContext::default(); - context.invoke_with_priority(glib::PRIORITY_HIGH, move || $h($e)); - }); - }}; -} - -#[cfg(not(target_os = "linux"))] macro_rules! run_file_dialog { ($e:expr, $h: ident) => {{ std::thread::spawn(move || $h(tauri::async_runtime::block_on($e))); }}; } -#[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 || $h($e)); - }); - }}; -} - impl From for rfd::MessageLevel { fn from(kind: MessageDialogKind) -> Self { match kind { @@ -105,9 +74,9 @@ impl HasWindowHandle for WindowHandle { } } -impl From> for FileDialog { +impl From> for AsyncFileDialog { fn from(d: FileDialogBuilder) -> Self { - let mut builder = FileDialog::new(); + let mut builder = AsyncFileDialog::new(); if let Some(title) = d.title { builder = builder.set_title(title); @@ -133,9 +102,9 @@ impl From> for FileDialog { } } -impl From> for MessageDialog { +impl From> for AsyncMessageDialog { fn from(d: MessageDialogBuilder) -> Self { - let mut dialog = MessageDialog::new() + let mut dialog = AsyncMessageDialog::new() .set_title(&d.title) .set_description(&d.message) .set_level(d.kind.into()); @@ -162,49 +131,44 @@ pub fn pick_file) + Send + 'static>( dialog: FileDialogBuilder, f: F, ) { - #[cfg(not(target_os = "linux"))] let f = |path: Option| f(path.map(|p| p.path().to_path_buf())); - run_file_dialog!(FileDialog::from(dialog).pick_file(), f) + run_file_dialog!(AsyncFileDialog::from(dialog).pick_file(), f) } pub fn pick_files>) + Send + 'static>( dialog: FileDialogBuilder, f: F, ) { - #[cfg(not(target_os = "linux"))] let f = |paths: Option>| { f(paths.map(|list| list.into_iter().map(|p| p.path().to_path_buf()).collect())) }; - run_file_dialog!(FileDialog::from(dialog).pick_files(), f) + run_file_dialog!(AsyncFileDialog::from(dialog).pick_files(), f) } pub fn pick_folder) + Send + 'static>( dialog: FileDialogBuilder, f: F, ) { - #[cfg(not(target_os = "linux"))] let f = |path: Option| f(path.map(|p| p.path().to_path_buf())); - run_file_dialog!(FileDialog::from(dialog).pick_folder(), f) + run_file_dialog!(AsyncFileDialog::from(dialog).pick_folder(), f) } pub fn pick_folders>) + Send + 'static>( dialog: FileDialogBuilder, f: F, ) { - #[cfg(not(target_os = "linux"))] let f = |paths: Option>| { f(paths.map(|list| list.into_iter().map(|p| p.path().to_path_buf()).collect())) }; - run_file_dialog!(FileDialog::from(dialog).pick_folders(), f) + run_file_dialog!(AsyncFileDialog::from(dialog).pick_folders(), f) } pub fn save_file) + Send + 'static>( dialog: FileDialogBuilder, f: F, ) { - #[cfg(not(target_os = "linux"))] let f = |path: Option| f(path.map(|p| p.path().to_path_buf())); - run_file_dialog!(FileDialog::from(dialog).save_file(), f) + run_file_dialog!(AsyncFileDialog::from(dialog).save_file(), f) } /// Shows a message dialog @@ -223,5 +187,5 @@ pub fn show_message_dialog( }); }; - run_dialog!(MessageDialog::from(dialog).show(), f); + run_dialog!(AsyncMessageDialog::from(dialog).show(), f); }