native_dialog/builder/
file.rs1use raw_window_handle::HasWindowHandle;
2use std::path::{Path, PathBuf};
3
4use crate::dialog::{Filter, OpenMultipleFile, OpenSingleDir, OpenSingleFile, SaveSingleFile};
5use crate::utils::UnsafeWindowHandle;
6
7#[derive(Debug, Clone, Default)]
9pub struct FileDialogBuilder {
10 pub filename: Option<String>,
11 pub location: Option<PathBuf>,
12 pub filters: Vec<Filter>,
13 pub owner: UnsafeWindowHandle,
14 pub title: Option<String>,
15}
16
17impl FileDialogBuilder {
18 pub fn set_title(mut self, title: impl ToString) -> Self {
20 self.title = Some(title.to_string());
21 self
22 }
23
24 pub fn set_filename(mut self, filename: impl ToString) -> Self {
27 self.filename = Some(filename.to_string());
28 self
29 }
30
31 pub fn reset_filename(mut self) -> Self {
33 self.filename = None;
34 self
35 }
36
37 pub fn set_location<P: AsRef<Path> + ?Sized>(mut self, path: &P) -> Self {
39 self.location = Some(path.as_ref().to_path_buf());
40 self
41 }
42
43 pub fn reset_location(mut self) -> Self {
46 self.location = None;
47 self
48 }
49
50 pub fn add_filter(mut self, description: impl ToString, extensions: &[impl ToString]) -> Self {
53 if extensions.is_empty() {
54 return self;
55 }
56
57 self.filters.push(Filter {
58 description: description.to_string(),
59 extensions: extensions.iter().map(ToString::to_string).collect(),
60 });
61
62 self
63 }
64
65 pub fn reset_filters(mut self) -> Self {
67 self.filters = vec![];
68 self
69 }
70
71 pub fn set_owner<W: HasWindowHandle>(mut self, window: &W) -> Self {
73 self.owner = UnsafeWindowHandle::new(window);
74 self
75 }
76
77 pub fn reset_owner(mut self) -> Self {
79 self.owner = UnsafeWindowHandle::default();
80 self
81 }
82
83 pub fn open_single_file(self) -> OpenSingleFile {
85 OpenSingleFile {
86 filename: self.filename,
87 location: self.location,
88 filters: self.filters,
89 owner: self.owner,
90 title: self.title.unwrap_or("Open File".to_string()),
91 }
92 }
93
94 pub fn open_multiple_file(self) -> OpenMultipleFile {
96 OpenMultipleFile {
97 filename: self.filename,
98 location: self.location,
99 filters: self.filters,
100 owner: self.owner,
101 title: self.title.unwrap_or("Open File".to_string()),
102 }
103 }
104
105 pub fn open_single_dir(self) -> OpenSingleDir {
107 OpenSingleDir {
108 filename: self.filename,
109 location: self.location,
110 owner: self.owner,
111 title: self.title.unwrap_or("Open Folder".to_string()),
112 }
113 }
114
115 pub fn save_single_file(self) -> SaveSingleFile {
117 SaveSingleFile {
118 filename: self.filename,
119 location: self.location,
120 filters: self.filters,
121 owner: self.owner,
122 title: self.title.unwrap_or("Save As".to_string()),
123 }
124 }
125}