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
use super::{MessageButtons, MessageDialogResult, MessageLevel};
pub use rfd::{FileDialog, MessageDialog};
use std::path::PathBuf;
/// # 异步打开目录folder
/// ```rust
///  e_utils::dialog::sync::file()
/// ```
pub fn folder() -> Option<PathBuf> {
  FileDialog::new().pick_folder()
}
/// # 异步打开文件
/// ```rust
///  e_utils::dialog::sync::file()
/// ```
pub fn file() -> Option<PathBuf> {
  FileDialog::new().pick_file()
}
/// # 异步打开多个文件
/// ```rust
///  e_utils::dialog::sync::files()
/// ```
pub fn files() -> Option<Vec<PathBuf>> {
  FileDialog::new().pick_files()
}
/// # 异步打开多个目录
/// ```rust
///  e_utils::dialog::sync::folders()
/// ```
pub fn folders() -> Option<Vec<PathBuf>> {
  FileDialog::new().pick_folders()
}
/// # 异步创建文件
/// ```rust
///  e_utils::dialog::sync::create_file("newfile.txt")
/// ```
pub fn create_file(filename: impl Into<String>) -> Option<PathBuf> {
  FileDialog::new().set_file_name(filename).save_file()
}
/// # Ok
/// ```rust
///  e_utils::dialog::sync::ok("Title","test")
/// ```
pub fn ok(title: &str, msg: impl Into<String>) {
  custom_one(title, msg, "OK")
}
/// # 确认
/// ```rust
///  e_utils::dialog::sync::ok_zh("Title","test")
/// ```
pub fn ok_zh(title: &str, msg: impl Into<String>) {
  custom_one(title, msg, "确认")
}
/// # Yes No
/// ```rust
///  e_utils::dialog::sync::yesno_zh("Title","test")
/// ```
pub fn yesno_zh(title: &str, msg: impl Into<String>) -> bool {
  custom_tow(title, msg, "同意", "拒绝")
}
/// # Yes NO
/// ```rust
///  e_utils::dialog::sync::yesno("Title","test")
/// ```
pub fn yesno(title: &str, msg: impl Into<String>) -> bool {
  custom_tow(title, msg, "YES", "NO")
}
/// # Info
/// ```rust
///  e_utils::dialog::sync::info("Title","test")
/// ```
pub fn info(title: &str, msg: impl Into<String>) {
  show(title, msg, MessageLevel::Info).show();
}
/// # Error
/// ```rust
///  e_utils::dialog::sync::error("Title","test")
/// ```
pub fn error(title: &str, msg: impl Into<String>) {
  show(title, msg, MessageLevel::Error).show();
}
/// # Warn
/// ```rust
///  e_utils::dialog::sync::warn("Title","test")
/// ```
pub fn warn(title: &str, msg: impl Into<String>) {
  show(title, msg, MessageLevel::Warning).show();
}
#[inline]
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();
  if let MessageDialogResult::Ok = dialog {
    true
  } else {
    false
  }
}
#[inline]
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();
}
#[inline]
fn show(title: &str, msg: impl Into<String>, msg_type: MessageLevel) -> MessageDialog {
  MessageDialog::new()
    .set_title(title)
    .set_description(msg)
    .set_level(msg_type)
}

#[cfg(test)]
mod test {
  use crate::dialog::sync::*;
  #[test]
  fn test_custom_tow() {
    assert_eq!(custom_tow("测试", "YES NO", "同意", "拒绝"), true)
  }
  #[test]
  fn test_custom_one() {
    assert_eq!(custom_one("测试", "custom_one", "确认"), ())
  }
  #[test]
  fn test_show() {
    assert_eq!(
      {
        show("测试", "show", MessageLevel::Info).show();
      },
      ()
    )
  }
  #[test]
  fn test_folder() {
    assert!(folder().is_some())
  }

  #[test]
  fn test_folders() {
    assert!(folders().is_some())
  }

  #[test]
  fn test_files() {
    assert!(files().is_some())
  }
  #[test]
  fn test_file() {
    assert!(file().is_some())
  }
  #[test]
  fn test_create_file() {
    assert!(create_file("test.txt").is_some())
  }
}