Skip to content

Commit

Permalink
feat: get currentColor from caller instead of parent (#2521)
Browse files Browse the repository at this point in the history
# Summary

Fixes #2520
When an element uses `currentColor`, it should look for color in its
caller, not in its parent.
Example: 
```svg
<Svg width="100" height="100" viewBox="0 0 100 100" color="red">
  <Defs color="blue">
    <G color="green">
      <Rect id="a" x="0" y="0" width="50" height="50" fill="currentColor"/>
    </G>
  </Defs>
  <G color="pink">
    <Use href="#a"/>												<!-- #1 -->
  </G>
  <Use href="#a" transform="translate(25 25)"/>						<!-- #2 -->
  <G color="green">
    <Use href="#a" transform="translate(50 50)"/>					<!-- #3 -->
  </G>
</Svg>
```

* `#1` should be **pink**
* `#2` should be **red**
* `#3` should be **green**


![image](https://github.com/user-attachments/assets/b7ba2ec6-ea05-4bcb-9f40-0cf024e5c749)

## Test Plan

Example app -> test -> Test2520

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| iOS     |    ✅      |
| MacOS   |    ✅      |
| Android |    ✅      |
  • Loading branch information
jakex7 authored Oct 31, 2024
1 parent 405ff97 commit 2a58016
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 43 deletions.
6 changes: 6 additions & 0 deletions android/src/main/java/com/horcrux/svg/RenderableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public abstract class RenderableView extends VirtualView implements ReactHitSlop
private @Nullable ArrayList<Object> mOriginProperties;
private @Nullable ArrayList<String> mPropList;
private @Nullable ArrayList<String> mAttributeList;
private @Nullable RenderableView mCaller;

@Nullable String mFilter;

Expand Down Expand Up @@ -134,6 +135,9 @@ int getCurrentColor() {
if (this.mCurrentColor != 0) {
return this.mCurrentColor;
}
if (this.mCaller != null) {
return this.mCaller.getCurrentColor();
}
ViewParent parent = this.getParent();
if (parent instanceof VirtualView) {
return ((RenderableView) parent).getCurrentColor();
Expand Down Expand Up @@ -756,6 +760,7 @@ private ArrayList<String> getAttributeList() {
}

void mergeProperties(RenderableView target) {
mCaller = target;
ArrayList<String> targetAttributeList = target.getAttributeList();

if (targetAttributeList == null || targetAttributeList.size() == 0) {
Expand Down Expand Up @@ -798,6 +803,7 @@ void resetProperties() {
mLastMergedList = null;
mOriginProperties = null;
mAttributeList = mPropList;
mCaller = null;
}
}

Expand Down
3 changes: 2 additions & 1 deletion android/src/main/java/com/horcrux/svg/SvgView.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
Expand Down Expand Up @@ -189,7 +190,7 @@ public int reactTagForTouch(float touchX, float touchY) {
final Matrix mInvViewBoxMatrix = new Matrix();
private boolean mInvertible = true;
private boolean mRendered = false;
int mCurrentColor = 0;
int mCurrentColor = Color.BLACK;

boolean notRendered() {
return !mRendered;
Expand Down
4 changes: 2 additions & 2 deletions apple/Brushes/RNSVGContextBrush.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ - (BOOL)applyFillColor:(CGContextRef)context opacity:(CGFloat)opacity
BOOL fillColor;

if (brush.class == RNSVGBrush.class) {
CGContextSetFillColorWithColor(context, [element.tintColor CGColor]);
CGContextSetFillColorWithColor(context, [element getCurrentColor]);
fillColor = YES;
} else {
fillColor = [brush applyFillColor:context opacity:opacity];
Expand All @@ -70,7 +70,7 @@ - (BOOL)applyStrokeColor:(CGContextRef)context opacity:(CGFloat)opacity
BOOL strokeColor;

if (brush.class == RNSVGBrush.class) {
CGContextSetStrokeColorWithColor(context, [element.tintColor CGColor]);
CGContextSetStrokeColorWithColor(context, [element getCurrentColor]);
strokeColor = YES;
} else {
strokeColor = [brush applyStrokeColor:context opacity:opacity];
Expand Down
1 change: 1 addition & 0 deletions apple/Elements/RNSVGSvgView.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

@interface RNSVGSvgView : RNSVGView <RNSVGContainer>

@property (nonatomic, strong) RNSVGColor *color;
@property (nonatomic, strong) RNSVGLength *bbWidth;
@property (nonatomic, strong) RNSVGLength *bbHeight;
@property (nonatomic, assign) CGFloat minX;
Expand Down
11 changes: 6 additions & 5 deletions apple/Elements/RNSVGSvgView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ - (instancetype)initWithFrame:(CGRect)frame
// This is necessary to ensure that [self setNeedsDisplay] actually triggers
// a redraw when our parent transitions between hidden and visible.
self.contentMode = UIViewContentModeRedraw;
// We don't want the dimming effect on tint as it's used as currentColor
self.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
#endif // TARGET_OS_OSX
rendered = false;
#ifdef RCT_NEW_ARCH_ENABLED
Expand Down Expand Up @@ -90,7 +88,7 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
self.align = RCTNSStringFromStringNilIfEmpty(newProps.align);
self.meetOrSlice = intToRNSVGVBMOS(newProps.meetOrSlice);
if (RCTUIColorFromSharedColor(newProps.color)) {
self.tintColor = RCTUIColorFromSharedColor(newProps.color);
self.color = RCTUIColorFromSharedColor(newProps.color);
}
[super updateProps:props oldProps:oldProps];
}
Expand Down Expand Up @@ -184,10 +182,13 @@ - (void)invalidate
[self setNeedsDisplay];
}

- (void)tintColorDidChange
- (void)setColor:(RNSVGColor *)color
{
if (color == _color) {
return;
}
[self invalidate];
[self clearChildCache];
_color = color;
}

- (void)setMinX:(CGFloat)minX
Expand Down
3 changes: 3 additions & 0 deletions apple/RNSVGRenderable.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@interface RNSVGRenderable : RNSVGNode

@property (class) RNSVGRenderable *contextElement;
@property (nonatomic, strong) RNSVGColor *color;
@property (nonatomic, strong) RNSVGBrush *fill;
@property (nonatomic, assign) CGFloat fillOpacity;
@property (nonatomic, assign) RNSVGCGFCRule fillRule;
Expand All @@ -45,4 +46,6 @@

- (void)resetProperties;

- (CGColor *)getCurrentColor;

@end
29 changes: 25 additions & 4 deletions apple/RNSVGRenderable.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ @implementation RNSVGRenderable {
NSArray<RNSVGLength *> *_sourceStrokeDashArray;
CGFloat *_strokeDashArrayData;
CGPathRef _srcHitPath;
RNSVGRenderable *_caller;
}

static RNSVGRenderable *_contextElement;
Expand Down Expand Up @@ -61,11 +62,11 @@ - (void)invalidate

- (void)setColor:(RNSVGColor *)color
{
if (color == self.tintColor) {
if (color == _color) {
return;
}
[self invalidate];
self.tintColor = color;
_color = color;
}

- (void)setFill:(RNSVGBrush *)fill
Expand Down Expand Up @@ -560,7 +561,7 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect

if (self.fill) {
if (self.fill.class == RNSVGBrush.class) {
CGContextSetFillColorWithColor(context, [self.tintColor CGColor]);
CGContextSetFillColorWithColor(context, [self getCurrentColor]);
fillColor = YES;
} else {
fillColor = [self.fill applyFillColor:context opacity:self.fillOpacity];
Expand Down Expand Up @@ -608,7 +609,7 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
BOOL strokeColor;

if (self.stroke.class == RNSVGBrush.class) {
CGContextSetStrokeColorWithColor(context, [self.tintColor CGColor]);
CGContextSetStrokeColorWithColor(context, [self getCurrentColor]);
strokeColor = YES;
} else {
strokeColor = [self.stroke applyStrokeColor:context opacity:self.strokeOpacity];
Expand Down Expand Up @@ -724,6 +725,7 @@ - (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

- (void)mergeProperties:(__kindof RNSVGRenderable *)target
{
_caller = target;
NSArray<NSString *> *targetAttributeList = [target getAttributeList];

if (targetAttributeList.count == 0) {
Expand Down Expand Up @@ -754,9 +756,28 @@ - (void)resetProperties
[self setValue:[_originProperties valueForKey:key] forKey:key];
}

_caller = nil;
_lastMergedList = nil;
_attributeList = _propList;
self.merging = false;
}

- (CGColor *)getCurrentColor
{
if (self.color != nil) {
return [self.color CGColor];
}
if (_caller != nil) {
return [_caller getCurrentColor];
}
RNSVGPlatformView *parentView = [self superview];
if ([parentView isKindOfClass:[RNSVGRenderable class]]) {
return [(RNSVGRenderable *)parentView getCurrentColor];
} else if ([parentView isKindOfClass:[RNSVGSvgView class]]) {
return [[(RNSVGSvgView *)parentView color] CGColor];
}

return nil;
}

@end
24 changes: 0 additions & 24 deletions apple/RNSVGUIKit.macos.mm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#import "RNSVGUIKit.h"

@implementation RNSVGView {
NSColor *_tintColor;
}

- (CGPoint)center
Expand All @@ -20,29 +19,6 @@ - (void)setCenter:(CGPoint)point
self.frame = CGRectMake(xOrigin, yOrigin, frameRect.size.width, frameRect.size.height);
}

- (NSColor *)tintColor
{
if (_tintColor != nil) {
return _tintColor;
}

// To mimic iOS's tintColor, we crawl up the view hierarchy until either:
// (a) we find a valid color
// (b) we reach a view that isn't an RNSVGView
NSView *parentView = [self superview];
if ([parentView isKindOfClass:[RNSVGView class]]) {
return [(RNSVGView *)parentView tintColor];
} else {
return [NSColor controlAccentColor];
}
}

- (void)setTintColor:(NSColor *)tintColor
{
_tintColor = tintColor;
[self setNeedsDisplay:YES];
}

@end

@implementation NSImage (RNSVGMacOSExtensions)
Expand Down
4 changes: 2 additions & 2 deletions apple/Text/RNSVGTSpan.mm
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
if (self.inlineSize != nil && self.inlineSize.value != 0) {
if (self.fill) {
if (self.fill.class == RNSVGBrush.class) {
CGColorRef color = [self.tintColor CGColor];
CGColorRef color = [self getCurrentColor];
[self drawWrappedText:context gc:gc rect:rect color:color];
} else {
CGColorRef color = [self.fill getColorWithOpacity:self.fillOpacity];
Expand All @@ -143,7 +143,7 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
}
if (self.stroke) {
if (self.stroke.class == RNSVGBrush.class) {
CGColorRef color = [self.tintColor CGColor];
CGColorRef color = [self getCurrentColor];
[self drawWrappedText:context gc:gc rect:rect color:color];
} else {
CGColorRef color = [self.stroke getColorWithOpacity:self.strokeOpacity];
Expand Down
2 changes: 1 addition & 1 deletion apple/ViewManagers/RNSVGRenderableManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ - (RNSVGRenderable *)node
return [RNSVGRenderable new];
}

RCT_REMAP_VIEW_PROPERTY(color, tintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(color, UIColor)
RCT_EXPORT_VIEW_PROPERTY(fill, RNSVGBrush)
RCT_EXPORT_VIEW_PROPERTY(fillOpacity, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(fillRule, RNSVGCGFCRule)
Expand Down
2 changes: 1 addition & 1 deletion apple/ViewManagers/RNSVGSvgViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ - (RNSVGPlatformView *)view
RCT_EXPORT_VIEW_PROPERTY(vbHeight, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(align, NSString)
RCT_EXPORT_VIEW_PROPERTY(meetOrSlice, RNSVGVBMOS)
RCT_REMAP_VIEW_PROPERTY(color, tintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(color, UIColor)
RCT_CUSTOM_VIEW_PROPERTY(hitSlop, UIEdgeInsets, RNSVGSvgView)
{
if ([view respondsToSelector:@selector(setHitTestEdgeInsets:)]) {
Expand Down
32 changes: 32 additions & 0 deletions apps/common/test/Test2520.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import {View} from 'react-native';
import {Defs, G, Rect, Svg, Use} from 'react-native-svg';

export default () => {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Svg width="100" height="100" viewBox="0 0 100 100" color="red">
{/* @ts-ignore */}
<Defs color="blue">
<G color="green">
<Rect
id="a"
x="0"
y="0"
width="50"
height="50"
fill="currentColor"
/>
</G>
</Defs>
<G color="pink">
<Use href="#a" />
</G>
<Use href="#a" transform="translate(25 25)" />
<G color="green">
<Use href="#a" transform="translate(50 50)" />
</G>
</Svg>
</View>
);
};
1 change: 1 addition & 0 deletions apps/common/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import Test2407 from './Test2407';
import Test2417 from './Test2417';
import Test2455 from './Test2455';
import Test2471 from './Test2471';
import Test2520 from './Test2520';

export default function App() {
return <ColorTest />;
Expand Down
3 changes: 0 additions & 3 deletions src/elements/Svg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export default class Svg extends Shape<SvgProps> {
...extracted,
};
let {
color,
width,
height,
focusable,
Expand Down Expand Up @@ -173,8 +172,6 @@ export default class Svg extends Shape<SvgProps> {

extractResponder(props, props, this as ResponderInstanceProps);

props.tintColor = color;

if (onLayout != null) {
props.onLayout = onLayout;
}
Expand Down

0 comments on commit 2a58016

Please sign in to comment.