Skip to content

Commit aa7e273

Browse files
authored
feat: use rfd::FileDialog#set_file_name if default_path is a file (#1598)
1 parent 9490b25 commit aa7e273

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
If the dialog `defaultPath` is a file, use it as starting file path.

core/tauri/src/api/dialog.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ impl FileDialogBuilder {
2626

2727
/// Set starting directory of the dialog.
2828
pub fn set_directory<P: AsRef<Path>>(mut self, directory: P) -> Self {
29-
self.0 = self.0.set_directory(&directory);
29+
self.0 = self.0.set_directory(directory);
30+
self
31+
}
32+
33+
/// Set starting file name of the dialog.
34+
pub fn set_file_name(mut self, file_name: &str) -> Self {
35+
self.0 = self.0.set_file_name(file_name);
3036
self
3137
}
3238

core/tauri/src/endpoints/dialog.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,43 @@ impl Cmd {
108108
}
109109
}
110110

111+
#[cfg(all(target_os = "linux", any(dialog_open, dialog_save)))]
112+
fn set_default_path(dialog_builder: FileDialogBuilder, default_path: PathBuf) -> FileDialogBuilder {
113+
if default_path.is_file() {
114+
dialog_builder.set_file_name(&default_path.to_string_lossy().to_string())
115+
} else {
116+
dialog_builder.set_directory(default_path)
117+
}
118+
}
119+
120+
#[cfg(all(any(windows, target_os = "macos"), any(dialog_open, dialog_save)))]
121+
fn set_default_path(
122+
mut dialog_builder: FileDialogBuilder,
123+
default_path: PathBuf,
124+
) -> FileDialogBuilder {
125+
if default_path.is_file() {
126+
if let Some(parent) = default_path.parent() {
127+
dialog_builder = dialog_builder.set_directory(parent);
128+
}
129+
dialog_builder = dialog_builder.set_file_name(
130+
&default_path
131+
.file_name()
132+
.unwrap()
133+
.to_string_lossy()
134+
.to_string(),
135+
);
136+
dialog_builder
137+
} else {
138+
dialog_builder.set_directory(default_path)
139+
}
140+
}
141+
111142
/// Shows an open dialog.
112143
#[cfg(dialog_open)]
113144
pub fn open(options: OpenDialogOptions) -> crate::Result<InvokeResponse> {
114145
let mut dialog_builder = FileDialogBuilder::new();
115146
if let Some(default_path) = options.default_path {
116-
dialog_builder = dialog_builder.set_directory(default_path);
147+
dialog_builder = set_default_path(dialog_builder, default_path);
117148
}
118149
for filter in options.filters {
119150
let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
@@ -134,7 +165,7 @@ pub fn open(options: OpenDialogOptions) -> crate::Result<InvokeResponse> {
134165
pub fn save(options: SaveDialogOptions) -> crate::Result<InvokeResponse> {
135166
let mut dialog_builder = FileDialogBuilder::new();
136167
if let Some(default_path) = options.default_path {
137-
dialog_builder = dialog_builder.set_directory(default_path);
168+
dialog_builder = set_default_path(dialog_builder, default_path);
138169
}
139170
for filter in options.filters {
140171
let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();

core/tauri/src/updater/core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ fn copy_files_and_run(tmp_dir: tempfile::TempDir, extract_path: PathBuf) -> Resu
522522
// Update server can provide a custom EXE (installer) who can run any task.
523523

524524
#[cfg(target_os = "windows")]
525+
#[allow(clippy::unnecessary_wraps)]
525526
fn copy_files_and_run(tmp_dir: tempfile::TempDir, _extract_path: PathBuf) -> Result {
526527
let paths = read_dir(&tmp_dir).unwrap();
527528
// This consumes the TempDir without deleting directory on the filesystem,

tooling/api/src/dialog.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface DialogFilter {
1212
export interface OpenDialogOptions {
1313
filters?: DialogFilter[]
1414
defaultPath?: string
15+
fileName?: string
1516
multiple?: boolean
1617
directory?: boolean
1718
}

0 commit comments

Comments
 (0)