|
9 | 9 |
|
10 | 10 | namespace winrt::Microsoft::Storage::Pickers::implementation
|
11 | 11 | {
|
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 | + } |
153 | 149 |
|
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 | + } |
170 | 152 | }
|
0 commit comments