Skip to content

Commit

Permalink
Save path from path edit
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxcode committed Oct 2, 2024
1 parent 4e152c8 commit d0526fb
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2506,21 +2506,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 +2763,20 @@ impl FileDialog {
return;
}

// Assume the user wants to save the given path when
// - An extension to the file name is given
// - The path is not an existing directory
// - 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.is_dir()
&& path.parent().is_some_and(|p| p.exists())
{
self.submit_save_file(path);
return;
}

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

Expand Down

0 comments on commit d0526fb

Please sign in to comment.