refactor(dialog)!: use enum instead of label for buttons (#1842)

* refactor(dialog)!: use enum instead of label

* Add change file

* Fix doc comment typo

* Move ok and cancel to lib.rs
This commit is contained in:
Tony
2024-10-01 20:34:20 +08:00
committed by GitHub
parent 3b2bd3065d
commit 04459afbb6
5 changed files with 95 additions and 57 deletions
+30 -17
View File
@@ -8,7 +8,10 @@ use serde::{Deserialize, Serialize};
use tauri::{command, Manager, Runtime, State, Window};
use tauri_plugin_fs::FsExt;
use crate::{Dialog, FileDialogBuilder, FilePath, MessageDialogKind, Result};
use crate::{
Dialog, FileDialogBuilder, FilePath, MessageDialogButtons, MessageDialogKind, Result, CANCEL,
OK,
};
#[derive(Serialize)]
#[serde(untagged)]
@@ -244,11 +247,12 @@ fn message_dialog<R: Runtime>(
title: Option<String>,
message: String,
kind: Option<MessageDialogKind>,
ok_button_label: Option<String>,
cancel_button_label: Option<String>,
buttons: MessageDialogButtons,
) -> bool {
let mut builder = dialog.message(message);
builder = builder.buttons(buttons);
if let Some(title) = title {
builder = builder.title(title);
}
@@ -262,14 +266,6 @@ fn message_dialog<R: Runtime>(
builder = builder.kind(kind);
}
if let Some(ok) = ok_button_label {
builder = builder.ok_button_label(ok);
}
if let Some(cancel) = cancel_button_label {
builder = builder.cancel_button_label(cancel);
}
builder.blocking_show()
}
@@ -288,8 +284,11 @@ pub(crate) async fn message<R: Runtime>(
title,
message,
kind,
ok_button_label,
None,
if let Some(ok_button_label) = ok_button_label {
MessageDialogButtons::OkCustom(ok_button_label)
} else {
MessageDialogButtons::Ok
},
))
}
@@ -309,8 +308,7 @@ pub(crate) async fn ask<R: Runtime>(
title,
message,
kind,
Some(ok_button_label.unwrap_or_else(|| "Yes".into())),
Some(cancel_button_label.unwrap_or_else(|| "No".into())),
get_ok_cancel_type(ok_button_label, cancel_button_label),
))
}
@@ -330,7 +328,22 @@ pub(crate) async fn confirm<R: Runtime>(
title,
message,
kind,
Some(ok_button_label.unwrap_or_else(|| "Ok".into())),
Some(cancel_button_label.unwrap_or_else(|| "Cancel".into())),
get_ok_cancel_type(ok_button_label, cancel_button_label),
))
}
fn get_ok_cancel_type(
ok_button_label: Option<String>,
cancel_button_label: Option<String>,
) -> MessageDialogButtons {
if let Some(ok_button_label) = ok_button_label {
MessageDialogButtons::OkCancelCustom(
ok_button_label,
cancel_button_label.unwrap_or(CANCEL.to_string()),
)
} else if let Some(cancel_button_label) = cancel_button_label {
MessageDialogButtons::OkCancelCustom(OK.to_string(), cancel_button_label)
} else {
MessageDialogButtons::OkCancel
}
}