Skip to content

Commit 3f21f39

Browse files
refactor(dialog): handle okLabel in js side (#3295)
* refactor(dialog): handle `okLabel` in js side * Allow unused instead of `cfg(desktop)`
1 parent 759c227 commit 3f21f39

5 files changed

Lines changed: 22 additions & 33 deletions

File tree

examples/api/src/views/Dialog.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
}
4444
4545
async function msg() {
46-
await message("Tauri is awesome!");
46+
await message("Tauri is awesome!").then((res) => onMessage(res));
4747
}
4848
4949
async function msgCustom(result) {

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/guest-js/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,14 @@ 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) {
408+
async function messageCommand(
409+
message: string,
410+
options?: Omit<MessageDialogOptions, 'okLabel'>
411+
) {
409412
return await invoke<MessageDialogResult>('plugin:dialog|message', {
410413
message,
411414
title: options?.title,
412415
kind: options?.kind,
413-
okButtonLabel: options?.okLabel,
414416
buttons: buttonsToRust(options?.buttons)
415417
})
416418
}
@@ -437,6 +439,9 @@ async function message(
437439
options?: string | MessageDialogOptions
438440
): Promise<MessageDialogResult> {
439441
const opts = typeof options === 'string' ? { title: options } : options
442+
if (opts && !opts.buttons && opts.okLabel) {
443+
opts.buttons = { ok: opts.okLabel }
444+
}
440445
return messageCommand(message, opts)
441446
}
442447

plugins/dialog/src/commands.rs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use tauri::{command, Manager, Runtime, State, Window};
99
use tauri_plugin_fs::FsExt;
1010

1111
use crate::{
12-
Dialog, FileAccessMode, FileDialogBuilder, FilePath, MessageDialogBuilder,
13-
MessageDialogButtons, MessageDialogKind, MessageDialogResult, PickerMode, Result,
12+
Dialog, FileAccessMode, FileDialogBuilder, FilePath, MessageDialogButtons, MessageDialogKind,
13+
MessageDialogResult, PickerMode, Result,
1414
};
1515

1616
#[derive(Serialize)]
@@ -258,17 +258,20 @@ pub(crate) async fn save<R: Runtime>(
258258
Ok(path.map(|p| p.simplified()))
259259
}
260260

261-
fn message_dialog<R: Runtime>(
262-
#[allow(unused_variables)] window: Window<R>,
261+
#[command]
262+
pub(crate) async fn message<R: Runtime>(
263+
#[allow(unused)] window: Window<R>,
263264
dialog: State<'_, Dialog<R>>,
264265
title: Option<String>,
265266
message: String,
266267
kind: Option<MessageDialogKind>,
267-
buttons: MessageDialogButtons,
268-
) -> MessageDialogBuilder<R> {
268+
buttons: Option<MessageDialogButtons>,
269+
) -> Result<MessageDialogResult> {
269270
let mut builder = dialog.message(message);
270271

271-
builder = builder.buttons(buttons);
272+
if let Some(buttons) = buttons {
273+
builder = builder.buttons(buttons);
274+
}
272275

273276
if let Some(title) = title {
274277
builder = builder.title(title);
@@ -283,24 +286,5 @@ fn message_dialog<R: Runtime>(
283286
builder = builder.kind(kind);
284287
}
285288

286-
builder
287-
}
288-
289-
#[command]
290-
pub(crate) async fn message<R: Runtime>(
291-
window: Window<R>,
292-
dialog: State<'_, Dialog<R>>,
293-
title: Option<String>,
294-
message: String,
295-
kind: Option<MessageDialogKind>,
296-
ok_button_label: Option<String>,
297-
buttons: Option<MessageDialogButtons>,
298-
) -> Result<MessageDialogResult> {
299-
let buttons = buttons.unwrap_or(if let Some(ok_button_label) = ok_button_label {
300-
MessageDialogButtons::OkCustom(ok_button_label)
301-
} else {
302-
MessageDialogButtons::Ok
303-
});
304-
305-
Ok(message_dialog(window, dialog, title, message, kind, buttons).blocking_show_with_result())
289+
Ok(builder.blocking_show_with_result())
306290
}

plugins/dialog/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ impl<R: Runtime> MessageDialogBuilder<R> {
247247
dialog,
248248
title: title.into(),
249249
message: message.into(),
250-
kind: Default::default(),
251-
buttons: Default::default(),
250+
kind: MessageDialogKind::default(),
251+
buttons: MessageDialogButtons::default(),
252252
#[cfg(desktop)]
253253
parent: None,
254254
}

0 commit comments

Comments
 (0)