use super::{MessageButtons, MessageDialogResult, MessageLevel};
pub use rfd::{FileDialog, MessageDialog};
use std::path::PathBuf;
pub fn folder() -> Option<PathBuf> {
FileDialog::new().pick_folder()
}
pub fn file() -> Option<PathBuf> {
FileDialog::new().pick_file()
}
pub fn files() -> Option<Vec<PathBuf>> {
FileDialog::new().pick_files()
}
pub fn folders() -> Option<Vec<PathBuf>> {
FileDialog::new().pick_folders()
}
pub fn create_file(filename: impl Into<String>) -> Option<PathBuf> {
FileDialog::new().set_file_name(filename).save_file()
}
pub fn ok(title: &str, msg: impl Into<String>) {
custom_one(title, msg, "OK")
}
pub fn ok_zh(title: &str, msg: impl Into<String>) {
custom_one(title, msg, "确认")
}
pub fn yesno_zh(title: &str, msg: impl Into<String>) -> bool {
custom_tow(title, msg, "同意", "拒绝")
}
pub fn yesno(title: &str, msg: impl Into<String>) -> bool {
custom_tow(title, msg, "YES", "NO")
}
pub fn info(title: &str, msg: impl Into<String>) {
show(title, msg, MessageLevel::Info).show();
}
pub fn error(title: &str, msg: impl Into<String>) {
show(title, msg, MessageLevel::Error).show();
}
pub fn warn(title: &str, msg: impl Into<String>) {
show(title, msg, MessageLevel::Warning).show();
}
#[inline]
fn custom_tow(
title: &str,
msg: impl Into<String>,
custom1: impl Into<String>,
custom2: impl Into<String>,
) -> bool {
let dialog = show(title, msg, MessageLevel::Info)
.set_buttons(MessageButtons::OkCancelCustom(
custom1.into(),
custom2.into(),
))
.show();
if let MessageDialogResult::Ok = dialog {
true
} else {
false
}
}
#[inline]
fn custom_one(title: &str, msg: impl Into<String>, custom: impl Into<String>) {
show(title, msg, MessageLevel::Info)
.set_buttons(MessageButtons::OkCustom(custom.into()))
.show();
}
#[inline]
fn show(title: &str, msg: impl Into<String>, msg_type: MessageLevel) -> MessageDialog {
MessageDialog::new()
.set_title(title)
.set_description(msg)
.set_level(msg_type)
}
#[cfg(test)]
mod test {
use crate::dialog::sync::*;
#[test]
fn test_custom_tow() {
assert_eq!(custom_tow("测试", "YES NO", "同意", "拒绝"), true)
}
#[test]
fn test_custom_one() {
assert_eq!(custom_one("测试", "custom_one", "确认"), ())
}
#[test]
fn test_show() {
assert_eq!(
{
show("测试", "show", MessageLevel::Info).show();
},
()
)
}
#[test]
fn test_folder() {
assert!(folder().is_some())
}
#[test]
fn test_folders() {
assert!(folders().is_some())
}
#[test]
fn test_files() {
assert!(files().is_some())
}
#[test]
fn test_file() {
assert!(file().is_some())
}
#[test]
fn test_create_file() {
assert!(create_file("test.txt").is_some())
}
}