Skip to content

Commit

Permalink
feat: Allow using WinUI 3 Controls (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickGerleman committed Oct 29, 2021
1 parent e366125 commit ea17798
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

| @react-native-picker/picker | react-native | react-native-windows |
| --- | --- | --- |
| master | 0.61+ | 0.64+ |
| >= 2.0.0 | 0.61+ | 0.64+ |
| >= 1.16.0 | 0.61+ | 0.61+ |
| >= 1.2.0 | 0.60+ or 0.59+ with [Jetifier](https://www.npmjs.com/package/jetifier) | N/A |
| >= 1.0.0 | 0.57 | N/A |
Expand Down
1 change: 1 addition & 0 deletions windows/ReactNativePicker/ReactNativePicker.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="16.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(SolutionDir)\ExperimentalFeatures.props" Condition="Exists('$(SolutionDir)\ExperimentalFeatures.props')" />
<Import Project="$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.210312.4\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.210312.4\build\native\Microsoft.Windows.CppWinRT.props')" />
<PropertyGroup Label="Globals">
<CppWinRTOptimized>true</CppWinRTOptimized>
Expand Down
29 changes: 18 additions & 11 deletions windows/ReactNativePicker/ReactPickerView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@
#include "ReactPickerView.g.cpp"

#include <winrt/Windows.Foundation.Metadata.h>
#include <winrt/Windows.UI.Xaml.Input.h>
#include <UI.Xaml.Input.h>

namespace winrt {
using namespace Microsoft::ReactNative;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Metadata;
using namespace Windows::UI;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace xaml;
using namespace xaml::Controls;
using namespace xaml::Input;
using namespace xaml::Media;
} // namespace winrt

namespace winrt::ReactNativePicker::implementation {

#ifdef USE_WINUI3
const bool ReactPickerView::s_isEditableComboboxSupported = true;
#else
const bool ReactPickerView::s_isEditableComboboxSupported = winrt::ApiInformation::IsPropertyPresent(
L"Windows.UI.Xaml.Controls.ComboBox", L"IsEditableProperty");
L"Windows.UI.Xaml.Controls.ComboBox", L"IsEditableProperty");
#endif

ReactPickerView::ReactPickerView(winrt::IReactContext const& reactContext) : m_reactContext(reactContext) {
this->AllowFocusOnInteraction(true);
Expand Down Expand Up @@ -64,9 +67,13 @@ namespace winrt::ReactNativePicker::implementation {
this->ClearValue(winrt::ComboBox::IsEditableProperty());
}
else {
if (auto iComboBox6 = this->try_as<winrt::Controls::IComboBox6>()) {
iComboBox6.IsEditable(propertyValue.AsBoolean());
}
#ifdef USE_WINUI3
this->IsEditable(propertyValue.AsBoolean());
#else
if (auto iComboBox6 = this->try_as<winrt::Controls::IComboBox6>()) {
iComboBox6.IsEditable(propertyValue.AsBoolean());
}
#endif
}
}
}
Expand Down Expand Up @@ -191,7 +198,7 @@ namespace winrt::ReactNativePicker::implementation {
}
}

void ReactPickerView::UpdateComboBoxItemForegroundResource(winrt::FrameworkElement const& item, winrt::Windows::UI::Xaml::Media::Brush const& color) {
void ReactPickerView::UpdateComboBoxItemForegroundResource(winrt::FrameworkElement const& item, xaml::Media::Brush const& color) {
if (auto comboBoxItem = item.try_as<winrt::ComboBoxItem>()) {
comboBoxItem.Foreground(color);
}
Expand Down
10 changes: 5 additions & 5 deletions windows/ReactNativePicker/ReactPickerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ namespace winrt::ReactNativePicker::implementation {
// FUTURE: remove when we can require RS5+
int32_t m_selectedIndex{ 0 };
std::vector<winrt::hstring> m_itemValues;
winrt::Windows::UI::Xaml::Media::Brush m_comboBoxColor{ nullptr };
winrt::Windows::UI::Xaml::Controls::ComboBox::SelectionChanged_revoker m_selectionChangedRevoker{};
winrt::Windows::UI::Xaml::Controls::ComboBox::DropDownClosed_revoker m_dropDownClosedRevoker{};
xaml::Media::Brush m_comboBoxColor{ nullptr };
xaml::Controls::ComboBox::SelectionChanged_revoker m_selectionChangedRevoker{};
xaml::Controls::ComboBox::DropDownClosed_revoker m_dropDownClosedRevoker{};

void RegisterEvents();
void RepopulateItems(winrt::Microsoft::ReactNative::JSValueArray const& items);
void SetSelectedIndex(int index);
void OnSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::SelectionChangedEventArgs const& args);
void UpdateComboBoxItemForegroundResource(winrt::Windows::UI::Xaml::FrameworkElement const& item, winrt::Windows::UI::Xaml::Media::Brush const& color);
void OnSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, xaml::Controls::SelectionChangedEventArgs const& args);
void UpdateComboBoxItemForegroundResource(xaml::FrameworkElement const& item, xaml::Media::Brush const& color);
};
} // namespace winrt::ReactNativePicker::implementation

Expand Down
4 changes: 3 additions & 1 deletion windows/ReactNativePicker/ReactPickerView.idl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <NamespaceRedirect.h>

namespace ReactNativePicker{

[default_interface]
runtimeclass ReactPickerView : Windows.UI.Xaml.Controls.ComboBox {
runtimeclass ReactPickerView : XAML_NAMESPACE.Controls.ComboBox {
ReactPickerView(Microsoft.ReactNative.IReactContext context);
void UpdateProperties(Microsoft.ReactNative.IJSValueReader reader);
};
Expand Down
2 changes: 1 addition & 1 deletion windows/ReactNativePicker/ReactPickerViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace winrt {
using namespace Microsoft::ReactNative;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace xaml;
}

namespace winrt::ReactNativePicker::implementation {
Expand Down
4 changes: 2 additions & 2 deletions windows/ReactNativePicker/ReactPickerViewManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace winrt::ReactNativePicker::implementation {

// IViewManager
winrt::hstring Name() noexcept;
winrt::Windows::UI::Xaml::FrameworkElement CreateView() noexcept;
xaml::FrameworkElement CreateView() noexcept;

// IViewManagerWithReactContext
winrt::Microsoft::ReactNative::IReactContext ReactContext() noexcept;
Expand All @@ -30,7 +30,7 @@ namespace winrt::ReactNativePicker::implementation {
NativeProps() noexcept;

void UpdateProperties(
winrt::Windows::UI::Xaml::FrameworkElement const& view,
xaml::FrameworkElement const& view,
winrt::Microsoft::ReactNative::IJSValueReader const& propertyMapReader) noexcept;

// IViewManagerWithExportedEventTypeConstants
Expand Down

0 comments on commit ea17798

Please sign in to comment.