Skip to content

Commit

Permalink
Merge pull request #1494 from amgleitman/amgleitman/macos
Browse files Browse the repository at this point in the history
Add macOS support
  • Loading branch information
amgleitman authored Mar 30, 2021
2 parents 9f9cde9 + efa4987 commit 9b2eb6a
Show file tree
Hide file tree
Showing 140 changed files with 336 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ Example/android/
Example/ios/
screenshots/
android/
ios/
apple/
src/lib/extract/transform.js
24 changes: 13 additions & 11 deletions RNSVG.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ require 'json'
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
s.name = 'RNSVG'
s.version = package['version']
s.summary = package['description']
s.license = package['license']
s.homepage = package['homepage']
s.authors = 'Horcrux Chen'
s.platforms = { :ios => "9.0", :tvos => "9.2" }
s.source = { :git => 'https://github.com/react-native-community/react-native-svg.git', :tag => "v#{s.version}" }
s.source_files = 'ios/**/*.{h,m}'
s.requires_arc = true
s.dependency 'React'
s.name = 'RNSVG'
s.version = package['version']
s.summary = package['description']
s.license = package['license']
s.homepage = package['homepage']
s.authors = 'Horcrux Chen'
s.platforms = { :osx => "10.14", :ios => "9.0", :tvos => "9.2" }
s.source = { :git => 'https://github.com/react-native-community/react-native-svg.git', :tag => "v#{s.version}" }
s.source_files = 'apple/**/*.{h,m}'
s.ios.exclude_files = '**/*.macos.{h,m}'
s.osx.exclude_files = '**/*.ios.{h,m}'
s.requires_arc = true
s.dependency 'React'
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions ios/Brushes/RNSVGPainter.m → apple/Brushes/RNSVGPainter.m
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ - (void)paintPattern:(CGContextRef)context bounds:(CGRect)bounds
CGFloat h = [self getVal:[_points objectAtIndex:3] relative:height];

CGAffineTransform viewbox = [self.pattern.svgView getViewBoxTransform];
#if TARGET_OS_OSX
// This is needed because macOS and iOS have different conventions for where the origin is.
// For macOS, it's in the bottom-left corner. For iOS, it's in the top-left corner.
viewbox = CGAffineTransformScale(viewbox, 1, -1);
#endif
CGRect newBounds = CGRectMake(x, y, w, h);
CGSize size = newBounds.size;
self.useObjectBoundingBoxForContentUnits = _useContentObjectBoundingBox;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ - (void)parseReference

- (BOOL)isSimpleClipPath
{
NSArray<UIView*> *children = self.subviews;
NSArray<RNSVGView*> *children = self.subviews;
if (children.count == 1) {
UIView* child = children[0];
RNSVGView* child = children[0];
if ([child class] != [RNSVGGroup class]) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Elements/RNSVGDefs.h → apple/Elements/RNSVGDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import "RNSVGNode.h"

/**
* RNSVG defination are implemented as abstract UIViews for all elements inside Defs.
* RNSVG defination are implemented as abstract views for all elements inside Defs.
*/

@interface RNSVGDefs : RNSVGNode
Expand Down
2 changes: 1 addition & 1 deletion ios/Elements/RNSVGDefs.m → apple/Elements/RNSVGDefs.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ - (void)parseReference
}];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@implementation RNSVGForeignObject

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
Expand Down Expand Up @@ -42,7 +42,7 @@ - (void)renderGroupTo:(CGContextRef)context rect:(CGRect)rect

__block CGRect bounds = CGRectNull;

[self traverseSubviews:^(UIView *node) {
[self traverseSubviews:^(RNSVGView *node) {
if ([node isKindOfClass:[RNSVGMask class]] || [node isKindOfClass:[RNSVGClipPath class]]) {
// no-op
} else if ([node isKindOfClass:[RNSVGNode class]]) {
Expand Down
4 changes: 3 additions & 1 deletion ios/Elements/RNSVGGroup.h → apple/Elements/RNSVGGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "RNSVGUIKit.h"

#import "RNSVGContainer.h"
#import "RNSVGCGFCRule.h"
#import "RNSVGSvgView.h"
Expand Down
12 changes: 6 additions & 6 deletions ios/Elements/RNSVGGroup.m → apple/Elements/RNSVGGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ - (void)renderGroupTo:(CGContextRef)context rect:(CGRect)rect

__block CGRect bounds = CGRectNull;

[self traverseSubviews:^(UIView *node) {
[self traverseSubviews:^(RNSVGView *node) {
if ([node isKindOfClass:[RNSVGMask class]] || [node isKindOfClass:[RNSVGClipPath class]]) {
// no-op
} else if ([node isKindOfClass:[RNSVGNode class]]) {
Expand Down Expand Up @@ -160,7 +160,7 @@ - (CGPathRef)getPath:(CGContextRef)context
return cached;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGPoint transformed = CGPointApplyAffineTransform(point, self.invmatrix);
transformed = CGPointApplyAffineTransform(transformed, self.invTransform);
Expand Down Expand Up @@ -192,7 +192,7 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
}
}

for (UIView *node in [self.subviews reverseObjectEnumerator]) {
for (RNSVGView *node in [self.subviews reverseObjectEnumerator]) {
if ([node isKindOfClass:[RNSVGNode class]]) {
if ([node isKindOfClass:[RNSVGMask class]]) {
continue;
Expand All @@ -201,21 +201,21 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
if (event) {
svgNode.active = NO;
}
UIView *hitChild = [svgNode hitTest:transformed withEvent:event];
RNSVGPlatformView *hitChild = [svgNode hitTest:transformed withEvent:event];
if (hitChild) {
svgNode.active = YES;
return (svgNode.responsible || (svgNode != hitChild)) ? hitChild : self;
}
} else if ([node isKindOfClass:[RNSVGSvgView class]]) {
RNSVGSvgView* svgView = (RNSVGSvgView*)node;
UIView *hitChild = [svgView hitTest:transformed withEvent:event];
RNSVGPlatformView *hitChild = [svgView hitTest:transformed withEvent:event];
if (hitChild) {
return hitChild;
}
}
}

UIView *hitSelf = [super hitTest:transformed withEvent:event];
RNSVGPlatformView *hitSelf = [super hitTest:transformed withEvent:event];
if (hitSelf) {
return hitSelf;
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ - (void)setGradientTransform:(CGAffineTransform)gradientTransform
[self invalidate];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@implementation RNSVGMarker

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion ios/Elements/RNSVGMask.m → apple/Elements/RNSVGMask.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@implementation RNSVGMask

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ - (instancetype)init
return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ - (void)setGradientTransform:(CGAffineTransform)gradientTransform
[self invalidate];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
Expand Down
7 changes: 3 additions & 4 deletions ios/Elements/RNSVGSvgView.h → apple/Elements/RNSVGSvgView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
* LICENSE file in the root directory of this source tree.
*/

#import <UIKit/UIKit.h>
#import "RNSVGUIKit.h"

#import "RNSVGPainter.h"
#import "RNSVGContainer.h"
#import "RNSVGVBMOS.h"

@class RNSVGNode;

@interface RNSVGSvgView : UIView <RNSVGContainer>
@interface RNSVGSvgView : RNSVGView <RNSVGContainer>

@property (nonatomic, strong) RNSVGLength *bbWidth;
@property (nonatomic, strong) RNSVGLength *bbHeight;
Expand All @@ -30,8 +31,6 @@
@property (nonatomic, assign) CGAffineTransform invInitialCTM;
@property (nonatomic, assign) CGAffineTransform viewBoxTransform;



/**
* define <ClipPath></ClipPath> content as clipPath template.
*/
Expand Down
20 changes: 11 additions & 9 deletions ios/Elements/RNSVGSvgView.m → apple/Elements/RNSVGSvgView.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@ @implementation RNSVGSvgView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
#if !TARGET_OS_OSX // Not available on macOS
// This is necessary to ensure that [self setNeedsDisplay] actually triggers
// a redraw when our parent transitions between hidden and visible.
self.contentMode = UIViewContentModeRedraw;
#endif
rendered = false;
}
return self;
}

- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
- (void)insertReactSubview:(RNSVGView *)subview atIndex:(NSInteger)atIndex
{
[super insertReactSubview:subview atIndex:atIndex];
[self insertSubview:subview atIndex:atIndex];
[self invalidate];
}

- (void)removeReactSubview:(UIView *)subview
- (void)removeReactSubview:(RNSVGView *)subview
{
[super removeReactSubview:subview];
[self invalidate];
Expand All @@ -66,7 +68,7 @@ - (void)clearChildCache

- (void)invalidate
{
UIView* parent = self.superview;
RNSVGPlatformView* parent = self.superview;
if ([parent isKindOfClass:[RNSVGNode class]]) {
if (!rendered) {
return;
Expand Down Expand Up @@ -190,7 +192,7 @@ - (void)drawToContext:(CGContextRef)context withRect:(CGRect)rect {
_invviewBoxTransform = CGAffineTransformIdentity;
}

for (UIView *node in self.subviews) {
for (RNSVGView *node in self.subviews) {
if ([node isKindOfClass:[RNSVGNode class]]) {
RNSVGNode *svg = (RNSVGNode *)node;
[svg renderTo:context
Expand All @@ -203,7 +205,7 @@ - (void)drawToContext:(CGContextRef)context withRect:(CGRect)rect {

- (void)drawRect:(CGRect)rect
{
UIView* parent = self.superview;
RNSVGPlatformView* parent = self.superview;
if ([parent isKindOfClass:[RNSVGNode class]]) {
return;
}
Expand All @@ -214,7 +216,7 @@ - (void)drawRect:(CGRect)rect
_boundingBox = rect;
CGContextRef context = UIGraphicsGetCurrentContext();

for (UIView *node in self.subviews) {
for (RNSVGPlatformView *node in self.subviews) {
if ([node isKindOfClass:[RNSVGNode class]]) {
RNSVGNode *svg = (RNSVGNode *)node;
if (svg.responsible && !self.responsible) {
Expand All @@ -228,7 +230,7 @@ - (void)drawRect:(CGRect)rect
[self drawToContext:context withRect:rect];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGPoint transformed = point;
if (self.align) {
Expand All @@ -243,7 +245,7 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
node.active = NO;
}

UIView *hitChild = [node hitTest:transformed withEvent:event];
RNSVGPlatformView *hitChild = [node hitTest:transformed withEvent:event];

if (hitChild) {
node.active = YES;
Expand Down Expand Up @@ -279,7 +281,7 @@ - (NSString *)getDataURLwithBounds:(CGRect)bounds
return base64;
}

- (void)reactSetInheritedBackgroundColor:(UIColor *)inheritedBackgroundColor
- (void)reactSetInheritedBackgroundColor:(RNSVGColor *)inheritedBackgroundColor
{
self.backgroundColor = inheritedBackgroundColor;
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions ios/Elements/RNSVGUse.m → apple/Elements/RNSVGUse.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
self.frame = bounds;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint transformed = CGPointApplyAffineTransform(point, self.invmatrix);
transformed = CGPointApplyAffineTransform(transformed, self.invTransform);
RNSVGNode const* template = [self.svgView getDefinedTemplate:self.href];
Expand All @@ -122,7 +122,7 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
} else if (self.active) {
return self;
}
UIView const* hitChild = [template hitTest:transformed withEvent:event];
RNSVGPlatformView const* hitChild = [template hitTest:transformed withEvent:event];
if (hitChild) {
self.active = YES;
return self;
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions ios/RNSVGNode.h → apple/RNSVGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
@class RNSVGGroup;

/**
* RNSVG nodes are implemented as base UIViews. They should be implementation for all basic
* RNSVG nodes are implemented as base NSViews/UIViews. They should be implementation for all basic
*interfaces for all non-definition nodes.
*/

@interface RNSVGNode : UIView
@interface RNSVGNode : RNSVGView

/*
N[1/Sqrt[2], 36]
Expand Down Expand Up @@ -132,7 +132,7 @@ extern CGFloat const RNSVG_DEFAULT_FONT_SIZE;

- (void)endTransparencyLayer:(CGContextRef)context;

- (void)traverseSubviews:(BOOL (^)(__kindof UIView *node))block;
- (void)traverseSubviews:(BOOL (^)(__kindof RNSVGView *node))block;

- (void)clearChildCache;

Expand Down
Loading

0 comments on commit 9b2eb6a

Please sign in to comment.