Skip to content

Commit

Permalink
[file_selector_windows] Fix the problem that the initial directory do…
Browse files Browse the repository at this point in the history
…es not work after completing a file selection on windows (flutter#5416)
  • Loading branch information
tgalpha authored Aug 19, 2022
1 parent cd586d7 commit a42835a
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 11 deletions.
4 changes: 4 additions & 0 deletions packages/file_selector/file_selector_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.1+2

* Fixes the problem that the initial directory does not work after completing a file selection.

## 0.9.1+1

* Updates README for endorsement.
Expand Down
2 changes: 1 addition & 1 deletion packages/file_selector/file_selector_windows/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: file_selector_windows
description: Windows implementation of the file_selector plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/file_selector/file_selector_windows
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
version: 0.9.1+1
version: 0.9.1+2

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ FileDialogController::FileDialogController(IFileDialog* dialog)

FileDialogController::~FileDialogController() {}

HRESULT FileDialogController::SetDefaultFolder(IShellItem* folder) {
return dialog_->SetDefaultFolder(folder);
HRESULT FileDialogController::SetFolder(IShellItem* folder) {
return dialog_->SetFolder(folder);
}

HRESULT FileDialogController::SetFileName(const wchar_t* name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FileDialogController {
FileDialogController& operator=(const FileDialogController&) = delete;

// IFileDialog wrappers:
virtual HRESULT SetDefaultFolder(IShellItem* folder);
virtual HRESULT SetFolder(IShellItem* folder);
virtual HRESULT SetFileName(const wchar_t* name);
virtual HRESULT SetFileTypes(UINT count, COMDLG_FILTERSPEC* filters);
virtual HRESULT SetOkButtonLabel(const wchar_t* text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ class DialogWrapper {

// Attempts to set the default folder for the dialog to |path|,
// if it exists.
void SetDefaultFolder(std::string_view path) {
void SetFolder(std::string_view path) {
std::wstring wide_path = Utf16FromUtf8(path);
IShellItemPtr item;
last_result_ = SHCreateItemFromParsingName(wide_path.c_str(), nullptr,
IID_PPV_ARGS(&item));
if (!SUCCEEDED(last_result_)) {
return;
}
dialog_controller_->SetDefaultFolder(item);
dialog_controller_->SetFolder(item);
}

// Sets the file name that is initially shown in the dialog.
Expand Down Expand Up @@ -230,7 +230,7 @@ ErrorOr<flutter::EncodableList> ShowDialog(
}

if (initial_directory) {
dialog.SetDefaultFolder(*initial_directory);
dialog.SetFolder(*initial_directory);
}
if (suggested_name) {
dialog.SetFileName(*suggested_name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ TEST(FileSelectorPlugin, TestOpenWithArguments) {
EXPECT_EQ(parent, fake_window);

// Validate arguments.
EXPECT_EQ(dialog.GetDefaultFolderPath(), L"C:\\Program Files");
EXPECT_EQ(dialog.GetDialogFolderPath(), L"C:\\Program Files");
// Make sure that the folder was called via SetFolder, not SetDefaultFolder.
EXPECT_EQ(dialog.GetSetFolderPath(), L"C:\\Program Files");
EXPECT_EQ(dialog.GetOkButtonLabel(), L"Open it!");

return MockShowResult(fake_result_array);
Expand Down Expand Up @@ -322,7 +324,10 @@ TEST(FileSelectorPlugin, TestSaveWithArguments) {
EXPECT_EQ(parent, fake_window);

// Validate arguments.
EXPECT_EQ(dialog.GetDefaultFolderPath(), L"C:\\Program Files");
EXPECT_EQ(dialog.GetDialogFolderPath(), L"C:\\Program Files");
// Make sure that the folder was called via SetFolder, not
// SetDefaultFolder.
EXPECT_EQ(dialog.GetSetFolderPath(), L"C:\\Program Files");
EXPECT_EQ(dialog.GetFileName(), L"a name");
EXPECT_EQ(dialog.GetOkButtonLabel(), L"Save it!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ TestFileDialogController::TestFileDialogController(IFileDialog* dialog,

TestFileDialogController::~TestFileDialogController() {}

HRESULT TestFileDialogController::SetFolder(IShellItem* folder) {
wchar_t* path_chars = nullptr;
if (SUCCEEDED(folder->GetDisplayName(SIGDN_FILESYSPATH, &path_chars))) {
set_folder_path_ = path_chars;
} else {
set_folder_path_ = L"";
}

return FileDialogController::SetFolder(folder);
}

HRESULT TestFileDialogController::SetFileTypes(UINT count,
COMDLG_FILTERSPEC* filters) {
filter_groups_.clear();
Expand Down Expand Up @@ -56,7 +67,11 @@ HRESULT TestFileDialogController::GetResults(
return S_OK;
}

std::wstring TestFileDialogController::GetDefaultFolderPath() const {
std::wstring TestFileDialogController::GetSetFolderPath() const {
return set_folder_path_;
}

std::wstring TestFileDialogController::GetDialogFolderPath() const {
IShellItemPtr item;
if (!SUCCEEDED(dialog_->GetFolder(&item))) {
return L"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,22 @@ class TestFileDialogController : public FileDialogController {
~TestFileDialogController();

// FileDialogController:
HRESULT SetFolder(IShellItem* folder) override;
HRESULT SetFileTypes(UINT count, COMDLG_FILTERSPEC* filters) override;
HRESULT SetOkButtonLabel(const wchar_t* text) override;
HRESULT Show(HWND parent) override;
HRESULT GetResult(IShellItem** out_item) const override;
HRESULT GetResults(IShellItemArray** out_items) const override;

// Accessors for validating IFileDialogController setter calls.
std::wstring GetDefaultFolderPath() const;
// Gets the folder path set by FileDialogController::SetFolder.
//
// This exists because there are multiple ways that the value returned by
// GetDialogFolderPath can be changed, so this allows specifically validating
// calls to SetFolder.
std::wstring GetSetFolderPath() const;
// Gets dialog folder path by calling IFileDialog::GetFolder.
std::wstring GetDialogFolderPath() const;
std::wstring GetFileName() const;
const std::vector<DialogFilter>& GetFileTypes() const;
std::wstring GetOkButtonLabel() const;
Expand All @@ -70,6 +78,7 @@ class TestFileDialogController : public FileDialogController {

// The last set values, for IFileDialog properties that have setters but no
// corresponding getters.
std::wstring set_folder_path_;
std::wstring ok_button_label_;
std::vector<DialogFilter> filter_groups_;
};
Expand Down

0 comments on commit a42835a

Please sign in to comment.