From 2e2c0a1b958fcbc7de855ef1d305b11855f2eb8e Mon Sep 17 00:00:00 2001 From: pashokitsme Date: Mon, 27 Nov 2023 20:55:26 +0300 Subject: [PATCH] fix(dialog): fix message dialog return with custom buttons, fixes #711 (#769) * fix confirm * remove unnecessary lines * fix * change file --- .changes/dialog-return-result.md | 6 +++++ plugins/dialog/src/desktop.rs | 39 +++++++++++++------------------- 2 files changed, 22 insertions(+), 23 deletions(-) create mode 100644 .changes/dialog-return-result.md diff --git a/.changes/dialog-return-result.md b/.changes/dialog-return-result.md new file mode 100644 index 000000000..e375a27d8 --- /dev/null +++ b/.changes/dialog-return-result.md @@ -0,0 +1,6 @@ +--- +"dialog": patch +"dialog-js": patch +--- + +Fix incorrect result for dialog messages. \ No newline at end of file diff --git a/plugins/dialog/src/desktop.rs b/plugins/dialog/src/desktop.rs index 2892d13d6..257fdf734 100644 --- a/plugins/dialog/src/desktop.rs +++ b/plugins/dialog/src/desktop.rs @@ -53,14 +53,8 @@ impl Dialog { #[cfg(not(target_os = "linux"))] macro_rules! run_dialog { - ($e:expr, $h: ident) => {{ - std::thread::spawn(move || { - let response = tauri::async_runtime::block_on($e); - $h(!matches!( - response, - rfd::MessageDialogResult::No | rfd::MessageDialogResult::Cancel - )); - }); + ($e:expr, $h: expr) => {{ + std::thread::spawn(move || $h(tauri::async_runtime::block_on($e))); }}; } @@ -69,13 +63,7 @@ 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 || { - let response = $e; - $h(!matches!( - response, - rfd::MessageDialogResult::No | rfd::MessageDialogResult::Cancel - )); - }); + context.invoke_with_priority(glib::PRIORITY_HIGH, move || $h($e)); }); }}; } @@ -83,10 +71,7 @@ macro_rules! run_dialog { #[cfg(not(target_os = "linux"))] macro_rules! run_file_dialog { ($e:expr, $h: ident) => {{ - std::thread::spawn(move || { - let response = tauri::async_runtime::block_on($e); - $h(response); - }); + std::thread::spawn(move || $h(tauri::async_runtime::block_on($e))); }}; } @@ -95,10 +80,7 @@ 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); - }); + context.invoke_with_priority(glib::PRIORITY_HIGH, move || $h($e)); }); }}; } @@ -226,5 +208,16 @@ pub fn show_message_dialog( dialog: MessageDialogBuilder, f: F, ) { + use rfd::MessageDialogResult; + + let ok_label = dialog.ok_button_label.clone(); + let f = move |res| { + f(match res { + MessageDialogResult::Ok | MessageDialogResult::Yes => true, + MessageDialogResult::Custom(s) => ok_label.map_or(s == OK, |ok_label| ok_label == s), + _ => false, + }); + }; + run_dialog!(MessageDialog::from(dialog).show(), f); }