@@ -33,12 +33,17 @@ pub enum FileDialogType {
3333
3434/// Options for file dialogs.
3535#[ derive( Debug , Clone , Default ) ]
36- pub struct FileDialogOptions {
36+ pub struct FileDialogOptions < ' a > {
3737 pub show_hidden : bool ,
38- pub allowed_types : Option < Vec < FileSpec > > ,
39- pub default_type : Option < FileSpec > ,
38+ pub allowed_types : Option < Vec < FileSpec < ' a > > > ,
39+ pub default_type : Option < FileSpec < ' a > > ,
4040 pub select_directories : bool ,
4141 pub multi_selection : bool ,
42+ pub default_name : Option < & ' a str > ,
43+ pub name_label : Option < & ' a str > ,
44+ pub title : Option < & ' a str > ,
45+ pub button_text : Option < & ' a str > ,
46+ pub starting_directory : Option < & ' a Path > ,
4247 // we don't want a library user to be able to construct this type directly
4348 __non_exhaustive : ( ) ,
4449}
@@ -52,7 +57,7 @@ pub struct FileDialogOptions {
5257///
5358/// [`COMDLG_FILTERSPEC`]: https://docs.microsoft.com/en-ca/windows/win32/api/shtypes/ns-shtypes-comdlg_filterspec
5459#[ derive( Debug , Clone , Copy , PartialEq ) ]
55- pub struct FileSpec {
60+ pub struct FileSpec < ' a > {
5661 /// A human readable name, describing this filetype.
5762 ///
5863 /// This is used in the Windows file dialog, where the user can select
@@ -61,11 +66,11 @@ pub struct FileSpec {
6166 /// This should not include the file extensions; they will be added automatically.
6267 /// For instance, if we are describing Word documents, the name would be "Word Document",
6368 /// and the displayed string would be "Word Document (*.doc)".
64- pub name : & ' static str ,
69+ pub name : & ' a str ,
6570 /// The file extensions used by this file type.
6671 ///
6772 /// This should not include the leading '.'.
68- pub extensions : & ' static [ & ' static str ] ,
73+ pub extensions : & ' a [ & ' a str ] ,
6974}
7075
7176impl FileInfo {
@@ -75,54 +80,108 @@ impl FileInfo {
7580 }
7681}
7782
78- impl FileDialogOptions {
83+ impl < ' a > FileDialogOptions < ' a > {
7984 /// Create a new set of options.
80- pub fn new ( ) -> FileDialogOptions {
85+ pub fn new ( ) -> FileDialogOptions < ' static > {
8186 FileDialogOptions :: default ( )
8287 }
8388
84- /// Set the 'show hidden files' bit.
89+ /// Set whether hidden files and folders are shown.
90+ ///
91+ /// # macOS
92+ ///
93+ /// This option only shows hidden files, folders remain hidden.
8594 pub fn show_hidden ( mut self ) -> Self {
8695 self . show_hidden = true ;
8796 self
8897 }
8998
90- /// Set whether folders should be selectable.
99+ /// Set whether folders should be selectable instead of files.
100+ ///
101+ /// This is only relevant for open dialogs.
91102 pub fn select_directories ( mut self ) -> Self {
92103 self . select_directories = true ;
93104 self
94105 }
95106
96- /// Set whether multiple files can be selected.
107+ /// Set whether multiple items can be selected.
108+ ///
109+ /// This is only relevant for open dialogs.
97110 pub fn multi_selection ( mut self ) -> Self {
98111 self . multi_selection = true ;
99112 self
100113 }
101114
102115 /// Set the file types the user is allowed to select.
103- pub fn allowed_types ( mut self , types : Vec < FileSpec > ) -> Self {
104- self . allowed_types = Some ( types) ;
116+ ///
117+ /// An empty collection is treated as no filter.
118+ pub fn allowed_types ( mut self , types : Vec < FileSpec < ' a > > ) -> Self {
119+ // An empty vector can cause platform issues, so treat it as no filter
120+ if types. is_empty ( ) {
121+ self . allowed_types = None ;
122+ } else {
123+ self . allowed_types = Some ( types) ;
124+ }
105125 self
106126 }
107127
108128 /// Set the default file type.
109- /// If it's `None` or not present in [`allowed_types`](#method.allowed_types)
110- /// then the first entry in [`allowed_types`](#method.allowed_types) will be used as default.
111- pub fn default_type ( mut self , default_type : FileSpec ) -> Self {
129+ ///
130+ /// The provided `FileSpec` must be also present in [`allowed_types`].
131+ ///
132+ /// If it's `None` then the first entry in [`allowed_types`] will be used as the default.
133+ ///
134+ /// [`allowed_types`]: #method.allowed_types
135+ pub fn default_type ( mut self , default_type : FileSpec < ' a > ) -> Self {
112136 self . default_type = Some ( default_type) ;
113137 self
114138 }
139+
140+ /// Set the default filename that appears in the dialog.
141+ pub fn default_name ( mut self , default_name : & ' a str ) -> Self {
142+ self . default_name = Some ( default_name) ;
143+ self
144+ }
145+
146+ /// Set the text in the label next to the filename editbox.
147+ pub fn name_label ( mut self , name_label : & ' a str ) -> Self {
148+ self . name_label = Some ( name_label) ;
149+ self
150+ }
151+
152+ /// Set the title text of the dialog.
153+ pub fn title ( mut self , title : & ' a str ) -> Self {
154+ self . title = Some ( title) ;
155+ self
156+ }
157+
158+ /// Set the text of the Open/Save button.
159+ pub fn button_text ( mut self , text : & ' a str ) -> Self {
160+ self . button_text = Some ( text) ;
161+ self
162+ }
163+
164+ /// Force the starting folder to the specified `Path`.
165+ ///
166+ /// # User experience
167+ ///
168+ /// This should almost never be used because it overrides the OS choice,
169+ /// which will usually be a folder that the user recently visited.
170+ pub fn force_starting_directory ( mut self , path : & ' a Path ) -> Self {
171+ self . starting_directory = Some ( path) ;
172+ self
173+ }
115174}
116175
117- impl FileSpec {
118- pub const TEXT : FileSpec = FileSpec :: new ( "Text" , & [ "txt" ] ) ;
119- pub const JPG : FileSpec = FileSpec :: new ( "Jpeg" , & [ "jpg" , "jpeg" ] ) ;
120- pub const GIF : FileSpec = FileSpec :: new ( "Gif" , & [ "gif" ] ) ;
121- pub const PDF : FileSpec = FileSpec :: new ( "PDF" , & [ "pdf" ] ) ;
122- pub const HTML : FileSpec = FileSpec :: new ( "Web Page" , & [ "htm" , "html" ] ) ;
176+ impl < ' a > FileSpec < ' a > {
177+ pub const TEXT : FileSpec < ' a > = FileSpec :: new ( "Text" , & [ "txt" ] ) ;
178+ pub const JPG : FileSpec < ' a > = FileSpec :: new ( "Jpeg" , & [ "jpg" , "jpeg" ] ) ;
179+ pub const GIF : FileSpec < ' a > = FileSpec :: new ( "Gif" , & [ "gif" ] ) ;
180+ pub const PDF : FileSpec < ' a > = FileSpec :: new ( "PDF" , & [ "pdf" ] ) ;
181+ pub const HTML : FileSpec < ' a > = FileSpec :: new ( "Web Page" , & [ "htm" , "html" ] ) ;
123182
124183 /// Create a new `FileSpec`.
125- pub const fn new ( name : & ' static str , extensions : & ' static [ & ' static str ] ) -> Self {
184+ pub const fn new ( name : & ' a str , extensions : & ' a [ & ' a str ] ) -> Self {
126185 FileSpec { name, extensions }
127186 }
128187}
0 commit comments