From 8d427637b329cc857d5951c537d51d4cfa69f1d9 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 17 Apr 2025 06:10:59 +0200 Subject: [PATCH] untagged & YesNoCancel --- package.json | 3 ++- plugins/dialog/api-iife.js | 2 +- plugins/dialog/guest-js/index.ts | 26 ++++++-------------------- plugins/dialog/src/desktop.rs | 5 ++++- plugins/dialog/src/lib.rs | 1 + plugins/dialog/src/models.rs | 3 +++ 6 files changed, 17 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 3da1fdff..795361c5 100644 --- a/package.json +++ b/package.json @@ -35,5 +35,6 @@ }, "engines": { "pnpm": "^10.0.0" - } + }, + "packageManager": "pnpm@10.6.5+sha512.cdf928fca20832cd59ec53826492b7dc25dc524d4370b6b4adbf65803d32efaa6c1c88147c0ae4e8d579a6c9eec715757b50d4fa35eea179d868eada4ed043af" } diff --git a/plugins/dialog/api-iife.js b/plugins/dialog/api-iife.js index 16506512..a357f2c0 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)}function e(t){if(void 0!==t)return"string"==typeof t?t:"ok"in t&&"cancel"in t?{OkCancelCustom:[t.ok,t.cancel]}:"yes"in t&&"no"in t&&"cancel"in t?{YesNoCancelCustom:[t.yes,t.no,t.cancel]}:"ok"in t?{OkCustom:t.ok}:void 0}return"function"==typeof SuppressedError&&SuppressedError,t.ask=async function(t,e){const o="string"==typeof e?{title:e}:e;return await n("plugin:dialog|ask",{message:t.toString(),title:o?.title?.toString(),kind:o?.kind,yesButtonLabel:o?.okLabel?.toString(),noButtonLabel:o?.cancelLabel?.toString()})},t.confirm=async function(t,e){const o="string"==typeof e?{title:e}:e;return await n("plugin:dialog|confirm",{message:t.toString(),title:o?.title?.toString(),kind:o?.kind,okButtonLabel:o?.okLabel?.toString(),cancelButtonLabel:o?.cancelLabel?.toString()})},t.message=async function(t,o){const i="string"==typeof o?{title:o}:o;return function(t){return"string"==typeof t?t:t.Custom}(await n("plugin:dialog|message",{message:t.toString(),title:i?.title?.toString(),kind:i?.kind,okButtonLabel:i?.okLabel?.toString(),buttons:e(i?.buttons)}))},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)}function e(t){if(void 0!==t)return"string"==typeof t?t:"ok"in t&&"cancel"in t?{OkCancelCustom:[t.ok,t.cancel]}:"yes"in t&&"no"in t&&"cancel"in t?{YesNoCancelCustom:[t.yes,t.no,t.cancel]}:"ok"in t?{OkCustom:t.ok}:void 0}return"function"==typeof SuppressedError&&SuppressedError,t.ask=async function(t,e){const o="string"==typeof e?{title:e}:e;return await n("plugin:dialog|ask",{message:t.toString(),title:o?.title?.toString(),kind:o?.kind,yesButtonLabel:o?.okLabel?.toString(),noButtonLabel:o?.cancelLabel?.toString()})},t.confirm=async function(t,e){const o="string"==typeof e?{title:e}:e;return await n("plugin:dialog|confirm",{message:t.toString(),title:o?.title?.toString(),kind:o?.kind,okButtonLabel:o?.okLabel?.toString(),cancelButtonLabel:o?.cancelLabel?.toString()})},t.message=async function(t,o){const i="string"==typeof o?{title:o}:o;return n("plugin:dialog|message",{message:t.toString(),title:i?.title?.toString(),kind:i?.kind,okButtonLabel:i?.okLabel?.toString(),buttons:e(i?.buttons)})},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 ff6cb803..9d5d9ed0 100644 --- a/plugins/dialog/guest-js/index.ts +++ b/plugins/dialog/guest-js/index.ts @@ -82,7 +82,11 @@ interface SaveDialogOptions { * * @since 2.3.0 */ -export type MessageDialogDefaultButtons = 'Ok' | 'OkCancel' | 'YesNo' +export type MessageDialogDefaultButtons = + | 'Ok' + | 'OkCancel' + | 'YesNo' + | 'YesNoCancel' /** * The Yes, No and Cancel buttons of a message dialog. @@ -307,22 +311,6 @@ async function save(options: SaveDialogOptions = {}): Promise { */ export type MessageDialogResult = 'Yes' | 'No' | 'Ok' | 'Cancel' | (string & {}) -type MessageDialogResultRust = - | 'Yes' - | 'No' - | 'Ok' - | 'Cancel' - | { Custom: string } - -/** Internal function to convert the result to JS. */ -function resultToJS(res: MessageDialogResultRust): MessageDialogResult { - if (typeof res === 'string') { - return res - } else { - return res.Custom - } -} - /** * Shows a message dialog with an `Ok` button. * @example @@ -346,15 +334,13 @@ async function message( ): Promise { const opts = typeof options === 'string' ? { title: options } : options - const res = await invoke('plugin:dialog|message', { + return invoke('plugin:dialog|message', { message: message.toString(), title: opts?.title?.toString(), kind: opts?.kind, okButtonLabel: opts?.okLabel?.toString(), buttons: buttonsToRust(opts?.buttons) }) - - return resultToJS(res) } /** diff --git a/plugins/dialog/src/desktop.rs b/plugins/dialog/src/desktop.rs index 013ff069..6066cef8 100644 --- a/plugins/dialog/src/desktop.rs +++ b/plugins/dialog/src/desktop.rs @@ -13,7 +13,7 @@ use rfd::{AsyncFileDialog, AsyncMessageDialog}; use serde::de::DeserializeOwned; use tauri::{plugin::PluginApi, AppHandle, Runtime}; -use crate::{models::*, FileDialogBuilder, FilePath, MessageDialogBuilder}; +use crate::{models::*, FileDialogBuilder, FilePath, MessageDialogBuilder, CANCEL, NO, YES}; pub fn init( app: &AppHandle, @@ -115,6 +115,9 @@ impl From for rfd::MessageButtons { MessageDialogButtons::YesNo => Self::YesNo, MessageDialogButtons::OkCustom(ok) => Self::OkCustom(ok), MessageDialogButtons::OkCancelCustom(ok, cancel) => Self::OkCancelCustom(ok, cancel), + MessageDialogButtons::YesNoCancel => { + Self::YesNoCancelCustom(YES.into(), NO.into(), CANCEL.into()) + } MessageDialogButtons::YesNoCancelCustom(yes, no, cancel) => { Self::YesNoCancelCustom(yes, no, cancel) } diff --git a/plugins/dialog/src/lib.rs b/plugins/dialog/src/lib.rs index cd7832a6..17d9a829 100644 --- a/plugins/dialog/src/lib.rs +++ b/plugins/dialog/src/lib.rs @@ -243,6 +243,7 @@ impl MessageDialogBuilder { MessageDialogButtons::Ok => (Some(OK), None, None), MessageDialogButtons::OkCancel => (Some(OK), None, Some(CANCEL)), MessageDialogButtons::YesNo => (Some(YES), Some(NO), None), + MessageDialogButtons::YesNoCancel => (Some(YES), Some(NO), Some(CANCEL)), MessageDialogButtons::OkCustom(ok) => (Some(ok.as_str()), None, None), MessageDialogButtons::OkCancelCustom(ok, cancel) => { (Some(ok.as_str()), None, Some(cancel.as_str())) diff --git a/plugins/dialog/src/models.rs b/plugins/dialog/src/models.rs index ea583f7a..6e39bbb9 100644 --- a/plugins/dialog/src/models.rs +++ b/plugins/dialog/src/models.rs @@ -61,6 +61,8 @@ pub enum MessageDialogButtons { OkCancel, /// 2 buttons `Yes` and `No` with OS default dialog texts YesNo, + /// 3 buttons `Yes`, `No` and `Cancel` with OS default dialog texts + YesNoCancel, /// A single `Ok` button with custom text OkCustom(String), /// 2 buttons `Ok` and `Cancel` with custom texts @@ -71,6 +73,7 @@ pub enum MessageDialogButtons { /// Result of a message dialog #[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)] +#[serde(untagged)] pub enum MessageDialogResult { Yes, No,