You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.4 KiB

/**
* 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 "RNSVGLinearGradient.h"
#import "RNSVGBrushType.h"
#import "RNSVGPainter.h"
#ifdef RCT_NEW_ARCH_ENABLED
#import <React/RCTConversions.h>
#import <React/RCTFabricComponentsPlugins.h>
#import <react/renderer/components/rnsvg/ComponentDescriptors.h>
#import <react/renderer/components/view/conversions.h>
#import "RNSVGFabricConversions.h"
#endif // RCT_NEW_ARCH_ENABLED
@implementation RNSVGLinearGradient
#ifdef RCT_NEW_ARCH_ENABLED
using namespace facebook::react;
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RNSVGLinearGradientProps>();
_props = defaultProps;
}
return self;
}
#pragma mark - RCTComponentViewProtocol
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<RNSVGLinearGradientComponentDescriptor>();
}
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &newProps = *std::static_pointer_cast<const RNSVGLinearGradientProps>(props);
self.x1 = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.x1)];
self.y1 = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.y1)];
self.x2 = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.x2)];
self.y2 = [RNSVGLength lengthWithString:RCTNSStringFromString(newProps.y2)];
if (newProps.gradient.size() > 0) {
NSMutableArray<NSNumber *> *gradientArray = [NSMutableArray new];
for (auto number : newProps.gradient) {
[gradientArray addObject:[NSNumber numberWithDouble:number]];
}
self.gradient = gradientArray;
}
self.gradientUnits = newProps.gradientUnits == 0 ? kRNSVGUnitsObjectBoundingBox : kRNSVGUnitsUserSpaceOnUse;
if (newProps.gradientTransform.size() == 6) {
self.gradientTransform = CGAffineTransformMake(
newProps.gradientTransform.at(0),
newProps.gradientTransform.at(1),
newProps.gradientTransform.at(2),
newProps.gradientTransform.at(3),
newProps.gradientTransform.at(4),
newProps.gradientTransform.at(5));
}
setCommonNodeProps(newProps, self);
_props = std::static_pointer_cast<RNSVGLinearGradientProps const>(props);
}
- (void)prepareForRecycle
{
[super prepareForRecycle];
_x1 = nil;
_y1 = nil;
_x2 = nil;
_y2 = nil;
_gradient = nil;
_gradientUnits = kRNSVGUnitsObjectBoundingBox;
_gradientTransform = CGAffineTransformIdentity;
}
#endif // RCT_NEW_ARCH_ENABLED
- (instancetype)init
{
if (self = [super init]) {
_gradientTransform = CGAffineTransformIdentity;
}
return self;
}
- (void)setX1:(RNSVGLength *)x1
{
if ([x1 isEqualTo:_x1]) {
return;
}
_x1 = x1;
[self invalidate];
}
- (void)setY1:(RNSVGLength *)y1
{
if ([y1 isEqualTo:_y1]) {
return;
}
_y1 = y1;
[self invalidate];
}
- (void)setX2:(RNSVGLength *)x2
{
if ([x2 isEqualTo:_x2]) {
return;
}
_x2 = x2;
[self invalidate];
}
- (void)setY2:(RNSVGLength *)y2
{
if ([y2 isEqualTo:_y2]) {
return;
}
_y2 = y2;
[self invalidate];
}
- (void)setGradient:(NSArray<NSNumber *> *)gradient
{
if (gradient == _gradient) {
return;
}
_gradient = gradient;
[self invalidate];
}
- (void)setGradientUnits:(RNSVGUnits)gradientUnits
{
if (gradientUnits == _gradientUnits) {
return;
}
_gradientUnits = gradientUnits;
[self invalidate];
}
- (void)setGradientTransform:(CGAffineTransform)gradientTransform
{
_gradientTransform = gradientTransform;
[self invalidate];
}
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
- (void)parseReference
{
self.dirty = false;
NSArray<RNSVGLength *> *points = @[ self.x1, self.y1, self.x2, self.y2 ];
RNSVGPainter *painter = [[RNSVGPainter alloc] initWithPointsArray:points];
[painter setUnits:self.gradientUnits];
[painter setTransform:self.gradientTransform];
[painter setLinearGradientColors:self.gradient];
if (self.gradientUnits == kRNSVGUnitsUserSpaceOnUse) {
[painter setUserSpaceBoundingBox:[self.svgView getContextBounds]];
}
[self.svgView definePainter:painter painterName:self.name];
}
@end
#ifdef RCT_NEW_ARCH_ENABLED
Class<RCTComponentViewProtocol> RNSVGLinearGradientCls(void)
{
return RNSVGLinearGradient.class;
}
#endif // RCT_NEW_ARCH_ENABLED