Skip to content

Commit

Permalink
feature: add COM FileSavePicker basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiang Hong (from Dev Box) committed Dec 7, 2024
1 parent 37a5a75 commit 8dea72a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 75 deletions.
173 changes: 105 additions & 68 deletions prototype-workingdir/Microsoft.Storage.Pickers/FileSavePicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,113 @@
namespace winrt::Microsoft::Storage::Pickers::implementation
{

FileSavePicker::FileSavePicker(winrt::Microsoft::UI::WindowId const& windowId)
: m_windowId(windowId)
{
}
hstring FileSavePicker::SettingsIdentifier()
{
return m_SettingsIdentifier;
}
void FileSavePicker::SettingsIdentifier(hstring const& value)
{
m_SettingsIdentifier = value;
}
winrt::Microsoft::Storage::Pickers::PickerLocationId FileSavePicker::SuggestedStartLocation()
{
return m_PickerLocationId;
}
void FileSavePicker::SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value)
{
m_PickerLocationId = value;
}
hstring FileSavePicker::CommitButtonText()
{
return m_commitButtonText;
}
void FileSavePicker::CommitButtonText(hstring const& value)
{
m_commitButtonText = value;
}
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> FileSavePicker::FileTypeChoices()
{
return m_fileTypeChoices;
}
hstring FileSavePicker::DefaultFileExtension()
{
return m_defaultFileExtension;
}
void FileSavePicker::DefaultFileExtension(hstring const& value)
{
m_defaultFileExtension = value;
}
winrt::Windows::Storage::StorageFile FileSavePicker::SuggestedSaveFile()
{
return m_suggestedSaveFile;
}
void FileSavePicker::SuggestedSaveFile(winrt::Windows::Storage::StorageFile const& value)
{
m_suggestedSaveFile = value;
}
hstring FileSavePicker::SuggestedFileName()
{
return m_suggestedFileName;
}
void FileSavePicker::SuggestedFileName(hstring const& value)
{
m_suggestedFileName = value;
}
FileSavePicker::FileSavePicker(winrt::Microsoft::UI::WindowId const& windowId)
: m_windowId(windowId)
{
}
hstring FileSavePicker::SettingsIdentifier()
{
return m_SettingsIdentifier;
}
void FileSavePicker::SettingsIdentifier(hstring const& value)
{
m_SettingsIdentifier = value;
}
winrt::Microsoft::Storage::Pickers::PickerLocationId FileSavePicker::SuggestedStartLocation()
{
return m_PickerLocationId;
}
void FileSavePicker::SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value)
{
m_PickerLocationId = value;
}
hstring FileSavePicker::CommitButtonText()
{
return m_commitButtonText;
}
void FileSavePicker::CommitButtonText(hstring const& value)
{
m_commitButtonText = value;
}
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> FileSavePicker::FileTypeChoices()
{
return m_fileTypeChoices;
}
hstring FileSavePicker::DefaultFileExtension()
{
return m_defaultFileExtension;
}
void FileSavePicker::DefaultFileExtension(hstring const& value)
{
m_defaultFileExtension = value;
}
winrt::Windows::Storage::StorageFile FileSavePicker::SuggestedSaveFile()
{
return m_suggestedSaveFile;
}
void FileSavePicker::SuggestedSaveFile(winrt::Windows::Storage::StorageFile const& value)
{
m_suggestedSaveFile = value;
}
hstring FileSavePicker::SuggestedFileName()
{
return m_suggestedFileName;
}
void FileSavePicker::SuggestedFileName(hstring const& value)
{
m_suggestedFileName = value;
}

struct FileOpenPickerParameters {
HWND HWnd;
winrt::hstring CommitButtonText;
winrt::hstring SettingsIdentifierId;
PickerLocationId PickerLocationId;
struct FileSavePickerParameters {
HWND HWnd;
winrt::hstring CommitButtonText;
winrt::hstring SettingsIdentifierId;
PickerLocationId PickerLocationId;

void ConfigureDialog(winrt::com_ptr<IFileOpenDialog> dialog) {
PickerCommon::ConfigureDialogCommon(dialog, CommitButtonText, SettingsIdentifierId, PickerLocationId);
void ConfigureDialog(winrt::com_ptr<IFileSaveDialog> dialog)
{
PickerCommon::ConfigureDialogCommon(dialog, CommitButtonText, SettingsIdentifierId, PickerLocationId);
}
};

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> FileSavePicker::PickSaveFileAsync()
{
FileSavePickerParameters parameters{};

co_await winrt::resume_background();
auto cancellationToken = co_await winrt::get_cancellation_token();
if (cancellationToken())
{
co_return nullptr;
}
};

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> FileSavePicker::PickSaveFileAsync()
{
throw hresult_not_implemented();
}
auto dialog = create_instance<IFileSaveDialog>(CLSID_FileSaveDialog, CONTEXT_ALL);

parameters.ConfigureDialog(dialog);

// TODO: set filter typse
COMDLG_FILTERSPEC save_filter[1];
save_filter[0].pszName = L"All files";
save_filter[0].pszSpec = L"*.*";
check_hresult(dialog->SetFileTypes(1, save_filter));

{
auto hr = dialog->Show(parameters.HWnd);
if (FAILED(hr))
{
co_return nullptr;
}
}

winrt::com_ptr<IShellItem> shellItem{};
check_hresult(dialog->GetResult(shellItem.put()));

auto& file = co_await PickerCommon::CreateStorageFileFromShellItem(shellItem);
if (cancellationToken())
{
co_return nullptr;
}

co_return file;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace PickerCommon {
using namespace winrt;

void ConfigureDialogCommon(
winrt::com_ptr<IFileOpenDialog> dialog,
winrt::com_ptr<IFileDialog> dialog,
winrt::hstring commitButtonText,
winrt::hstring settingIdentifierId,
winrt::Microsoft::Storage::Pickers::PickerLocationId pickerLocationId
Expand Down
4 changes: 2 additions & 2 deletions prototype-workingdir/Microsoft.Storage.Pickers/PickerCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

namespace PickerCommon {
void ConfigureDialogCommon(
winrt::com_ptr<IFileOpenDialog> dialog,
winrt::com_ptr<IFileDialog> dialog,
winrt::hstring commitButtonText,
winrt::hstring settingIdentifierId,
winrt::Microsoft::Storage::Pickers::PickerLocationId pickerLocationId
);

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> CreateStorageFileFromShellItem(winrt::com_ptr<IShellItem> shellItem);
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFolder> CreateStorageFolderFromShellItem(winrt::com_ptr<IShellItem> shellItem);
}
}
1 change: 1 addition & 0 deletions prototype-workingdir/PickerUsageApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<RadioButton Content="Thumbnail"></RadioButton>
</RadioButtons>
</StackPanel>
<TextBlock>!!! WARN !!! Save picker will overwrite Context in selected file with test data</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Click="SDKClick">Run SDK Picker</Button>
<Button Click="UWPClick">Run UWP Picker</Button>
Expand Down
4 changes: 0 additions & 4 deletions prototype-workingdir/PickerUsageApp/MainWindow.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ using namespace Microsoft::UI::Xaml;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.



namespace winrt::PickerUsageApp::implementation
{
Windows::Storage::Pickers::PickerViewMode Convert(Microsoft::Storage::Pickers::PickerViewMode viewMode)
Expand Down Expand Up @@ -177,8 +175,6 @@ namespace winrt::PickerUsageApp::implementation
co_return L"Folder Picker does not support multi selection";
}
co_return L"no selection";

throw hresult_not_implemented();
}

Windows::Foundation::IAsyncOperation<hstring> MainWindow::OpenFileSDKClick(IInspectable const&, RoutedEventArgs const&)
Expand Down

0 comments on commit 8dea72a

Please sign in to comment.