@tauri-apps/plugin-dialog 2.3.3 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist-js/index.cjs CHANGED
@@ -5,6 +5,29 @@ var core = require('@tauri-apps/api/core');
5
5
  // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
6
6
  // SPDX-License-Identifier: Apache-2.0
7
7
  // SPDX-License-Identifier: MIT
8
+ /**
9
+ * Internal function to convert the buttons to the Rust type.
10
+ */
11
+ function buttonsToRust(buttons) {
12
+ if (buttons === undefined) {
13
+ return undefined;
14
+ }
15
+ if (typeof buttons === 'string') {
16
+ return buttons;
17
+ }
18
+ else if ('ok' in buttons && 'cancel' in buttons) {
19
+ return { OkCancelCustom: [buttons.ok, buttons.cancel] };
20
+ }
21
+ else if ('yes' in buttons && 'no' in buttons && 'cancel' in buttons) {
22
+ return {
23
+ YesNoCancelCustom: [buttons.yes, buttons.no, buttons.cancel]
24
+ };
25
+ }
26
+ else if ('ok' in buttons) {
27
+ return { OkCustom: buttons.ok };
28
+ }
29
+ return undefined;
30
+ }
8
31
  /**
9
32
  * Open a file/directory selection dialog.
10
33
  *
@@ -112,11 +135,12 @@ async function save(options = {}) {
112
135
  */
113
136
  async function message(message, options) {
114
137
  const opts = typeof options === 'string' ? { title: options } : options;
115
- await core.invoke('plugin:dialog|message', {
138
+ return core.invoke('plugin:dialog|message', {
116
139
  message: message.toString(),
117
140
  title: opts?.title?.toString(),
118
141
  kind: opts?.kind,
119
- okButtonLabel: opts?.okLabel?.toString()
142
+ okButtonLabel: opts?.okLabel?.toString(),
143
+ buttons: buttonsToRust(opts?.buttons)
120
144
  });
121
145
  }
122
146
  /**
@@ -68,6 +68,61 @@ interface SaveDialogOptions {
68
68
  /** Whether to allow creating directories in the dialog. Enabled by default. **macOS Only** */
69
69
  canCreateDirectories?: boolean;
70
70
  }
71
+ /**
72
+ * Default buttons for a message dialog.
73
+ *
74
+ * @since 2.4.0
75
+ */
76
+ export type MessageDialogDefaultButtons = 'Ok' | 'OkCancel' | 'YesNo' | 'YesNoCancel';
77
+ /** All possible button keys. */
78
+ type ButtonKey = 'ok' | 'cancel' | 'yes' | 'no';
79
+ /** Ban everything except a set of keys. */
80
+ type BanExcept<Allowed extends ButtonKey> = Partial<Record<Exclude<ButtonKey, Allowed>, never>>;
81
+ /**
82
+ * The Yes, No and Cancel buttons of a message dialog.
83
+ *
84
+ * @since 2.4.0
85
+ */
86
+ export type MessageDialogButtonsYesNoCancel = {
87
+ /** The Yes button. */
88
+ yes: string;
89
+ /** The No button. */
90
+ no: string;
91
+ /** The Cancel button. */
92
+ cancel: string;
93
+ } & BanExcept<'yes' | 'no' | 'cancel'>;
94
+ /**
95
+ * The Ok and Cancel buttons of a message dialog.
96
+ *
97
+ * @since 2.4.0
98
+ */
99
+ export type MessageDialogButtonsOkCancel = {
100
+ /** The Ok button. */
101
+ ok: string;
102
+ /** The Cancel button. */
103
+ cancel: string;
104
+ } & BanExcept<'ok' | 'cancel'>;
105
+ /**
106
+ * The Ok button of a message dialog.
107
+ *
108
+ * @since 2.4.0
109
+ */
110
+ export type MessageDialogButtonsOk = {
111
+ /** The Ok button. */
112
+ ok: string;
113
+ } & BanExcept<'ok'>;
114
+ /**
115
+ * Custom buttons for a message dialog.
116
+ *
117
+ * @since 2.4.0
118
+ */
119
+ export type MessageDialogCustomButtons = MessageDialogButtonsYesNoCancel | MessageDialogButtonsOkCancel | MessageDialogButtonsOk;
120
+ /**
121
+ * The buttons of a message dialog.
122
+ *
123
+ * @since 2.4.0
124
+ */
125
+ export type MessageDialogButtons = MessageDialogDefaultButtons | MessageDialogCustomButtons;
71
126
  /**
72
127
  * @since 2.0.0
73
128
  */
@@ -76,8 +131,35 @@ interface MessageDialogOptions {
76
131
  title?: string;
77
132
  /** The kind of the dialog. Defaults to `info`. */
78
133
  kind?: 'info' | 'warning' | 'error';
79
- /** The label of the confirm button. */
134
+ /**
135
+ * The label of the Ok button.
136
+ *
137
+ * @deprecated Use {@linkcode MessageDialogOptions.buttons} instead.
138
+ */
80
139
  okLabel?: string;
140
+ /**
141
+ * The buttons of the dialog.
142
+ *
143
+ * @example
144
+ *
145
+ * ```ts
146
+ * // Use system default buttons texts
147
+ * await message('Hello World!', { buttons: 'Ok' })
148
+ * await message('Hello World!', { buttons: 'OkCancel' })
149
+ *
150
+ * // Or with custom button texts
151
+ * await message('Hello World!', { buttons: { ok: 'Yes!' } })
152
+ * await message('Take on the task?', {
153
+ * buttons: { ok: 'Accept', cancel: 'Cancel' }
154
+ * })
155
+ * await message('Show the file content?', {
156
+ * buttons: { yes: 'Show content', no: 'Show in folder', cancel: 'Cancel' }
157
+ * })
158
+ * ```
159
+ *
160
+ * @since 2.4.0
161
+ */
162
+ buttons?: MessageDialogButtons;
81
163
  }
82
164
  interface ConfirmDialogOptions {
83
165
  /** The title of the dialog. Defaults to the app name. */
@@ -168,6 +250,15 @@ declare function open<T extends OpenDialogOptions>(options?: T): Promise<OpenDia
168
250
  * @since 2.0.0
169
251
  */
170
252
  declare function save(options?: SaveDialogOptions): Promise<string | null>;
253
+ /**
254
+ * The result of a message dialog.
255
+ *
256
+ * The result is a string if the dialog has custom buttons,
257
+ * otherwise it is one of the default buttons.
258
+ *
259
+ * @since 2.4.0
260
+ */
261
+ export type MessageDialogResult = 'Yes' | 'No' | 'Ok' | 'Cancel' | (string & {});
171
262
  /**
172
263
  * Shows a message dialog with an `Ok` button.
173
264
  * @example
@@ -185,7 +276,7 @@ declare function save(options?: SaveDialogOptions): Promise<string | null>;
185
276
  * @since 2.0.0
186
277
  *
187
278
  */
188
- declare function message(message: string, options?: string | MessageDialogOptions): Promise<void>;
279
+ declare function message(message: string, options?: string | MessageDialogOptions): Promise<MessageDialogResult>;
189
280
  /**
190
281
  * Shows a question dialog with `Yes` and `No` buttons.
191
282
  * @example
package/dist-js/index.js CHANGED
@@ -3,6 +3,29 @@ import { invoke } from '@tauri-apps/api/core';
3
3
  // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
4
4
  // SPDX-License-Identifier: Apache-2.0
5
5
  // SPDX-License-Identifier: MIT
6
+ /**
7
+ * Internal function to convert the buttons to the Rust type.
8
+ */
9
+ function buttonsToRust(buttons) {
10
+ if (buttons === undefined) {
11
+ return undefined;
12
+ }
13
+ if (typeof buttons === 'string') {
14
+ return buttons;
15
+ }
16
+ else if ('ok' in buttons && 'cancel' in buttons) {
17
+ return { OkCancelCustom: [buttons.ok, buttons.cancel] };
18
+ }
19
+ else if ('yes' in buttons && 'no' in buttons && 'cancel' in buttons) {
20
+ return {
21
+ YesNoCancelCustom: [buttons.yes, buttons.no, buttons.cancel]
22
+ };
23
+ }
24
+ else if ('ok' in buttons) {
25
+ return { OkCustom: buttons.ok };
26
+ }
27
+ return undefined;
28
+ }
6
29
  /**
7
30
  * Open a file/directory selection dialog.
8
31
  *
@@ -110,11 +133,12 @@ async function save(options = {}) {
110
133
  */
111
134
  async function message(message, options) {
112
135
  const opts = typeof options === 'string' ? { title: options } : options;
113
- await invoke('plugin:dialog|message', {
136
+ return invoke('plugin:dialog|message', {
114
137
  message: message.toString(),
115
138
  title: opts?.title?.toString(),
116
139
  kind: opts?.kind,
117
- okButtonLabel: opts?.okLabel?.toString()
140
+ okButtonLabel: opts?.okLabel?.toString(),
141
+ buttons: buttonsToRust(opts?.buttons)
118
142
  });
119
143
  }
120
144
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-dialog",
3
- "version": "2.3.3",
3
+ "version": "2.4.1",
4
4
  "license": "MIT OR Apache-2.0",
5
5
  "authors": [
6
6
  "Tauri Programme within The Commons Conservancy"