From 287747cc044d62fc44920dd3c93c0c5596302776 Mon Sep 17 00:00:00 2001 From: Vlad Solomenchuk Date: Tue, 1 Oct 2019 11:31:21 -0700 Subject: [PATCH] fix a node background color in non layer baked mode. --- Source/ASDisplayNode.mm | 11 ++-- Source/Private/_ASPendingState.mm | 2 +- Tests/ASDisplayNodeSnapshotTests.mm | 50 ++++++++++++++++-- ...kgroundDynamicColorLayerBacked_dark@2x.png | Bin 0 -> 1547 bytes ...groundDynamicColorLayerBacked_light@2x.png | Bin 0 -> 1547 bytes .../testBackgroundDynamicColor_dark@2x.png | Bin 0 -> 1547 bytes .../testBackgroundDynamicColor_light@2x.png | Bin 0 -> 1547 bytes 7 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColorLayerBacked_dark@2x.png create mode 100644 Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColorLayerBacked_light@2x.png create mode 100644 Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColor_dark@2x.png create mode 100644 Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColor_light@2x.png diff --git a/Source/ASDisplayNode.mm b/Source/ASDisplayNode.mm index ffbdc07ec..93798051d 100644 --- a/Source/ASDisplayNode.mm +++ b/Source/ASDisplayNode.mm @@ -447,14 +447,13 @@ - (void)asyncTraitCollectionDidChangeWithPreviousTraitCollection:(ASPrimitiveTra ASCornerRoundingType cornerRoundingType = _cornerRoundingType; UIColor *backgroundColor = _backgroundColor; if (_loaded(self)) { - if (self.isLayerBacked) { + CGColorRef cgBackgroundColor = backgroundColor.CGColor; + if (!CGColorEqualToColor(_layer.backgroundColor, cgBackgroundColor)) { // Background colors do not dynamically update for layer backed nodes since they utilize CGColorRef - // instead of UIColor. We utilize the _backgroundColor instance variable to track the full dynamic color + // instead of UIColor. Non layer backed node also receive color to the layer (see [_ASPendingState -applyToView:withSpecialPropertiesHandling:]). + // We utilize the _backgroundColor instance variable to track the full dynamic color // and apply any changes here when trait collection updates occur. - CGColorRef cgBackgroundColor = backgroundColor.CGColor; - if (!CGColorEqualToColor(_layer.backgroundColor, cgBackgroundColor)) { - _layer.backgroundColor = cgBackgroundColor; - } + _layer.backgroundColor = cgBackgroundColor; } // If we have clipping corners, re-render the clipping corner layer upon user interface style change if (cornerRoundingType == ASCornerRoundingTypeClipping && cornerRadius > 0.0f) { diff --git a/Source/Private/_ASPendingState.mm b/Source/Private/_ASPendingState.mm index 7d8404447..8593bd583 100644 --- a/Source/Private/_ASPendingState.mm +++ b/Source/Private/_ASPendingState.mm @@ -1334,7 +1334,7 @@ + (_ASPendingState *)pendingViewStateFromView:(UIView *)view pendingState.contentsScale = layer.contentsScale; pendingState.rasterizationScale = layer.rasterizationScale; pendingState.clipsToBounds = view.clipsToBounds; - pendingState.backgroundColor = [UIColor colorWithCGColor:layer.backgroundColor]; + pendingState.backgroundColor = view.backgroundColor; pendingState.tintColor = view.tintColor; pendingState.opaque = layer.opaque; pendingState.hidden = view.hidden; diff --git a/Tests/ASDisplayNodeSnapshotTests.mm b/Tests/ASDisplayNodeSnapshotTests.mm index 28a16d113..07ebbc632 100644 --- a/Tests/ASDisplayNodeSnapshotTests.mm +++ b/Tests/ASDisplayNodeSnapshotTests.mm @@ -16,6 +16,13 @@ @interface ASDisplayNodeSnapshotTests : ASSnapshotTestCase @implementation ASDisplayNodeSnapshotTests +- (void)setUp { + [super setUp]; + ASConfiguration *config = [ASConfiguration new]; + config.experimentalFeatures = ASExperimentalTraitCollectionDidChangeWithPreviousCollection; + [ASConfigurationManager test_resetWithConfiguration:config]; +} + - (void)testBasicHierarchySnapshotTesting { ASDisplayNode *node = [[ASDisplayNode alloc] init]; @@ -112,10 +119,6 @@ - (void)testClippingCornerRounding - (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]; @@ -134,6 +137,45 @@ - (void)testUserInterfaceStyleSnapshotTesting } } +- (void)testBackgroundDynamicColor { + if (@available(iOS 13.0, *)) { + ASDisplayNode *node = [[ASImageNode alloc] init]; + node.backgroundColor = [UIColor systemGray6Color]; + auto bounds = CGRectMake(0, 0, 100, 100); + node.frame = bounds; + + UITraitCollection *tcLight = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]; + [tcLight performAsCurrentTraitCollection: ^{ + ASSnapshotVerifyNode(node, @"light"); + }]; + + UITraitCollection *tcDark = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]; + [tcDark performAsCurrentTraitCollection: ^{ + ASSnapshotVerifyNode(node, @"dark"); + }]; + } +} + +- (void)testBackgroundDynamicColorLayerBacked { + if (@available(iOS 13.0, *)) { + ASDisplayNode *node = [[ASImageNode alloc] init]; + node.backgroundColor = [UIColor systemGray6Color]; + node.layerBacked = YES; + auto bounds = CGRectMake(0, 0, 100, 100); + node.frame = bounds; + + UITraitCollection *tcLight = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]; + [tcLight performAsCurrentTraitCollection: ^{ + ASSnapshotVerifyNode(node, @"light"); + }]; + + UITraitCollection *tcDark = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]; + [tcDark performAsCurrentTraitCollection: ^{ + ASSnapshotVerifyNode(node, @"dark"); + }]; + } +} + #endif // #if AS_AT_LEAST_IOS13 @end diff --git a/Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColorLayerBacked_dark@2x.png b/Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColorLayerBacked_dark@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..2516cc86c857700bd7fcfdb2e5d5ff6fe32e1712 GIT binary patch literal 1547 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST_$yF%}28J29*~C-V}>sYs3RO!M_+ z&;qhK7+4sk7+66{fY^>v8qRiO)L>u+i!(7Wv}ZD~K-KsGX%LtI#2`Hont1^uR5eg+ z0TW#Hi3Q9EHb|T2wN?`##aZAHS1sUI->0- zf3o=QJ<bP0l+XkK5|whJ literal 0 HcmV?d00001 diff --git a/Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColorLayerBacked_light@2x.png b/Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColorLayerBacked_light@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..ca7d38e4398901463c8092a7f61f09e43c616c11 GIT binary patch literal 1547 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST_$yF%}28J29*~C-V}>sYs3RO!M_+ z&;qhK7+4sk7+66{fY^>v8qRiO)L>u+i!(7Wv}ZD~K-KsGX%LtI#2`Hont1^uR5eg+ z0TW#Hi3Q9EHb|T2wN?`##aZAHS1sUI->0- zf3o=QJ<sYs3RO!M_+ z&;qhK7+4sk7+66{fY^>v8qRiO)L>u+i!(7Wv}ZD~K-KsGX%LtI#2`Hont1^uR5eg+ z0TW#Hi3Q9EHb|T2wN?`##aZAHS1sUI->0- zf3o=QJ<bP0l+XkK5|whJ literal 0 HcmV?d00001 diff --git a/Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColor_light@2x.png b/Tests/ReferenceImages_iOS_10/ASDisplayNodeSnapshotTests/testBackgroundDynamicColor_light@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..ca7d38e4398901463c8092a7f61f09e43c616c11 GIT binary patch literal 1547 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST_$yF%}28J29*~C-V}>sYs3RO!M_+ z&;qhK7+4sk7+66{fY^>v8qRiO)L>u+i!(7Wv}ZD~K-KsGX%LtI#2`Hont1^uR5eg+ z0TW#Hi3Q9EHb|T2wN?`##aZAHS1sUI->0- zf3o=QJ<