From 2e7ef46d8b557abb11e1758340835a213f8578d7 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Wed, 14 Feb 2024 07:05:54 +0800 Subject: [PATCH] refactor!(dialog): change `type` field in JS to `kind` (#945) * Fix dialog type not working * gen files * type -> kind * update API example --------- Co-authored-by: amrbashir --- .changes/dialog-type_-to-kind.md | 6 ++++++ examples/api/src-tauri/capabilities/base.json | 1 + examples/api/src/App.svelte | 3 ++- plugins/dialog/guest-js/index.ts | 20 +++++++++---------- plugins/dialog/src/api-iife.js | 2 +- plugins/dialog/src/commands.rs | 18 ++++++++--------- 6 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 .changes/dialog-type_-to-kind.md diff --git a/.changes/dialog-type_-to-kind.md b/.changes/dialog-type_-to-kind.md new file mode 100644 index 00000000..e30387e2 --- /dev/null +++ b/.changes/dialog-type_-to-kind.md @@ -0,0 +1,6 @@ +--- +"dialog": "patch" +"dialog-js": "patch" +--- + +**Breaking change** Renamed `MessageDialogOptions.type` and `ConfigrmDialogOptions.type` fields to `kind` to match the Rust API. diff --git a/examples/api/src-tauri/capabilities/base.json b/examples/api/src-tauri/capabilities/base.json index 82ca54cd..559e8c81 100644 --- a/examples/api/src-tauri/capabilities/base.json +++ b/examples/api/src-tauri/capabilities/base.json @@ -26,6 +26,7 @@ "dialog:allow-open", "dialog:allow-save", "dialog:allow-confirm", + "dialog:allow-message", { "identifier": "shell:allow-execute", "allow": [ diff --git a/examples/api/src/App.svelte b/examples/api/src/App.svelte index 151b692b..117afa22 100644 --- a/examples/api/src/App.svelte +++ b/examples/api/src/App.svelte @@ -2,6 +2,7 @@ import { writable } from "svelte/store"; import { open } from "@tauri-apps/plugin-shell"; import { getCurrent } from "@tauri-apps/api/window"; + import { getCurrent as getCurrentWebview } from "@tauri-apps/api/webview"; import * as os from "@tauri-apps/plugin-os"; import Welcome from "./views/Welcome.svelte"; @@ -35,7 +36,7 @@ }); } - appWindow.onFileDropEvent((event) => { + getCurrentWebview().onFileDropEvent((event) => { onMessage(`File drop: ${JSON.stringify(event.payload)}`); }); diff --git a/plugins/dialog/guest-js/index.ts b/plugins/dialog/guest-js/index.ts index 1f1b6135..28dc2529 100644 --- a/plugins/dialog/guest-js/index.ts +++ b/plugins/dialog/guest-js/index.ts @@ -81,8 +81,8 @@ interface SaveDialogOptions { interface MessageDialogOptions { /** The title of the dialog. Defaults to the app name. */ title?: string; - /** The type of the dialog. Defaults to `info`. */ - type?: "info" | "warning" | "error"; + /** The kind of the dialog. Defaults to `info`. */ + kind?: "info" | "warning" | "error"; /** The label of the confirm button. */ okLabel?: string; } @@ -90,8 +90,8 @@ interface MessageDialogOptions { interface ConfirmDialogOptions { /** The title of the dialog. Defaults to the app name. */ title?: string; - /** The type of the dialog. Defaults to `info`. */ - type?: "info" | "warning" | "error"; + /** The kind of the dialog. Defaults to `info`. */ + kind?: "info" | "warning" | "error"; /** The label of the confirm button. */ okLabel?: string; /** The label of the cancel button. */ @@ -206,7 +206,7 @@ async function save(options: SaveDialogOptions = {}): Promise { * ```typescript * import { message } from '@tauri-apps/plugin-dialog'; * await message('Tauri is awesome', 'Tauri'); - * await message('File not found', { title: 'Tauri', type: 'error' }); + * await message('File not found', { title: 'Tauri', kind: 'error' }); * ``` * * @param message The message to show. @@ -225,7 +225,7 @@ async function message( return invoke("plugin:dialog|message", { message: message.toString(), title: opts?.title?.toString(), - type_: opts?.type, + kind: opts?.kind, okButtonLabel: opts?.okLabel?.toString(), }); } @@ -236,7 +236,7 @@ async function message( * ```typescript * import { ask } from '@tauri-apps/plugin-dialog'; * const yes = await ask('Are you sure?', 'Tauri'); - * const yes2 = await ask('This action cannot be reverted. Are you sure?', { title: 'Tauri', type: 'warning' }); + * const yes2 = await ask('This action cannot be reverted. Are you sure?', { title: 'Tauri', kind: 'warning' }); * ``` * * @param message The message to show. @@ -254,7 +254,7 @@ async function ask( return invoke("plugin:dialog|ask", { message: message.toString(), title: opts?.title?.toString(), - type_: opts?.type, + kind: opts?.kind, okButtonLabel: opts?.okLabel?.toString() ?? "Yes", cancelButtonLabel: opts?.cancelLabel?.toString() ?? "No", }); @@ -266,7 +266,7 @@ async function ask( * ```typescript * import { confirm } from '@tauri-apps/plugin-dialog'; * const confirmed = await confirm('Are you sure?', 'Tauri'); - * const confirmed2 = await confirm('This action cannot be reverted. Are you sure?', { title: 'Tauri', type: 'warning' }); + * const confirmed2 = await confirm('This action cannot be reverted. Are you sure?', { title: 'Tauri', kind: 'warning' }); * ``` * * @param message The message to show. @@ -284,7 +284,7 @@ async function confirm( return invoke("plugin:dialog|confirm", { message: message.toString(), title: opts?.title?.toString(), - type_: opts?.type, + kind: opts?.kind, okButtonLabel: opts?.okLabel?.toString() ?? "Ok", cancelButtonLabel: opts?.cancelLabel?.toString() ?? "Cancel", }); diff --git a/plugins/dialog/src/api-iife.js b/plugins/dialog/src/api-iife.js index 6b3883da..7aa97582 100644 --- a/plugins/dialog/src/api-iife.js +++ b/plugins/dialog/src/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_DIALOG__=function(t){"use strict";async function e(t,e={},n){return window.__TAURI_INTERNALS__.invoke(t,e,n)}return"function"==typeof SuppressedError&&SuppressedError,t.ask=async function(t,n){const o="string"==typeof n?{title:n}:n;return e("plugin:dialog|ask",{message:t.toString(),title:o?.title?.toString(),type_:o?.type,okButtonLabel:o?.okLabel?.toString()??"Yes",cancelButtonLabel:o?.cancelLabel?.toString()??"No"})},t.confirm=async function(t,n){const o="string"==typeof n?{title:n}:n;return e("plugin:dialog|confirm",{message:t.toString(),title:o?.title?.toString(),type_:o?.type,okButtonLabel:o?.okLabel?.toString()??"Ok",cancelButtonLabel:o?.cancelLabel?.toString()??"Cancel"})},t.message=async function(t,n){const o="string"==typeof n?{title:n}:n;return e("plugin:dialog|message",{message:t.toString(),title:o?.title?.toString(),type_:o?.type,okButtonLabel:o?.okLabel?.toString()})},t.open=async function(t={}){return"object"==typeof t&&Object.freeze(t),e("plugin:dialog|open",{options:t})},t.save=async function(t={}){return"object"==typeof t&&Object.freeze(t),e("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 o="string"==typeof e?{title:e}:e;return n("plugin:dialog|ask",{message:t.toString(),title:o?.title?.toString(),kind:o?.kind,okButtonLabel:o?.okLabel?.toString()??"Yes",cancelButtonLabel:o?.cancelLabel?.toString()??"No"})},t.confirm=async function(t,e){const o="string"==typeof e?{title:e}:e;return n("plugin:dialog|confirm",{message:t.toString(),title:o?.title?.toString(),kind:o?.kind,okButtonLabel:o?.okLabel?.toString()??"Ok",cancelButtonLabel:o?.cancelLabel?.toString()??"Cancel"})},t.message=async function(t,e){const o="string"==typeof e?{title:e}:e;return n("plugin:dialog|message",{message:t.toString(),title:o?.title?.toString(),kind:o?.kind,okButtonLabel:o?.okLabel?.toString()})},t.open=async function(t={}){return"object"==typeof t&&Object.freeze(t),n("plugin:dialog|open",{options:t})},t.save=async function(t={}){return"object"==typeof t&&Object.freeze(t),n("plugin:dialog|save",{options:t})},t}({});Object.defineProperty(window.__TAURI__,"dialog",{value:__TAURI_PLUGIN_DIALOG__})} diff --git a/plugins/dialog/src/commands.rs b/plugins/dialog/src/commands.rs index 02c80f0d..1a525df0 100644 --- a/plugins/dialog/src/commands.rs +++ b/plugins/dialog/src/commands.rs @@ -207,7 +207,7 @@ fn message_dialog( dialog: State<'_, Dialog>, title: Option, message: String, - type_: Option, + kind: Option, ok_button_label: Option, cancel_button_label: Option, ) -> bool { @@ -222,8 +222,8 @@ fn message_dialog( builder = builder.parent(&window); } - if let Some(type_) = type_ { - builder = builder.kind(type_); + if let Some(kind) = kind { + builder = builder.kind(kind); } if let Some(ok) = ok_button_label { @@ -243,7 +243,7 @@ pub(crate) async fn message( dialog: State<'_, Dialog>, title: Option, message: String, - type_: Option, + kind: Option, ok_button_label: Option, ) -> Result { Ok(message_dialog( @@ -251,7 +251,7 @@ pub(crate) async fn message( dialog, title, message, - type_, + kind, ok_button_label, None, )) @@ -263,7 +263,7 @@ pub(crate) async fn ask( dialog: State<'_, Dialog>, title: Option, message: String, - type_: Option, + kind: Option, ok_button_label: Option, cancel_button_label: Option, ) -> Result { @@ -272,7 +272,7 @@ pub(crate) async fn ask( dialog, title, message, - type_, + kind, Some(ok_button_label.unwrap_or_else(|| "Yes".into())), Some(cancel_button_label.unwrap_or_else(|| "No".into())), )) @@ -284,7 +284,7 @@ pub(crate) async fn confirm( dialog: State<'_, Dialog>, title: Option, message: String, - type_: Option, + kind: Option, ok_button_label: Option, cancel_button_label: Option, ) -> Result { @@ -293,7 +293,7 @@ pub(crate) async fn confirm( dialog, title, message, - type_, + kind, Some(ok_button_label.unwrap_or_else(|| "Ok".into())), Some(cancel_button_label.unwrap_or_else(|| "Cancel".into())), ))