Skip to content

Commit

Permalink
fix(dialog): fix message dialog return with custom buttons, fixes #711 (
Browse files Browse the repository at this point in the history
#769)

* fix confirm

* remove unnecessary lines

* fix

* change file
  • Loading branch information
pashokitsme authored Nov 27, 2023
1 parent f49b391 commit 2e2c0a1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changes/dialog-return-result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"dialog": patch
"dialog-js": patch
---

Fix incorrect result for dialog messages.
39 changes: 16 additions & 23 deletions plugins/dialog/src/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,8 @@ impl<R: Runtime> Dialog<R> {

#[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)));
}};
}

Expand All @@ -69,24 +63,15 @@ 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));
});
}};
}

#[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)));
}};
}

Expand All @@ -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));
});
}};
}
Expand Down Expand Up @@ -226,5 +208,16 @@ pub fn show_message_dialog<R: Runtime, F: FnOnce(bool) + Send + 'static>(
dialog: MessageDialogBuilder<R>,
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);
}

0 comments on commit 2e2c0a1

Please sign in to comment.