Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
60 changes: 57 additions & 3 deletions AsyncDisplayKit/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,40 @@ - (void)dealloc

#pragma mark - Core

- (void)__tearDown:(BOOL)tearDown subnodesOfNode:(ASDisplayNode *)node
{
for (ASDisplayNode *subnode in node.subnodes) {
if (tearDown) {
[subnode __unloadNode];
} else {
[subnode __loadNode];
}
}
}

- (void)__unloadNode
{
ASDisplayNodeAssertThreadAffinity(self);
ASDN::MutexLocker l(_propertyLock);

if (_flags.layerBacked)
_pendingViewState = [_ASPendingState pendingViewStateFromLayer:_layer];
else
_pendingViewState = [_ASPendingState pendingViewStateFromView:_view];

[_view removeFromSuperview];
_view = nil;
if (_flags.layerBacked)
_layer.delegate = nil;
[_layer removeFromSuperlayer];
_layer = nil;
}

- (void)__loadNode
{
[self layer];
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems a little sparse, but smart :). Multiple loads and unloads of a node is a first for the public ASDK, something we had some challenges implementing a while back when building prototypes of view reuse. I assume each time will use layerBlock or viewBlock accordingly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It appears so.

}

- (ASDisplayNode *)__rasterizedContainerNode
{
ASDisplayNode *node = self.supernode;
Expand All @@ -340,7 +374,7 @@ - (ASDisplayNode *)__rasterizedContainerNode
}
node = node.supernode;
}

return nil;
}

Expand Down Expand Up @@ -619,11 +653,22 @@ - (void)setShouldRasterizeDescendants:(BOOL)flag
{
ASDisplayNodeAssertThreadAffinity(self);
ASDN::MutexLocker l(_propertyLock);

if (_flags.shouldRasterizeDescendants == flag)
return;

_flags.shouldRasterizeDescendants = flag;

if (self.isNodeLoaded) {
//recursively tear down or build up subnodes
[self recursivelyClearContents];
[self __tearDown:flag subnodesOfNode:self];
if (flag == NO) {
[self _addSubnodeViewsAndLayers];
}

[self recursivelyDisplayImmediately];
}
}

- (CGFloat)contentsScaleForDisplay
Expand Down Expand Up @@ -653,6 +698,15 @@ - (void)displayImmediately
[[self asyncLayer] displayImmediately];
}

- (void)recursivelyDisplayImmediately
Copy link
Contributor

Choose a reason for hiding this comment

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

Check out -recursivelyEnsureDisplay; will it do what you need? If so (I am not confident with this quick review), it would be notably faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I replace recursivelyDisplayImmediately with recursivelySetNeedsDisplay and recursivelyEnsureDisplay it doesn’t seem to complete synchronously, resulting in the UI flashing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, that's exceedingly odd. I'm OK with landing this approach, but will consider it a followup for me to debug that issue and consolidate the display mechanisms. Thank you for trying it.

{
ASDN::MutexLocker l(_propertyLock);
for (ASDisplayNode *child in _subnodes) {
[child recursivelyDisplayImmediately];
}
[self displayImmediately];
}

- (void)__setNeedsLayout
{
ASDisplayNodeAssertThreadAffinity(self);
Expand Down
3 changes: 3 additions & 0 deletions AsyncDisplayKit/Private/_ASPendingState.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
- (void)applyToView:(UIView *)view;
- (void)applyToLayer:(CALayer *)layer;

+ (_ASPendingState *)pendingViewStateFromLayer:(CALayer *)layer;
+ (_ASPendingState *)pendingViewStateFromView:(UIView *)view;

@end
208 changes: 208 additions & 0 deletions AsyncDisplayKit/Private/_ASPendingState.m
Original file line number Diff line number Diff line change
Expand Up @@ -796,4 +796,212 @@ - (void)applyToView:(UIView *)view
view.accessibilityIdentifier = accessibilityIdentifier;
}

+ (_ASPendingState *)pendingViewStateFromLayer:(CALayer *)layer
{
_ASPendingState *pendingState = [[_ASPendingState alloc] init];

pendingState.anchorPoint = layer.anchorPoint;
(pendingState->_flags).setAnchorPoint = YES;

pendingState.position = layer.position;
(pendingState->_flags).setPosition = YES;

pendingState.zPosition = layer.zPosition;
(pendingState->_flags).setZPosition = YES;

pendingState.bounds = layer.bounds;
(pendingState->_flags).setBounds = YES;

pendingState.contentsScale = layer.contentsScale;
(pendingState->_flags).setContentsScale = YES;

pendingState.transform = layer.transform;
(pendingState->_flags).setTransform = YES;

pendingState.sublayerTransform = layer.sublayerTransform;
(pendingState->_flags).setSublayerTransform = YES;

pendingState.contents = layer.contents;
(pendingState->_flags).setContents = YES;

pendingState.clipsToBounds = layer.masksToBounds;
(pendingState->_flags).setClipsToBounds = YES;

pendingState.backgroundColor = layer.backgroundColor;
(pendingState->_flags).setBackgroundColor = YES;

pendingState.opaque = layer.opaque;
(pendingState->_flags).setOpaque = YES;

pendingState.hidden = layer.hidden;
(pendingState->_flags).setHidden = YES;

pendingState.alpha = layer.opacity;
(pendingState->_flags).setAlpha = YES;

pendingState.cornerRadius = layer.cornerRadius;
(pendingState->_flags).setCornerRadius = YES;

pendingState.contentMode = ASDisplayNodeUIContentModeFromCAContentsGravity(layer.contentsGravity);
(pendingState->_flags).setContentMode = YES;

pendingState.shadowColor = layer.shadowColor;
(pendingState->_flags).setShadowColor = YES;

pendingState.shadowOpacity = layer.shadowOpacity;
(pendingState->_flags).setShadowOpacity = YES;

pendingState.shadowOffset = layer.shadowOffset;
(pendingState->_flags).setShadowOffset = YES;

pendingState.shadowRadius = layer.shadowRadius;
(pendingState->_flags).setShadowRadius = YES;

pendingState.borderWidth = layer.borderWidth;
(pendingState->_flags).setBorderWidth = YES;

pendingState.borderColor = layer.borderColor;
(pendingState->_flags).setBorderColor = YES;

pendingState.needsDisplayOnBoundsChange = layer.needsDisplayOnBoundsChange;
(pendingState->_flags).setNeedsDisplayOnBoundsChange = YES;

pendingState.allowsEdgeAntialiasing = layer.allowsEdgeAntialiasing;
(pendingState->_flags).setAllowsEdgeAntialiasing = YES;

pendingState.edgeAntialiasingMask = layer.edgeAntialiasingMask;
(pendingState->_flags).setEdgeAntialiasingMask = YES;

return pendingState;
}

+ (_ASPendingState *)pendingViewStateFromView:(UIView *)view
{
_ASPendingState *pendingState = [[_ASPendingState alloc] init];

CALayer *layer = view.layer;

pendingState.anchorPoint = layer.anchorPoint;
(pendingState->_flags).setAnchorPoint = YES;

pendingState.position = layer.position;
(pendingState->_flags).setPosition = YES;

pendingState.zPosition = layer.zPosition;
(pendingState->_flags).setZPosition = YES;

pendingState.bounds = view.bounds;
(pendingState->_flags).setBounds = YES;

pendingState.contentsScale = layer.contentsScale;
(pendingState->_flags).setContentsScale = YES;

pendingState.transform = layer.transform;
(pendingState->_flags).setTransform = YES;

pendingState.sublayerTransform = layer.sublayerTransform;
(pendingState->_flags).setSublayerTransform = YES;

pendingState.contents = layer.contents;
(pendingState->_flags).setContents = YES;

pendingState.clipsToBounds = view.clipsToBounds;
(pendingState->_flags).setClipsToBounds = YES;

pendingState.backgroundColor = layer.backgroundColor;
(pendingState->_flags).setBackgroundColor = YES;

pendingState.tintColor = view.tintColor;
(pendingState->_flags).setTintColor = YES;

pendingState.opaque = layer.opaque;
(pendingState->_flags).setOpaque = YES;

pendingState.hidden = view.hidden;
(pendingState->_flags).setHidden = YES;

pendingState.alpha = view.alpha;
(pendingState->_flags).setAlpha = YES;

pendingState.cornerRadius = layer.cornerRadius;
(pendingState->_flags).setCornerRadius = YES;

pendingState.contentMode = view.contentMode;
(pendingState->_flags).setContentMode = YES;

pendingState.userInteractionEnabled = view.userInteractionEnabled;
(pendingState->_flags).setUserInteractionEnabled = YES;

pendingState.exclusiveTouch = view.exclusiveTouch;
(pendingState->_flags).setExclusiveTouch = YES;

pendingState.shadowColor = layer.shadowColor;
(pendingState->_flags).setShadowColor = YES;

pendingState.shadowOpacity = layer.shadowOpacity;
(pendingState->_flags).setShadowOpacity = YES;

pendingState.shadowOffset = layer.shadowOffset;
(pendingState->_flags).setShadowOffset = YES;

pendingState.shadowRadius = layer.shadowRadius;
(pendingState->_flags).setShadowRadius = YES;

pendingState.borderWidth = layer.borderWidth;
(pendingState->_flags).setBorderWidth = YES;

pendingState.borderColor = layer.borderColor;
(pendingState->_flags).setBorderColor = YES;

pendingState.autoresizingMask = view.autoresizingMask;
(pendingState->_flags).setAutoresizingMask = YES;

pendingState.autoresizesSubviews = view.autoresizesSubviews;
(pendingState->_flags).setAutoresizesSubviews = YES;

pendingState.needsDisplayOnBoundsChange = layer.needsDisplayOnBoundsChange;
(pendingState->_flags).setNeedsDisplayOnBoundsChange = YES;

pendingState.allowsEdgeAntialiasing = layer.allowsEdgeAntialiasing;
(pendingState->_flags).setAllowsEdgeAntialiasing = YES;

pendingState.edgeAntialiasingMask = layer.edgeAntialiasingMask;
(pendingState->_flags).setEdgeAntialiasingMask = YES;

pendingState.isAccessibilityElement = view.isAccessibilityElement;
(pendingState->_flags).setIsAccessibilityElement = YES;

pendingState.accessibilityLabel = view.accessibilityLabel;
(pendingState->_flags).setAccessibilityLabel = YES;

pendingState.accessibilityHint = view.accessibilityHint;
(pendingState->_flags).setAccessibilityHint = YES;

pendingState.accessibilityValue = view.accessibilityValue;
(pendingState->_flags).setAccessibilityValue = YES;

pendingState.accessibilityTraits = view.accessibilityTraits;
(pendingState->_flags).setAccessibilityTraits = YES;

pendingState.accessibilityFrame = view.accessibilityFrame;
(pendingState->_flags).setAccessibilityFrame = YES;

pendingState.accessibilityLanguage = view.accessibilityLanguage;
(pendingState->_flags).setAccessibilityLanguage = YES;

pendingState.accessibilityElementsHidden = view.accessibilityElementsHidden;
(pendingState->_flags).setAccessibilityElementsHidden = YES;

pendingState.accessibilityViewIsModal = view.accessibilityViewIsModal;
(pendingState->_flags).setAccessibilityViewIsModal = YES;

pendingState.shouldGroupAccessibilityChildren = view.shouldGroupAccessibilityChildren;
(pendingState->_flags).setShouldGroupAccessibilityChildren = YES;

pendingState.accessibilityIdentifier = view.accessibilityIdentifier;
(pendingState->_flags).setAccessibilityIdentifier = YES;

return pendingState;
}

@end
6 changes: 6 additions & 0 deletions AsyncDisplayKitTests/ASSnapshotTestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
{ \
[ASSnapshotTestCase hackilySynchronouslyRecursivelyRenderNode:node__]; \
FBSnapshotVerifyLayer(node__.layer, identifier__); \
[node__ setShouldRasterizeDescendants:YES]; \
[ASSnapshotTestCase hackilySynchronouslyRecursivelyRenderNode:node__]; \
FBSnapshotVerifyLayer(node__.layer, identifier__); \
[node__ setShouldRasterizeDescendants:NO]; \
[ASSnapshotTestCase hackilySynchronouslyRecursivelyRenderNode:node__]; \
FBSnapshotVerifyLayer(node__.layer, identifier__); \
}

@interface ASSnapshotTestCase : FBSnapshotTestCase
Expand Down