Skip to content

Commit

Permalink
feat: use rfd::FileDialog#set_file_name if default_path is a file (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 23, 2021
1 parent 9490b25 commit aa7e273
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/dialog-default-path-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

If the dialog `defaultPath` is a file, use it as starting file path.
8 changes: 7 additions & 1 deletion core/tauri/src/api/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ impl FileDialogBuilder {

/// Set starting directory of the dialog.
pub fn set_directory<P: AsRef<Path>>(mut self, directory: P) -> Self {
self.0 = self.0.set_directory(&directory);
self.0 = self.0.set_directory(directory);
self
}

/// Set starting file name of the dialog.
pub fn set_file_name(mut self, file_name: &str) -> Self {
self.0 = self.0.set_file_name(file_name);
self
}

Expand Down
35 changes: 33 additions & 2 deletions core/tauri/src/endpoints/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,43 @@ impl Cmd {
}
}

#[cfg(all(target_os = "linux", any(dialog_open, dialog_save)))]
fn set_default_path(dialog_builder: FileDialogBuilder, default_path: PathBuf) -> FileDialogBuilder {
if default_path.is_file() {
dialog_builder.set_file_name(&default_path.to_string_lossy().to_string())
} else {
dialog_builder.set_directory(default_path)
}
}

#[cfg(all(any(windows, target_os = "macos"), any(dialog_open, dialog_save)))]
fn set_default_path(
mut dialog_builder: FileDialogBuilder,
default_path: PathBuf,
) -> FileDialogBuilder {
if default_path.is_file() {
if let Some(parent) = default_path.parent() {
dialog_builder = dialog_builder.set_directory(parent);
}
dialog_builder = dialog_builder.set_file_name(
&default_path
.file_name()
.unwrap()
.to_string_lossy()
.to_string(),
);
dialog_builder
} else {
dialog_builder.set_directory(default_path)
}
}

/// Shows an open dialog.
#[cfg(dialog_open)]
pub fn open(options: OpenDialogOptions) -> crate::Result<InvokeResponse> {
let mut dialog_builder = FileDialogBuilder::new();
if let Some(default_path) = options.default_path {
dialog_builder = dialog_builder.set_directory(default_path);
dialog_builder = set_default_path(dialog_builder, default_path);
}
for filter in options.filters {
let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
Expand All @@ -134,7 +165,7 @@ pub fn open(options: OpenDialogOptions) -> crate::Result<InvokeResponse> {
pub fn save(options: SaveDialogOptions) -> crate::Result<InvokeResponse> {
let mut dialog_builder = FileDialogBuilder::new();
if let Some(default_path) = options.default_path {
dialog_builder = dialog_builder.set_directory(default_path);
dialog_builder = set_default_path(dialog_builder, default_path);
}
for filter in options.filters {
let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/updater/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ fn copy_files_and_run(tmp_dir: tempfile::TempDir, extract_path: PathBuf) -> Resu
// Update server can provide a custom EXE (installer) who can run any task.

#[cfg(target_os = "windows")]
#[allow(clippy::unnecessary_wraps)]
fn copy_files_and_run(tmp_dir: tempfile::TempDir, _extract_path: PathBuf) -> Result {
let paths = read_dir(&tmp_dir).unwrap();
// This consumes the TempDir without deleting directory on the filesystem,
Expand Down
1 change: 1 addition & 0 deletions tooling/api/src/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface DialogFilter {
export interface OpenDialogOptions {
filters?: DialogFilter[]
defaultPath?: string
fileName?: string
multiple?: boolean
directory?: boolean
}
Expand Down

0 comments on commit aa7e273

Please sign in to comment.