From 2302c2db1c49673e61dcbda8cdb01b2c57e9ba6f Mon Sep 17 00:00:00 2001
From: Tony <68118705+Legend-Master@users.noreply.github.com>
Date: Sun, 20 Oct 2024 19:48:45 +0800
Subject: [PATCH] fix(dialog): `ask` and `confirm` not using system button
 texts (#1910)

* Fix `ask`'s button texts being ok and cancel

* Update change file
---
 .changes/dialog-native-button-texts.md |  6 ++++
 plugins/dialog/api-iife.js             |  2 +-
 plugins/dialog/guest-js/index.ts       |  8 ++---
 plugins/dialog/src/commands.rs         | 44 ++++++++++++++------------
 plugins/dialog/src/desktop.rs          |  1 +
 plugins/dialog/src/lib.rs              |  3 ++
 plugins/dialog/src/models.rs           |  2 ++
 7 files changed, 40 insertions(+), 26 deletions(-)
 create mode 100644 .changes/dialog-native-button-texts.md

diff --git a/.changes/dialog-native-button-texts.md b/.changes/dialog-native-button-texts.md
new file mode 100644
index 000000000..f8e055bcb
--- /dev/null
+++ b/.changes/dialog-native-button-texts.md
@@ -0,0 +1,6 @@
+---
+"dialog": "patch"
+"dialog-js": "patch"
+---
+
+Fix `ask` and `confirm` not using system button texts
diff --git a/plugins/dialog/api-iife.js b/plugins/dialog/api-iife.js
index ee6045707..c2e0870c8 100644
--- a/plugins/dialog/api-iife.js
+++ b/plugins/dialog/api-iife.js
@@ -1 +1 @@
-if("__TAURI__"in window){var __TAURI_PLUGIN_DIALOG__=function(t){"use strict";async function n(t,n={},e){return window.__TAURI_INTERNALS__.invoke(t,n,e)}return"function"==typeof SuppressedError&&SuppressedError,t.ask=async function(t,e){const i="string"==typeof e?{title:e}:e;return await n("plugin:dialog|ask",{message:t.toString(),title:i?.title?.toString(),kind:i?.kind,okButtonLabel:i?.okLabel?.toString()??"Yes",cancelButtonLabel:i?.cancelLabel?.toString()??"No"})},t.confirm=async function(t,e){const i="string"==typeof e?{title:e}:e;return await n("plugin:dialog|confirm",{message:t.toString(),title:i?.title?.toString(),kind:i?.kind,okButtonLabel:i?.okLabel?.toString()??"Ok",cancelButtonLabel:i?.cancelLabel?.toString()??"Cancel"})},t.message=async function(t,e){const i="string"==typeof e?{title:e}:e;await n("plugin:dialog|message",{message:t.toString(),title:i?.title?.toString(),kind:i?.kind,okButtonLabel:i?.okLabel?.toString()})},t.open=async function(t={}){return"object"==typeof t&&Object.freeze(t),await n("plugin:dialog|open",{options:t})},t.save=async function(t={}){return"object"==typeof t&&Object.freeze(t),await n("plugin:dialog|save",{options:t})},t}({});Object.defineProperty(window.__TAURI__,"dialog",{value:__TAURI_PLUGIN_DIALOG__})}
+if("__TAURI__"in window){var __TAURI_PLUGIN_DIALOG__=function(t){"use strict";async function n(t,n={},e){return window.__TAURI_INTERNALS__.invoke(t,n,e)}return"function"==typeof SuppressedError&&SuppressedError,t.ask=async function(t,e){const i="string"==typeof e?{title:e}:e;return await n("plugin:dialog|ask",{message:t.toString(),title:i?.title?.toString(),kind:i?.kind,yesButtonLabel:i?.okLabel?.toString(),noButtonLabel:i?.cancelLabel?.toString()})},t.confirm=async function(t,e){const i="string"==typeof e?{title:e}:e;return await n("plugin:dialog|confirm",{message:t.toString(),title:i?.title?.toString(),kind:i?.kind,okButtonLabel:i?.okLabel?.toString(),cancelButtonLabel:i?.cancelLabel?.toString()})},t.message=async function(t,e){const i="string"==typeof e?{title:e}:e;await n("plugin:dialog|message",{message:t.toString(),title:i?.title?.toString(),kind:i?.kind,okButtonLabel:i?.okLabel?.toString()})},t.open=async function(t={}){return"object"==typeof t&&Object.freeze(t),await n("plugin:dialog|open",{options:t})},t.save=async function(t={}){return"object"==typeof t&&Object.freeze(t),await n("plugin:dialog|save",{options:t})},t}({});Object.defineProperty(window.__TAURI__,"dialog",{value:__TAURI_PLUGIN_DIALOG__})}
diff --git a/plugins/dialog/guest-js/index.ts b/plugins/dialog/guest-js/index.ts
index a6301ebee..150be95a0 100644
--- a/plugins/dialog/guest-js/index.ts
+++ b/plugins/dialog/guest-js/index.ts
@@ -257,8 +257,8 @@ async function ask(
     message: message.toString(),
     title: opts?.title?.toString(),
     kind: opts?.kind,
-    okButtonLabel: opts?.okLabel?.toString() ?? 'Yes',
-    cancelButtonLabel: opts?.cancelLabel?.toString() ?? 'No'
+    yesButtonLabel: opts?.okLabel?.toString(),
+    noButtonLabel: opts?.cancelLabel?.toString()
   })
 }
 
@@ -287,8 +287,8 @@ async function confirm(
     message: message.toString(),
     title: opts?.title?.toString(),
     kind: opts?.kind,
-    okButtonLabel: opts?.okLabel?.toString() ?? 'Ok',
-    cancelButtonLabel: opts?.cancelLabel?.toString() ?? 'Cancel'
+    okButtonLabel: opts?.okLabel?.toString(),
+    cancelButtonLabel: opts?.cancelLabel?.toString()
   })
 }
 
diff --git a/plugins/dialog/src/commands.rs b/plugins/dialog/src/commands.rs
index 8690a8b02..4129b7b6a 100644
--- a/plugins/dialog/src/commands.rs
+++ b/plugins/dialog/src/commands.rs
@@ -10,7 +10,7 @@ use tauri_plugin_fs::FsExt;
 
 use crate::{
     Dialog, FileDialogBuilder, FilePath, MessageDialogButtons, MessageDialogKind, Result, CANCEL,
-    OK,
+    NO, OK, YES,
 };
 
 #[derive(Serialize)]
@@ -299,8 +299,8 @@ pub(crate) async fn ask<R: Runtime>(
     title: Option<String>,
     message: String,
     kind: Option<MessageDialogKind>,
-    ok_button_label: Option<String>,
-    cancel_button_label: Option<String>,
+    yes_button_label: Option<String>,
+    no_button_label: Option<String>,
 ) -> Result<bool> {
     Ok(message_dialog(
         window,
@@ -308,7 +308,16 @@ pub(crate) async fn ask<R: Runtime>(
         title,
         message,
         kind,
-        get_ok_cancel_type(ok_button_label, cancel_button_label),
+        if let Some(yes_button_label) = yes_button_label {
+            MessageDialogButtons::OkCancelCustom(
+                yes_button_label,
+                no_button_label.unwrap_or(NO.to_string()),
+            )
+        } else if let Some(no_button_label) = no_button_label {
+            MessageDialogButtons::OkCancelCustom(YES.to_string(), no_button_label)
+        } else {
+            MessageDialogButtons::YesNo
+        },
     ))
 }
 
@@ -328,22 +337,15 @@ pub(crate) async fn confirm<R: Runtime>(
         title,
         message,
         kind,
-        get_ok_cancel_type(ok_button_label, cancel_button_label),
+        if let Some(ok_button_label) = ok_button_label {
+            MessageDialogButtons::OkCancelCustom(
+                ok_button_label,
+                cancel_button_label.unwrap_or(CANCEL.to_string()),
+            )
+        } else if let Some(cancel_button_label) = cancel_button_label {
+            MessageDialogButtons::OkCancelCustom(OK.to_string(), cancel_button_label)
+        } else {
+            MessageDialogButtons::OkCancel
+        },
     ))
 }
-
-fn get_ok_cancel_type(
-    ok_button_label: Option<String>,
-    cancel_button_label: Option<String>,
-) -> MessageDialogButtons {
-    if let Some(ok_button_label) = ok_button_label {
-        MessageDialogButtons::OkCancelCustom(
-            ok_button_label,
-            cancel_button_label.unwrap_or(CANCEL.to_string()),
-        )
-    } else if let Some(cancel_button_label) = cancel_button_label {
-        MessageDialogButtons::OkCancelCustom(OK.to_string(), cancel_button_label)
-    } else {
-        MessageDialogButtons::OkCancel
-    }
-}
diff --git a/plugins/dialog/src/desktop.rs b/plugins/dialog/src/desktop.rs
index d30f6bfee..d1a3e8b21 100644
--- a/plugins/dialog/src/desktop.rs
+++ b/plugins/dialog/src/desktop.rs
@@ -112,6 +112,7 @@ impl From<MessageDialogButtons> for rfd::MessageButtons {
         match value {
             MessageDialogButtons::Ok => Self::Ok,
             MessageDialogButtons::OkCancel => Self::OkCancel,
+            MessageDialogButtons::YesNo => Self::YesNo,
             MessageDialogButtons::OkCustom(ok) => Self::OkCustom(ok),
             MessageDialogButtons::OkCancelCustom(ok, cancel) => Self::OkCancelCustom(ok, cancel),
         }
diff --git a/plugins/dialog/src/lib.rs b/plugins/dialog/src/lib.rs
index a7538e1b1..3d7464d90 100644
--- a/plugins/dialog/src/lib.rs
+++ b/plugins/dialog/src/lib.rs
@@ -43,6 +43,8 @@ use mobile::*;
 
 pub(crate) const OK: &str = "Ok";
 pub(crate) const CANCEL: &str = "Cancel";
+pub(crate) const YES: &str = "Yes";
+pub(crate) const NO: &str = "No";
 
 macro_rules! blocking_fn {
     ($self:ident, $fn:ident) => {{
@@ -236,6 +238,7 @@ impl<R: Runtime> MessageDialogBuilder<R> {
         let (ok_button_label, cancel_button_label) = match &self.buttons {
             MessageDialogButtons::Ok => (Some(OK), None),
             MessageDialogButtons::OkCancel => (Some(OK), Some(CANCEL)),
+            MessageDialogButtons::YesNo => (Some(YES), Some(NO)),
             MessageDialogButtons::OkCustom(ok) => (Some(ok.as_str()), Some(CANCEL)),
             MessageDialogButtons::OkCancelCustom(ok, cancel) => {
                 (Some(ok.as_str()), Some(cancel.as_str()))
diff --git a/plugins/dialog/src/models.rs b/plugins/dialog/src/models.rs
index 3f9eb6c13..d6452bce7 100644
--- a/plugins/dialog/src/models.rs
+++ b/plugins/dialog/src/models.rs
@@ -59,6 +59,8 @@ pub enum MessageDialogButtons {
     Ok,
     /// 2 buttons `Ok` and `Cancel` with OS default dialog texts
     OkCancel,
+    /// 2 buttons `Yes` and `No` with OS default dialog texts
+    YesNo,
     /// A single `Ok` button with custom text
     OkCustom(String),
     /// 2 buttons `Ok` and `Cancel` with custom texts