e_utils/dialog/
a_sync.rs

1use super::{FileHandle, MessageButtons, MessageDialogResult, MessageLevel};
2pub use rfd::{AsyncFileDialog, AsyncMessageDialog};
3use std::path::PathBuf;
4/// # 异步打开目录folder
5/// ```rust
6///  e_utils::dialog::a_sync::file().await
7/// ```
8pub async fn folder() -> Option<PathBuf> {
9  AsyncFileDialog::new()
10    .pick_folder()
11    .await
12    .map(|f| f.path().to_owned())
13}
14/// # 异步打开文件
15/// ```rust
16///  e_utils::dialog::a_sync::file().await
17/// ```
18pub async fn file() -> Option<PathBuf> {
19  AsyncFileDialog::new()
20    .pick_file()
21    .await
22    .map(|f| f.path().to_owned())
23}
24/// # 异步打开多个文件
25/// ```rust
26///  e_utils::dialog::a_sync::files().await
27/// ```
28pub async fn files() -> Option<Vec<PathBuf>> {
29  AsyncFileDialog::new().pick_files().await.map(paths)
30}
31/// # 异步打开多个目录
32/// ```rust
33///  e_utils::dialog::a_sync::folders().await
34/// ```
35pub async fn folders() -> Option<Vec<PathBuf>> {
36  AsyncFileDialog::new().pick_folders().await.map(paths)
37}
38/// # 异步创建文件
39/// ```rust
40///  e_utils::dialog::a_sync::create_file("newfile.txt").await
41/// ```
42pub 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}
49/// # Ok
50/// ```rust
51///  e_utils::dialog::a_sync::ok("Title","test").await
52/// ```
53pub async fn ok(title: &str, msg: impl Into<String>) {
54  custom_one(title, msg, "OK").await
55}
56/// # 确认
57/// ```rust
58///  e_utils::dialog::a_sync::ok_zh("Title","test").await
59/// ```
60pub async fn ok_zh(title: &str, msg: impl Into<String>) {
61  custom_one(title, msg, "确认").await
62}
63/// # Yes No
64/// ```rust
65///  e_utils::dialog::a_sync::yesno_zh("Title","test").await
66/// ```
67pub async fn yesno_zh(title: &str, msg: impl Into<String>) -> bool {
68  custom_tow(title, msg, "同意", "拒绝").await
69}
70/// # Yes NO
71/// ```rust
72///  e_utils::dialog::a_sync::yesno("Title","test").await
73/// ```
74pub async fn yesno(title: &str, msg: impl Into<String>) -> bool {
75  custom_tow(title, msg, "YES", "NO").await
76}
77/// # Info
78/// ```rust
79///  e_utils::dialog::a_sync::info("Title","test").await
80/// ```
81pub async fn info(title: &str, msg: impl Into<String>) {
82  show(title, msg, MessageLevel::Info).show().await;
83}
84/// # Error
85/// ```rust
86///  e_utils::dialog::a_sync::error("Title","test").await
87/// ```
88pub async fn error(title: &str, msg: impl Into<String>) {
89  show(title, msg, MessageLevel::Error).show().await;
90}
91/// # Warn
92/// ```rust
93///  e_utils::dialog::a_sync::warn("Title","test").await
94/// ```
95pub 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}