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
use std::path::PathBuf;
use winio_handle::MaybeBorrowedWindow;
use crate::{sys, sys::Result};
/// File open/save box.
#[derive(Debug, Default, Clone)]
pub struct FileBox(sys::FileBox);
impl FileBox {
/// Create [`FileBox`].
pub fn new() -> Self {
Self(sys::FileBox::new())
}
/// Box title.
pub fn title(mut self, title: impl AsRef<str>) -> Self {
self.0.title(title.as_ref());
self
}
/// Default filename.
pub fn filename(mut self, filename: impl AsRef<str>) -> Self {
self.0.filename(filename.as_ref());
self
}
/// Set file filters.
pub fn filters(mut self, filters: impl IntoIterator<Item = FileFilter>) -> Self {
self.0.filters(filters.into_iter().map(|f| f.0));
self
}
/// Add a file filter.
pub fn add_filter(mut self, filter: impl Into<FileFilter>) -> Self {
self.0.add_filter(filter.into().0);
self
}
/// Show open file dialog.
///
/// This method is not a usual `async fn`. It shows the dialog immediately,
/// and returns a future that waits for the result. This design allows you
/// to spawn the returned future.
///
/// ## Platform specific
/// * AppKit: This method is blocking without parent window.
pub fn open<'a>(
self,
parent: impl Into<MaybeBorrowedWindow<'a>>,
) -> Result<impl Future<Output = Result<Option<PathBuf>>> + 'static> {
self.0.open(parent.into().0)
}
/// Show open file dialog, allowing multiple selection.
///
/// This method is not a usual `async fn`. It shows the dialog immediately,
/// and returns a future that waits for the result. This design allows you
/// to spawn the returned future.
///
/// ## Platform specific
/// * AppKit: This method is blocking without parent window.
pub fn open_multiple<'a>(
self,
parent: impl Into<MaybeBorrowedWindow<'a>>,
) -> Result<impl Future<Output = Result<Vec<PathBuf>>> + 'static> {
self.0.open_multiple(parent.into().0)
}
/// Show open file dialog, select folder only.
///
/// This method is not a usual `async fn`. It shows the dialog immediately,
/// and returns a future that waits for the result. This design allows you
/// to spawn the returned future.
///
/// ## Platform specific
/// * AppKit: This method is blocking without parent window.
pub fn open_folder<'a>(
self,
parent: impl Into<MaybeBorrowedWindow<'a>>,
) -> Result<impl Future<Output = Result<Option<PathBuf>>> + 'static> {
self.0.open_folder(parent.into().0)
}
/// Show save file dialog.
///
/// This method is not a usual `async fn`. It shows the dialog immediately,
/// and returns a future that waits for the result. This design allows you
/// to spawn the returned future.
///
/// ## Platform specific
/// * AppKit: This method is blocking without parent window.
pub fn save<'a>(
self,
parent: impl Into<MaybeBorrowedWindow<'a>>,
) -> Result<impl Future<Output = Result<Option<PathBuf>>> + 'static> {
self.0.save(parent.into().0)
}
}
/// File type filter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileFilter(sys::FileFilter);
impl FileFilter {
/// Create [`FileFilter`].
///
/// The pattern should be like `*.txt` or `*.*`.
pub fn new(name: impl AsRef<str>, pattern: impl AsRef<str>) -> Self {
Self(sys::FileFilter::new(name.as_ref(), pattern.as_ref()))
}
}
impl<S: AsRef<str>, P: AsRef<str>> From<(S, P)> for FileFilter {
fn from((name, pattern): (S, P)) -> Self {
Self::new(name, pattern)
}
}