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 <amr.bashir2015@gmail.com>
pull/951/head
Tony 1 year ago committed by GitHub
parent dac8b6331c
commit 2e7ef46d8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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.

@ -26,6 +26,7 @@
"dialog:allow-open", "dialog:allow-open",
"dialog:allow-save", "dialog:allow-save",
"dialog:allow-confirm", "dialog:allow-confirm",
"dialog:allow-message",
{ {
"identifier": "shell:allow-execute", "identifier": "shell:allow-execute",
"allow": [ "allow": [

@ -2,6 +2,7 @@
import { writable } from "svelte/store"; import { writable } from "svelte/store";
import { open } from "@tauri-apps/plugin-shell"; import { open } from "@tauri-apps/plugin-shell";
import { getCurrent } from "@tauri-apps/api/window"; 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 * as os from "@tauri-apps/plugin-os";
import Welcome from "./views/Welcome.svelte"; import Welcome from "./views/Welcome.svelte";
@ -35,7 +36,7 @@
}); });
} }
appWindow.onFileDropEvent((event) => { getCurrentWebview().onFileDropEvent((event) => {
onMessage(`File drop: ${JSON.stringify(event.payload)}`); onMessage(`File drop: ${JSON.stringify(event.payload)}`);
}); });

@ -81,8 +81,8 @@ interface SaveDialogOptions {
interface MessageDialogOptions { interface MessageDialogOptions {
/** The title of the dialog. Defaults to the app name. */ /** The title of the dialog. Defaults to the app name. */
title?: string; title?: string;
/** The type of the dialog. Defaults to `info`. */ /** The kind of the dialog. Defaults to `info`. */
type?: "info" | "warning" | "error"; kind?: "info" | "warning" | "error";
/** The label of the confirm button. */ /** The label of the confirm button. */
okLabel?: string; okLabel?: string;
} }
@ -90,8 +90,8 @@ interface MessageDialogOptions {
interface ConfirmDialogOptions { interface ConfirmDialogOptions {
/** The title of the dialog. Defaults to the app name. */ /** The title of the dialog. Defaults to the app name. */
title?: string; title?: string;
/** The type of the dialog. Defaults to `info`. */ /** The kind of the dialog. Defaults to `info`. */
type?: "info" | "warning" | "error"; kind?: "info" | "warning" | "error";
/** The label of the confirm button. */ /** The label of the confirm button. */
okLabel?: string; okLabel?: string;
/** The label of the cancel button. */ /** The label of the cancel button. */
@ -206,7 +206,7 @@ async function save(options: SaveDialogOptions = {}): Promise<string | null> {
* ```typescript * ```typescript
* import { message } from '@tauri-apps/plugin-dialog'; * import { message } from '@tauri-apps/plugin-dialog';
* await message('Tauri is awesome', 'Tauri'); * 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. * @param message The message to show.
@ -225,7 +225,7 @@ async function message(
return invoke("plugin:dialog|message", { return invoke("plugin:dialog|message", {
message: message.toString(), message: message.toString(),
title: opts?.title?.toString(), title: opts?.title?.toString(),
type_: opts?.type, kind: opts?.kind,
okButtonLabel: opts?.okLabel?.toString(), okButtonLabel: opts?.okLabel?.toString(),
}); });
} }
@ -236,7 +236,7 @@ async function message(
* ```typescript * ```typescript
* import { ask } from '@tauri-apps/plugin-dialog'; * import { ask } from '@tauri-apps/plugin-dialog';
* const yes = await ask('Are you sure?', 'Tauri'); * 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. * @param message The message to show.
@ -254,7 +254,7 @@ async function ask(
return invoke("plugin:dialog|ask", { return invoke("plugin:dialog|ask", {
message: message.toString(), message: message.toString(),
title: opts?.title?.toString(), title: opts?.title?.toString(),
type_: opts?.type, kind: opts?.kind,
okButtonLabel: opts?.okLabel?.toString() ?? "Yes", okButtonLabel: opts?.okLabel?.toString() ?? "Yes",
cancelButtonLabel: opts?.cancelLabel?.toString() ?? "No", cancelButtonLabel: opts?.cancelLabel?.toString() ?? "No",
}); });
@ -266,7 +266,7 @@ async function ask(
* ```typescript * ```typescript
* import { confirm } from '@tauri-apps/plugin-dialog'; * import { confirm } from '@tauri-apps/plugin-dialog';
* const confirmed = await confirm('Are you sure?', 'Tauri'); * 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. * @param message The message to show.
@ -284,7 +284,7 @@ async function confirm(
return invoke("plugin:dialog|confirm", { return invoke("plugin:dialog|confirm", {
message: message.toString(), message: message.toString(),
title: opts?.title?.toString(), title: opts?.title?.toString(),
type_: opts?.type, kind: opts?.kind,
okButtonLabel: opts?.okLabel?.toString() ?? "Ok", okButtonLabel: opts?.okLabel?.toString() ?? "Ok",
cancelButtonLabel: opts?.cancelLabel?.toString() ?? "Cancel", cancelButtonLabel: opts?.cancelLabel?.toString() ?? "Cancel",
}); });

@ -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__})}

@ -207,7 +207,7 @@ fn message_dialog<R: Runtime>(
dialog: State<'_, Dialog<R>>, dialog: State<'_, Dialog<R>>,
title: Option<String>, title: Option<String>,
message: String, message: String,
type_: Option<MessageDialogKind>, kind: Option<MessageDialogKind>,
ok_button_label: Option<String>, ok_button_label: Option<String>,
cancel_button_label: Option<String>, cancel_button_label: Option<String>,
) -> bool { ) -> bool {
@ -222,8 +222,8 @@ fn message_dialog<R: Runtime>(
builder = builder.parent(&window); builder = builder.parent(&window);
} }
if let Some(type_) = type_ { if let Some(kind) = kind {
builder = builder.kind(type_); builder = builder.kind(kind);
} }
if let Some(ok) = ok_button_label { if let Some(ok) = ok_button_label {
@ -243,7 +243,7 @@ pub(crate) async fn message<R: Runtime>(
dialog: State<'_, Dialog<R>>, dialog: State<'_, Dialog<R>>,
title: Option<String>, title: Option<String>,
message: String, message: String,
type_: Option<MessageDialogKind>, kind: Option<MessageDialogKind>,
ok_button_label: Option<String>, ok_button_label: Option<String>,
) -> Result<bool> { ) -> Result<bool> {
Ok(message_dialog( Ok(message_dialog(
@ -251,7 +251,7 @@ pub(crate) async fn message<R: Runtime>(
dialog, dialog,
title, title,
message, message,
type_, kind,
ok_button_label, ok_button_label,
None, None,
)) ))
@ -263,7 +263,7 @@ pub(crate) async fn ask<R: Runtime>(
dialog: State<'_, Dialog<R>>, dialog: State<'_, Dialog<R>>,
title: Option<String>, title: Option<String>,
message: String, message: String,
type_: Option<MessageDialogKind>, kind: Option<MessageDialogKind>,
ok_button_label: Option<String>, ok_button_label: Option<String>,
cancel_button_label: Option<String>, cancel_button_label: Option<String>,
) -> Result<bool> { ) -> Result<bool> {
@ -272,7 +272,7 @@ pub(crate) async fn ask<R: Runtime>(
dialog, dialog,
title, title,
message, message,
type_, kind,
Some(ok_button_label.unwrap_or_else(|| "Yes".into())), Some(ok_button_label.unwrap_or_else(|| "Yes".into())),
Some(cancel_button_label.unwrap_or_else(|| "No".into())), Some(cancel_button_label.unwrap_or_else(|| "No".into())),
)) ))
@ -284,7 +284,7 @@ pub(crate) async fn confirm<R: Runtime>(
dialog: State<'_, Dialog<R>>, dialog: State<'_, Dialog<R>>,
title: Option<String>, title: Option<String>,
message: String, message: String,
type_: Option<MessageDialogKind>, kind: Option<MessageDialogKind>,
ok_button_label: Option<String>, ok_button_label: Option<String>,
cancel_button_label: Option<String>, cancel_button_label: Option<String>,
) -> Result<bool> { ) -> Result<bool> {
@ -293,7 +293,7 @@ pub(crate) async fn confirm<R: Runtime>(
dialog, dialog,
title, title,
message, message,
type_, kind,
Some(ok_button_label.unwrap_or_else(|| "Ok".into())), Some(ok_button_label.unwrap_or_else(|| "Ok".into())),
Some(cancel_button_label.unwrap_or_else(|| "Cancel".into())), Some(cancel_button_label.unwrap_or_else(|| "Cancel".into())),
)) ))

Loading…
Cancel
Save