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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
// Copyright 2019 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! File open/save dialogs.
use std::path::{Path, PathBuf};
/// Information about the path to be opened or saved.
///
/// This path might point to a file or a directory.
#[derive(Debug, Clone)]
pub struct FileInfo {
pub(crate) path: PathBuf,
}
/// Type of file dialog.
#[cfg(not(all(target_os = "linux", feature = "x11")))]
#[derive(Clone, Copy, PartialEq)]
pub enum FileDialogType {
/// File open dialog.
Open,
/// File save dialog.
Save,
}
/// Options for file dialogs.
///
/// File dialogs let the user choose a specific path to open or save.
///
/// By default the file dialogs operate in *files mode* where the user can only choose files.
/// Importantly these are files from the user's perspective, but technically the returned path
/// will be a directory when the user chooses a package. You can read more about [packages] below.
/// It's also possible for users to manually specify a path which they might otherwise not be able
/// to choose. Thus it is important to verify that all the returned paths match your expectations.
///
/// The open dialog can also be switched to *directories mode* via [`select_directories`].
///
/// # Cross-platform compatibility
///
/// You could write platform specific code that really makes the best use of each platform.
/// However if you want to write universal code that will work on all platforms then
/// you have to keep some restrictions in mind.
///
/// ## Don't depend on directories with extensions
///
/// Your application should avoid having to deal with directories that have extensions
/// in their name, e.g. `my_stuff.pkg`. This clashes with [packages] on macOS and you
/// will either need platform specific code or a degraded user experience on macOS
/// via [`packages_as_directories`].
///
/// ## Use the save dialog only for new paths
///
/// Don't direct the user to choose an existing file with the save dialog.
/// Selecting existing files for overwriting is possible but extremely cumbersome on macOS.
/// The much more optimized flow is to have the user select a file with the open dialog
/// and then keep saving to that file without showing a save dialog.
/// Use the save dialog only for selecting a new location.
///
/// # macOS
///
/// The file dialog works a bit differently on macOS. For a lot of applications this doesn't matter
/// and you don't need to know the details. However if your application makes extensive use
/// of file dialogs and you target macOS then you should understand the macOS specifics.
///
/// ## Packages
///
/// On macOS directories with known extensions are considered to be packages, e.g. `app_files.pkg`.
/// Furthermore the packages are divided into two groups based on their extension.
/// First there are packages that have been defined at the OS level, and secondly there are
/// packages that are defined at the file dialog level based on [`allowed_types`].
/// These two types have slightly different behavior in the file dialogs. Generally packages
/// behave similarly to regular files in many contexts, including the file dialogs.
/// This package concept can be turned off in the file dialog via [`packages_as_directories`].
///
///  | Packages as files. File filters apply to packages. | Packages as directories.
/// -------- | -------------------------------------------------- | ------------------------
/// Open directory | Not selectable. Not traversable. | Selectable. Traversable.
/// Open file | Selectable. Not traversable. | Not selectable. Traversable.
/// Save file | OS packages [clickable] but not traversable.<br/>Dialog packages traversable but not selectable. | Not selectable. Traversable.
///
/// Keep in mind that the file dialog may start inside any package if the user has traversed
/// into one just recently. The user might also manually specify a path inside a package.
///
/// Generally this behavior should be kept, because it's least surprising to macOS users.
/// However if your application requires selecting directories with extensions as directories
/// or the user needs to be able to traverse into them to select a specific file,
/// then you can change the default behavior via [`packages_as_directories`]
/// to force macOS to behave like other platforms and not give special treatment to packages.
///
/// ## Selecting files for overwriting in the save dialog is cumbersome
///
/// Existing files can be clicked on in the save dialog, but that only copies their base file name.
/// If the clicked file's extension is different than the first extension of the default type
/// then the returned path does not actually match the path of the file that was clicked on.
/// Clicking on a file doesn't change the base path either. Keep in mind that the macOS file dialog
/// can have several directories open at once. So if a user has traversed into `/Users/Joe/foo/`
/// and then clicks on an existing file `/Users/Joe/old.txt` in another directory then the returned
/// path will actually be `/Users/Joe/foo/old.rtf` if the default type's first extension is `rtf`.
///
/// ## Have a really good save dialog default type
///
/// There is no way for the user to choose which extension they want to save a file as via the UI.
/// They have no way of knowing which extensions are even supported and must manually type it out.
///
/// *Hopefully it's a temporary problem and we can find a way to show the file formats in the UI.
/// This is being tracked in [druid#998].*
///
/// [clickable]: #selecting-files-for-overwriting-in-the-save-dialog-is-cumbersome
/// [packages]: #packages
/// [`select_directories`]: #method.select_directories
/// [`allowed_types`]: #method.allowed_types
/// [`packages_as_directories`]: #method.packages_as_directories
/// [druid#998]: https://github.com/xi-editor/druid/issues/998
#[derive(Debug, Clone, Default)]
pub struct FileDialogOptions {
pub(crate) show_hidden: bool,
pub(crate) allowed_types: Option<Vec<FileSpec>>,
pub(crate) default_type: Option<FileSpec>,
pub(crate) select_directories: bool,
pub(crate) packages_as_directories: bool,
pub(crate) multi_selection: bool,
pub(crate) default_name: Option<String>,
pub(crate) name_label: Option<String>,
pub(crate) title: Option<String>,
pub(crate) button_text: Option<String>,
pub(crate) starting_directory: Option<PathBuf>,
}
/// A description of a filetype, for specifiying allowed types in a file dialog.
///
/// # Windows
///
/// Each instance of this type is converted to a [`COMDLG_FILTERSPEC`] struct.
///
/// # macOS
///
/// These file types also apply to directories to define them as [packages].
///
/// [`COMDLG_FILTERSPEC`]: https://docs.microsoft.com/en-ca/windows/win32/api/shtypes/ns-shtypes-comdlg_filterspec
/// [packages]: struct.FileDialogOptions.html#packages
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FileSpec {
/// A human readable name, describing this filetype.
///
/// This is used in the Windows file dialog, where the user can select
/// from a dropdown the type of file they would like to choose.
///
/// This should not include the file extensions; they will be added automatically.
/// For instance, if we are describing Word documents, the name would be "Word Document",
/// and the displayed string would be "Word Document (*.doc)".
pub name: &'static str,
/// The file extensions used by this file type.
///
/// This should not include the leading '.'.
pub extensions: &'static [&'static str],
}
impl FileInfo {
/// Returns the underlying path.
pub fn path(&self) -> &Path {
&self.path
}
}
impl FileDialogOptions {
/// Create a new set of options.
pub fn new() -> FileDialogOptions {
FileDialogOptions::default()
}
/// Set hidden files and directories to be visible.
pub fn show_hidden(mut self) -> Self {
self.show_hidden = true;
self
}
/// Set directories to be selectable instead of files.
///
/// This is only relevant for open dialogs.
pub fn select_directories(mut self) -> Self {
self.select_directories = true;
self
}
/// Set [packages] to be treated as directories instead of files.
///
/// This allows for writing more universal cross-platform code at the cost of user experience.
///
/// This is only relevant on macOS.
///
/// [packages]: #packages
pub fn packages_as_directories(mut self) -> Self {
self.packages_as_directories = true;
self
}
/// Set multiple items to be selectable.
///
/// This is only relevant for open dialogs.
pub fn multi_selection(mut self) -> Self {
self.multi_selection = true;
self
}
/// Set the file types the user is allowed to select.
///
/// This filter is only applied to files and [packages], but not to directories.
///
/// An empty collection is treated as no filter.
///
/// # macOS
///
/// These file types also apply to directories to define [packages].
/// Which means the directories that match the filter are no longer considered directories.
/// The packages are defined by this collection even in *directories mode*.
///
/// [packages]: #packages
pub fn allowed_types(mut self, types: Vec<FileSpec>) -> Self {
// An empty vector can cause platform issues, so treat it as no filter
if types.is_empty() {
self.allowed_types = None;
} else {
self.allowed_types = Some(types);
}
self
}
/// Set the default file type.
///
/// The provided `default_type` must also be present in [`allowed_types`].
///
/// If it's `None` then the first entry in [`allowed_types`] will be used as the default.
///
/// This is only relevant in *files mode*.
///
/// [`allowed_types`]: #method.allowed_types
pub fn default_type(mut self, default_type: FileSpec) -> Self {
self.default_type = Some(default_type);
self
}
/// Set the default filename that appears in the dialog.
pub fn default_name(mut self, default_name: impl Into<String>) -> Self {
self.default_name = Some(default_name.into());
self
}
/// Set the text in the label next to the filename editbox.
pub fn name_label(mut self, name_label: impl Into<String>) -> Self {
self.name_label = Some(name_label.into());
self
}
/// Set the title text of the dialog.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Set the text of the Open/Save button.
pub fn button_text(mut self, text: impl Into<String>) -> Self {
self.button_text = Some(text.into());
self
}
/// Force the starting directory to the specified `path`.
///
/// # User experience
///
/// This should almost never be used because it overrides the OS choice,
/// which will usually be a directory that the user recently visited.
pub fn force_starting_directory(mut self, path: impl Into<PathBuf>) -> Self {
self.starting_directory = Some(path.into());
self
}
}
impl FileSpec {
pub const TEXT: FileSpec = FileSpec::new("Text", &["txt"]);
pub const JPG: FileSpec = FileSpec::new("Jpeg", &["jpg", "jpeg"]);
pub const GIF: FileSpec = FileSpec::new("Gif", &["gif"]);
pub const PNG: FileSpec = FileSpec::new("Portable network graphics (png)", &["png"]);
pub const PDF: FileSpec = FileSpec::new("PDF", &["pdf"]);
pub const HTML: FileSpec = FileSpec::new("Web Page", &["htm", "html"]);
/// Create a new `FileSpec`.
pub const fn new(name: &'static str, extensions: &'static [&'static str]) -> Self {
FileSpec { name, extensions }
}
}