Skip to content

Commit 2415447

Browse files
refactor(dialog): reuse message command for confirm (#3287)
* chore(dialog): reuse `message` command for confirm * Add change file * Remove ask and confirm from default permissions * Format * Remove extra `toString` * Point `allow-confirm` to `allow-message`
1 parent 2574ec8 commit 2415447

14 files changed

Lines changed: 111 additions & 166 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"dialog": minor
3+
"dialog-js": minor
4+
---
5+
6+
Re-use `message` command in Rust side for `ask` and `confirm` commands, `allow-ask` and `allow-confirm` permissions are now aliases to `allow-message`

examples/api/src-tauri/capabilities/base.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323
"core:window:allow-start-dragging",
2424
"notification:default",
2525
"os:allow-platform",
26-
"dialog:allow-open",
27-
"dialog:allow-ask",
28-
"dialog:allow-save",
29-
"dialog:allow-confirm",
30-
"dialog:allow-message",
26+
"dialog:default",
3127
{
3228
"identifier": "shell:allow-spawn",
3329
"allow": [

plugins/dialog/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/dialog/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
const COMMANDS: &[&str] = &["open", "save", "message", "ask", "confirm"];
5+
const COMMANDS: &[&str] = &["open", "save", "message"];
66

77
fn main() {
88
let result = tauri_plugin::Builder::new(COMMANDS)

plugins/dialog/guest-js/index.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,16 @@ async function save(options: SaveDialogOptions = {}): Promise<string | null> {
405405
*/
406406
export type MessageDialogResult = 'Yes' | 'No' | 'Ok' | 'Cancel' | (string & {})
407407

408+
async function messageCommand(message: string, options?: MessageDialogOptions) {
409+
return await invoke<MessageDialogResult>('plugin:dialog|message', {
410+
message,
411+
title: options?.title,
412+
kind: options?.kind,
413+
okButtonLabel: options?.okLabel,
414+
buttons: buttonsToRust(options?.buttons)
415+
})
416+
}
417+
408418
/**
409419
* Shows a message dialog with an `Ok` button.
410420
* @example
@@ -427,18 +437,14 @@ async function message(
427437
options?: string | MessageDialogOptions
428438
): Promise<MessageDialogResult> {
429439
const opts = typeof options === 'string' ? { title: options } : options
430-
431-
return invoke<MessageDialogResult>('plugin:dialog|message', {
432-
message: message.toString(),
433-
title: opts?.title?.toString(),
434-
kind: opts?.kind,
435-
okButtonLabel: opts?.okLabel?.toString(),
436-
buttons: buttonsToRust(opts?.buttons)
437-
})
440+
return messageCommand(message, opts)
438441
}
439442

440443
/**
441444
* Shows a question dialog with `Yes` and `No` buttons.
445+
*
446+
* Convenient wrapper for `await message('msg', { buttons: 'YesNo' }) === 'Yes'`
447+
*
442448
* @example
443449
* ```typescript
444450
* import { ask } from '@tauri-apps/plugin-dialog';
@@ -458,17 +464,24 @@ async function ask(
458464
options?: string | ConfirmDialogOptions
459465
): Promise<boolean> {
460466
const opts = typeof options === 'string' ? { title: options } : options
461-
return await invoke('plugin:dialog|ask', {
462-
message: message.toString(),
463-
title: opts?.title?.toString(),
464-
kind: opts?.kind,
465-
yesButtonLabel: opts?.okLabel?.toString(),
466-
noButtonLabel: opts?.cancelLabel?.toString()
467-
})
467+
const customButtons = opts?.okLabel || opts?.cancelLabel
468+
const okLabel = opts?.okLabel ?? 'Yes'
469+
return (
470+
(await messageCommand(message, {
471+
title: opts?.title,
472+
kind: opts?.kind,
473+
buttons: customButtons
474+
? { ok: okLabel, cancel: opts.cancelLabel ?? 'No' }
475+
: 'YesNo'
476+
})) === okLabel
477+
)
468478
}
469479

470480
/**
471481
* Shows a question dialog with `Ok` and `Cancel` buttons.
482+
*
483+
* Convenient wrapper for `await message('msg', { buttons: 'OkCancel' }) === 'Ok'`
484+
*
472485
* @example
473486
* ```typescript
474487
* import { confirm } from '@tauri-apps/plugin-dialog';
@@ -488,13 +501,17 @@ async function confirm(
488501
options?: string | ConfirmDialogOptions
489502
): Promise<boolean> {
490503
const opts = typeof options === 'string' ? { title: options } : options
491-
return await invoke('plugin:dialog|confirm', {
492-
message: message.toString(),
493-
title: opts?.title?.toString(),
494-
kind: opts?.kind,
495-
okButtonLabel: opts?.okLabel?.toString(),
496-
cancelButtonLabel: opts?.cancelLabel?.toString()
497-
})
504+
const customButtons = opts?.okLabel || opts?.cancelLabel
505+
const okLabel = opts?.okLabel ?? 'Ok'
506+
return (
507+
(await messageCommand(message, {
508+
title: opts?.title,
509+
kind: opts?.kind,
510+
buttons: customButtons
511+
? { ok: okLabel, cancel: opts.cancelLabel ?? 'Cancel' }
512+
: 'OkCancel'
513+
})) === okLabel
514+
)
498515
}
499516

500517
export type {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"$schema" = "schemas/schema.json"
2+
3+
[[permission]]
4+
identifier = "allow-ask"
5+
description = "Enables the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)"
6+
commands.allow = ["message"]
7+
8+
[[permission]]
9+
identifier = "deny-ask"
10+
description = "Denies the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)"
11+
commands.deny = ["message"]

plugins/dialog/permissions/autogenerated/commands/ask.toml

Lines changed: 0 additions & 13 deletions
This file was deleted.

plugins/dialog/permissions/autogenerated/commands/confirm.toml

Lines changed: 0 additions & 13 deletions
This file was deleted.

plugins/dialog/permissions/autogenerated/reference.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ All dialog types are enabled.
99

1010
#### This default permission set includes the following:
1111

12-
- `allow-ask`
13-
- `allow-confirm`
1412
- `allow-message`
1513
- `allow-save`
1614
- `allow-open`
@@ -32,7 +30,7 @@ All dialog types are enabled.
3230
</td>
3331
<td>
3432

35-
Enables the ask command without any pre-configured scope.
33+
Enables the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)
3634

3735
</td>
3836
</tr>
@@ -45,111 +43,111 @@ Enables the ask command without any pre-configured scope.
4543
</td>
4644
<td>
4745

48-
Denies the ask command without any pre-configured scope.
46+
Denies the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)
4947

5048
</td>
5149
</tr>
5250

5351
<tr>
5452
<td>
5553

56-
`dialog:allow-confirm`
54+
`dialog:allow-message`
5755

5856
</td>
5957
<td>
6058

61-
Enables the confirm command without any pre-configured scope.
59+
Enables the message command without any pre-configured scope.
6260

6361
</td>
6462
</tr>
6563

6664
<tr>
6765
<td>
6866

69-
`dialog:deny-confirm`
67+
`dialog:deny-message`
7068

7169
</td>
7270
<td>
7371

74-
Denies the confirm command without any pre-configured scope.
72+
Denies the message command without any pre-configured scope.
7573

7674
</td>
7775
</tr>
7876

7977
<tr>
8078
<td>
8179

82-
`dialog:allow-message`
80+
`dialog:allow-open`
8381

8482
</td>
8583
<td>
8684

87-
Enables the message command without any pre-configured scope.
85+
Enables the open command without any pre-configured scope.
8886

8987
</td>
9088
</tr>
9189

9290
<tr>
9391
<td>
9492

95-
`dialog:deny-message`
93+
`dialog:deny-open`
9694

9795
</td>
9896
<td>
9997

100-
Denies the message command without any pre-configured scope.
98+
Denies the open command without any pre-configured scope.
10199

102100
</td>
103101
</tr>
104102

105103
<tr>
106104
<td>
107105

108-
`dialog:allow-open`
106+
`dialog:allow-save`
109107

110108
</td>
111109
<td>
112110

113-
Enables the open command without any pre-configured scope.
111+
Enables the save command without any pre-configured scope.
114112

115113
</td>
116114
</tr>
117115

118116
<tr>
119117
<td>
120118

121-
`dialog:deny-open`
119+
`dialog:deny-save`
122120

123121
</td>
124122
<td>
125123

126-
Denies the open command without any pre-configured scope.
124+
Denies the save command without any pre-configured scope.
127125

128126
</td>
129127
</tr>
130128

131129
<tr>
132130
<td>
133131

134-
`dialog:allow-save`
132+
`dialog:allow-confirm`
135133

136134
</td>
137135
<td>
138136

139-
Enables the save command without any pre-configured scope.
137+
Enables the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)
140138

141139
</td>
142140
</tr>
143141

144142
<tr>
145143
<td>
146144

147-
`dialog:deny-save`
145+
`dialog:deny-confirm`
148146

149147
</td>
150148
<td>
151149

152-
Denies the save command without any pre-configured scope.
150+
Denies the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)
153151

154152
</td>
155153
</tr>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"$schema" = "schemas/schema.json"
2+
3+
[[permission]]
4+
identifier = "allow-confirm"
5+
description = "Enables the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)"
6+
commands.allow = ["message"]
7+
8+
[[permission]]
9+
identifier = "deny-confirm"
10+
description = "Denies the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)"
11+
commands.deny = ["message"]

0 commit comments

Comments
 (0)