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
+19 -15
View File
@@ -13,9 +13,7 @@ use rfd::{AsyncFileDialog, AsyncMessageDialog};
use serde::de::DeserializeOwned;
use tauri::{plugin::PluginApi, AppHandle, Runtime};
use crate::{models::*, FileDialogBuilder, FilePath, MessageDialogBuilder};
const OK: &str = "Ok";
use crate::{models::*, FileDialogBuilder, FilePath, MessageDialogBuilder, OK};
pub fn init<R: Runtime, C: DeserializeOwned>(
app: &AppHandle<R>,
@@ -109,22 +107,24 @@ impl<R: Runtime> From<FileDialogBuilder<R>> for AsyncFileDialog {
}
}
impl From<MessageDialogButtons> for rfd::MessageButtons {
fn from(value: MessageDialogButtons) -> Self {
match value {
MessageDialogButtons::Ok => Self::Ok,
MessageDialogButtons::OkCancel => Self::OkCancel,
MessageDialogButtons::OkCustom(ok) => Self::OkCustom(ok),
MessageDialogButtons::OkCancelCustom(ok, cancel) => Self::OkCancelCustom(ok, cancel),
}
}
}
impl<R: Runtime> From<MessageDialogBuilder<R>> for AsyncMessageDialog {
fn from(d: MessageDialogBuilder<R>) -> Self {
let mut dialog = AsyncMessageDialog::new()
.set_title(&d.title)
.set_description(&d.message)
.set_level(d.kind.into());
let buttons = match (d.ok_button_label, d.cancel_button_label) {
(Some(ok), Some(cancel)) => Some(rfd::MessageButtons::OkCancelCustom(ok, cancel)),
(Some(ok), None) => Some(rfd::MessageButtons::OkCustom(ok)),
(None, Some(cancel)) => Some(rfd::MessageButtons::OkCancelCustom(OK.into(), cancel)),
(None, None) => None,
};
if let Some(buttons) = buttons {
dialog = dialog.set_buttons(buttons);
}
.set_level(d.kind.into())
.set_buttons(d.buttons.into());
if let Some(parent) = d.parent {
dialog = dialog.set_parent(&parent);
@@ -213,7 +213,11 @@ pub fn show_message_dialog<R: Runtime, F: FnOnce(bool) + Send + 'static>(
) {
use rfd::MessageDialogResult;
let ok_label = dialog.ok_button_label.clone();
let ok_label = match &dialog.buttons {
MessageDialogButtons::OkCustom(ok) => Some(ok.clone()),
MessageDialogButtons::OkCancelCustom(ok, _) => Some(ok.clone()),
_ => None,
};
let f = move |res| {
f(match res {
MessageDialogResult::Ok | MessageDialogResult::Yes => true,