Skip to content

Commit 756bafa

Browse files
author
Xiang Hong (from Dev Box)
committed
fix: fix file type filter issues and refactor to use PickParameters
1 parent 8dea72a commit 756bafa

File tree

10 files changed

+408
-377
lines changed

10 files changed

+408
-377
lines changed

prototype-workingdir/Microsoft.Storage.Pickers/FileOpenPicker.cpp

Lines changed: 139 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -9,162 +9,144 @@
99

1010
namespace winrt::Microsoft::Storage::Pickers::implementation
1111
{
12-
FileOpenPicker::FileOpenPicker(winrt::Microsoft::UI::WindowId const& windowId)
13-
: m_windowId(windowId)
14-
{
15-
}
16-
17-
winrt::Microsoft::Storage::Pickers::PickerViewMode FileOpenPicker::ViewMode()
18-
{
19-
return m_ViewMode;
20-
}
21-
void FileOpenPicker::ViewMode(winrt::Microsoft::Storage::Pickers::PickerViewMode const& value)
22-
{
23-
m_ViewMode = value;
24-
}
25-
hstring FileOpenPicker::SettingsIdentifier()
26-
{
27-
return m_SettingsIdentifier;
28-
}
29-
void FileOpenPicker::SettingsIdentifier(hstring const& value)
30-
{
31-
m_SettingsIdentifier = value;
32-
}
33-
winrt::Microsoft::Storage::Pickers::PickerLocationId FileOpenPicker::SuggestedStartLocation()
34-
{
35-
return m_PickerLocationId;
36-
}
37-
void FileOpenPicker::SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value)
38-
{
39-
m_PickerLocationId = value;
40-
}
41-
winrt::hstring FileOpenPicker::CommitButtonText()
42-
{
43-
return m_commitButtonText;
44-
}
45-
void FileOpenPicker::CommitButtonText(winrt::hstring const& value)
46-
{
47-
m_commitButtonText = value;
48-
}
49-
winrt::Windows::Foundation::Collections::IVector<hstring> FileOpenPicker::FileTypeFilter()
50-
{
51-
return m_fileTypeFilter;
52-
}
53-
54-
struct FileOpenPickerParameters {
55-
winrt::hstring CommitButtonText;
56-
winrt::hstring SettingsIdentifierId;
57-
PickerLocationId PickerLocationId;
58-
std::vector<winrt::hstring> FileTypeFilterData{};
59-
std::vector<COMDLG_FILTERSPEC> FileTypeFilterPara{};
60-
HWND HWnd;
61-
62-
void ConfigureDialog(winrt::com_ptr<IFileOpenDialog> dialog) {
63-
PickerCommon::ConfigureDialogCommon(dialog, CommitButtonText, SettingsIdentifierId, PickerLocationId);
64-
check_hresult(dialog->SetFileTypes(FileTypeFilterPara.size(), FileTypeFilterPara.data()));
65-
}
66-
};
67-
68-
void FileOpenPicker::CaptureParameters(FileOpenPickerParameters& parameters) {
69-
parameters.CommitButtonText = m_commitButtonText;
70-
parameters.SettingsIdentifierId = m_SettingsIdentifier;
71-
parameters.PickerLocationId = m_PickerLocationId;
72-
for (size_t i = 0; i < m_fileTypeFilter.Size(); i++)
73-
{
74-
parameters.FileTypeFilterData.push_back(m_fileTypeFilter.GetAt(i));
75-
parameters.FileTypeFilterPara.push_back({ L"", parameters.FileTypeFilterData[i].c_str() });
76-
}
77-
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
78-
}
79-
80-
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> FileOpenPicker::PickSingleFileAsync()
81-
{
82-
FileOpenPickerParameters parameters{};
83-
84-
CaptureParameters(parameters);
85-
86-
co_await winrt::resume_background();
87-
88-
auto cancellationToken = co_await winrt::get_cancellation_token();
89-
if (cancellationToken())
90-
{
91-
co_return nullptr;
92-
}
93-
94-
// TODO: should we initialize COM?
95-
// wil::com_initialize_ex initializeCom{ COINIT_APARTMENTTHREADED };
96-
97-
auto dialog = create_instance<IFileOpenDialog>(CLSID_FileOpenDialog, CONTEXT_ALL);
98-
99-
parameters.ConfigureDialog(dialog);
100-
101-
auto hr = dialog->Show(parameters.HWnd);
102-
if (FAILED(hr) || cancellationToken())
103-
{
104-
co_return nullptr;
105-
}
106-
107-
winrt::com_ptr<IShellItem> shellItem{};
108-
check_hresult(dialog->GetResult(shellItem.put()));
109-
110-
auto& file = co_await PickerCommon::CreateStorageFileFromShellItem(shellItem);
111-
if (cancellationToken())
112-
{
113-
co_return nullptr;
114-
}
115-
116-
co_return file;
117-
}
118-
119-
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVectorView<winrt::Windows::Storage::StorageFile>> FileOpenPicker::PickMultipleFilesAsync()
120-
{
121-
FileOpenPickerParameters parameters{};
122-
123-
CaptureParameters(parameters);
124-
125-
co_await winrt::resume_background();
126-
127-
winrt::Windows::Foundation::Collections::IVector<winrt::Windows::Storage::StorageFile> results{ winrt::single_threaded_vector<winrt::Windows::Storage::StorageFile>() };
128-
129-
auto cancellationToken = co_await winrt::get_cancellation_token();
130-
if (cancellationToken())
131-
{
132-
co_return results.GetView();
133-
}
134-
135-
auto dialog = create_instance<IFileOpenDialog>(CLSID_FileOpenDialog, CONTEXT_ALL);
136-
137-
parameters.ConfigureDialog(dialog);
138-
139-
auto pickerOptions = FOS_ALLOWMULTISELECT;
140-
check_hresult(dialog->SetOptions(pickerOptions));
141-
142-
auto hr = dialog->Show(parameters.HWnd);
143-
if (FAILED(hr) || cancellationToken())
144-
{
145-
co_return results.GetView();
146-
}
147-
148-
winrt::com_ptr<IShellItemArray> shellItems{};
149-
check_hresult(dialog->GetResults(shellItems.put()));
150-
151-
DWORD itemCount = 0;
152-
check_hresult(shellItems->GetCount(&itemCount));
12+
FileOpenPicker::FileOpenPicker(winrt::Microsoft::UI::WindowId const& windowId)
13+
: m_windowId(windowId)
14+
{
15+
}
16+
17+
winrt::Microsoft::Storage::Pickers::PickerViewMode FileOpenPicker::ViewMode()
18+
{
19+
return m_ViewMode;
20+
}
21+
void FileOpenPicker::ViewMode(winrt::Microsoft::Storage::Pickers::PickerViewMode const& value)
22+
{
23+
m_ViewMode = value;
24+
}
25+
hstring FileOpenPicker::SettingsIdentifier()
26+
{
27+
return m_SettingsIdentifier;
28+
}
29+
void FileOpenPicker::SettingsIdentifier(hstring const& value)
30+
{
31+
m_SettingsIdentifier = value;
32+
}
33+
winrt::Microsoft::Storage::Pickers::PickerLocationId FileOpenPicker::SuggestedStartLocation()
34+
{
35+
return m_PickerLocationId;
36+
}
37+
void FileOpenPicker::SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value)
38+
{
39+
m_PickerLocationId = value;
40+
}
41+
winrt::hstring FileOpenPicker::CommitButtonText()
42+
{
43+
return m_commitButtonText;
44+
}
45+
void FileOpenPicker::CommitButtonText(winrt::hstring const& value)
46+
{
47+
m_commitButtonText = value;
48+
}
49+
winrt::Windows::Foundation::Collections::IVector<hstring> FileOpenPicker::FileTypeFilter()
50+
{
51+
return m_fileTypeFilter;
52+
}
53+
54+
void FileOpenPicker::CaptureParameters(PickerCommon::PickerParameters& parameters)
55+
{
56+
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
57+
parameters.CommitButtonText = m_commitButtonText;
58+
parameters.SettingsIdentifierId = m_SettingsIdentifier;
59+
parameters.PickerLocationId = m_PickerLocationId;
60+
parameters.FileTypeFilterPara = PickerCommon::CaptureFilterSpec(parameters.FileTypeFilterData, m_fileTypeFilter.GetView());
61+
}
62+
63+
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> FileOpenPicker::PickSingleFileAsync()
64+
{
65+
PickerCommon::PickerParameters parameters{};
66+
67+
CaptureParameters(parameters);
68+
69+
co_await winrt::resume_background();
70+
71+
auto cancellationToken = co_await winrt::get_cancellation_token();
72+
if (cancellationToken())
73+
{
74+
co_return nullptr;
75+
}
76+
77+
// TODO: should we initialize COM?
78+
// wil::com_initialize_ex initializeCom{ COINIT_APARTMENTTHREADED };
79+
80+
auto dialog = create_instance<IFileOpenDialog>(CLSID_FileOpenDialog, CONTEXT_ALL);
81+
82+
parameters.ConfigureDialog(dialog);
83+
84+
auto hr = dialog->Show(parameters.HWnd);
85+
if (FAILED(hr) || cancellationToken())
86+
{
87+
co_return nullptr;
88+
}
89+
90+
winrt::com_ptr<IShellItem> shellItem{};
91+
check_hresult(dialog->GetResult(shellItem.put()));
92+
93+
auto file = co_await PickerCommon::CreateStorageFileFromShellItem(shellItem);
94+
if (cancellationToken())
95+
{
96+
co_return nullptr;
97+
}
98+
99+
co_return file;
100+
}
101+
102+
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVectorView<winrt::Windows::Storage::StorageFile>> FileOpenPicker::PickMultipleFilesAsync()
103+
{
104+
PickerCommon::PickerParameters parameters{};
105+
106+
CaptureParameters(parameters);
107+
108+
co_await winrt::resume_background();
109+
110+
winrt::Windows::Foundation::Collections::IVector<winrt::Windows::Storage::StorageFile> results{ winrt::single_threaded_vector<winrt::Windows::Storage::StorageFile>() };
111+
112+
auto cancellationToken = co_await winrt::get_cancellation_token();
113+
if (cancellationToken())
114+
{
115+
co_return results.GetView();
116+
}
117+
118+
auto dialog = create_instance<IFileOpenDialog>(CLSID_FileOpenDialog, CONTEXT_ALL);
119+
120+
parameters.ConfigureDialog(dialog);
121+
122+
check_hresult(dialog->SetOptions(FOS_ALLOWMULTISELECT));
123+
124+
auto hr = dialog->Show(parameters.HWnd);
125+
if (FAILED(hr) || cancellationToken())
126+
{
127+
co_return results.GetView();
128+
}
129+
130+
winrt::com_ptr<IShellItemArray> shellItems{};
131+
check_hresult(dialog->GetResults(shellItems.put()));
132+
133+
DWORD itemCount = 0;
134+
check_hresult(shellItems->GetCount(&itemCount));
135+
136+
winrt::com_ptr<IShellItem> shellItem{};
137+
for (DWORD i = 0; i < itemCount; i++)
138+
{
139+
check_hresult(shellItems->GetItemAt(i, shellItem.put()));
140+
auto file = co_await PickerCommon::CreateStorageFileFromShellItem(shellItem);
141+
results.Append(file);
142+
}
143+
144+
if (cancellationToken())
145+
{
146+
results.Clear();
147+
co_return results.GetView();
148+
}
153149

154-
winrt::com_ptr<IShellItem> shellItem{};
155-
for (auto i = 0; i < itemCount; i++) {
156-
check_hresult(shellItems->GetItemAt(i, shellItem.put()));
157-
auto& file = co_await PickerCommon::CreateStorageFileFromShellItem(shellItem);
158-
results.Append(file);
159-
}
160-
161-
162-
if (cancellationToken())
163-
{
164-
results.Clear();
165-
co_return results.GetView();
166-
}
167-
168-
co_return results.GetView();
169-
}
150+
co_return results.GetView();
151+
}
170152
}
Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
#pragma once
22
#include "FileOpenPicker.g.h"
3+
#include "PickerCommon.h"
34

45
namespace winrt::Microsoft::Storage::Pickers::implementation
56
{
6-
struct FileOpenPickerParameters;
7+
struct FileOpenPicker : FileOpenPickerT<FileOpenPicker>
8+
{
9+
FileOpenPicker(winrt::Microsoft::UI::WindowId const& windowId);
710

8-
struct FileOpenPicker : FileOpenPickerT<FileOpenPicker>
9-
{
10-
FileOpenPicker(winrt::Microsoft::UI::WindowId const& windowId);
11+
winrt::Microsoft::Storage::Pickers::PickerViewMode ViewMode();
12+
void ViewMode(winrt::Microsoft::Storage::Pickers::PickerViewMode const& value);
13+
hstring SettingsIdentifier();
14+
void SettingsIdentifier(hstring const& value);
15+
winrt::Microsoft::Storage::Pickers::PickerLocationId SuggestedStartLocation();
16+
void SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value);
17+
winrt::hstring CommitButtonText();
18+
void CommitButtonText(winrt::hstring const& value);
1119

12-
winrt::Microsoft::Storage::Pickers::PickerViewMode ViewMode();
13-
void ViewMode(winrt::Microsoft::Storage::Pickers::PickerViewMode const& value);
14-
hstring SettingsIdentifier();
15-
void SettingsIdentifier(hstring const& value);
16-
winrt::Microsoft::Storage::Pickers::PickerLocationId SuggestedStartLocation();
17-
void SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value);
18-
winrt::hstring CommitButtonText();
19-
void CommitButtonText(winrt::hstring const& value);
20+
winrt::Windows::Foundation::Collections::IVector<hstring> FileTypeFilter();
2021

21-
winrt::Windows::Foundation::Collections::IVector<hstring> FileTypeFilter();
22+
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> PickSingleFileAsync();
23+
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVectorView<winrt::Windows::Storage::StorageFile>> PickMultipleFilesAsync();
2224

23-
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> PickSingleFileAsync();
24-
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVectorView<winrt::Windows::Storage::StorageFile>> PickMultipleFilesAsync();
25+
private:
26+
winrt::Microsoft::UI::WindowId m_windowId{};
27+
winrt::hstring m_SettingsIdentifier{};
28+
PickerLocationId m_PickerLocationId{ PickerLocationId::Unspecified };
29+
winrt::hstring m_commitButtonText{};
2530

26-
private:
27-
winrt::Microsoft::UI::WindowId m_windowId{};
28-
winrt::hstring m_SettingsIdentifier{};
29-
PickerLocationId m_PickerLocationId{ PickerLocationId::Unspecified };
30-
winrt::hstring m_commitButtonText{};
31+
PickerViewMode m_ViewMode{ PickerViewMode::List };
3132

32-
PickerViewMode m_ViewMode{ PickerViewMode::List };
33+
winrt::Windows::Foundation::Collections::IVector<hstring> m_fileTypeFilter{ winrt::single_threaded_vector<hstring>() };
3334

34-
winrt::Windows::Foundation::Collections::IVector<hstring> m_fileTypeFilter{ winrt::single_threaded_vector<hstring>() };
35-
36-
void CaptureParameters(FileOpenPickerParameters& parameters);
37-
};
35+
void CaptureParameters(PickerCommon::PickerParameters& parameters);
36+
};
3837
}
3938
namespace winrt::Microsoft::Storage::Pickers::factory_implementation
4039
{
41-
struct FileOpenPicker : FileOpenPickerT<FileOpenPicker, implementation::FileOpenPicker>
42-
{
43-
};
40+
struct FileOpenPicker : FileOpenPickerT<FileOpenPicker, implementation::FileOpenPicker>
41+
{
42+
};
4443
}

0 commit comments

Comments
 (0)