diff --git a/windows/RNSVG/RenderableView.cpp b/windows/RNSVG/RenderableView.cpp index f91da5e1..c1a69a65 100644 --- a/windows/RNSVG/RenderableView.cpp +++ b/windows/RNSVG/RenderableView.cpp @@ -66,30 +66,29 @@ void RenderableView::UpdateProperties(IJSValueReader const &reader, bool forceUp } else if (propertyName == "stroke") { prop = RNSVG::BaseProp::Stroke; if (forceUpdate || !m_propSetMap[prop]) { - if (propertyValue.Type() == JSValueType::Array) { - auto const &brush{propertyValue.AsArray()}; - m_strokeBrushId = to_hstring(Utils::JSValueAsString(brush.at(1))); - } else { - Windows::UI::Color fallbackColor{(parent && !strokeSet) ? parent.Stroke() : Windows::UI::Colors::Transparent()}; - m_stroke = Utils::JSValueAsColor(propertyValue, fallbackColor); + Windows::UI::Color fallbackColor{(parent && !strokeSet) ? parent.Stroke() : Windows::UI::Colors::Transparent()}; + + if (!m_strokeBrushId.empty()) { + m_strokeBrushId.clear(); } + + SetColor(propertyValue.AsObject(), fallbackColor, propertyName); } } else if (propertyName == "fill") { prop = RNSVG::BaseProp::Fill; if (forceUpdate || !m_propSetMap[prop]) { - if (propertyValue.Type() == JSValueType::Array) { - auto const &brush{propertyValue.AsArray()}; - m_fillBrushId = to_hstring(Utils::JSValueAsString(brush.at(1))); - } else { - Windows::UI::Color fallbackColor{Windows::UI::Colors::Black()}; - if (propertyValue.IsNull() && fillSet) { - fallbackColor = Windows::UI::Colors::Transparent(); - } else if (parent) { - fallbackColor = parent.Fill(); - } - - m_fill = Utils::JSValueAsColor(propertyValue, fallbackColor); + Windows::UI::Color fallbackColor{Windows::UI::Colors::Black()}; + if (propertyValue.IsNull() && fillSet) { + fallbackColor = Windows::UI::Colors::Transparent(); + } else if (parent) { + fallbackColor = parent.Fill(); } + + if (!m_fillBrushId.empty()) { + m_fillBrushId.clear(); + } + + SetColor(propertyValue.AsObject(), fallbackColor, propertyName); } } else if (propertyName == "strokeLinecap") { prop = RNSVG::BaseProp::StrokeLineCap; @@ -338,4 +337,24 @@ void RenderableView::Unload() { m_propSetMap.clear(); m_strokeDashArray.Clear(); } + +void RenderableView::SetColor(const JSValueObject& propValue, Windows::UI::Color fallbackColor, std::string propName) { + switch (propValue["type"].AsInt64()) { + case 1: { + auto const &brushId{to_hstring(Utils::JSValueAsString(propValue["brushRef"]))}; + propName == "fill" ? m_fillBrushId = brushId : m_strokeBrushId = brushId; + break; + } + case 2: // currentColor + case 3: // context-fill + case 4: // context-stroke + propName == "fill" ? m_fillBrushId = L"currentColor" : m_strokeBrushId = L"currentColor"; + break; + default: { + auto const &color {Utils::JSValueAsColor(propValue["payload"], fallbackColor)}; + propName == "fill" ? m_fill = color : m_stroke = color; + break; + } + } +} } // namespace winrt::RNSVG::implementation diff --git a/windows/RNSVG/RenderableView.h b/windows/RNSVG/RenderableView.h index 48241cf7..8b75f3e4 100644 --- a/windows/RNSVG/RenderableView.h +++ b/windows/RNSVG/RenderableView.h @@ -89,6 +89,8 @@ struct RenderableView : RenderableViewT { Microsoft::Graphics::Canvas::Geometry::CanvasLineJoin::Miter}; Microsoft::Graphics::Canvas::Geometry::CanvasFilledRegionDetermination m_fillRule{ Microsoft::Graphics::Canvas::Geometry::CanvasFilledRegionDetermination::Winding}; + + void SetColor(const Microsoft::ReactNative::JSValueObject &propValue, Windows::UI::Color fallbackColor, std::string propName); }; } // namespace winrt::RNSVG::implementation diff --git a/windows/RNSVG/SvgView.cpp b/windows/RNSVG/SvgView.cpp index 771c4b90..368ab739 100644 --- a/windows/RNSVG/SvgView.cpp +++ b/windows/RNSVG/SvgView.cpp @@ -81,6 +81,8 @@ void SvgView::UpdateProperties(IJSValueReader const &reader, bool forceUpdate, b m_align = Utils::JSValueAsString(propertyValue); } else if (propertyName == "meetOrSlice") { m_meetOrSlice = Utils::GetMeetOrSlice(propertyValue); + } else if (propertyName == "color") { + m_currentColor = Utils::JSValueAsColor(propertyValue); } } diff --git a/windows/RNSVG/SvgView.h b/windows/RNSVG/SvgView.h index 2c4a49e9..4051c0eb 100644 --- a/windows/RNSVG/SvgView.h +++ b/windows/RNSVG/SvgView.h @@ -22,6 +22,8 @@ struct SvgView : SvgViewT { float SvgScale() { return m_scale; } + Windows::UI::Color CurrentColor() { return m_currentColor; } + Windows::Foundation::Collections::IMap Templates() { return m_templates; } @@ -78,6 +80,7 @@ struct SvgView : SvgViewT { RNSVG::SVGLength m_height{}; std::string m_align{""}; RNSVG::MeetOrSlice m_meetOrSlice{RNSVG::MeetOrSlice::Meet}; + Windows::UI::Color m_currentColor{Windows::UI::Colors::Black()}; Windows::Foundation::Collections::IMap m_templates{ winrt::single_threaded_map()}; diff --git a/windows/RNSVG/SvgViewManager.cpp b/windows/RNSVG/SvgViewManager.cpp index 99a0cd8d..04fb355c 100644 --- a/windows/RNSVG/SvgViewManager.cpp +++ b/windows/RNSVG/SvgViewManager.cpp @@ -43,6 +43,7 @@ IMapView SvgViewManager::NativeProps() { nativeProps.Insert(L"height", ViewManagerPropertyType::Number); nativeProps.Insert(L"width", ViewManagerPropertyType::Number); + nativeProps.Insert(L"color", ViewManagerPropertyType::Color); // viewBox nativeProps.Insert(L"minX", ViewManagerPropertyType::Number); diff --git a/windows/RNSVG/Utils.h b/windows/RNSVG/Utils.h index beb40ec7..2b48b340 100644 --- a/windows/RNSVG/Utils.h +++ b/windows/RNSVG/Utils.h @@ -325,7 +325,9 @@ struct Utils { ICanvasResourceCreator const &resourceCreator) { Brushes::ICanvasBrush brush{nullptr}; if (root && brushId != L"") { - if (auto const &brushView{root.Brushes().TryLookup(brushId)}) { + if (brushId == L"currentColor") { + brush = Brushes::CanvasSolidColorBrush(resourceCreator, root.CurrentColor()); + } else if (auto const &brushView{root.Brushes().TryLookup(brushId)}) { brushView.SetBounds(geometry.ComputeBounds()); brush = brushView.Brush(); } diff --git a/windows/RNSVG/Views.idl b/windows/RNSVG/Views.idl index ac76c5ba..546e64c7 100644 --- a/windows/RNSVG/Views.idl +++ b/windows/RNSVG/Views.idl @@ -26,6 +26,7 @@ namespace RNSVG Single SvgScale{ get; }; GroupView Group; + Windows.UI.Color CurrentColor{ get; }; Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl Canvas{ get; }; Windows.Foundation.Collections.IMap Templates{ get; }; Windows.Foundation.Collections.IMap Brushes{ get; };