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( title: Option, message: String, kind: Option, - ok_button_label: Option, - cancel_button_label: Option, + yes_button_label: Option, + no_button_label: Option, ) -> Result { Ok(message_dialog( window, @@ -308,7 +308,16 @@ pub(crate) async fn ask( 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( 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, - cancel_button_label: Option, -) -> 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 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 MessageDialogBuilder { 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