1use super::{MessageButtons, MessageDialogResult, MessageLevel};
2pub use rfd::{FileDialog, MessageDialog};
3use std::path::PathBuf;
4pub fn folder() -> Option<PathBuf> {
9 FileDialog::new().pick_folder()
10}
11pub fn file() -> Option<PathBuf> {
16 FileDialog::new().pick_file()
17}
18pub fn files() -> Option<Vec<PathBuf>> {
23 FileDialog::new().pick_files()
24}
25pub fn folders() -> Option<Vec<PathBuf>> {
30 FileDialog::new().pick_folders()
31}
32pub fn create_file(filename: impl Into<String>) -> Option<PathBuf> {
37 FileDialog::new().set_file_name(filename).save_file()
38}
39pub fn ok(title: &str, msg: impl Into<String>) {
44 custom_one(title, msg, "OK")
45}
46pub fn ok_zh(title: &str, msg: impl Into<String>) {
51 custom_one(title, msg, "确认")
52}
53pub fn yesno_zh(title: &str, msg: impl Into<String>) -> bool {
58 custom_tow(title, msg, "同意", "拒绝")
59}
60pub fn yesno(title: &str, msg: impl Into<String>) -> bool {
65 custom_tow(title, msg, "YES", "NO")
66}
67pub fn info(title: &str, msg: impl Into<String>) {
72 show(title, msg, MessageLevel::Info).show();
73}
74pub fn error(title: &str, msg: impl Into<String>) {
79 show(title, msg, MessageLevel::Error).show();
80}
81pub fn warn(title: &str, msg: impl Into<String>) {
86 show(title, msg, MessageLevel::Warning).show();
87}
88#[inline]
89fn custom_tow(
90 title: &str,
91 msg: impl Into<String>,
92 custom1: impl Into<String>,
93 custom2: impl Into<String>,
94) -> bool {
95 let dialog = show(title, msg, MessageLevel::Info)
96 .set_buttons(MessageButtons::OkCancelCustom(
97 custom1.into(),
98 custom2.into(),
99 ))
100 .show();
101 if let MessageDialogResult::Ok = dialog {
102 true
103 } else {
104 false
105 }
106}
107#[inline]
108fn custom_one(title: &str, msg: impl Into<String>, custom: impl Into<String>) {
109 show(title, msg, MessageLevel::Info)
110 .set_buttons(MessageButtons::OkCustom(custom.into()))
111 .show();
112}
113#[inline]
114fn show(title: &str, msg: impl Into<String>, msg_type: MessageLevel) -> MessageDialog {
115 MessageDialog::new()
116 .set_title(title)
117 .set_description(msg)
118 .set_level(msg_type)
119}
120
121#[cfg(test)]
122mod test {
123 use crate::dialog::sync::*;
124 #[test]
125 fn test_custom_tow() {
126 assert_eq!(custom_tow("测试", "YES NO", "同意", "拒绝"), true)
127 }
128 #[test]
129 fn test_custom_one() {
130 assert_eq!(custom_one("测试", "custom_one", "确认"), ())
131 }
132 #[test]
133 fn test_show() {
134 assert_eq!(
135 {
136 show("测试", "show", MessageLevel::Info).show();
137 },
138 ()
139 )
140 }
141 #[test]
142 fn test_folder() {
143 assert!(folder().is_some())
144 }
145
146 #[test]
147 fn test_folders() {
148 assert!(folders().is_some())
149 }
150
151 #[test]
152 fn test_files() {
153 assert!(files().is_some())
154 }
155 #[test]
156 fn test_file() {
157 assert!(file().is_some())
158 }
159 #[test]
160 fn test_create_file() {
161 assert!(create_file("test.txt").is_some())
162 }
163}