Skip to content

Commit

Permalink
Update ASPendingState type for backgroundColor to UIColor
Browse files Browse the repository at this point in the history
Given that UIColor is now a dynamic provider we can no longer rely on the fact that there is a 1:1 mapping between UIColor <-> CGColorRef.

I've updated the UIViewBridge and ASPendingState accordingly to use UIColor and translate to CGColorRef only when interacting with layers. This should preserve any dynamic capabilities we might need in the future when trying to re-render layer backed nodes.
  • Loading branch information
rahul-malik authored Jul 21, 2019
1 parent d4d6897 commit 4ebd38c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Source/Details/UIView+ASConvenience.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) CGFloat borderWidth;
@property (nonatomic, getter = isOpaque) BOOL opaque;
@property (nonatomic) __attribute__((NSObject)) CGColorRef borderColor;
@property (nonatomic) __attribute__((NSObject)) CGColorRef backgroundColor;
@property (nonatomic) UIColor *backgroundColor;
@property (nonatomic) BOOL allowsGroupOpacity;
@property (nonatomic) BOOL allowsEdgeAntialiasing;
@property (nonatomic) unsigned int edgeAntialiasingMask;
Expand Down
13 changes: 6 additions & 7 deletions Source/Private/ASDisplayNode+UIViewBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -750,24 +750,23 @@ - (UIColor *)backgroundColor
if (!_flags.layerBacked) {
return _view.backgroundColor;
} else {
return [UIColor colorWithCGColor:_getFromLayer(backgroundColor)];
return [UIColor colorWithCGColor:_layer.backgroundColor];
}
}
return [UIColor colorWithCGColor:ASDisplayNodeGetPendingState(self).backgroundColor];
return ASDisplayNodeGetPendingState(self).backgroundColor;
}

- (void)setBackgroundColor:(UIColor *)newBackgroundColor
{
_bridge_prologue_write;


BOOL shouldApply = ASDisplayNodeShouldApplyBridgedWriteToView(self);
CGColorRef newBackgroundCGColor = newBackgroundColor.CGColor;
CGColorRef newBackgroundCGColor = nil;
if (shouldApply) {
CGColorRef oldBackgroundCGColor = _layer.backgroundColor;

if (_flags.layerBacked) {
_layer.backgroundColor = newBackgroundColor.CGColor;
newBackgroundCGColor = _layer.backgroundColor;
} else {
/*
NOTE: Setting to the view and layer individually is necessary.
Expand All @@ -778,9 +777,9 @@ - (void)setBackgroundColor:(UIColor *)newBackgroundColor
*/
_view.backgroundColor = newBackgroundColor;
_layer.backgroundColor = _view.backgroundColor.CGColor;
// Gather the CGColorRef from the view incase there are any changes it might apply to which CGColorRef is returned for dynamic colors
newBackgroundCGColor = _view.backgroundColor.CGColor;
_layer.backgroundColor = newBackgroundCGColor;
}

if (!CGColorEqualToColor(oldBackgroundCGColor, newBackgroundCGColor)) {
Expand All @@ -790,7 +789,7 @@ - (void)setBackgroundColor:(UIColor *)newBackgroundColor
// NOTE: If we're in the background, we cannot read the current value of bgcolor (if loaded).
// When the pending state is applied to the view on main, we will call `setNeedsDisplay` if
// the new background color doesn't match the one on the layer.
ASDisplayNodeGetPendingState(self).backgroundColor = newBackgroundCGColor;
ASDisplayNodeGetPendingState(self).backgroundColor = newBackgroundColor;
}
}

Expand Down
36 changes: 18 additions & 18 deletions Source/Private/_ASPendingState.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
#import <AsyncDisplayKit/ASDisplayNodeInternal.h>
#import <AsyncDisplayKit/ASInternalHelpers.h>

#define __shouldSetNeedsDisplay(layer) (flags.needsDisplay \
#define __shouldSetNeedsDisplayForView(view) (flags.needsDisplay \
|| (flags.setOpaque && _flags.opaque != (view).opaque)\
|| (flags.setBackgroundColor && ![backgroundColor isEqual:(view).backgroundColor]))

#define __shouldSetNeedsDisplayForLayer(layer) (flags.needsDisplay \
|| (flags.setOpaque && _flags.opaque != (layer).opaque)\
|| (flags.setBackgroundColor && !CGColorEqualToColor(backgroundColor, (layer).backgroundColor)))
|| (flags.setBackgroundColor && ![backgroundColor isEqual:[UIColor colorWithCGColor:(layer).backgroundColor]]))

typedef struct {
// Properties
Expand Down Expand Up @@ -102,7 +106,7 @@ @implementation _ASPendingState
unsigned int edgeAntialiasingMask;
CGRect frame; // Frame is only to be used for synchronous views wrapped by nodes (see setFrame:)
CGRect bounds;
CGColorRef backgroundColor;
UIColor *backgroundColor;
CGFloat alpha;
CGFloat cornerRadius;
UIViewContentMode contentMode;
Expand Down Expand Up @@ -415,19 +419,17 @@ - (void)setBounds:(CGRect)newBounds
_stateToApplyFlags.setBounds = YES;
}

- (CGColorRef)backgroundColor
- (UIColor *)backgroundColor
{
return backgroundColor;
}

- (void)setBackgroundColor:(CGColorRef)color
- (void)setBackgroundColor:(UIColor *)color
{
if (color == backgroundColor) {
if ([color isEqual:backgroundColor]) {
return;
}

CGColorRelease(backgroundColor);
backgroundColor = CGColorRetain(color);
backgroundColor = color;
_stateToApplyFlags.setBackgroundColor = YES;
}

Expand Down Expand Up @@ -926,7 +928,7 @@ - (void)applyToLayer:(CALayer *)layer
{
ASPendingStateFlags flags = _stateToApplyFlags;

if (__shouldSetNeedsDisplay(layer)) {
if (__shouldSetNeedsDisplayForLayer(layer)) {
[layer setNeedsDisplay];
}

Expand Down Expand Up @@ -964,7 +966,7 @@ - (void)applyToLayer:(CALayer *)layer
layer.masksToBounds = _flags.clipsToBounds;

if (flags.setBackgroundColor)
layer.backgroundColor = backgroundColor;
layer.backgroundColor = backgroundColor.CGColor;

if (flags.setOpaque)
layer.opaque = _flags.opaque;
Expand Down Expand Up @@ -1048,7 +1050,7 @@ - (void)applyToView:(UIView *)view withSpecialPropertiesHandling:(BOOL)specialPr
unowned CALayer *layer = view.layer;

ASPendingStateFlags flags = _stateToApplyFlags;
if (__shouldSetNeedsDisplay(layer)) {
if (__shouldSetNeedsDisplayForView(view)) {
[view setNeedsDisplay];
}

Expand Down Expand Up @@ -1095,8 +1097,8 @@ - (void)applyToView:(UIView *)view withSpecialPropertiesHandling:(BOOL)specialPr
view.clipsToBounds = _flags.clipsToBounds;

if (flags.setBackgroundColor) {
view.backgroundColor = [UIColor colorWithCGColor:backgroundColor];
layer.backgroundColor = backgroundColor;
view.backgroundColor = backgroundColor;
layer.backgroundColor = backgroundColor.CGColor;
}

if (flags.setTintColor)
Expand Down Expand Up @@ -1286,7 +1288,7 @@ + (_ASPendingState *)pendingViewStateFromLayer:(CALayer *)layer
pendingState.contentsScale = layer.contentsScale;
pendingState.rasterizationScale = layer.rasterizationScale;
pendingState.clipsToBounds = layer.masksToBounds;
pendingState.backgroundColor = layer.backgroundColor;
pendingState.backgroundColor = [UIColor colorWithCGColor:layer.backgroundColor];
pendingState.opaque = layer.opaque;
pendingState.hidden = layer.hidden;
pendingState.alpha = layer.opacity;
Expand Down Expand Up @@ -1327,7 +1329,7 @@ + (_ASPendingState *)pendingViewStateFromView:(UIView *)view
pendingState.contentsScale = layer.contentsScale;
pendingState.rasterizationScale = layer.rasterizationScale;
pendingState.clipsToBounds = view.clipsToBounds;
pendingState.backgroundColor = layer.backgroundColor;
pendingState.backgroundColor = [UIColor colorWithCGColor:layer.backgroundColor];
pendingState.tintColor = view.tintColor;
pendingState.opaque = layer.opaque;
pendingState.hidden = view.hidden;
Expand Down Expand Up @@ -1404,8 +1406,6 @@ - (BOOL)hasChanges

- (void)dealloc
{
CGColorRelease(backgroundColor);

if (shadowColor != blackColorRef) {
CGColorRelease(shadowColor);
}
Expand Down

0 comments on commit 4ebd38c

Please sign in to comment.