diff --git a/prototype-workingdir/PickerUsageApp/MainWindow.xaml b/prototype-workingdir/PickerUsageApp/MainWindow.xaml index 1318f71657..b7428cfd75 100644 --- a/prototype-workingdir/PickerUsageApp/MainWindow.xaml +++ b/prototype-workingdir/PickerUsageApp/MainWindow.xaml @@ -9,8 +9,15 @@ mc:Ignorable="d" Title="PickerUsageApp"> + + + + + + + - Multi Selection + Multi Selection @@ -46,8 +53,8 @@ - - + + diff --git a/prototype-workingdir/PickerUsageApp/MainWindow.xaml.cpp b/prototype-workingdir/PickerUsageApp/MainWindow.xaml.cpp index 5ca511bf8f..a98fa3471f 100644 --- a/prototype-workingdir/PickerUsageApp/MainWindow.xaml.cpp +++ b/prototype-workingdir/PickerUsageApp/MainWindow.xaml.cpp @@ -19,168 +19,260 @@ using namespace Microsoft::UI::Xaml; namespace winrt::PickerUsageApp::implementation { + Windows::Storage::Pickers::PickerViewMode Convert(Microsoft::Storage::Pickers::PickerViewMode viewMode) + { + switch (viewMode) + { + case winrt::Microsoft::Storage::Pickers::PickerViewMode::List: + return Windows::Storage::Pickers::PickerViewMode::List; + case winrt::Microsoft::Storage::Pickers::PickerViewMode::Thumbnail: + return Windows::Storage::Pickers::PickerViewMode::Thumbnail; + default: + return Windows::Storage::Pickers::PickerViewMode::List; + } + } - Windows::Storage::Pickers::PickerViewMode Convert(Microsoft::Storage::Pickers::PickerViewMode viewMode) { - switch (viewMode) - { - case winrt::Microsoft::Storage::Pickers::PickerViewMode::List: - return Windows::Storage::Pickers::PickerViewMode::List; - case winrt::Microsoft::Storage::Pickers::PickerViewMode::Thumbnail: - return Windows::Storage::Pickers::PickerViewMode::Thumbnail; - default: - return Windows::Storage::Pickers::PickerViewMode::List; - } - } - - Windows::Foundation::IAsyncAction MainWindow::OpenFileUWPClick(IInspectable const&, RoutedEventArgs const&) - { - hstring error_mesasge{}; - try { - labelBlock().Text(L""); - auto windowNative = this->m_inner.as(); - HWND hWnd = nullptr; - check_hresult(windowNative->get_WindowHandle(&hWnd)); - - winrt::Windows::Storage::Pickers::FileOpenPicker picker{}; - - picker.as<::IInitializeWithWindow>()->Initialize(hWnd); - SetPickerOptions(picker); - //SetPickerOptions(picker); - picker.ViewMode(Convert(m_ViewMode)); - - if (!m_MultipleSelect) { - auto& file = co_await picker.PickSingleFileAsync(); - if (file != nullptr) { - labelBlock().Text(file.Path()); - } - } - else { - auto& files = co_await picker.PickMultipleFilesAsync(); - winrt::hstring names = L""; - for (auto& file : files) - { - names = names + file.Path() + L"\n"; - } - labelBlock().Text(names); - } - - co_return; - } - catch (winrt::hresult_error const& ex_) - { - error_mesasge = ex_.message(); - } - if (!error_mesasge.empty()) - { - winrt::Windows::UI::Popups::MessageDialog dialog(error_mesasge); - co_await dialog.ShowAsync(); - } - } - - Windows::Foundation::IAsyncAction MainWindow::OpenFileSDKClick(IInspectable const&, RoutedEventArgs const&) - { - hstring error_mesasge{}; - try { - labelBlock().Text(L""); - auto id = AppWindow().Id(); - winrt::Microsoft::Storage::Pickers::FileOpenPicker picker{ id }; - - SetPickerOptions(picker); - //SetPickerOptions(picker); - picker.ViewMode(m_ViewMode); - if (!m_MultipleSelect) { - auto& file = co_await picker.PickSingleFileAsync(); - if (file != nullptr) { - labelBlock().Text(file.Path()); - } - } - else { - auto& files = co_await picker.PickMultipleFilesAsync(); - winrt::hstring names = L""; - for (auto& file : files) - { - names = names + file.Path() + L"\n"; - } - labelBlock().Text(names); - } - co_return; - } - catch (winrt::hresult_error const& ex_) - { - error_mesasge = ex_.message(); - } - if (!error_mesasge.empty()) - { - winrt::Windows::UI::Popups::MessageDialog dialog(error_mesasge); - co_await dialog.ShowAsync(); - } - } + winrt::Windows::Foundation::IAsyncAction MainWindow::SDKClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args) + { + labelBlock().Text(L""); + hstring message{}; + try + { + switch (m_PickerTypeIndex) + { + case 0: + message = co_await OpenFileSDKClick(sender, args); + break; + case 2: + message = co_await OpenFolderSDKClick(); + break; + default: + message = L"Unsupported Picker Type"; + break; + } + } + catch (winrt::hresult_error const& ex_) + { + message = ex_.message(); + } + labelBlock().Text(message); + } + + winrt::Windows::Foundation::IAsyncAction MainWindow::UWPClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args) + { + labelBlock().Text(L""); + hstring message{}; + try + { + switch (m_PickerTypeIndex) + { + case 0: + message = co_await OpenFileUWPClick(sender, args); + break; + case 2: + message = co_await OpenFolderUWPClick(); + break; + + default: + message = L"Unsupported Picker Type"; + break; + } + } + catch (winrt::hresult_error const& ex_) + { + message = ex_.message(); + } + labelBlock().Text(message); + } + + + Windows::Foundation::IAsyncOperation MainWindow::OpenFileUWPClick(IInspectable const&, RoutedEventArgs const&) + { + auto windowNative = this->m_inner.as(); + HWND hWnd = nullptr; + check_hresult(windowNative->get_WindowHandle(&hWnd)); + + winrt::Windows::Storage::Pickers::FileOpenPicker picker{}; + + picker.as<::IInitializeWithWindow>()->Initialize(hWnd); + SetPickerOptions(picker); + picker.ViewMode(Convert(m_ViewMode)); + + if (!m_MultipleSelect) + { + auto& file = co_await picker.PickSingleFileAsync(); + if (file != nullptr) + { + co_return file.Path(); + } + } + else + { + auto& files = co_await picker.PickMultipleFilesAsync(); + winrt::hstring names = L""; + for (auto& file : files) + { + names = names + file.Path() + L"\n"; + } + co_return names; + } + co_return L"no selection"; + } + + winrt::Windows::Foundation::IAsyncOperation MainWindow::OpenFolderUWPClick() + { + auto windowNative = this->m_inner.as(); + HWND hWnd = nullptr; + check_hresult(windowNative->get_WindowHandle(&hWnd)); + + winrt::Windows::Storage::Pickers::FolderPicker picker{}; + + picker.as<::IInitializeWithWindow>()->Initialize(hWnd); + SetPickerOptions(picker); + picker.ViewMode(Convert(m_ViewMode)); + + if (!m_MultipleSelect) + { + auto& folder = co_await picker.PickSingleFolderAsync(); + if (folder != nullptr) + { + co_return folder.Path(); + } + } + else + { + co_return L"Folder multi selection is not support"; + } + co_return L"no selection"; + + throw hresult_not_implemented(); + } + + Windows::Foundation::IAsyncOperation MainWindow::OpenFileSDKClick(IInspectable const&, RoutedEventArgs const&) + { + auto id = AppWindow().Id(); + winrt::Microsoft::Storage::Pickers::FileOpenPicker picker{ id }; + + SetPickerOptions(picker); + //SetPickerOptions(picker); + picker.ViewMode(m_ViewMode); + if (!m_MultipleSelect) + { + auto& file = co_await picker.PickSingleFileAsync(); + if (file != nullptr) + { + co_return file.Path(); + } + } + else + { + auto& files = co_await picker.PickMultipleFilesAsync(); + winrt::hstring names = L""; + for (auto& file : files) + { + names = names + file.Path() + L"\n"; + } + co_return names; + } + co_return L"no selection"; + } + + winrt::Windows::Foundation::IAsyncOperation MainWindow::OpenFolderSDKClick() + { + auto id = AppWindow().Id(); + winrt::Microsoft::Storage::Pickers::FolderPicker picker{ id }; + + SetPickerOptions(picker); + picker.ViewMode(m_ViewMode); + if (!m_MultipleSelect) + { + auto& folder = co_await picker.PickSingleFolderAsync(); + if (folder != nullptr) + { + co_return folder.Path(); + } + } + else + { + co_return L"Folder multiple selection is not supported"; + } + co_return L"no selection"; + } } void winrt::PickerUsageApp::implementation::MainWindow::UIFronzenTestClick(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e) { - counterBlock().Text(std::to_wstring(count)); - count++; + counterBlock().Text(std::to_wstring(count)); + count++; } void winrt::PickerUsageApp::implementation::MainWindow::toggleCustomLableClick(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e) { - m_isUseCustomLabel = !m_isUseCustomLabel; - if (m_isUseCustomLabel) - { - useCustomLabelToggle().Content(box_value(L"Disable")); - customLabelBox().Visibility(Visibility::Visible); - } - else - { - useCustomLabelToggle().Content(box_value(L"Enable")); - customLabelBox().Visibility(Visibility::Collapsed); - } + m_isUseCustomLabel = !m_isUseCustomLabel; + if (m_isUseCustomLabel) + { + useCustomLabelToggle().Content(box_value(L"Disable")); + customLabelBox().Visibility(Visibility::Visible); + } + else + { + useCustomLabelToggle().Content(box_value(L"Enable")); + customLabelBox().Visibility(Visibility::Collapsed); + } } void winrt::PickerUsageApp::implementation::MainWindow::ViewModeSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e) { - auto radioButtons = sender.as(); - auto selectedIndex = radioButtons.SelectedIndex(); - switch (selectedIndex) - { - case 0: - m_ViewMode = Microsoft::Storage::Pickers::PickerViewMode::List; - break; - case 1: - m_ViewMode = Microsoft::Storage::Pickers::PickerViewMode::Thumbnail; - break; - default: - break; - } + auto radioButtons = sender.as(); + auto selectedIndex = radioButtons.SelectedIndex(); + switch (selectedIndex) + { + case 0: + m_ViewMode = Microsoft::Storage::Pickers::PickerViewMode::List; + break; + case 1: + m_ViewMode = Microsoft::Storage::Pickers::PickerViewMode::Thumbnail; + break; + default: + break; + } + + } void winrt::PickerUsageApp::implementation::MainWindow::FilterTypeSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e) { - auto radioButtons = sender.as(); + auto radioButtons = sender.as(); - m_FilterTypeIndex = radioButtons.SelectedIndex(); + m_FilterTypeIndex = radioButtons.SelectedIndex(); } void winrt::PickerUsageApp::implementation::MainWindow::SettingsIdentifierChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e) { - auto radioButtons = sender.as(); - m_SettingsIdentifierIndex = radioButtons.SelectedIndex(); + auto radioButtons = sender.as(); + m_SettingsIdentifierIndex = radioButtons.SelectedIndex(); } void winrt::PickerUsageApp::implementation::MainWindow::PickerLocationIdChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e) { - auto radioButtons = sender.as(); - m_PickerLocationIdIndex = radioButtons.SelectedIndex(); + auto radioButtons = sender.as(); + m_PickerLocationIdIndex = radioButtons.SelectedIndex(); } void winrt::PickerUsageApp::implementation::MainWindow::MultiSelectToggled(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e) { - m_MultipleSelect = sender.as().IsOn(); + m_MultipleSelect = sender.as().IsOn(); } + +void winrt::PickerUsageApp::implementation::MainWindow::PickerTypeChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e) +{ + m_PickerTypeIndex = sender.as().SelectedIndex(); +} + + diff --git a/prototype-workingdir/PickerUsageApp/MainWindow.xaml.h b/prototype-workingdir/PickerUsageApp/MainWindow.xaml.h index f3c1321111..189e6ca994 100644 --- a/prototype-workingdir/PickerUsageApp/MainWindow.xaml.h +++ b/prototype-workingdir/PickerUsageApp/MainWindow.xaml.h @@ -5,89 +5,100 @@ namespace winrt::PickerUsageApp::implementation { - struct MainWindow : MainWindowT - { - int count{ 0 }; + struct MainWindow : MainWindowT + { + int count{ 0 }; - MainWindow() - { - // Xaml objects should not call InitializeComponent during construction. - // See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent - } + MainWindow() + { + // Xaml objects should not call InitializeComponent during construction. + // See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent + } - winrt::Windows::Foundation::IAsyncAction OpenFileSDKClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args); - winrt::Windows::Foundation::IAsyncAction OpenFileUWPClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args); + winrt::Windows::Foundation::IAsyncOperation OpenFileSDKClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args); + winrt::Windows::Foundation::IAsyncOperation OpenFileUWPClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args); - void UIFronzenTestClick(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); - void toggleCustomLableClick(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); - void ViewModeSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e); - void FilterTypeSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e); - void SettingsIdentifierChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e); - void PickerLocationIdChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e); - void MultiSelectToggled(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); - private: - bool m_isUseCustomLabel{ false }; - winrt::Microsoft::Storage::Pickers::PickerViewMode m_ViewMode{ Microsoft::Storage::Pickers::PickerViewMode::List }; - int m_FilterTypeIndex{ 0 }; - int m_SettingsIdentifierIndex{ 0 }; - int m_PickerLocationIdIndex{ 0 }; - bool m_MultipleSelect{ false }; + winrt::Windows::Foundation::IAsyncOperation OpenFolderSDKClick(); + winrt::Windows::Foundation::IAsyncOperation OpenFolderUWPClick(); - // using this template approach helps us ensure use same logic on compatible user facing API - template void SetPickerOptions(TPicker picker) { - if (m_isUseCustomLabel) - { - picker.CommitButtonText(customLabelBox().Text()); - } - switch (m_SettingsIdentifierIndex) - { - case 1: - picker.SettingsIdentifier(L"Identifier1"); - break; - case 2: - picker.SettingsIdentifier(L"Identifier2"); - break; - default: - picker.SettingsIdentifier({}); - break; - } - switch (m_FilterTypeIndex) - { - case 1: - picker.FileTypeFilter().Append(L".jpg"); - picker.FileTypeFilter().Append(L".png"); - break; - case 2: - picker.FileTypeFilter().Append(L".jpg"); - picker.FileTypeFilter().Append(L".png"); - picker.FileTypeFilter().Append(L".json"); - break; - default: - picker.FileTypeFilter().Append(L"*"); - break; - } - switch (m_PickerLocationIdIndex) - { - case 1: - picker.SuggestedStartLocation(TPickerLocationId::DocumentsLibrary); - break; - case 2: - picker.SuggestedStartLocation(TPickerLocationId::Desktop); - break; - default: - break; - } + winrt::Windows::Foundation::IAsyncAction SDKClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args); + winrt::Windows::Foundation::IAsyncAction UWPClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args); + void UIFronzenTestClick(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); + void toggleCustomLableClick(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); + void ViewModeSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e); + void FilterTypeSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e); + void SettingsIdentifierChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e); + void PickerLocationIdChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e); + void MultiSelectToggled(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); + void PickerTypeChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); - } + private: + bool m_isUseCustomLabel{ false }; + winrt::Microsoft::Storage::Pickers::PickerViewMode m_ViewMode{ Microsoft::Storage::Pickers::PickerViewMode::List }; + int m_FilterTypeIndex{ 0 }; + int m_SettingsIdentifierIndex{ 0 }; + int m_PickerLocationIdIndex{ 0 }; + int m_PickerTypeIndex{ 0 }; + bool m_MultipleSelect{ false }; - }; + + // using this template approach helps us ensure use same logic on compatible user facing API + template void SetPickerOptions(TPicker picker) + { + if (m_isUseCustomLabel) + { + picker.CommitButtonText(customLabelBox().Text()); + } + switch (m_SettingsIdentifierIndex) + { + case 1: + picker.SettingsIdentifier(L"Identifier1"); + break; + case 2: + picker.SettingsIdentifier(L"Identifier2"); + break; + default: + picker.SettingsIdentifier({}); + break; + } + switch (m_FilterTypeIndex) + { + case 1: + picker.FileTypeFilter().Append(L".jpg"); + picker.FileTypeFilter().Append(L".png"); + break; + case 2: + picker.FileTypeFilter().Append(L".jpg"); + picker.FileTypeFilter().Append(L".png"); + picker.FileTypeFilter().Append(L".json"); + break; + default: + picker.FileTypeFilter().Append(L"*"); + break; + } + switch (m_PickerLocationIdIndex) + { + case 1: + picker.SuggestedStartLocation(TPickerLocationId::DocumentsLibrary); + break; + case 2: + picker.SuggestedStartLocation(TPickerLocationId::Desktop); + break; + default: + break; + } + + + } + + }; } namespace winrt::PickerUsageApp::factory_implementation { - struct MainWindow : MainWindowT - { - }; + struct MainWindow : MainWindowT + { + }; }