mirror of
https://github.com/zoriya/react-native-svg.git
synced 2025-12-06 07:06:11 +00:00
feat: add Fabric on iOS without ComponentViews (#1821)
Version of #1754 without usage of ComponentViews. It seems like a more proper way, but introduces the necessity of clearing whole state of each component on recycling for it not to be used when view is recycled. Still known problems: We stringify props of type NumberProp since codegen only accepts props of a single type. It is the fastest way of dealing with it, but maybe there could be a better way to handle it. Image resolving should be probably handled the same as in RN core SvgView needs to set opaque = NO so it is does not have black background (it comes from the fact that RCTViewComponentView overrides backgroundColor setter which in turn somehow messes with the view being opaque). All other svg components do it already so maybe it is not such an issue. transform prop won't work when set natively since it is not parsed in Fabric
This commit is contained in:
258
apple/Elements/RNSVGPattern.mm
Normal file
258
apple/Elements/RNSVGPattern.mm
Normal file
@@ -0,0 +1,258 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Horcrux.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT-style license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
#import "RNSVGPattern.h"
|
||||
#import "RNSVGPainter.h"
|
||||
#import "RNSVGBrushType.h"
|
||||
#import "RNSVGNode.h"
|
||||
|
||||
#ifdef RN_FABRIC_ENABLED
|
||||
#import <react/renderer/components/rnsvg/ComponentDescriptors.h>
|
||||
#import "RCTFabricComponentsPlugins.h"
|
||||
#import "RCTConversions.h"
|
||||
#import <react/renderer/components/view/conversions.h>
|
||||
#import "RNSVGFabricConversions.h"
|
||||
#endif // RN_FABRIC_ENABLED
|
||||
|
||||
@implementation RNSVGPattern
|
||||
|
||||
#ifdef RN_FABRIC_ENABLED
|
||||
using namespace facebook::react;
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
{
|
||||
if (self = [super initWithFrame:frame]) {
|
||||
static const auto defaultProps = std::make_shared<const RNSVGPatternProps>();
|
||||
_props = defaultProps;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - RCTComponentViewProtocol
|
||||
|
||||
+ (ComponentDescriptorProvider)componentDescriptorProvider
|
||||
{
|
||||
return concreteComponentDescriptorProvider<RNSVGPatternComponentDescriptor>();
|
||||
}
|
||||
|
||||
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
|
||||
{
|
||||
const auto &newProps = *std::static_pointer_cast<const RNSVGPatternProps>(props);
|
||||
|
||||
self.x = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.x)];
|
||||
self.y = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.y)];
|
||||
if (RCTNSStringFromStringNilIfEmpty(newProps.patternheight)) {
|
||||
self.patternheight = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.patternheight)];
|
||||
}
|
||||
if (RCTNSStringFromStringNilIfEmpty(newProps.patternwidth)) {
|
||||
self.patternwidth = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.patternwidth)];
|
||||
}
|
||||
if (RCTNSStringFromStringNilIfEmpty(newProps.height)) {
|
||||
self.patternheight = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.height)];
|
||||
}
|
||||
if (RCTNSStringFromStringNilIfEmpty(newProps.width)) {
|
||||
self.patternwidth = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.width)];
|
||||
}
|
||||
self.patternUnits = newProps.patternUnits == 0 ? kRNSVGUnitsObjectBoundingBox : kRNSVGUnitsUserSpaceOnUse;
|
||||
self.patternContentUnits = newProps.patternUnits == 0 ? kRNSVGUnitsObjectBoundingBox : kRNSVGUnitsUserSpaceOnUse;
|
||||
if (newProps.patternTransform.size() == 6) {
|
||||
self.patternTransform = CGAffineTransformMake(newProps.patternTransform.at(0), newProps.patternTransform.at(1), newProps.patternTransform.at(2), newProps.patternTransform.at(3), newProps.patternTransform.at(4), newProps.patternTransform.at(5));
|
||||
}
|
||||
self.minX = newProps.minX;
|
||||
self.minY = newProps.minY;
|
||||
self.vbWidth = newProps.vbWidth;
|
||||
self.vbHeight = newProps.vbHeight;
|
||||
self.align = RCTNSStringFromStringNilIfEmpty(newProps.align);
|
||||
self.meetOrSlice = intToRNSVGVBMOS(newProps.meetOrSlice);
|
||||
|
||||
setCommonGroupProps(newProps, self);
|
||||
}
|
||||
|
||||
- (void)prepareForRecycle
|
||||
{
|
||||
[super prepareForRecycle];
|
||||
_x = nil;
|
||||
_y = nil;
|
||||
_patternheight = nil;
|
||||
_patternwidth = nil;
|
||||
_patternUnits = kRNSVGUnitsObjectBoundingBox;
|
||||
_patternContentUnits = kRNSVGUnitsObjectBoundingBox;
|
||||
_patternTransform = CGAffineTransformIdentity;
|
||||
|
||||
_minX = 0;
|
||||
_minY = 0;
|
||||
_vbWidth = 0;
|
||||
_vbHeight = 0;
|
||||
_align = nil;
|
||||
_meetOrSlice = kRNSVGVBMOSMeet;
|
||||
}
|
||||
#endif // RN_FABRIC_ENABLED
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
_patternTransform = CGAffineTransformIdentity;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)parseReference
|
||||
{
|
||||
self.dirty = false;
|
||||
NSArray<RNSVGLength *> *points = @[self.x, self.y, self.patternwidth, self.patternheight];
|
||||
RNSVGPainter *painter = [[RNSVGPainter alloc] initWithPointsArray:points];
|
||||
[painter setUnits:self.patternUnits];
|
||||
[painter setContentUnits:self.patternContentUnits];
|
||||
[painter setTransform:self.patternTransform];
|
||||
[painter setPattern:self];
|
||||
|
||||
if (self.patternUnits == kRNSVGUnitsUserSpaceOnUse || self.patternContentUnits == kRNSVGUnitsUserSpaceOnUse) {
|
||||
[painter setUserSpaceBoundingBox:[self.svgView getContextBounds]];
|
||||
}
|
||||
|
||||
[self.svgView definePainter:painter painterName:self.name];
|
||||
}
|
||||
|
||||
- (void)setX:(RNSVGLength *)x
|
||||
{
|
||||
if ([x isEqualTo:_x]) {
|
||||
return;
|
||||
}
|
||||
|
||||
_x = x;
|
||||
[self invalidate];
|
||||
}
|
||||
|
||||
- (void)setY:(RNSVGLength *)y
|
||||
{
|
||||
if ([y isEqualTo:_y]) {
|
||||
return;
|
||||
}
|
||||
|
||||
_y = y;
|
||||
[self invalidate];
|
||||
}
|
||||
|
||||
- (void)setPatternwidth:(RNSVGLength *)patternwidth
|
||||
{
|
||||
if ([patternwidth isEqualTo:_patternwidth]) {
|
||||
return;
|
||||
}
|
||||
|
||||
_patternwidth = patternwidth;
|
||||
[self invalidate];
|
||||
}
|
||||
|
||||
- (void)setPatternheight:(RNSVGLength *)patternheight
|
||||
{
|
||||
if ([patternheight isEqualTo:_patternheight]) {
|
||||
return;
|
||||
}
|
||||
|
||||
_patternheight = patternheight;
|
||||
[self invalidate];
|
||||
}
|
||||
|
||||
- (void)setPatternUnits:(RNSVGUnits)patternUnits
|
||||
{
|
||||
if (patternUnits == _patternUnits) {
|
||||
return;
|
||||
}
|
||||
|
||||
_patternUnits = patternUnits;
|
||||
[self invalidate];
|
||||
}
|
||||
|
||||
- (void)setPatternContentUnits:(RNSVGUnits)patternContentUnits
|
||||
{
|
||||
if (patternContentUnits == _patternContentUnits) {
|
||||
return;
|
||||
}
|
||||
|
||||
_patternContentUnits = patternContentUnits;
|
||||
[self invalidate];
|
||||
}
|
||||
|
||||
- (void)setPatternTransform:(CGAffineTransform)patternTransform
|
||||
{
|
||||
_patternTransform = patternTransform;
|
||||
[self invalidate];
|
||||
}
|
||||
|
||||
- (void)setMinX:(CGFloat)minX
|
||||
{
|
||||
if (minX == _minX) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self invalidate];
|
||||
_minX = minX;
|
||||
}
|
||||
|
||||
- (void)setMinY:(CGFloat)minY
|
||||
{
|
||||
if (minY == _minY) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self invalidate];
|
||||
_minY = minY;
|
||||
}
|
||||
|
||||
- (void)setVbWidth:(CGFloat)vbWidth
|
||||
{
|
||||
if (vbWidth == _vbWidth) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self invalidate];
|
||||
_vbWidth = vbWidth;
|
||||
}
|
||||
|
||||
- (void)setVbHeight:(CGFloat)vbHeight
|
||||
{
|
||||
if (_vbHeight == vbHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self invalidate];
|
||||
_vbHeight = vbHeight;
|
||||
}
|
||||
|
||||
- (void)setAlign:(NSString *)align
|
||||
{
|
||||
if ([align isEqualToString:_align]) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self invalidate];
|
||||
_align = align;
|
||||
}
|
||||
|
||||
- (void)setMeetOrSlice:(RNSVGVBMOS)meetOrSlice
|
||||
{
|
||||
if (meetOrSlice == _meetOrSlice) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self invalidate];
|
||||
_meetOrSlice = meetOrSlice;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#ifdef RN_FABRIC_ENABLED
|
||||
Class<RCTComponentViewProtocol> RNSVGPatternCls(void)
|
||||
{
|
||||
return RNSVGPattern.class;
|
||||
}
|
||||
#endif // RN_FABRIC_ENABLED
|
||||
Reference in New Issue
Block a user