e_utils/dialog/
a_sync.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use super::{FileHandle, MessageButtons, MessageDialogResult, MessageLevel};
pub use rfd::{AsyncFileDialog, AsyncMessageDialog};
use std::path::PathBuf;
/// # 异步打开目录folder
/// ```rust
///  e_utils::dialog::a_sync::file().await
/// ```
pub async fn folder() -> Option<PathBuf> {
  AsyncFileDialog::new()
    .pick_folder()
    .await
    .map(|f| f.path().to_owned())
}
/// # 异步打开文件
/// ```rust
///  e_utils::dialog::a_sync::file().await
/// ```
pub async fn file() -> Option<PathBuf> {
  AsyncFileDialog::new()
    .pick_file()
    .await
    .map(|f| f.path().to_owned())
}
/// # 异步打开多个文件
/// ```rust
///  e_utils::dialog::a_sync::files().await
/// ```
pub async fn files() -> Option<Vec<PathBuf>> {
  AsyncFileDialog::new().pick_files().await.map(paths)
}
/// # 异步打开多个目录
/// ```rust
///  e_utils::dialog::a_sync::folders().await
/// ```
pub async fn folders() -> Option<Vec<PathBuf>> {
  AsyncFileDialog::new().pick_folders().await.map(paths)
}
/// # 异步创建文件
/// ```rust
///  e_utils::dialog::a_sync::create_file("newfile.txt").await
/// ```
pub async fn create_file(filename: impl Into<String>) -> Option<PathBuf> {
  AsyncFileDialog::new()
    .set_file_name(filename)
    .save_file()
    .await
    .map(|handle| handle.path().to_owned())
}
/// # Ok
/// ```rust
///  e_utils::dialog::a_sync::ok("Title","test").await
/// ```
pub async fn ok(title: &str, msg: impl Into<String>) {
  custom_one(title, msg, "OK").await
}
/// # 确认
/// ```rust
///  e_utils::dialog::a_sync::ok_zh("Title","test").await
/// ```
pub async fn ok_zh(title: &str, msg: impl Into<String>) {
  custom_one(title, msg, "确认").await
}
/// # Yes No
/// ```rust
///  e_utils::dialog::a_sync::yesno_zh("Title","test").await
/// ```
pub async fn yesno_zh(title: &str, msg: impl Into<String>) -> bool {
  custom_tow(title, msg, "同意", "拒绝").await
}
/// # Yes NO
/// ```rust
///  e_utils::dialog::a_sync::yesno("Title","test").await
/// ```
pub async fn yesno(title: &str, msg: impl Into<String>) -> bool {
  custom_tow(title, msg, "YES", "NO").await
}
/// # Info
/// ```rust
///  e_utils::dialog::a_sync::info("Title","test").await
/// ```
pub async fn info(title: &str, msg: impl Into<String>) {
  show(title, msg, MessageLevel::Info).show().await;
}
/// # Error
/// ```rust
///  e_utils::dialog::a_sync::error("Title","test").await
/// ```
pub async fn error(title: &str, msg: impl Into<String>) {
  show(title, msg, MessageLevel::Error).show().await;
}
/// # Warn
/// ```rust
///  e_utils::dialog::a_sync::warn("Title","test").await
/// ```
pub async fn warn(title: &str, msg: impl Into<String>) {
  show(title, msg, MessageLevel::Warning).show().await;
}
#[inline]
async 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()
    .await;
  if let MessageDialogResult::Ok = dialog {
    true
  } else {
    false
  }
}
#[inline]
async 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()
    .await;
}
#[inline]
fn show(title: &str, msg: impl Into<String>, msg_type: MessageLevel) -> AsyncMessageDialog {
  AsyncMessageDialog::new()
    .set_title(title)
    .set_description(msg)
    .set_level(msg_type)
}

fn paths(handles: Vec<FileHandle>) -> Vec<PathBuf> {
  handles.into_iter().map(|d| d.path().to_owned()).collect()
}

#[cfg(test)]
mod test {
  use crate::dialog::a_sync::*;
  #[tokio::test]
  async fn test_async_custom_tow() {
    assert_eq!(custom_tow("测试", "YES NO", "同意", "拒绝").await, true)
  }
  #[tokio::test]
  async fn test_async_custom_one() {
    assert_eq!(custom_one("测试", "custom_one", "确认").await, ())
  }
  #[tokio::test]
  async fn test_async_show() {
    assert_eq!(
      {
        show("测试", "show", MessageLevel::Info).show().await;
      },
      ()
    )
  }
  #[tokio::test]
  async fn test_async_folder() {
    assert!(folder().await.is_some())
  }

  #[tokio::test]
  async fn test_async_folders() {
    assert!(folders().await.is_some())
  }

  #[tokio::test]
  async fn test_async_files() {
    assert!(files().await.is_some())
  }
  #[tokio::test]
  async fn test_async_file() {
    assert!(file().await.is_some())
  }
  #[tokio::test]
  async fn test_async_create_file() {
    assert!(create_file("test.txt").await.is_some())
  }
}