Skip to content

Commit f4b98bd

Browse files
Respect starting directory for dialogs on Windows (#1452)
* Respect starting directory for dialogs on Windows Before this change, FileDialogOptions::starting_directory had not been used on Windows and therefore had no influence on the starting directory of a FileDialog window. This has now been fixed. The use of `SHCreateItemFromParsingName` makes it mandatory to explicitly link against shell32.lib because this is not done automatically since Rust 1.33.0. See rust-lang/rust#56568 for details. * Fix: Use winapi feature instead of build.rs As it turns out, the winapi crate already exposes shell32.lib via a cargo feature, so there is no need to link it manually.
1 parent 5e2f821 commit f4b98bd

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Andrey Kabylin
1414
Garrett Risley
1515
Robert Wittams
1616
Jaap Aarts
17+
Maximilian Köstler

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ You can find its changes [documented below](#060---2020-06-01).
5757
- `TextBox::with_text_alignment` and `TextBox::set_text_alignment` ([#1371] by [@cmyr])
5858
- Add default minimum size to `WindowConfig`. ([#1438] by [@colinfruit])
5959
- Open and save dialogs send configurable commands. ([#1463] by [@jneem])
60+
- Windows: Dialogs now respect the parameter passed to `force_starting_directory()` ([#1452] by [@MaximilianKoestler])
6061

6162
### Changed
6263

@@ -364,6 +365,7 @@ Last release without a changelog :(
364365
[@colinfruit]: https://github.com/colinfruit
365366
[@Maan2003]: https://github.com/Maan2003
366367
[@derekdreery]: https://github.com/derekdreery
368+
[@MaximilianKoestler]: https://github.com/MaximilianKoestler
367369

368370
[#599]: https://github.com/linebender/druid/pull/599
369371
[#611]: https://github.com/linebender/druid/pull/611
@@ -553,6 +555,7 @@ Last release without a changelog :(
553555
[#1441]: https://github.com/linebender/druid/pull/1441
554556
[#1448]: https://github.com/linebender/druid/pull/1448
555557
[#1463]: https://github.com/linebender/druid/pull/1463
558+
[#1452]: https://github.com/linebender/druid/pull/1452
556559

557560
[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
558561
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0

druid-shell/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ wio = "0.2.2"
6060
version = "0.3.9"
6161
features = ["d2d1_1", "dwrite", "winbase", "libloaderapi", "errhandlingapi", "winuser",
6262
"shellscalingapi", "shobjidl", "combaseapi", "synchapi", "dxgi1_3", "dcomp",
63-
"d3d11", "dwmapi", "wincon", "fileapi", "processenv", "winbase", "handleapi"]
63+
"d3d11", "dwmapi", "wincon", "fileapi", "processenv", "winbase", "handleapi",
64+
"shellapi"]
6465

6566
[target.'cfg(target_os="macos")'.dependencies]
6667
block = "0.1.6"

druid-shell/src/platform/windows/dialog.rs

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::convert::TryInto;
2626
use std::ffi::OsString;
2727
use std::ptr::null_mut;
2828

29+
use winapi::ctypes::c_void;
2930
use winapi::shared::minwindef::*;
3031
use winapi::shared::ntdef::LPWSTR;
3132
use winapi::shared::windef::*;
@@ -147,6 +148,21 @@ pub(crate) unsafe fn get_file_dialog_path(
147148

148149
as_result(file_dialog.SetOptions(flags))?;
149150

151+
// set a starting directory
152+
if let Some(path) = options.starting_directory {
153+
let mut item: *mut IShellItem = null_mut();
154+
if let Err(err) = as_result(SHCreateItemFromParsingName(
155+
path.as_os_str().to_wide().as_ptr(),
156+
null_mut(),
157+
&IShellItem::uuidof(),
158+
&mut item as *mut *mut IShellItem as *mut *mut c_void,
159+
)) {
160+
log::warn!("Failed to convert path: {}", err.to_string());
161+
} else {
162+
as_result(file_dialog.SetDefaultFolder(item))?;
163+
}
164+
}
165+
150166
// show the dialog
151167
as_result(file_dialog.Show(hwnd_owner))?;
152168
let mut result_ptr: *mut IShellItem = null_mut();

0 commit comments

Comments
 (0)