Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions dev/Common/TerminalVelocityFeatures-StoragePickers2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
<!-- Enabled variants -->
<feature>
<name>Feature_StoragePickers2</name>
<description>New functionalities in StoragePickers for the WindowsAppRuntime: SuggestedDefaultFolder, FileTypeChoices</description>
<description>New functionalities in Microsoft.Windows.Storage.Pickers in WindowsAppSDK 2.0</description>
<state>AlwaysEnabled</state>
<alwaysDisabledChannelTokens>
<channelToken>Preview</channelToken>
<channelToken>Stable</channelToken>
</alwaysDisabledChannelTokens>
</feature>
</features>
64 changes: 40 additions & 24 deletions dev/Interop/StoragePickers/FileOpenPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
FileOpenPicker::FileOpenPicker(winrt::Microsoft::UI::WindowId const& windowId)
: m_windowId(windowId)
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers::IsEnabled());
}

winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode FileOpenPicker::ViewMode()
Expand Down Expand Up @@ -50,34 +49,56 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_commitButtonText = value;
}
winrt::hstring FileOpenPicker::Title()
{
return m_title;
}
void FileOpenPicker::Title(winrt::hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_title = value;
}
winrt::hstring FileOpenPicker::SettingsIdentifier()
{
return m_settingsIdentifier;
}
void FileOpenPicker::SettingsIdentifier(winrt::hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_settingsIdentifier = value;
}
winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::Windows::Foundation::Collections::IVector<winrt::hstring>> FileOpenPicker::FileTypeChoices()
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers2::IsEnabled());
return m_fileTypeChoices;
}
winrt::Windows::Foundation::Collections::IVector<winrt::hstring> FileOpenPicker::FileTypeFilter()
{
return m_fileTypeFilter;
}
int FileOpenPicker::InitialFileTypeIndex()
{
return m_initialFileTypeIndex;
}
void FileOpenPicker::InitialFileTypeIndex(int value)
{
PickerCommon::ValidateInitialFileTypeIndex(value);
m_initialFileTypeIndex = value;
}
winrt::hstring FileOpenPicker::SuggestedFolder()
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers2::IsEnabled());
return m_suggestedFolder;
}
void FileOpenPicker::SuggestedFolder(winrt::hstring const& value)
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers2::IsEnabled());
PickerCommon::ValidateFolderPath(value, "SuggestedFolder");
m_suggestedFolder = value;
}
winrt::hstring FileOpenPicker::SuggestedStartFolder()
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers2::IsEnabled());
return m_suggestedStartFolder;
}
void FileOpenPicker::SuggestedStartFolder(winrt::hstring const& value)
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers2::IsEnabled());
PickerCommon::ValidateFolderPath(value, "SuggestedStartFolder");
m_suggestedStartFolder = value;
}
Expand All @@ -86,15 +107,18 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
parameters.CommitButtonText = m_commitButtonText;
parameters.Title = m_title;
parameters.SettingsIdentifier = m_settingsIdentifier;
parameters.SuggestedFolder = m_suggestedFolder;
parameters.SuggestedStartLocation = m_suggestedStartLocation;
parameters.SuggestedStartFolder = m_suggestedStartFolder;
parameters.CaptureFilterSpecData(m_fileTypeFilter.GetView(), m_fileTypeChoices.GetView());
// Note: the InitialFileTypeIndex will be decided while capturing the filter spec data.
parameters.CaptureFilterSpecData(m_fileTypeFilter.GetView(), m_fileTypeChoices.GetView(), m_initialFileTypeIndex);
}

winrt::Windows::Foundation::IAsyncOperation<winrt::Microsoft::Windows::Storage::Pickers::PickFileResult> FileOpenPicker::PickSingleFileAsync()
{
// TODO: remove get strong reference when telementry is safe stop
// Keep a strong ref so m_telemetryHelper (a member of the picker) stays valid after co_awaits.
auto lifetime{ get_strong() };

auto logTelemetry{ StoragePickersTelemetry::FileOpenPickerPickSingleFile::Start(m_telemetryHelper) };
Expand All @@ -118,13 +142,10 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation

parameters.ConfigureDialog(dialog);

if (FAILED(dialog->Show(parameters.HWnd)) || cancellationToken())
{
auto hr = dialog->Show(parameters.HWnd);
if (FAILED(hr) || cancellationToken())
{
logTelemetry.Stop(m_telemetryHelper, false);
co_return nullptr;
}
logTelemetry.Stop(m_telemetryHelper, false);
co_return nullptr;
}

winrt::com_ptr<IShellItem> shellItem{};
Expand All @@ -145,12 +166,11 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVectorView<winrt::Microsoft::Windows::Storage::Pickers::PickFileResult> > FileOpenPicker::PickMultipleFilesAsync()
{
// TODO: remove get strong reference when telementry is safe stop
// Keep a strong ref so m_telemetryHelper (a member of the picker) stays valid after co_awaits.
auto lifetime{ get_strong() };

auto logTelemetry{ StoragePickersTelemetry::FileOpenPickerPickMultipleFile::Start(m_telemetryHelper) };

// capture parameters to avoid using get strong referece of picker
PickerCommon::PickerParameters parameters{};
parameters.AllFilesText = PickerLocalization::GetStoragePickersLocalizationText(PickerCommon::AllFilesLocalizationKey);

Expand All @@ -176,13 +196,10 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
check_hresult(dialog->GetOptions(&dialogOptions));
check_hresult(dialog->SetOptions(dialogOptions | FOS_ALLOWMULTISELECT));

if (FAILED(dialog->Show(parameters.HWnd)) || cancellationToken())
{
auto hr = dialog->Show(parameters.HWnd);
if (FAILED(hr) || cancellationToken())
{
logTelemetry.Stop(m_telemetryHelper, true, false);
co_return results.GetView();
}
logTelemetry.Stop(m_telemetryHelper, true, false);
co_return results.GetView();
}

winrt::com_ptr<IShellItemArray> shellItems{};
Expand All @@ -206,7 +223,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
results.Clear();
isCancelled = true;
}
auto resultView = results.GetView();

if (results.Size() > 0)
{
Expand All @@ -216,6 +232,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
logTelemetry.Stop(m_telemetryHelper, isCancelled, false);
}
co_return resultView;
co_return results.GetView();
}
}
14 changes: 13 additions & 1 deletion dev/Interop/StoragePickers/FileOpenPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
winrt::hstring CommitButtonText();
void CommitButtonText(winrt::hstring const& value);

winrt::hstring Title();
void Title(winrt::hstring const& value);

winrt::hstring SettingsIdentifier();
void SettingsIdentifier(winrt::hstring const& value);

winrt::Windows::Foundation::Collections::IVector<winrt::hstring> FileTypeFilter();
winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::Windows::Foundation::Collections::IVector<winrt::hstring>> FileTypeChoices();

int InitialFileTypeIndex();
void InitialFileTypeIndex(int value);

winrt::hstring SuggestedFolder();
void SuggestedFolder(winrt::hstring const& value);

Expand All @@ -41,17 +50,20 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
PickerViewMode m_viewMode{ PickerViewMode::List };
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
winrt::hstring m_commitButtonText{};
winrt::hstring m_title{};
winrt::hstring m_settingsIdentifier{};

winrt::Windows::Foundation::Collections::IVector<winrt::hstring> m_fileTypeFilter{ make<FileTypeFilterVector>() };
winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::Windows::Foundation::Collections::IVector<winrt::hstring>> m_fileTypeChoices{
[]()
{
auto map = winrt::make_self<FileTypeChoicesMap>();
map->ForFeature_StoragePickers2 = true;
return map.as<winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::Windows::Foundation::Collections::IVector<winrt::hstring>>>();
}()
};

int m_initialFileTypeIndex{ PickerCommon::DefaultInitialFileTypeIndex };

winrt::hstring m_suggestedFolder{};
winrt::hstring m_suggestedStartFolder{};

Expand Down
61 changes: 46 additions & 15 deletions dev/Interop/StoragePickers/FileSavePicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
FileSavePicker::FileSavePicker(winrt::Microsoft::UI::WindowId const& windowId)
: m_windowId(windowId)
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers::IsEnabled());
}
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FileSavePicker::SuggestedStartLocation()
{
Expand All @@ -46,14 +45,49 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_commitButtonText = value;
}
winrt::hstring FileSavePicker::Title()
{
return m_title;
}
void FileSavePicker::Title(winrt::hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_title = value;
}
winrt::hstring FileSavePicker::SettingsIdentifier()
{
return m_settingsIdentifier;
}
void FileSavePicker::SettingsIdentifier(winrt::hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_settingsIdentifier = value;
}
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> FileSavePicker::FileTypeChoices()
{
return m_fileTypeChoices;
}
int FileSavePicker::InitialFileTypeIndex()
{
return m_initialFileTypeIndex;
}
void FileSavePicker::InitialFileTypeIndex(int value)
{
PickerCommon::ValidateInitialFileTypeIndex(value);
m_initialFileTypeIndex = value;
}
hstring FileSavePicker::DefaultFileExtension()
{
return m_defaultFileExtension;
}
bool FileSavePicker::ShowOverwritePrompt()
{
return m_showOverwritePrompt;
}
void FileSavePicker::ShowOverwritePrompt(bool value)
{
m_showOverwritePrompt = value;
}
void FileSavePicker::DefaultFileExtension(hstring const& value)
{
m_defaultFileExtension = value;
Expand All @@ -70,12 +104,10 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation

hstring FileSavePicker::SuggestedStartFolder()
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers2::IsEnabled());
return m_suggestedStartFolder;
}
void FileSavePicker::SuggestedStartFolder(hstring const& value)
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers2::IsEnabled());
PickerCommon::ValidateFolderPath(value, "SuggestedStartFolder");
m_suggestedStartFolder = value;
}
Expand All @@ -95,18 +127,24 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
parameters.CommitButtonText = m_commitButtonText;
parameters.Title = m_title;
parameters.SettingsIdentifier = m_settingsIdentifier;
parameters.SuggestedFileName = m_suggestedFileName;
parameters.SuggestedFolder = m_suggestedFolder;
parameters.SuggestedStartLocation = m_suggestedStartLocation;
parameters.SuggestedStartFolder = m_suggestedStartFolder;

// Note: the InitialFileTypeIndex will be decided while capturing the filter spec data.
parameters.CaptureFilterSpecData(
winrt::Windows::Foundation::Collections::IVectorView<winrt::hstring>{},
m_fileTypeChoices.GetView());
m_fileTypeChoices.GetView(),
m_initialFileTypeIndex);
parameters.ShowOverwritePrompt = m_showOverwritePrompt;
}

winrt::Windows::Foundation::IAsyncOperation<winrt::Microsoft::Windows::Storage::Pickers::PickFileResult> FileSavePicker::PickSaveFileAsync()
{
// TODO: remove get strong reference when telementry is safe stop
// Keep a strong ref so m_telemetryHelper (a member of the picker) stays valid after co_awaits.
auto lifetime{ get_strong() };

auto logTelemetry{ StoragePickersTelemetry::FileSavePickerPickSingleFile::Start(m_telemetryHelper) };
Expand Down Expand Up @@ -150,13 +188,10 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
check_hresult(dialog->GetOptions(&dialogOptions));
check_hresult(dialog->SetOptions(dialogOptions | FOS_STRICTFILETYPES));

if (FAILED(dialog->Show(parameters.HWnd)))
{
auto hr = dialog->Show(parameters.HWnd);
if (FAILED(hr))
{
logTelemetry.Stop(m_telemetryHelper, false);
co_return nullptr;
}
logTelemetry.Stop(m_telemetryHelper, false);
co_return nullptr;
}

winrt::com_ptr<IShellItem> shellItem{};
Expand All @@ -171,10 +206,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
check_hresult(shellItem->GetDisplayName(SIGDN_NORMALDISPLAY, fileName.put()));
std::wstring fileNameStr(fileName.get());

// Create an empty file if the file doesn't exist,
// If the file already exists, do nothing.
auto [handle, _] = wil::try_open_or_create_file(pathStr.c_str(), GENERIC_WRITE);

if (cancellationToken())
{
logTelemetry.Stop(m_telemetryHelper, false);
Expand Down
16 changes: 16 additions & 0 deletions dev/Interop/StoragePickers/FileSavePicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
hstring CommitButtonText();
void CommitButtonText(hstring const& value);

hstring Title();
void Title(hstring const& value);

hstring SettingsIdentifier();
void SettingsIdentifier(hstring const& value);

winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> FileTypeChoices();

int InitialFileTypeIndex();
void InitialFileTypeIndex(int value);

hstring DefaultFileExtension();
void DefaultFileExtension(hstring const& value);

bool ShowOverwritePrompt();
void ShowOverwritePrompt(bool value);

hstring SuggestedFolder();
void SuggestedFolder(hstring const& value);

Expand All @@ -41,8 +53,12 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
winrt::Microsoft::UI::WindowId m_windowId{};
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
hstring m_commitButtonText{};
hstring m_title{};
hstring m_settingsIdentifier{};
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> m_fileTypeChoices{ make<FileTypeChoicesMap>() };
int m_initialFileTypeIndex{ PickerCommon::DefaultInitialFileTypeIndex };
hstring m_defaultFileExtension{};
bool m_showOverwritePrompt{ true };
hstring m_suggestedFolder{};
hstring m_suggestedStartFolder{};
hstring m_suggestedFileName{};
Expand Down
5 changes: 0 additions & 5 deletions dev/Interop/StoragePickers/FileTypeChoicesMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation

bool FileTypeChoicesMap::Insert(hstring const& key, winrt::Windows::Foundation::Collections::IVector<hstring> const& value)
{
if (ForFeature_StoragePickers2)
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::Pickers::Feature_StoragePickers2::IsEnabled());
}

// Create a new FileTypeFilterVector and copy all values from the input vector
auto validatingVector{ make<FileTypeFilterVector>() };

Expand Down
2 changes: 0 additions & 2 deletions dev/Interop/StoragePickers/FileTypeChoicesMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
FileTypeChoicesMap();

bool ForFeature_StoragePickers2{ false };

// IMap<hstring, IVector<hstring>>
winrt::Windows::Foundation::Collections::IVector<hstring> Lookup(hstring const& key) const;
uint32_t Size() const noexcept;
Expand Down
Loading