Files
react-native-svg/windows/RNSVG/CircleView.cpp
Marlene Cota f88532d195 [Windows] Port to Direct2D to remove win2d dependency (#2052)
This change removes the win2d (Direct2D wrapper) dependency by using D2D directly. This removes the manual step of adding the win2d to any new react-native-windows projects that want to use react-native-svg. It is also a stepping stone to an easier Fabric implementation for windows.
2023-11-14 11:33:19 +01:00

49 lines
1.5 KiB
C++

#include "pch.h"
#include "CircleView.h"
#include "CircleView.g.cpp"
#include "JSValueXaml.h"
#include "Utils.h"
using namespace winrt;
using namespace Microsoft::ReactNative;
namespace winrt::RNSVG::implementation {
void CircleView::UpdateProperties(IJSValueReader const &reader, bool forceUpdate, bool invalidate) {
const JSValueObject &propertyMap{JSValue::ReadObjectFrom(reader)};
for (auto const &pair : propertyMap) {
auto const &propertyName{pair.first};
auto const &propertyValue{pair.second};
if (propertyName == "r") {
m_r = SVGLength::From(propertyValue);
} else if (propertyName == "cx") {
m_cx = SVGLength::From(propertyValue);
} else if (propertyName == "cy") {
m_cy = SVGLength::From(propertyValue);
}
}
__super::UpdateProperties(reader, forceUpdate, invalidate);
}
void CircleView::CreateGeometry() {
auto const root{SvgRoot()};
float cx{Utils::GetAbsoluteLength(m_cx, root.ActualWidth())};
float cy{Utils::GetAbsoluteLength(m_cy, root.ActualHeight())};
float r{Utils::GetAbsoluteLength(m_r, Utils::GetCanvasDiagonal(root.ActualSize()))};
com_ptr<ID2D1DeviceContext> deviceContext{get_self<D2DDeviceContext>(root.DeviceContext())->Get()};
com_ptr<ID2D1Factory> factory;
deviceContext->GetFactory(factory.put());
com_ptr<ID2D1EllipseGeometry> geometry;
check_hresult(factory->CreateEllipseGeometry(D2D1::Ellipse({cx, cy}, r, r), geometry.put()));
Geometry(make<RNSVG::implementation::D2DGeometry>(geometry.as<ID2D1Geometry>()));
}
} // namespace winrt::RNSVG::implementation