mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-06-09 17:30:58 +00:00
feat: use codegenNativeComponent to import native views (#1847)
Changed `requireNativeComponent` to `codegenNativeComponent` so that upcoming changes (Static View Configs, Bridgeless Mode and idk what more) in `react-native` are available in the library. Also, types and native components are now taken directly from `fabric` folder to make sure the values passed to the native components are the ones defined in props. It should work on all supported versions since `codegenNativeComponent` function exists from RN v. 0.61.0. Suggested by @RSNara and @cipolleschi Reason for [`5394bbb` (#1847)](https://github.com/software-mansion/react-native-svg/pull/1847/commits/5394bbbced6c1838e67240997cf4621d7f783197): - on `Paper`, `Animated` uses `setNativeProps` method when we set `useNativeDriver` to `false`, and does not rerender the component. Therefore, new transform lands only in `SvgView` and is parsed in `RCTViewManager.m` . - on `Fabric`, the same code makes the components rerender. Due to this, information about new transform is passed to the `SvgView` child: `G` , making it apply translations from the transform in its `updateProps` method. - other than `Animated` use-case, on both archs, if we just passed `transform` prop to `Svg` component, it would end up in double transformations now as well. All of those changes are due to https://github.com/software-mansion/react-native-svg/pull/1895, which added proper parsing of RN style `transform` prop (array of transformations objects) therefore making `G` properly handle `transform` prop passed from `Svg`. Reason for [`19bcb24` (#1847)](https://github.com/software-mansion/react-native-svg/pull/1847/commits/19bcb2464b3a40a47e4396ea66f49d6a95bfc9bc): Same as https://github.com/software-mansion/react-native-screens/pull/1624
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
/**
|
||||
* 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 "RNSVGRenderableModule.h"
|
||||
#import <React/RCTBridge.h>
|
||||
#import <React/RCTUIManager.h>
|
||||
#import <React/RCTUIManagerUtils.h>
|
||||
#import "RNSVGPathMeasure.h"
|
||||
#import "RNSVGRenderable.h"
|
||||
|
||||
#import "RCTConvert+RNSVG.h"
|
||||
#import "RNSVGCGFCRule.h"
|
||||
|
||||
@implementation RNSVGRenderableModule
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
|
||||
@synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;
|
||||
|
||||
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isPointInFill : (nonnull NSNumber *)reactTag options : (NSDictionary *)options)
|
||||
{
|
||||
RNSVGPlatformView *view = [self getRenderableView:reactTag];
|
||||
|
||||
if (![view isKindOfClass:[RNSVGRenderable class]]) {
|
||||
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGRenderable, got: %@", view);
|
||||
return [NSNumber numberWithBool:false];
|
||||
}
|
||||
if (options == nil) {
|
||||
RCTLogError(@"Invalid options given to isPointInFill, got: %@", options);
|
||||
return [NSNumber numberWithBool:false];
|
||||
}
|
||||
id xo = [options objectForKey:@"x"];
|
||||
id yo = [options objectForKey:@"y"];
|
||||
if (![xo isKindOfClass:NSNumber.class] || ![yo isKindOfClass:NSNumber.class]) {
|
||||
RCTLogError(@"Invalid x or y given to isPointInFill");
|
||||
return [NSNumber numberWithBool:false];
|
||||
}
|
||||
RNSVGRenderable *svg = (RNSVGRenderable *)view;
|
||||
CGFloat x = (CGFloat)[xo doubleValue];
|
||||
CGFloat y = (CGFloat)[yo doubleValue];
|
||||
CGPoint point = CGPointMake(x, y);
|
||||
RNSVGPlatformView *target = [svg hitTest:point withEvent:nil];
|
||||
BOOL hit = target != nil;
|
||||
return [NSNumber numberWithBool:hit];
|
||||
}
|
||||
|
||||
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isPointInStroke : (nonnull NSNumber *)reactTag options : (NSDictionary *)options)
|
||||
{
|
||||
RNSVGPlatformView *view = [self getRenderableView:reactTag];
|
||||
|
||||
if (![view isKindOfClass:[RNSVGRenderable class]]) {
|
||||
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGRenderable, got: %@", view);
|
||||
return [NSNumber numberWithBool:false];
|
||||
}
|
||||
if (options == nil) {
|
||||
RCTLogError(@"Invalid options given to isPointInFill, got: %@", options);
|
||||
return [NSNumber numberWithBool:false];
|
||||
}
|
||||
id xo = [options objectForKey:@"x"];
|
||||
id yo = [options objectForKey:@"y"];
|
||||
if (![xo isKindOfClass:NSNumber.class] || ![yo isKindOfClass:NSNumber.class]) {
|
||||
RCTLogError(@"Invalid x or y given to isPointInFill");
|
||||
return [NSNumber numberWithBool:false];
|
||||
}
|
||||
RNSVGRenderable *svg = (RNSVGRenderable *)view;
|
||||
CGFloat x = (CGFloat)[xo doubleValue];
|
||||
CGFloat y = (CGFloat)[yo doubleValue];
|
||||
CGPoint point = CGPointMake(x, y);
|
||||
BOOL hit = CGPathContainsPoint(svg.strokePath, nil, point, NO);
|
||||
|
||||
return [NSNumber numberWithBool:hit];
|
||||
}
|
||||
|
||||
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getTotalLength : (nonnull NSNumber *)reactTag)
|
||||
{
|
||||
RNSVGPlatformView *view = [self getRenderableView:reactTag];
|
||||
|
||||
if (![view isKindOfClass:[RNSVGRenderable class]]) {
|
||||
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGRenderable, got: %@", view);
|
||||
return [NSNumber numberWithDouble:0];
|
||||
}
|
||||
|
||||
RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc] init];
|
||||
RNSVGRenderable *svg = (RNSVGRenderable *)view;
|
||||
CGPathRef target = [svg getPath:nil];
|
||||
[measure extractPathData:target];
|
||||
|
||||
return [NSNumber numberWithDouble:measure.pathLength];
|
||||
}
|
||||
|
||||
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getPointAtLength
|
||||
: (nonnull NSNumber *)reactTag options
|
||||
: (NSDictionary *)options)
|
||||
{
|
||||
RNSVGPlatformView *view = [self getRenderableView:reactTag];
|
||||
|
||||
if (![view isKindOfClass:[RNSVGRenderable class]]) {
|
||||
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGRenderable, got: %@", view);
|
||||
return nil;
|
||||
}
|
||||
|
||||
CGFloat position = (CGFloat)[[options objectForKey:@"length"] doubleValue];
|
||||
RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc] init];
|
||||
RNSVGRenderable *svg = (RNSVGRenderable *)view;
|
||||
CGPathRef target = [svg getPath:nil];
|
||||
[measure extractPathData:target];
|
||||
|
||||
CGFloat x;
|
||||
CGFloat y;
|
||||
CGFloat angle;
|
||||
double midPoint = fmax(0, fmin(position, measure.pathLength));
|
||||
[measure getPosAndTan:&angle midPoint:midPoint x:&x y:&y];
|
||||
|
||||
return @{@"x" : @(x), @"y" : @(y), @"angle" : @(angle)};
|
||||
}
|
||||
|
||||
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getBBox : (nonnull NSNumber *)reactTag options : (NSDictionary *)options)
|
||||
{
|
||||
RNSVGPlatformView *view = [self getRenderableView:reactTag];
|
||||
|
||||
if (![view isKindOfClass:[RNSVGRenderable class]]) {
|
||||
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGRenderable, got: %@", view);
|
||||
return nil;
|
||||
}
|
||||
|
||||
RNSVGRenderable *svg = (RNSVGRenderable *)view;
|
||||
BOOL fill = [[options objectForKey:@"fill"] boolValue];
|
||||
BOOL stroke = [[options objectForKey:@"stroke"] boolValue];
|
||||
BOOL markers = [[options objectForKey:@"markers"] boolValue];
|
||||
BOOL clipped = [[options objectForKey:@"clipped"] boolValue];
|
||||
[svg getPath:nil];
|
||||
|
||||
CGRect bounds = CGRectZero;
|
||||
if (fill) {
|
||||
bounds = CGRectUnion(bounds, svg.fillBounds);
|
||||
}
|
||||
if (stroke) {
|
||||
bounds = CGRectUnion(bounds, svg.strokeBounds);
|
||||
}
|
||||
if (markers) {
|
||||
bounds = CGRectUnion(bounds, svg.markerBounds);
|
||||
}
|
||||
if (clipped) {
|
||||
CGPathRef clipPath = [svg getClipPath];
|
||||
CGRect clipBounds = CGPathGetBoundingBox(clipPath);
|
||||
if (clipPath && !CGRectIsEmpty(clipBounds)) {
|
||||
bounds = CGRectIntersection(bounds, clipBounds);
|
||||
}
|
||||
}
|
||||
|
||||
CGPoint origin = bounds.origin;
|
||||
CGSize size = bounds.size;
|
||||
return @{@"x" : @(origin.x), @"y" : @(origin.y), @"width" : @(size.width), @"height" : @(size.height)};
|
||||
}
|
||||
|
||||
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getCTM : (nonnull NSNumber *)reactTag)
|
||||
{
|
||||
RNSVGPlatformView *view = [self getRenderableView:reactTag];
|
||||
|
||||
if (![view isKindOfClass:[RNSVGRenderable class]]) {
|
||||
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGRenderable, got: %@", view);
|
||||
return nil;
|
||||
}
|
||||
|
||||
RNSVGRenderable *svg = (RNSVGRenderable *)view;
|
||||
CGAffineTransform ctm = svg.ctm;
|
||||
return @{@"a" : @(ctm.a), @"b" : @(ctm.b), @"c" : @(ctm.c), @"d" : @(ctm.d), @"e" : @(ctm.tx), @"f" : @(ctm.ty)};
|
||||
}
|
||||
|
||||
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getScreenCTM : (nonnull NSNumber *)reactTag)
|
||||
{
|
||||
RNSVGPlatformView *view = [self getRenderableView:reactTag];
|
||||
|
||||
if (![view isKindOfClass:[RNSVGRenderable class]]) {
|
||||
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGRenderable, got: %@", view);
|
||||
return nil;
|
||||
}
|
||||
|
||||
RNSVGRenderable *svg = (RNSVGRenderable *)view;
|
||||
CGAffineTransform ctm = svg.ctm;
|
||||
return @{@"a" : @(ctm.a), @"b" : @(ctm.b), @"c" : @(ctm.c), @"d" : @(ctm.d), @"e" : @(ctm.tx), @"f" : @(ctm.ty)};
|
||||
}
|
||||
|
||||
- (void)getRawResource:(NSString *)name resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
||||
{
|
||||
}
|
||||
|
||||
- (RNSVGPlatformView *)getRenderableView:(NSNumber *)reactTag
|
||||
{
|
||||
__block RNSVGPlatformView *view;
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
view = [self.viewRegistry_DEPRECATED viewForReactTag:reactTag];
|
||||
});
|
||||
return view;
|
||||
}
|
||||
|
||||
#ifdef RN_FABRIC_ENABLED
|
||||
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
||||
(const facebook::react::ObjCTurboModule::InitParams &)params
|
||||
{
|
||||
return std::make_shared<facebook::react::NativeSvgRenderableModuleSpecJSI>(params);
|
||||
}
|
||||
#endif
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user