Skip to content

Commit

Permalink
Save path from path edit (fluxxcode#160)
Browse files Browse the repository at this point in the history
* Save path from path edit

* Update changelog
  • Loading branch information
fluxxcode authored Oct 2, 2024
1 parent 4e152c8 commit 55c54ae
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# egui-file-dialog changelog

## Unreleased
### 🔧 Changes
- Use path edit as file to save [#160](https://github.com/fluxxcode/egui-file-dialog/pull/160)

## 2024-10-01 - v0.7.0 - egui update and QoL changes
### 🚨 Breaking Changes
- Updated `egui` from version `0.28.0` to version `0.29.1` [#155](https://github.com/fluxxcode/egui-file-dialog/pull/155) and [#157](https://github.com/fluxxcode/egui-file-dialog/pull/157) (thanks [@crumblingstatue](https://github.com/crumblingstatue)!)
Expand Down
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
53 changes: 43 additions & 10 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 @@ -2506,21 +2519,25 @@ impl FileDialog {
// Should always contain a value since `is_selection_valid` is used to
// validate the selection.
if let Some(path) = self.current_directory() {
let mut full_path = path.to_path_buf();
full_path.push(&self.file_name_input);

if full_path.exists() {
self.open_modal(Box::new(OverwriteFileModal::new(full_path)));

return;
}

self.state = DialogState::Selected(full_path);
let full_path = path.join(&self.file_name_input);
self.submit_save_file(full_path);
}
}
}
}

/// Submits the file dialog with the specified path and opens the `OverwriteFileModal`
/// if the path already exists.
fn submit_save_file(&mut self, path: PathBuf) {
if path.exists() {
self.open_modal(Box::new(OverwriteFileModal::new(path)));

return;
}

self.state = DialogState::Selected(path);
}

/// Cancels the dialog.
fn cancel(&mut self) {
self.state = DialogState::Cancelled;
Expand Down Expand Up @@ -2759,6 +2776,22 @@ impl FileDialog {
return;
}

// Assume the user wants to save the given path when
// - 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()
|| self.config.allow_path_edit_to_save_file_without_extension)
&& !path.is_dir()
&& path.parent().is_some_and(std::path::Path::exists)
{
self.submit_save_file(path);
return;
}

let _ = self.load_directory(&path);
}

Expand Down

0 comments on commit 55c54ae

Please sign in to comment.