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 3 commits
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
8 changes: 6 additions & 2 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
1 change: 1 addition & 0 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ @implementation ASImageNode
UIImage *_image;
ASWeakMapEntry *_weakCacheEntry; // Holds a reference that keeps our contents in cache.
UIColor *_placeholderColor;
UIColor *_tintColor; // Used to cache color information for layer backed nodes. View backed will use UIViewBridge

void (^_displayCompletionBlock)(BOOL canceled);

Expand Down
37 changes: 33 additions & 4 deletions Source/Private/ASDisplayNode+UIViewBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -796,15 +796,44 @@ - (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;
// Tint color has changed. Unlock here before calling subclasses and exit-early
AS::MutexLocker unlock(__instanceLock__);
Copy link
Member

Choose a reason for hiding this comment

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

This might not be safe because the MutexLocker (which basically is std::lock_guard) will try to unlock again when it's destroyed and yield an exception. Instead I think it'd be easier to just manage the lock manually (i.e remove _bridge_prologue_write and this line, and call lock/unlock directly on __instanceLock__.

[self tintColorDidChange];
return;
}
} 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
46 changes: 46 additions & 0 deletions Tests/ASImageNodeSnapshotTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,52 @@ - (void)testTintColorOnNodePropertyAlwaysOriginal
ASSnapshotVerifyNode(node, nil);
}

- (void)testTintColorOnNodePropertyAlwaysTemplateLayerBackedNode
{
// Test support for layerBacked image node tinting
UIImage *test = [self testImage];
ASImageNode *node = [[ASImageNode alloc] init];
[node setLayerBacked:YES];
node.tintColor = UIColor.redColor;
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
ASDisplayNodeSizeToFitSize(node, test.size);
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.