makepad_platform/
file_dialogs.rs

1// mildly stripped down version of native_dialog_rs dialog interface.
2use std::path::{PathBuf};
3
4
5/// Represents a set of file extensions and their description.
6#[derive(Debug, PartialEq)]
7pub struct Filter {
8    pub description: String,
9    pub extensions: Vec<String>,
10}
11
12/// Builds and shows file dialogs.
13
14#[derive(Debug, PartialEq)]
15pub struct FileDialog {
16    pub filename: Option<String>,
17    pub location: Option<PathBuf>,
18    pub filters: Vec<Filter>,
19    pub title: Option<String>,
20}
21
22impl FileDialog {
23    /// Creates a file dialog builder.
24    pub fn new() -> Self {
25        FileDialog {
26            filename: None,
27            location: None,
28            filters: vec![],           
29            title: None,
30        }
31    }
32
33    /// Sets the window title for the dialog.
34    pub fn set_title(mut self, title: String) -> Self {
35        self.title = Some(title);
36        self
37    }
38
39    /// Sets the default value of the filename text field in the dialog. For open dialogs of macOS
40    /// and zenity, this is a no-op because there's no such text field on the dialog.
41    pub fn set_filename(mut self, filename:  String) -> Self {
42        self.filename = Some(filename);
43        self
44    }
45
46    /// Resets the default value of the filename field in the dialog.
47    pub fn reset_filename(mut self) -> Self {
48        self.filename = None;
49        self
50    }
51
52    /// Sets the default location that the dialog shows at open.
53    pub fn set_location(mut self, path:  PathBuf) -> Self {
54        self.location = Some(path);
55        self
56    }
57
58    /// Resets the default location that the dialog shows at open. Without a default location set,
59    /// the dialog will probably use the current working directory as default location.
60    pub fn reset_location(mut self) -> Self {
61        self.location = None;
62        self
63    }
64
65    /// Adds a file type filter. The filter must contains at least one extension, otherwise this
66    /// method will panic. For dialogs that open directories, this is a no-op.
67    pub fn add_filter(mut self, description: String, extensions:  Vec<String>) -> Self {
68        if extensions.is_empty() {
69            panic!("The file extensions of a filter must be specified.")
70        }
71        self.filters.push(Filter {
72            description,
73            extensions,
74        });
75        self
76    }
77
78    /// Removes all file type filters.
79    pub fn remove_all_filters(mut self) -> Self {
80        self.filters = vec![];
81        self
82    }
83
84
85
86}
87
88
89
90impl Default for FileDialog {
91    fn default() -> Self {
92        Self::new()
93    }
94}
95