Skip to content

Commit

Permalink
Merge codes from branch 'user/xianghong/pickers-prototype' at \protot…
Browse files Browse the repository at this point in the history
…ype-workingdir\Microsoft.Storage.Pickers

Squashed commit of the following:

commit 4ce840f
Author: Dinah Gao <x.kic8462852@gmail.com>
Date:   Wed Dec 11 15:00:41 2024 +0800

    add one telemetry

commit bbf72f8
Author: Dinah Gao <116714259+DinahK-2SO@users.noreply.github.com>
Date:   Tue Dec 10 11:01:23 2024 +0800

    Microsoft.Windows.Storage.Pickers.FileOpenPicker.PickSingleFileAsync (#4945)

    Creating new API Microsoft.Windows.Storage.Pickers.FileOpenPicker.PickSingleFileAsync

commit ff3f2a8
Author: Xiang Hong <hx.hongxiang@gmail.com>
Date:   Mon Dec 9 18:14:16 2024 +0800

    fix: fix project configuration and add certificate notes to README.md

commit e11c8e6
Author: Xiang Hong (from Dev Box) <xianghong@microsoft.com>
Date:   Sun Dec 8 20:54:23 2024 +0800

    update readme for folder description

commit c6966f3
Author: Xiang Hong (from Dev Box) <xianghong@microsoft.com>
Date:   Sun Dec 8 20:08:46 2024 +0800

    add a simple READEME

commit c17750f
Author: Xiang Hong (from Dev Box) <xianghong@microsoft.com>
Date:   Sun Dec 8 11:27:04 2024 +0800

    feature: add basic implementations for SavePickers FileExtension/SuggestedSaveFile/Name functionalities

commit 3be9dc1
Author: Xiang Hong (from Dev Box) <xianghong@microsoft.com>
Date:   Sun Dec 8 10:35:59 2024 +0800

    clean: code clean on naming styles etc

commit 9fcc249
Author: Xiang Hong (from Dev Box) <xianghong@microsoft.com>
Date:   Sat Dec 7 22:22:21 2024 +0800

    refactor: use WinRT MD5 hash string to Guid

commit 756bafa
Author: Xiang Hong (from Dev Box) <xianghong@microsoft.com>
Date:   Sat Dec 7 22:02:55 2024 +0800

    fix: fix file type filter issues and refactor to use PickParameters

commit 8dea72a
Author: Xiang Hong (from Dev Box) <xianghong@microsoft.com>
Date:   Sat Dec 7 19:46:45 2024 +0800

    feature: add COM FileSavePicker basic implementation

commit 37a5a75
Author: Xiang Hong <hx.hongxiang@gmail.com>
Date:   Sat Dec 7 17:13:55 2024 +0800

    add save file dialog usage in demo app

commit fc9a03a
Author: Xiang Hong <hx.hongxiang@gmail.com>
Date:   Sat Dec 7 12:06:47 2024 +0800

    feature: add folder functionalities to demo test app

commit 34556bd
Author: Xiang Hong <hx.hongxiang@gmail.com>
Date:   Fri Dec 6 22:51:55 2024 +0800

    feature: add COM based picker prototype basic implementation
  • Loading branch information
DinahK-2SO committed Dec 12, 2024
1 parent 7a282f2 commit e6997b5
Show file tree
Hide file tree
Showing 16 changed files with 1,096 additions and 0 deletions.
154 changes: 154 additions & 0 deletions prototype-workingdir/Microsoft.Storage.Pickers/FileOpenPicker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include "pch.h"
#include "FileOpenPicker.h"
#include "FileOpenPicker.g.cpp"
#include <windows.h>
#include <shobjidl.h>
#include <shobjidl_core.h>
#include <winrt/Microsoft.UI.Interop.h>
#include "PickerCommon.h"

namespace winrt::Microsoft::Storage::Pickers::implementation
{
FileOpenPicker::FileOpenPicker(winrt::Microsoft::UI::WindowId const& windowId)
: m_windowId(windowId)
{
}

winrt::Microsoft::Storage::Pickers::PickerViewMode FileOpenPicker::ViewMode()
{
return m_viewMode;
}
void FileOpenPicker::ViewMode(winrt::Microsoft::Storage::Pickers::PickerViewMode const& value)
{
m_viewMode = value;
}
hstring FileOpenPicker::SettingsIdentifier()
{
return m_settingsIdentifier;
}
void FileOpenPicker::SettingsIdentifier(hstring const& value)
{
m_settingsIdentifier = value;
}
winrt::Microsoft::Storage::Pickers::PickerLocationId FileOpenPicker::SuggestedStartLocation()
{
return m_suggestedStartLocation;
}
void FileOpenPicker::SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value)
{
m_suggestedStartLocation = value;
}
winrt::hstring FileOpenPicker::CommitButtonText()
{
return m_commitButtonText;
}
void FileOpenPicker::CommitButtonText(winrt::hstring const& value)
{
m_commitButtonText = value;
}
winrt::Windows::Foundation::Collections::IVector<hstring> FileOpenPicker::FileTypeFilter()
{
return m_fileTypeFilter;
}

void FileOpenPicker::CaptureParameters(PickerCommon::PickerParameters& parameters)
{
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
parameters.CommitButtonText = m_commitButtonText;
parameters.SettingsIdentifierId = m_settingsIdentifier;
parameters.PickerLocationId = m_suggestedStartLocation;
parameters.FileTypeFilterPara = PickerCommon::CaptureFilterSpec(parameters.FileTypeFilterData, m_fileTypeFilter.GetView());
}

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> FileOpenPicker::PickSingleFileAsync()
{
PickerCommon::PickerParameters parameters{};

CaptureParameters(parameters);

co_await winrt::resume_background();

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

// TODO: should we initialize COM?
// wil::com_initialize_ex initializeCom{ COINIT_APARTMENTTHREADED };

auto dialog = create_instance<IFileOpenDialog>(CLSID_FileOpenDialog, CONTEXT_ALL);

parameters.ConfigureDialog(dialog);

{
auto hr = dialog->Show(parameters.HWnd);
if (FAILED(hr) || cancellationToken())
{
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;
}

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVectorView<winrt::Windows::Storage::StorageFile>> FileOpenPicker::PickMultipleFilesAsync()
{
PickerCommon::PickerParameters parameters{};

CaptureParameters(parameters);

co_await winrt::resume_background();

winrt::Windows::Foundation::Collections::IVector<winrt::Windows::Storage::StorageFile> results{ winrt::single_threaded_vector<winrt::Windows::Storage::StorageFile>() };

auto cancellationToken = co_await winrt::get_cancellation_token();
if (cancellationToken())
{
co_return results.GetView();
}

auto dialog = create_instance<IFileOpenDialog>(CLSID_FileOpenDialog, CONTEXT_ALL);

parameters.ConfigureDialog(dialog);

check_hresult(dialog->SetOptions(FOS_ALLOWMULTISELECT));

{
auto hr = dialog->Show(parameters.HWnd);
if (FAILED(hr) || cancellationToken())
{
co_return results.GetView();
}
}

winrt::com_ptr<IShellItemArray> shellItems{};
check_hresult(dialog->GetResults(shellItems.put()));

DWORD itemCount = 0;
check_hresult(shellItems->GetCount(&itemCount));

winrt::com_ptr<IShellItem> shellItem{};
for (DWORD i = 0; i < itemCount; i++)
{
check_hresult(shellItems->GetItemAt(i, shellItem.put()));
auto file = co_await PickerCommon::CreateStorageFileFromShellItem(shellItem);
results.Append(file);
}

if (cancellationToken())
{
results.Clear();
co_return results.GetView();
}
co_return results.GetView();
}
}
44 changes: 44 additions & 0 deletions prototype-workingdir/Microsoft.Storage.Pickers/FileOpenPicker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
#include "FileOpenPicker.g.h"
#include "PickerCommon.h"

namespace winrt::Microsoft::Storage::Pickers::implementation
{
struct FileOpenPicker : FileOpenPickerT<FileOpenPicker>
{
FileOpenPicker(winrt::Microsoft::UI::WindowId const& windowId);

winrt::Microsoft::Storage::Pickers::PickerViewMode ViewMode();
void ViewMode(winrt::Microsoft::Storage::Pickers::PickerViewMode const& value);

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

winrt::Microsoft::Storage::Pickers::PickerLocationId SuggestedStartLocation();
void SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value);

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

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

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> PickSingleFileAsync();
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVectorView<winrt::Windows::Storage::StorageFile>> PickMultipleFilesAsync();

private:
winrt::Microsoft::UI::WindowId m_windowId{};
PickerViewMode m_viewMode{ PickerViewMode::List };
winrt::hstring m_settingsIdentifier{};
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
winrt::hstring m_commitButtonText{};
winrt::Windows::Foundation::Collections::IVector<hstring> m_fileTypeFilter{ winrt::single_threaded_vector<hstring>() };

void CaptureParameters(PickerCommon::PickerParameters& parameters);
};
}
namespace winrt::Microsoft::Storage::Pickers::factory_implementation
{
struct FileOpenPicker : FileOpenPickerT<FileOpenPicker, implementation::FileOpenPicker>
{
};
}
138 changes: 138 additions & 0 deletions prototype-workingdir/Microsoft.Storage.Pickers/FileSavePicker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "pch.h"
#include "FileSavePicker.h"
#include "FileSavePicker.g.cpp"
#include <windows.h>
#include <shobjidl.h>
#include <shobjidl_core.h>
#include <KnownFolders.h>
#include <wil/cppwinrt.h>
#include <wil/com.h>
#include <wil/resource.h>
#include <Microsoft.Ui.Xaml.Window.h>
#include <winrt/Microsoft.UI.Interop.h>
#include <winrt/Windows.Foundation.Collections.h>
#include "PickerCommon.h"

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_suggestedStartLocation;
}
void FileSavePicker::SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value)
{
m_suggestedStartLocation = 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;
}


void FileSavePicker::CaptureParameters(PickerCommon::PickerParameters& parameters)
{
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
parameters.CommitButtonText = m_commitButtonText;
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();
if (cancellationToken())
{
co_return nullptr;
}

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))
{
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;
}
}
53 changes: 53 additions & 0 deletions prototype-workingdir/Microsoft.Storage.Pickers/FileSavePicker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#include "FileSavePicker.g.h"
#include "PickerCommon.h"

namespace winrt::Microsoft::Storage::Pickers::implementation
{
struct FileSavePickerParameters;

struct FileSavePicker : FileSavePickerT<FileSavePicker>
{
FileSavePicker(winrt::Microsoft::UI::WindowId const& windowId);

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

winrt::Microsoft::Storage::Pickers::PickerLocationId SuggestedStartLocation();
void SuggestedStartLocation(winrt::Microsoft::Storage::Pickers::PickerLocationId const& value);

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

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

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

winrt::Windows::Storage::StorageFile SuggestedSaveFile();
void SuggestedSaveFile(winrt::Windows::Storage::StorageFile const& value);

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

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> PickSaveFileAsync();

private:
winrt::Microsoft::UI::WindowId m_windowId{};
hstring m_settingsIdentifier{};
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
hstring m_commitButtonText{};
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> m_fileTypeChoices{ winrt::single_threaded_map<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>() };
hstring m_defaultFileExtension{};
winrt::Windows::Storage::StorageFile m_suggestedSaveFile{ nullptr };
hstring m_suggestedFileName{};

void CaptureParameters(PickerCommon::PickerParameters& parameters);
};
}
namespace winrt::Microsoft::Storage::Pickers::factory_implementation
{
struct FileSavePicker : FileSavePickerT<FileSavePicker, implementation::FileSavePicker>
{
};
}
Loading

0 comments on commit e6997b5

Please sign in to comment.