1use super::{FileHandle, MessageButtons, MessageDialogResult, MessageLevel};
2pub use rfd::{AsyncFileDialog, AsyncMessageDialog};
3use std::path::PathBuf;
4pub async fn folder() -> Option<PathBuf> {
9 AsyncFileDialog::new()
10 .pick_folder()
11 .await
12 .map(|f| f.path().to_owned())
13}
14pub async fn file() -> Option<PathBuf> {
19 AsyncFileDialog::new()
20 .pick_file()
21 .await
22 .map(|f| f.path().to_owned())
23}
24pub async fn files() -> Option<Vec<PathBuf>> {
29 AsyncFileDialog::new().pick_files().await.map(paths)
30}
31pub async fn folders() -> Option<Vec<PathBuf>> {
36 AsyncFileDialog::new().pick_folders().await.map(paths)
37}
38pub async fn create_file(filename: impl Into<String>) -> Option<PathBuf> {
43 AsyncFileDialog::new()
44 .set_file_name(filename)
45 .save_file()
46 .await
47 .map(|handle| handle.path().to_owned())
48}
49pub async fn ok(title: &str, msg: impl Into<String>) {
54 custom_one(title, msg, "OK").await
55}
56pub async fn ok_zh(title: &str, msg: impl Into<String>) {
61 custom_one(title, msg, "确认").await
62}
63pub async fn yesno_zh(title: &str, msg: impl Into<String>) -> bool {
68 custom_tow(title, msg, "同意", "拒绝").await
69}
70pub async fn yesno(title: &str, msg: impl Into<String>) -> bool {
75 custom_tow(title, msg, "YES", "NO").await
76}
77pub async fn info(title: &str, msg: impl Into<String>) {
82 show(title, msg, MessageLevel::Info).show().await;
83}
84pub async fn error(title: &str, msg: impl Into<String>) {
89 show(title, msg, MessageLevel::Error).show().await;
90}
91pub async fn warn(title: &str, msg: impl Into<String>) {
96 show(title, msg, MessageLevel::Warning).show().await;
97}
98#[inline]
99async fn custom_tow(
100 title: &str,
101 msg: impl Into<String>,
102 custom1: impl Into<String>,
103 custom2: impl Into<String>,
104) -> bool {
105 let dialog = show(title, msg, MessageLevel::Info)
106 .set_buttons(MessageButtons::OkCancelCustom(
107 custom1.into(),
108 custom2.into(),
109 ))
110 .show()
111 .await;
112 if let MessageDialogResult::Ok = dialog {
113 true
114 } else {
115 false
116 }
117}
118#[inline]
119async fn custom_one(title: &str, msg: impl Into<String>, custom: impl Into<String>) {
120 show(title, msg, MessageLevel::Info)
121 .set_buttons(MessageButtons::OkCustom(custom.into()))
122 .show()
123 .await;
124}
125#[inline]
126fn show(title: &str, msg: impl Into<String>, msg_type: MessageLevel) -> AsyncMessageDialog {
127 AsyncMessageDialog::new()
128 .set_title(title)
129 .set_description(msg)
130 .set_level(msg_type)
131}
132
133fn paths(handles: Vec<FileHandle>) -> Vec<PathBuf> {
134 handles.into_iter().map(|d| d.path().to_owned()).collect()
135}
136
137#[cfg(test)]
138mod test {
139 use crate::dialog::a_sync::*;
140 #[tokio::test]
141 async fn test_async_custom_tow() {
142 assert_eq!(custom_tow("测试", "YES NO", "同意", "拒绝").await, true)
143 }
144 #[tokio::test]
145 async fn test_async_custom_one() {
146 assert_eq!(custom_one("测试", "custom_one", "确认").await, ())
147 }
148 #[tokio::test]
149 async fn test_async_show() {
150 assert_eq!(
151 {
152 show("测试", "show", MessageLevel::Info).show().await;
153 },
154 ()
155 )
156 }
157 #[tokio::test]
158 async fn test_async_folder() {
159 assert!(folder().await.is_some())
160 }
161
162 #[tokio::test]
163 async fn test_async_folders() {
164 assert!(folders().await.is_some())
165 }
166
167 #[tokio::test]
168 async fn test_async_files() {
169 assert!(files().await.is_some())
170 }
171 #[tokio::test]
172 async fn test_async_file() {
173 assert!(file().await.is_some())
174 }
175 #[tokio::test]
176 async fn test_async_create_file() {
177 assert!(create_file("test.txt").await.is_some())
178 }
179}