e_utils/dialog/
sync.rs

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