Skip to content

Commit d8aa8c7

Browse files
Resolve dynamic background colors against the node's trait collection (behind experiment flag) (#2142)
* Resolve dynamic background colors against node's trait collection Layer-backed nodes use CGColorRef instead of UIColor, so dynamic (e.g. dark mode) background colors were not resolved for the current trait collection. Resolve the UIColor via resolvedColorWithTraitCollection: using the node's primitive trait collection before extracting the CGColor, both when setting the background color and when reapplying it on trait collection changes. * Create ResolveBackgroundColorWithNodeTraits experiment * Put resolve background colors for UIViewBridge behind an experiment flag
1 parent f9f298f commit d8aa8c7

4 files changed

Lines changed: 17 additions & 6 deletions

File tree

Source/ASDisplayNode.mm

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,17 @@ - (void)asyncTraitCollectionDidChangeWithPreviousTraitCollection:(ASPrimitiveTra
458458
CGFloat cornerRadius = self->_cornerRadius;
459459
ASCornerRoundingType cornerRoundingType = self->_cornerRoundingType;
460460
UIColor *backgroundColor = self->_backgroundColor;
461+
ASPrimitiveTraitCollection currentPrimitiveTraitCollection = self->_primitiveTraitCollection;
461462
self->__instanceLock__.unlock();
462-
// TODO: we should resolve color using node's trait collection
463-
// but Texture changes it from many places, so we may receive the wrong one.
464-
CGColorRef cgBackgroundColor = backgroundColor.CGColor;
463+
CGColorRef cgBackgroundColor;
464+
if (ASActivateExperimentalFeature(ASExperimentalResolveBackgroundColorWithNodeTraits)) {
465+
UITraitCollection *traitCollection = ASPrimitiveTraitCollectionToUITraitCollection(currentPrimitiveTraitCollection);
466+
cgBackgroundColor = [backgroundColor resolvedColorWithTraitCollection:traitCollection].CGColor;
467+
} else {
468+
// TODO: we should resolve color using node's trait collection
469+
// but Texture changes it from many places, so we may receive the wrong one.
470+
cgBackgroundColor = backgroundColor.CGColor;
471+
}
465472
if (!CGColorEqualToColor(self->_layer.backgroundColor, cgBackgroundColor)) {
466473
// Background colors do not dynamically update for layer backed nodes since they utilize CGColorRef
467474
// instead of UIColor. Non layer backed node also receive color to the layer (see [_ASPendingState -applyToView:withSpecialPropertiesHandling:]).

Source/ASExperimentalFeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef NS_OPTIONS(NSUInteger, ASExperimentalFeatures) {
3535
ASExperimentalLockTextRendererCache = 1 << 14, // exp_lock_text_renderer_cache
3636
ASExperimentalHierarchyDisplayDidFinishIsRecursive = 1 << 15, // exp_hierarchy_display_did_finish_is_recursive
3737
ASExperimentalCheckBatchFetchingOnScroll = 1 << 16, // exp_check_batch_fetching_on_scroll
38+
ASExperimentalResolveBackgroundColorWithNodeTraits = 1 << 17, // exp_resolve_background_color_with_node_traits
3839
ASExperimentalFeatureAll = 0xFFFFFFFF
3940
};
4041

Source/ASExperimentalFeatures.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
@"exp_no_text_renderer_cache",
2929
@"exp_lock_text_renderer_cache",
3030
@"exp_hierarchy_display_did_finish_is_recursive",
31-
@"exp_check_batch_fetching_on_scroll"]));
31+
@"exp_check_batch_fetching_on_scroll",
32+
@"exp_resolve_background_color_with_node_traits"]));
3233

3334
if (flags == ASExperimentalFeatureAll) {
3435
return allNames;

Source/Private/ASDisplayNode+UIViewBridge.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,10 @@ - (void)setBackgroundColor:(UIColor *)newBackgroundColor
762762
if (shouldApply) {
763763
UIColor *oldBackgroundColor = _backgroundColor;
764764
_backgroundColor = newBackgroundColor;
765+
BOOL resolveWithNodeTraits = ASActivateExperimentalFeature(ASExperimentalResolveBackgroundColorWithNodeTraits);
766+
UITraitCollection *traitCollection = resolveWithNodeTraits ? ASPrimitiveTraitCollectionToUITraitCollection(_primitiveTraitCollection) : nil;
765767
if (_flags.layerBacked) {
766-
_layer.backgroundColor = _backgroundColor.CGColor;
768+
_layer.backgroundColor = resolveWithNodeTraits ? [_backgroundColor resolvedColorWithTraitCollection:traitCollection].CGColor : _backgroundColor.CGColor;
767769
} else {
768770
/*
769771
NOTE: Setting to the view and layer individually is necessary.
@@ -775,7 +777,7 @@ - (void)setBackgroundColor:(UIColor *)newBackgroundColor
775777
*/
776778
_view.backgroundColor = _backgroundColor;
777779
// Gather the CGColorRef from the view incase there are any changes it might apply to which CGColorRef is returned for dynamic colors
778-
_layer.backgroundColor = _view.backgroundColor.CGColor;
780+
_layer.backgroundColor = resolveWithNodeTraits ? [_backgroundColor resolvedColorWithTraitCollection:traitCollection].CGColor : _view.backgroundColor.CGColor;
779781
}
780782

781783
if (![oldBackgroundColor isEqual:newBackgroundColor]) {

0 commit comments

Comments
 (0)