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

Store backgroundColor in ASDisplayNode #1640

Merged
merged 3 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions Source/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ - (void)asyncTraitCollectionDidChangeWithPreviousTraitCollection:(ASPrimitiveTra
if (self.primitiveTraitCollection.userInterfaceStyle != previousTraitCollection.userInterfaceStyle) {
// When changing between light and dark mode, often the entire node needs to re-render.
// This change doesn't happen frequently so it's fairly safe to render nodes again
if (_loaded(self) && self.isLayerBacked && _backgroundColor) {
CGColorRef cgBackgroundColor = _backgroundColor.CGColor;
if (!CGColorEqualToColor(_layer.backgroundColor, cgBackgroundColor)) {
_layer.backgroundColor = cgBackgroundColor;
}
}
__instanceLock__.unlock();
[self setNeedsDisplay];
return;
Expand Down
20 changes: 8 additions & 12 deletions Source/Private/ASDisplayNode+UIViewBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -748,24 +748,20 @@ - (UIColor *)backgroundColor
*/
if (!_flags.layerBacked) {
return _view.backgroundColor;
} else {
return [UIColor colorWithCGColor:_layer.backgroundColor];
}
}
return ASDisplayNodeGetPendingState(self).backgroundColor;
return _backgroundColor;
}

- (void)setBackgroundColor:(UIColor *)newBackgroundColor
{
_bridge_prologue_write;
BOOL shouldApply = ASDisplayNodeShouldApplyBridgedWriteToView(self);
CGColorRef newBackgroundCGColor = nil;
if (shouldApply) {
CGColorRef oldBackgroundCGColor = _layer.backgroundColor;

UIColor *oldBackgroundColor = _backgroundColor;
_backgroundColor = newBackgroundColor;
if (_flags.layerBacked) {
_layer.backgroundColor = newBackgroundColor.CGColor;
newBackgroundCGColor = _layer.backgroundColor;
_layer.backgroundColor = _backgroundColor.CGColor;
} else {
/*
NOTE: Setting to the view and layer individually is necessary.
Expand All @@ -775,19 +771,19 @@ - (void)setBackgroundColor:(UIColor *)newBackgroundColor
Given that UIColor / UIView has dynamic capabilties now, we should set directly to the view and make sure that the layers value is consistent here.

*/
_view.backgroundColor = newBackgroundColor;
_view.backgroundColor = _backgroundColor;
// 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;
_layer.backgroundColor = _view.backgroundColor.CGColor;
}

if (!CGColorEqualToColor(oldBackgroundCGColor, newBackgroundCGColor)) {
if (![oldBackgroundColor isEqual:newBackgroundColor]) {
[self setNeedsDisplay];
}
} else {
// 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.
_backgroundColor = newBackgroundColor;
ASDisplayNodeGetPendingState(self).backgroundColor = newBackgroundColor;
}
}
Expand Down
8 changes: 6 additions & 2 deletions Source/Private/ASDisplayNodeInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ static constexpr CACornerMask kASCACornerAllCorners =
ASCornerRoundingType _cornerRoundingType;
ASDisplayNodePerformanceMeasurementOptions _measurementOptions;
ASDisplayNodeMethodOverrides _methodOverrides;
// Tinting support
UIColor *_tintColor;

// Dynamic colors support
UIColor *_backgroundColor;

@protected
ASDisplayNode * __weak _supernode;
Expand Down Expand Up @@ -253,8 +258,7 @@ 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
51 changes: 28 additions & 23 deletions Tests/ASDisplayNodeSnapshotTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,33 @@ - (void)testClippingCornerRounding
}
}

// Enable test when background color on ASDisplayNode PR merges
//- (void)testUserInterfaceStyleSnapshotTesting
//{
// if (@available(iOS 13.0, *)) {
// UITraitCollection.currentTraitCollection = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight];
//
// ASDisplayNode *node = [[ASDisplayNode alloc] init];
// [node setLayerBacked:YES];
//
// node.backgroundColor = [UIColor systemBackgroundColor];
//
// node.style.preferredSize = CGSizeMake(100, 100);
// ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY, INFINITY)));
//
// [[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight] performAsCurrentTraitCollection:^{
// ASSnapshotVerifyNode(node, @"user_interface_style_light");
// }];
//
// [[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark] performAsCurrentTraitCollection:^{
// ASSnapshotVerifyNode(node, @"user_interface_style_dark");
// }];
// }
//}
#if AS_AT_LEAST_IOS13

- (void)testUserInterfaceStyleSnapshotTesting
{
if (@available(iOS 13.0, *)) {
ASConfiguration *config = [ASConfiguration new];
config.experimentalFeatures = ASExperimentalTraitCollectionDidChangeWithPreviousCollection;
[ASConfigurationManager test_resetWithConfiguration:config];

ASDisplayNode *node = [[ASDisplayNode alloc] init];
[node setLayerBacked:YES];

node.backgroundColor = [UIColor systemBackgroundColor];

node.style.preferredSize = CGSizeMake(100, 100);
ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY, INFINITY)));

[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, @"user_interface_style_light");
}];

[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, @"user_interface_style_dark");
}];
}
}

#endif // #if AS_AT_LEAST_IOS13

@end
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.