Skip to content

Commit d0290e9

Browse files
authored
[stable] Microsoft.Windows.Storage.Pickers APIs (#5649)
This PR implements suggestions raised in the official API review of `Microsoft.Windows.Storage.Pickers` APIs (#5634 ). 1. Removal of `SettingsIdentifier` Property. 2. Replace method `TrySetSuggestedSaveFilePath` with the property's own setter. 3. For FileSavePicker, updated underlying logic to ensure that the file name in `SuggestedSaveFilePath` takes precedence over the `SuggestedFileName`, even if the folder of `SuggestedSaveFilePath` does not exists. This is also the existing behavior of UWP pickers ( the `Windows.Storage.Pickers`) 4. Test Additions and Updates. 5. XML and Feature Flag Cleanup.
1 parent 4b555dc commit d0290e9

12 files changed

+116
-183
lines changed

dev/Common/TerminalVelocityFeatures-StoragePickers.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,5 @@
1212
<name>Feature_StoragePickers</name>
1313
<description>StoragePickers for the WindowsAppRuntime</description>
1414
<state>AlwaysEnabled</state>
15-
<alwaysDisabledChannelTokens>
16-
<channelToken>Preview</channelToken>
17-
<channelToken>Stable</channelToken>
18-
</alwaysDisabledChannelTokens>
1915
</feature>
2016
</features>

dev/Interop/StoragePickers/FileOpenPicker.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3131
PickerCommon::ValidateViewMode(value);
3232
m_viewMode = value;
3333
}
34-
hstring FileOpenPicker::SettingsIdentifier()
35-
{
36-
return m_settingsIdentifier;
37-
}
38-
void FileOpenPicker::SettingsIdentifier(hstring const& value)
39-
{
40-
PickerCommon::ValidateStringNoEmbeddedNulls(value);
41-
m_settingsIdentifier = value;
42-
}
4334
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FileOpenPicker::SuggestedStartLocation()
4435
{
4536
return m_suggestedStartLocation;
@@ -67,7 +58,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
6758
{
6859
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
6960
parameters.CommitButtonText = m_commitButtonText;
70-
parameters.SettingsIdentifierId = m_settingsIdentifier;
7161
parameters.PickerLocationId = m_suggestedStartLocation;
7262
parameters.CaptureFilterSpec(m_fileTypeFilter.GetView());
7363
}

dev/Interop/StoragePickers/FileOpenPicker.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
1717
winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode ViewMode();
1818
void ViewMode(winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const& value);
1919

20-
hstring SettingsIdentifier();
21-
void SettingsIdentifier(hstring const& value);
22-
2320
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId SuggestedStartLocation();
2421
void SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value);
2522

@@ -34,7 +31,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3431
private:
3532
winrt::Microsoft::UI::WindowId m_windowId{};
3633
PickerViewMode m_viewMode{ PickerViewMode::List };
37-
winrt::hstring m_settingsIdentifier{};
3834
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
3935
winrt::hstring m_commitButtonText{};
4036

dev/Interop/StoragePickers/FileSavePicker.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
2727
{
2828
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers::IsEnabled());
2929
}
30-
hstring FileSavePicker::SettingsIdentifier()
31-
{
32-
return m_settingsIdentifier;
33-
}
34-
void FileSavePicker::SettingsIdentifier(hstring const& value)
35-
{
36-
PickerCommon::ValidateStringNoEmbeddedNulls(value);
37-
m_settingsIdentifier = value;
38-
}
3930
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FileSavePicker::SuggestedStartLocation()
4031
{
4132
return m_suggestedStartLocation;
@@ -70,19 +61,10 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
7061
{
7162
return m_suggestedSaveFilePath;
7263
}
73-
74-
bool FileSavePicker::TrySetSuggestedSaveFilePath(hstring const& filePath)
64+
void FileSavePicker::SuggestedSaveFilePath(hstring const& value)
7565
{
76-
auto parseResult = PickerCommon::ParseFolderItemAndFileName(filePath);
77-
winrt::com_ptr<IShellItem> folderItem = parseResult.first;
78-
79-
if (!folderItem)
80-
{
81-
return false;
82-
}
83-
84-
m_suggestedSaveFilePath = filePath;
85-
return true;
66+
PickerCommon::ValidateSuggestedSaveFilePath(value);
67+
m_suggestedSaveFilePath = value;
8668
}
8769

8870
hstring FileSavePicker::SuggestedFileName()
@@ -100,7 +82,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
10082
{
10183
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
10284
parameters.CommitButtonText = m_commitButtonText;
103-
parameters.SettingsIdentifierId = m_settingsIdentifier;
10485
parameters.PickerLocationId = m_suggestedStartLocation;
10586
parameters.SuggestedFileName = m_suggestedFileName;
10687
parameters.SuggestedSaveFilePath = m_suggestedSaveFilePath;

dev/Interop/StoragePickers/FileSavePicker.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
1515
{
1616
FileSavePicker(winrt::Microsoft::UI::WindowId const& windowId);
1717

18-
hstring SettingsIdentifier();
19-
void SettingsIdentifier(hstring const& value);
20-
2118
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId SuggestedStartLocation();
2219
void SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value);
2320

@@ -30,7 +27,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3027
void DefaultFileExtension(hstring const& value);
3128

3229
hstring SuggestedSaveFilePath();
33-
bool TrySetSuggestedSaveFilePath(hstring const& filePath);
30+
void SuggestedSaveFilePath(hstring const& value);
3431

3532
hstring SuggestedFileName();
3633
void SuggestedFileName(hstring const& value);
@@ -39,7 +36,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3936

4037
private:
4138
winrt::Microsoft::UI::WindowId m_windowId{};
42-
hstring m_settingsIdentifier{};
4339
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
4440
hstring m_commitButtonText{};
4541
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> m_fileTypeChoices{ make<FileTypeChoicesMap>() };

dev/Interop/StoragePickers/FolderPicker.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
2929
PickerCommon::ValidateViewMode(value);
3030
m_viewMode = value;
3131
}
32-
hstring FolderPicker::SettingsIdentifier()
33-
{
34-
return m_settingsIdentifier;
35-
}
36-
void FolderPicker::SettingsIdentifier(hstring const& value)
37-
{
38-
PickerCommon::ValidateStringNoEmbeddedNulls(value);
39-
m_settingsIdentifier = value;
40-
}
4132
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FolderPicker::SuggestedStartLocation()
4233
{
4334
return m_suggestedStartLocation;
@@ -61,7 +52,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
6152
{
6253
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
6354
parameters.CommitButtonText = m_commitButtonText;
64-
parameters.SettingsIdentifierId = m_settingsIdentifier;
6555
parameters.PickerLocationId = m_suggestedStartLocation;
6656
}
6757

dev/Interop/StoragePickers/FolderPicker.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
1515
winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode ViewMode();
1616
void ViewMode(winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const& value);
1717

18-
hstring SettingsIdentifier();
19-
void SettingsIdentifier(hstring const& value);
20-
2118
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId SuggestedStartLocation();
2219
void SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value);
2320

@@ -30,7 +27,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3027
winrt::Microsoft::UI::WindowId m_windowId{};
3128

3229
PickerViewMode m_viewMode{ PickerViewMode::List };
33-
hstring m_settingsIdentifier{};
3430
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
3531
hstring m_commitButtonText{};
3632
StoragePickersTelemetryHelper m_telemetryHelper{};

dev/Interop/StoragePickers/Microsoft.Windows.Storage.Pickers.idl

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
// Copyright (c) Microsoft Corporation and Contributors.
22
// Licensed under the MIT License.
33

4-
#include <TerminalVelocityFeatures-StoragePickers.h>
54
namespace Microsoft.Windows.Storage.Pickers
65
{
76
[contractversion(1.8)]
87
apicontract StoragePickersContract {};
98

109
[contract(StoragePickersContract, 1.8)]
11-
[feature(Feature_StoragePickers)]
1210
enum PickerViewMode
1311
{
1412
List,
1513
Thumbnail,
1614
};
1715

1816
[contract(StoragePickersContract, 1.8)]
19-
[feature(Feature_StoragePickers)]
2017
enum PickerLocationId
2118
{
2219
DocumentsLibrary,
@@ -31,20 +28,17 @@ namespace Microsoft.Windows.Storage.Pickers
3128
};
3229

3330
[contract(StoragePickersContract, 1.8)]
34-
[feature(Feature_StoragePickers)]
3531
runtimeclass PickFileResult
3632
{
3733
String Path { get; };
3834
}
3935

4036
[contract(StoragePickersContract, 1.8)]
41-
[feature(Feature_StoragePickers)]
4237
runtimeclass FileOpenPicker
4338
{
4439
FileOpenPicker(Microsoft.UI.WindowId windowId);
4540

4641
Microsoft.Windows.Storage.Pickers.PickerViewMode ViewMode;
47-
String SettingsIdentifier;
4842
Microsoft.Windows.Storage.Pickers.PickerLocationId SuggestedStartLocation;
4943
String CommitButtonText;
5044
Windows.Foundation.Collections.IVector<String> FileTypeFilter{ get; };
@@ -54,39 +48,32 @@ namespace Microsoft.Windows.Storage.Pickers
5448
}
5549

5650
[contract(StoragePickersContract, 1.8)]
57-
[feature(Feature_StoragePickers)]
5851
runtimeclass FileSavePicker
5952
{
6053
FileSavePicker(Microsoft.UI.WindowId windowId);
6154

62-
String SettingsIdentifier;
6355
Microsoft.Windows.Storage.Pickers.PickerLocationId SuggestedStartLocation;
6456
String CommitButtonText;
6557
Windows.Foundation.Collections.IMap<String, Windows.Foundation.Collections.IVector<String> > FileTypeChoices{ get; };
6658
String DefaultFileExtension;
6759
String SuggestedFileName;
68-
69-
String SuggestedSaveFilePath{ get; };
70-
Boolean TrySetSuggestedSaveFilePath(String filePath);
60+
String SuggestedSaveFilePath;
7161

7262
[remote_sync] Windows.Foundation.IAsyncOperation<PickFileResult> PickSaveFileAsync();
7363
}
7464

7565
[contract(StoragePickersContract, 1.8)]
76-
[feature(Feature_StoragePickers)]
7766
runtimeclass PickFolderResult
7867
{
7968
String Path { get; };
8069
}
8170

8271
[contract(StoragePickersContract, 1.8)]
83-
[feature(Feature_StoragePickers)]
8472
runtimeclass FolderPicker
8573
{
8674
FolderPicker(Microsoft.UI.WindowId windowId);
8775

8876
Microsoft.Windows.Storage.Pickers.PickerViewMode ViewMode;
89-
String SettingsIdentifier;
9077
Microsoft.Windows.Storage.Pickers.PickerLocationId SuggestedStartLocation;
9178
String CommitButtonText;
9279

dev/Interop/StoragePickers/PickerCommon.cpp

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,24 @@ namespace PickerCommon {
121121
return { nullptr, L"" };
122122
}
123123

124-
auto folderPath = path.parent_path();
125-
if (folderPath.empty())
126-
{
127-
// If the path does not have a parent, we cannot set folder.
128-
return { nullptr, L"" };
129-
}
124+
auto fileName = path.filename().wstring();
130125

131126
// If the parent folder does not exist or is not a directory, we cannot set folder.
127+
auto folderPath = path.parent_path();
132128
if (!std::filesystem::exists(folderPath) || !std::filesystem::is_directory(folderPath))
133129
{
134-
return { nullptr, L"" };
130+
return { nullptr, fileName };
135131
}
136132

137133
winrt::com_ptr<IShellItem> shellItem;
138134
HRESULT hr = SHCreateItemFromParsingName(folderPath.c_str(), nullptr, IID_PPV_ARGS(shellItem.put()));
139135
if (SUCCEEDED(hr))
140136
{
141-
auto fileName = path.filename().wstring();
142137
return { shellItem, fileName };
143138
}
144139

145-
// If the shellitem cannot be created, we cannot set the folder.
146-
return { nullptr, L""};
140+
// If the we cannot set the folder, we can at least set the file name suggested by developer.
141+
return { nullptr, fileName};
147142
}
148143

149144
void ValidateViewMode(winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const& value)
@@ -234,6 +229,38 @@ namespace PickerCommon {
234229
ValidateStringNoEmbeddedNulls(suggestedFileName);
235230
}
236231

232+
void ValidateSuggestedSaveFilePath(winrt::hstring const& path)
233+
{
234+
if (path.empty())
235+
{
236+
// allow empty path.
237+
return;
238+
}
239+
240+
ValidateStringNoEmbeddedNulls(path);
241+
242+
wil::unique_cotaskmem_ptr<ITEMIDLIST> pidl(SHSimpleIDListFromPath(path.c_str()));
243+
if (!pidl)
244+
{
245+
throw std::invalid_argument("SuggestedSaveFilePath");
246+
}
247+
248+
std::filesystem::path p(path.c_str());
249+
auto folderPath = p.parent_path();
250+
if (folderPath.empty())
251+
{
252+
// If the path does not have a parent, we cannot set folder.
253+
throw std::invalid_argument("SuggestedSaveFilePath");
254+
}
255+
256+
auto fileName = p.filename().wstring();
257+
if (fileName.size() > MAX_PATH)
258+
{
259+
throw winrt::hresult_invalid_argument(
260+
PickerLocalization::GetStoragePickersLocalizationText(MaxSaveFileLengthExceededLocalizationKey));
261+
}
262+
}
263+
237264
winrt::hstring PickerParameters::FormatExtensionWithWildcard(winrt::hstring extension)
238265
{
239266
if (!extension.empty() && extension[0] == L'*')
@@ -359,12 +386,6 @@ namespace PickerCommon {
359386
check_hresult(dialog->SetOkButtonLabel(CommitButtonText.c_str()));
360387
}
361388

362-
if (!IsHStringNullOrEmpty(SettingsIdentifierId))
363-
{
364-
auto guid = HashHStringToGuid(SettingsIdentifierId);
365-
check_hresult(dialog->SetClientGuid(guid));
366-
}
367-
368389
auto defaultFolder = GetKnownFolderFromId(PickerLocationId);
369390
if (defaultFolder != nullptr)
370391
{

dev/Interop/StoragePickers/PickerCommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ namespace PickerCommon {
2828
void ValidateSuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value);
2929
void ValidateSingleFileTypeFilterElement(winrt::hstring const& filter);
3030
void ValidateSuggestedFileName(winrt::hstring const& suggestedFileName);
31+
void ValidateSuggestedSaveFilePath(winrt::hstring const& path);
3132

3233
struct PickerParameters {
3334
HWND HWnd{};
3435
winrt::hstring CommitButtonText;
35-
winrt::hstring SettingsIdentifierId;
3636
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId PickerLocationId;
3737
std::vector<winrt::hstring> FileTypeFilterData{};
3838
std::vector<COMDLG_FILTERSPEC> FileTypeFilterPara{};

0 commit comments

Comments
 (0)