Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tinting layer-backed ASDisplayNode #1617

Merged
merged 4 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Source/ASButtonNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ - (ASTextNode *)titleNode
ASLockScopeSelf();
if (!_titleNode) {
_titleNode = [[ASTextNode alloc] init];
// Intentionally not layer-backing the image node since tintColor may be applied
#if TARGET_OS_IOS
// tvOS needs access to the underlying view
// of the button node to add a touch handler.
[_titleNode setLayerBacked:YES];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is from the original impl, but the comment and the code don't add up. If the view is needed under tvOS, then this node should not be layer-backed under it (and can be layer-backed otherwise)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment might be misleading here, I think the intention was to gate this layer backing only for iOS and by doing nothing in tvOS it will automatically be view backed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make this clearer at least

#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for fixing this.

_titleNode.style.flexShrink = 1.0;
_titleNode.textColorFollowsTintColor = YES;
}
Expand All @@ -66,7 +70,7 @@ - (ASImageNode *)imageNode
ASLockScopeSelf();
if (!_imageNode) {
_imageNode = [[ASImageNode alloc] init];
// Intentionally not layer-backing the image node since tintColor may be applied
[_imageNode setLayerBacked:YES];
}
return _imageNode;
}
Expand Down Expand Up @@ -133,7 +137,7 @@ -(void)tintColorDidChange
[super tintColorDidChange];
// UIButton documentation states that it tints the image and title of buttons when tintColor is set.
// | "The tint color to apply to the button title and image."
// | From: https://developer.apple.com/documentation/uikit/uibutton/1624025-tintcolor
// | From: https://developerx.apple.com/documentation/uikit/uibutton/1624025-tintcolor
rahul-malik marked this conversation as resolved.
Show resolved Hide resolved
UIColor *tintColor = self.tintColor;
self.imageNode.tintColor = tintColor;
self.titleNode.tintColor = tintColor;
Expand Down
23 changes: 0 additions & 23 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -290,29 +290,6 @@ - (void)setPlaceholderColor:(UIColor *)placeholderColor
}
}

- (UIColor *)tintColor
{
ASLockScopeSelf();
if (_flags.layerBacked) {
return _tintColor;
} else {
return super.tintColor;
}
}

- (void)setTintColor:(UIColor *)tintColor
{
ASLockScopeSelf();
// CALayer does not have a tintColor property but we can support this capability so use the local ivar instead of the bridge
if (_flags.layerBacked && ![_tintColor isEqual:tintColor]) {
_tintColor = tintColor;
[self setNeedsDisplay];
} else {
// If we are view-backed, rely on the tintColor value from the UIViewBridge
[super setTintColor:tintColor];
}
}

#pragma mark - Drawing

- (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
Expand Down
34 changes: 30 additions & 4 deletions Source/Private/ASDisplayNode+UIViewBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -796,15 +796,41 @@ - (void)setBackgroundColor:(UIColor *)newBackgroundColor
- (UIColor *)tintColor
{
_bridge_prologue_read;
ASDisplayNodeAssert(!_flags.layerBacked, @"Danger: this property is undefined on layer-backed nodes.");
return _getFromViewOnly(tintColor);
if (_loaded(self)) {
if (_flags.layerBacked) {
// The first nondefault tint color value in the view’s hierarchy, ascending from and starting with the view itself.
return _tintColor ?: self.supernode.tintColor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our locking principles don't allow walking up the hierarchy while holding the node's lock. So you'd need to do it after releasing the lock.

} else {
return _getFromViewOnly(tintColor);
}
} else {
if (_flags.layerBacked) {
return _tintColor;
} else {
return ASDisplayNodeGetPendingState(self).tintColor;
}
}
}

- (void)setTintColor:(UIColor *)color
{
_bridge_prologue_write;
ASDisplayNodeAssert(!_flags.layerBacked, @"Danger: this property is undefined on layer-backed nodes.");
_setToViewOnly(tintColor, color);
if (_loaded(self)) {
if (_flags.layerBacked) {
if (![_tintColor isEqual:color]) {
_tintColor = color;
[self tintColorDidChange];
}
} else {
_setToViewOnly(tintColor, color);
}
} else {
if (_flags.layerBacked) {
_tintColor = color;
} else {
ASDisplayNodeGetPendingState(self).tintColor = color;
}
}
}

- (void)tintColorDidChange
Expand Down
3 changes: 3 additions & 0 deletions Source/Private/ASDisplayNodeInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ static constexpr CACornerMask kASCACornerAllCorners =
// These properties are used on iOS 10 and lower, where safe area is not supported by UIKit.
UIEdgeInsets _fallbackSafeAreaInsets;

// Tinting support
UIColor *_tintColor;

#pragma mark - ASDisplayNode (Debugging)
ASLayout *_unflattenedLayout;

Expand Down
35 changes: 35 additions & 0 deletions Tests/ASImageNodeSnapshotTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,41 @@ - (void)testTintColorOnNodePropertyAlwaysTemplateLayerBackedNode
ASSnapshotVerifyNode(node, nil);
}


- (void)testTintColorInheritsFromSupernodeLayerBacked
{
// Test support for layerBacked image node tinting
ASDisplayNode *container = [[ASDisplayNode alloc] init];
[container setLayerBacked:YES];
container.tintColor = UIColor.redColor;
UIImage *test = [self testImage];
ASImageNode *node = [[ASImageNode alloc] init];
[node setLayerBacked:YES];
node.tintColor = UIColor.redColor;
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[container addSubnode:node];
container.style.preferredSize = test.size;
ASDisplayNodeSizeToFitSize(node, test.size);
ASSnapshotVerifyNode(node, nil);
}

- (void)testTintColorInheritsFromSupernodeViewBacked
{
// Test support for layerBacked image node tinting
ASDisplayNode *container = [[ASDisplayNode alloc] init];
[container setLayerBacked:NO];
container.tintColor = UIColor.redColor;
UIImage *test = [self testImage];
ASImageNode *node = [[ASImageNode alloc] init];
[node setLayerBacked:YES];
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[container addSubnode:node];
container.style.preferredSize = test.size;
ASDisplayNodeSizeToFitSize(node, test.size);
ASSnapshotVerifyNode(node, nil);
}


- (void)testRoundedCornerBlock
{
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.