From d2c17bb006c2212975e35d154b5e02d2beaee950 Mon Sep 17 00:00:00 2001 From: Jannis Date: Wed, 2 Oct 2024 18:28:53 +0200 Subject: [PATCH] Add option --- src/config/mod.rs | 10 ++++++++++ src/file_dialog.rs | 19 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 544e2652..5d343ee3 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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, @@ -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, diff --git a/src/file_dialog.rs b/src/file_dialog.rs index b9bc4447..9c24911d 100644 --- a/src/file_dialog.rs +++ b/src/file_dialog.rs @@ -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 { @@ -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) {