Skip to content

Commit

Permalink
add save file dialog usage in demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
Hong-Xiang committed Dec 7, 2024
1 parent fc9a03a commit 37a5a75
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 23 deletions.
69 changes: 59 additions & 10 deletions prototype-workingdir/PickerUsageApp/MainWindow.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ namespace winrt::PickerUsageApp::implementation
case 0:
message = co_await OpenFileSDKClick(sender, args);
break;
case 1:
message = co_await SaveFileSDKClick();
break;
case 2:
message = co_await OpenFolderSDKClick();
break;
Expand All @@ -69,6 +72,9 @@ namespace winrt::PickerUsageApp::implementation
case 0:
message = co_await OpenFileUWPClick(sender, args);
break;
case 1:
message = co_await SaveFileUWPClick();
break;
case 2:
message = co_await OpenFolderUWPClick();
break;
Expand All @@ -95,7 +101,7 @@ namespace winrt::PickerUsageApp::implementation
winrt::Windows::Storage::Pickers::FileOpenPicker picker{};

picker.as<::IInitializeWithWindow>()->Initialize(hWnd);
SetPickerOptions<winrt::Windows::Storage::Pickers::FileOpenPicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);
SetOpenPickerOptions<winrt::Windows::Storage::Pickers::FileOpenPicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);
picker.ViewMode(Convert(m_ViewMode));

if (!m_MultipleSelect)
Expand All @@ -119,6 +125,33 @@ namespace winrt::PickerUsageApp::implementation
co_return L"no selection";
}

Windows::Foundation::IAsyncOperation<hstring> MainWindow::SaveFileUWPClick()
{
auto windowNative = this->m_inner.as<IWindowNative>();
HWND hWnd = nullptr;
check_hresult(windowNative->get_WindowHandle(&hWnd));

winrt::Windows::Storage::Pickers::FileSavePicker picker{};
picker.FileTypeChoices().Insert(L"Plain Text", winrt::single_threaded_vector<hstring>({ L".txt" }));

picker.as<::IInitializeWithWindow>()->Initialize(hWnd);
SetPickerOptions<winrt::Windows::Storage::Pickers::FileSavePicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);

if (!m_MultipleSelect)
{
auto& file = co_await picker.PickSaveFileAsync();
if (file != nullptr)
{
co_return file.Path();
}
}
else
{
co_return L"File Save Picker does not support multi selection";
}
co_return L"no selection";
}

winrt::Windows::Foundation::IAsyncOperation<hstring> MainWindow::OpenFolderUWPClick()
{
auto windowNative = this->m_inner.as<IWindowNative>();
Expand All @@ -128,7 +161,7 @@ namespace winrt::PickerUsageApp::implementation
winrt::Windows::Storage::Pickers::FolderPicker picker{};

picker.as<::IInitializeWithWindow>()->Initialize(hWnd);
SetPickerOptions<winrt::Windows::Storage::Pickers::FolderPicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);
SetOpenPickerOptions<winrt::Windows::Storage::Pickers::FolderPicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);
picker.ViewMode(Convert(m_ViewMode));

if (!m_MultipleSelect)
Expand All @@ -141,7 +174,7 @@ namespace winrt::PickerUsageApp::implementation
}
else
{
co_return L"Folder multi selection is not support";
co_return L"Folder Picker does not support multi selection";
}
co_return L"no selection";

Expand All @@ -153,8 +186,7 @@ namespace winrt::PickerUsageApp::implementation
auto id = AppWindow().Id();
winrt::Microsoft::Storage::Pickers::FileOpenPicker picker{ id };

SetPickerOptions<winrt::Microsoft::Storage::Pickers::FileOpenPicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
//SetPickerOptions(picker);
SetOpenPickerOptions<winrt::Microsoft::Storage::Pickers::FileOpenPicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
picker.ViewMode(m_ViewMode);
if (!m_MultipleSelect)
{
Expand All @@ -177,12 +209,33 @@ namespace winrt::PickerUsageApp::implementation
co_return L"no selection";
}

Windows::Foundation::IAsyncOperation<hstring> MainWindow::SaveFileSDKClick()
{
auto id = AppWindow().Id();
winrt::Microsoft::Storage::Pickers::FileSavePicker picker{ id };

SetPickerOptions<winrt::Microsoft::Storage::Pickers::FileSavePicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
if (!m_MultipleSelect)
{
auto& file = co_await picker.PickSaveFileAsync();
if (file != nullptr)
{
co_return file.Path();
}
}
else
{
co_return L"FileSavePicker does not support multi selection";
}
co_return L"no selection";
}

winrt::Windows::Foundation::IAsyncOperation<hstring> MainWindow::OpenFolderSDKClick()
{
auto id = AppWindow().Id();
winrt::Microsoft::Storage::Pickers::FolderPicker picker{ id };

SetPickerOptions<winrt::Microsoft::Storage::Pickers::FolderPicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
SetOpenPickerOptions<winrt::Microsoft::Storage::Pickers::FolderPicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
picker.ViewMode(m_ViewMode);
if (!m_MultipleSelect)
{
Expand Down Expand Up @@ -239,8 +292,6 @@ void winrt::PickerUsageApp::implementation::MainWindow::ViewModeSelectionChanged
default:
break;
}


}


Expand Down Expand Up @@ -274,5 +325,3 @@ void winrt::PickerUsageApp::implementation::MainWindow::PickerTypeChanged(winrt:
{
m_PickerTypeIndex = sender.as<Microsoft::UI::Xaml::Controls::RadioButtons>().SelectedIndex();
}


31 changes: 18 additions & 13 deletions prototype-workingdir/PickerUsageApp/MainWindow.xaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace winrt::PickerUsageApp::implementation
winrt::Windows::Foundation::IAsyncOperation<hstring> OpenFileSDKClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
winrt::Windows::Foundation::IAsyncOperation<hstring> OpenFileUWPClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);

winrt::Windows::Foundation::IAsyncOperation<hstring> SaveFileUWPClick();
winrt::Windows::Foundation::IAsyncOperation<hstring> SaveFileSDKClick();

winrt::Windows::Foundation::IAsyncOperation<hstring> OpenFolderSDKClick();
winrt::Windows::Foundation::IAsyncOperation<hstring> OpenFolderUWPClick();

Expand Down Expand Up @@ -63,36 +66,38 @@ namespace winrt::PickerUsageApp::implementation
picker.SettingsIdentifier({});
break;
}
switch (m_FilterTypeIndex)
switch (m_PickerLocationIdIndex)
{
case 1:
picker.FileTypeFilter().Append(L".jpg");
picker.FileTypeFilter().Append(L".png");
picker.SuggestedStartLocation(TPickerLocationId::DocumentsLibrary);
break;
case 2:
picker.FileTypeFilter().Append(L".jpg");
picker.FileTypeFilter().Append(L".png");
picker.FileTypeFilter().Append(L".json");
picker.SuggestedStartLocation(TPickerLocationId::Desktop);
break;
default:
picker.FileTypeFilter().Append(L"*");
break;
}
switch (m_PickerLocationIdIndex)
}

template<typename TPicker, typename TPickerLocationId> void SetOpenPickerOptions(TPicker picker)
{
SetPickerOptions<TPicker, TPickerLocationId>(picker);
switch (m_FilterTypeIndex)
{
case 1:
picker.SuggestedStartLocation(TPickerLocationId::DocumentsLibrary);
picker.FileTypeFilter().Append(L".jpg");
picker.FileTypeFilter().Append(L".png");
break;
case 2:
picker.SuggestedStartLocation(TPickerLocationId::Desktop);
picker.FileTypeFilter().Append(L".jpg");
picker.FileTypeFilter().Append(L".png");
picker.FileTypeFilter().Append(L".json");
break;
default:
picker.FileTypeFilter().Append(L"*");
break;
}


}

};
}

Expand Down

0 comments on commit 37a5a75

Please sign in to comment.