Skip to content

Commit

Permalink
Simplify set prop value (#251)
Browse files Browse the repository at this point in the history
Fixes #250 
This may be a compiler issue (since it only started to show up in recent
msvc updates), but regardless it makes sense to simplify this code a bit
anyway.
###### Microsoft Reviewers: [Open in
CodeFlow](https://portal.fabricbot.ms/api/codeflow?pullrequest=https://github.com/microsoft/react-native-xaml/pull/251)
  • Loading branch information
asklar committed Mar 14, 2023
1 parent da5f7db commit 006afaf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "simplify overload resolution for SetPropValue",
"packageName": "react-native-xaml",
"email": "asklar@microsoft.com",
"dependentChangeType": "patch"
}
111 changes: 43 additions & 68 deletions package/windows/ReactNativeXaml/XamlMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,94 +57,69 @@ namespace winrt::Microsoft::ReactNative {

template <typename T> bool IsType(const winrt::Windows::Foundation::IInspectable& i) { return i.try_as<T>() != nullptr; }

template<typename T, std::enable_if_t<std::is_enum<T>::value, int> = 0>
void SetPropValue(const xaml::DependencyObject o, const xaml::DependencyProperty& prop, const winrt::Microsoft::ReactNative::JSValue& v, const winrt::Microsoft::ReactNative::IReactContext&) {
auto valueEnum = MakeEnum<T>(v.AsInt32());
o.SetValue(prop, valueEnum);

inline void ReadValue(JSValue const& jsValue, xaml::Media::Geometry& value) noexcept {
const auto v = winrt::to_hstring(jsValue.AsJSString());
value = Markup::XamlBindingHelper::ConvertValue(winrt::xaml_typename<xaml::Media::PathGeometry>(), winrt::box_value(v)).as<xaml::Media::Geometry>();
}

template<typename T, std::enable_if_t<
!std::is_enum<T>::value &&
!std::is_same<winrt::hstring, T>::value &&
!std::is_same<winrt::Windows::Foundation::IInspectable, T>::value &&
!std::is_same<winrt::Windows::Foundation::Uri, T>::value &&
!std::is_same<xaml::Media::ImageSource, T>::value
, int> = 0>
void SetPropValue(const xaml::DependencyObject& o, const xaml::DependencyProperty& prop, const winrt::Microsoft::ReactNative::JSValue& v, const winrt::Microsoft::ReactNative::IReactContext&) {
auto b = v.To<T>();
o.SetValue(prop, winrt::box_value(b));
inline void ReadValue(JSValue const& jsValue, Windows::UI::Text::FontWeight& value) noexcept {
value.Weight = jsValue.AsInt16();
}
}

template<typename T, std::enable_if_t<
std::is_same<xaml::Media::ImageSource, T>::value, int> = 0>
void SetPropValue(const xaml::DependencyObject& o, const xaml::DependencyProperty& prop, const winrt::Microsoft::ReactNative::JSValue& v, const winrt::Microsoft::ReactNative::IReactContext&) {

template<typename T>
void SetPropValue(const xaml::DependencyObject& o, const xaml::DependencyProperty& prop,
const winrt::Microsoft::ReactNative::JSValue& v, const winrt::Microsoft::ReactNative::IReactContext& context) {
if constexpr (std::is_same_v<winrt::Windows::UI::Xaml::Controls::Maps::MapStyle, T>) {
auto boxed = v.To<winrt::Windows::UI::Xaml::Controls::Maps::MapStyle>();
o.SetValue(prop, winrt::box_value(boxed));
} else if constexpr (std::is_same_v<T, winrt::hstring>) {
auto b = v.AsString();
o.SetValue(prop, winrt::box_value(winrt::to_hstring(b)));
} else if constexpr (std::is_same_v<T, winrt::Windows::Foundation::Uri>) {
auto cn = winrt::get_class_name(o);
auto uri = Uri{ winrt::to_hstring(v.AsString()) };
o.SetValue(prop, uri);
} else if constexpr (std::is_same_v<T, winrt::Windows::Foundation::IInspectable>) {
switch (v.Type()) {
case JSValueType::String: return SetPropValue<winrt::hstring>(o, prop, v, context);
case JSValueType::Boolean: return SetPropValue<bool>(o, prop, v, context);
case JSValueType::Double: return SetPropValue<double>(o, prop, v, context);
case JSValueType::Int64: return SetPropValue<int64_t>(o, prop, v, context);
case JSValueType::Object: {
const auto& obj = v.AsObject();
if (obj.find("string") != obj.cend()) {
const auto& value = obj["string"];
return SetPropValue<winrt::Windows::Foundation::IInspectable>(o, prop, value, context);
}
}
}
} else if constexpr (std::is_enum_v<T>) {
auto valueEnum = MakeEnum<T>(v.AsInt32());
o.SetValue(prop, valueEnum);
} else if constexpr (std::is_same_v<xaml::Media::ImageSource, T>) {
const auto str = v.AsString();
const auto uri = Uri{ winrt::to_hstring(str) };

xaml::Media::ImageSource value{ nullptr };
if (uri.SchemeName() == L"data") {
SetImageSourceForInlineData(str, o, prop);
}
else if (str.ends_with(".svg") || str.ends_with(".svgz")) {
} else if (str.ends_with(".svg") || str.ends_with(".svgz")) {
value = xaml::Media::Imaging::SvgImageSource{ uri };
o.SetValue(prop, value);
}
else {
} else {
value = xaml::Media::Imaging::BitmapImage{ uri };
o.SetValue(prop, value);
}
}

inline void ReadValue(JSValue const& jsValue, xaml::Media::Geometry& value) noexcept {
const auto v = winrt::to_hstring(jsValue.AsJSString());
value = Markup::XamlBindingHelper::ConvertValue(winrt::xaml_typename<xaml::Media::PathGeometry>(), winrt::box_value(v)).as<xaml::Media::Geometry>();
}

inline void ReadValue(JSValue const& jsValue, Windows::UI::Text::FontWeight& value) noexcept {
value.Weight = jsValue.AsInt16();
} else {
auto b = v.To<T>();
o.SetValue(prop, winrt::box_value(b));
}
}


// MapStyle has a bug where it expects the property to be set as an IReference<MapStyle> always, and does not support IReference<uint32_t>
template<typename T, std::enable_if_t<
std::is_same<winrt::Windows::UI::Xaml::Controls::Maps::MapStyle, T>::value, int> = 0>
void SetPropValue(const xaml::DependencyObject& o, const xaml::DependencyProperty& prop, const winrt::Microsoft::ReactNative::JSValue& v, const winrt::Microsoft::ReactNative::IReactContext&) {
auto boxed = v.To<winrt::Windows::UI::Xaml::Controls::Maps::MapStyle>();
o.SetValue(prop, winrt::box_value(boxed));
}

template<typename T, std::enable_if_t<std::is_same<T, winrt::hstring>::value, int> = 0>
void SetPropValue(const xaml::DependencyObject& o, const xaml::DependencyProperty& prop, const winrt::Microsoft::ReactNative::JSValue& v, const winrt::Microsoft::ReactNative::IReactContext&) {
auto b = v.AsString();
o.SetValue(prop, winrt::box_value(winrt::to_hstring(b)));
}

template<typename T, std::enable_if_t<std::is_same<T, winrt::Windows::Foundation::Uri>::value, int> = 0>
void SetPropValue(const xaml::DependencyObject& o, const xaml::DependencyProperty& prop, const winrt::Microsoft::ReactNative::JSValue& v, const winrt::Microsoft::ReactNative::IReactContext&) {
auto cn = winrt::get_class_name(o);
auto uri = Uri{ winrt::to_hstring(v.AsString()) };
o.SetValue(prop, uri);
}

template<typename T, std::enable_if_t<std::is_same<T, winrt::Windows::Foundation::IInspectable>::value, int> = 0>
void SetPropValue(const xaml::DependencyObject& o, const xaml::DependencyProperty& prop, const winrt::Microsoft::ReactNative::JSValue& v, const winrt::Microsoft::ReactNative::IReactContext& context) {
switch (v.Type()) {
case JSValueType::String: return SetPropValue<winrt::hstring>(o, prop, v, context);
case JSValueType::Boolean: return SetPropValue<bool>(o, prop, v, context);
case JSValueType::Double: return SetPropValue<double>(o, prop, v, context);
case JSValueType::Int64: return SetPropValue<int64_t>(o, prop, v, context);
case JSValueType::Object: {
const auto& obj = v.AsObject();
if (obj.find("string") != obj.cend()) {
const auto& value = obj["string"];
return SetPropValue<winrt::Windows::Foundation::IInspectable>(o, prop, value, context);
}
}
}
}

struct PropInfo {
stringKey propName;

Expand Down

0 comments on commit 006afaf

Please sign in to comment.