Skip to content

Commit

Permalink
feature: add basic implementations for SavePickers FileExtension/Sugg…
Browse files Browse the repository at this point in the history
…estedSaveFile/Name functionalities
  • Loading branch information
Xiang Hong (from Dev Box) committed Dec 8, 2024
1 parent 3be9dc1 commit c17750f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
19 changes: 19 additions & 0 deletions prototype-workingdir/Microsoft.Storage.Pickers/FileSavePicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ namespace winrt::Microsoft::Storage::Pickers::implementation
parameters.SettingsIdentifierId = m_settingsIdentifier;
parameters.PickerLocationId = m_suggestedStartLocation;
parameters.FileTypeFilterPara = PickerCommon::CaptureFilterSpec(parameters.FileTypeFilterData, m_fileTypeChoices.GetView());

}

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> FileSavePicker::PickSaveFileAsync()
{
PickerCommon::PickerParameters parameters{};
CaptureParameters(parameters);
auto defaultFileExtension = m_defaultFileExtension;
auto suggestedSaveFile = m_suggestedSaveFile;
auto suggestedFileName = m_suggestedFileName;

co_await winrt::resume_background();
auto cancellationToken = co_await winrt::get_cancellation_token();
Expand All @@ -98,6 +102,21 @@ namespace winrt::Microsoft::Storage::Pickers::implementation
auto dialog = create_instance<IFileSaveDialog>(CLSID_FileSaveDialog, CONTEXT_ALL);
parameters.ConfigureDialog(dialog);

if (!PickerCommon::IsHStringNullOrEmpty(defaultFileExtension))
{
check_hresult(dialog->SetDefaultExtension(defaultFileExtension.c_str()));
}
if (!PickerCommon::IsHStringNullOrEmpty(suggestedFileName))
{
check_hresult(dialog->SetFileName(suggestedFileName.c_str()));
}
if (suggestedSaveFile != nullptr)
{
winrt::com_ptr<IShellItem> shellItem;
check_hresult(SHCreateItemFromParsingName(suggestedSaveFile.Path().c_str(), nullptr, IID_PPV_ARGS(shellItem.put())));
check_hresult(dialog->SetSaveAsItem(shellItem.get()));
}

{
auto hr = dialog->Show(parameters.HWnd);
if (FAILED(hr))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
<ClCompile>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(CoreLibraryDependencies);%(AdditionalDependencies);shell32.lib</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
<ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,4 @@
<ItemGroup>
<Text Include="readme.txt" />
</ItemGroup>
<ItemGroup>
<Natvis Include="$(MSBuildThisFileDirectory)..\..\natvis\wil.natvis" />
</ItemGroup>
</Project>
12 changes: 7 additions & 5 deletions prototype-workingdir/Microsoft.Storage.Pickers/PickerCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
#include <KnownFolders.h>

namespace {
bool IsHStringNullOrEmpty(winrt::hstring value)
{
// TODO: proper handling of null string reference?
return value.empty();
}

GUID HashHStringToGuid(winrt::hstring const& input)
{
Expand Down Expand Up @@ -121,6 +116,13 @@ namespace PickerCommon {

using namespace winrt;

bool IsHStringNullOrEmpty(winrt::hstring value)
{
// TODO: proper handling of null string reference?
return value.empty();
}


// TODO: better way to convert ShellItem a StorageFile without relying on path?.
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> CreateStorageFileFromShellItem(winrt::com_ptr<IShellItem> shellItem)
{
Expand Down
3 changes: 3 additions & 0 deletions prototype-workingdir/Microsoft.Storage.Pickers/PickerCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace PickerCommon {
std::vector<COMDLG_FILTERSPEC> CaptureFilterSpec(std::vector<winrt::hstring>& buffer, winrt::Windows::Foundation::Collections::IVectorView<winrt::hstring> filters);
std::vector<COMDLG_FILTERSPEC> CaptureFilterSpec(std::vector<winrt::hstring>& buffer, winrt::Windows::Foundation::Collections::IMapView<winrt::hstring, winrt::Windows::Foundation::Collections::IVector<winrt::hstring>> filters);

// TODO: remove this if we know proper and null safe empty test for string
bool IsHStringNullOrEmpty(winrt::hstring value);

struct PickerParameters {
HWND HWnd;
winrt::hstring CommitButtonText;
Expand Down

0 comments on commit c17750f

Please sign in to comment.