Skip to content

Commit

Permalink
Add option
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxcode committed Oct 2, 2024
1 parent ae96d63 commit d2c17bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ pub struct FileDialogConfig {
/// If the user is allowed to select an already existing file when the dialog is
/// in `DialogMode::SaveFile` mode.
pub allow_file_overwrite: bool,
/// If the path edit is allowed to select the path as the file to save
/// if it does not have an extension.
///
/// This can lead to confusion if the user wants to open a directory with the path edit,
/// types it incorrectly and the dialog tries to select the incorrectly typed folder as
/// the file to be saved.
///
/// This only affects the `DialogMode::SaveFile` mode.
pub allow_path_edit_to_save_file_without_extension: bool,
/// Sets the separator of the directories when displaying a path.
/// Currently only used when the current path is displayed in the top panel.
pub directory_separator: String,
Expand Down Expand Up @@ -196,6 +205,7 @@ impl Default for FileDialogConfig {
initial_directory: std::env::current_dir().unwrap_or_default(),
default_file_name: String::new(),
allow_file_overwrite: true,
allow_path_edit_to_save_file_without_extension: false,
directory_separator: String::from(">"),
canonicalize_paths: true,

Expand Down
19 changes: 17 additions & 2 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,19 @@ impl FileDialog {
self
}

/// Sets if the path edit is allowed to select the path as the file to save
/// if it does not have an extension.
///
/// This can lead to confusion if the user wants to open a directory with the path edit,
/// types it incorrectly and the dialog tries to select the incorrectly typed folder as
/// the file to be saved.
///
/// This only affects the `DialogMode::SaveFile` mode.
pub const fn allow_path_edit_to_save_file_without_extension(mut self, allow: bool) -> Self {
self.config.allow_path_edit_to_save_file_without_extension = allow;
self
}

/// Sets the separator of the directories when displaying a path.
/// Currently only used when the current path is displayed in the top panel.
pub fn directory_separator(mut self, separator: &str) -> Self {
Expand Down Expand Up @@ -2764,12 +2777,14 @@ impl FileDialog {
}

// Assume the user wants to save the given path when
// - an extension to the file name is given,
// - an extension to the file name is given or the path
// edit is allowed to save a file without extension,
// - the path is not an existing directory,
// - and the parent directory exists
// Otherwise we will assume the user wants to open the path as a directory.
if self.mode == DialogMode::SaveFile
&& path.extension().is_some()
&& (path.extension().is_some()
|| self.config.allow_path_edit_to_save_file_without_extension)
&& !path.is_dir()
&& path.parent().is_some_and(std::path::Path::exists)
{
Expand Down

0 comments on commit d2c17bb

Please sign in to comment.