-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 clipping only specific corners, add unit tests #1415
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,6 +324,7 @@ - (void)_initializeInstance | |
|
||
_contentsScaleForDisplay = ASScreenScale(); | ||
_drawingPriority = ASDefaultTransactionPriority; | ||
_maskedCorners = kASCACornerAllCorners; | ||
|
||
_primitiveTraitCollection = ASPrimitiveTraitCollectionMakeDefault(); | ||
|
||
|
@@ -1526,17 +1527,17 @@ - (void)recursivelySetNeedsDisplayAtScale:(CGFloat)contentsScale | |
- (void)_layoutClipCornersIfNeeded | ||
{ | ||
ASDisplayNodeAssertMainThread(); | ||
if (_clipCornerLayers[0] == nil) { | ||
if (_clipCornerLayers[0] == nil && _clipCornerLayers[1] == nil && _clipCornerLayers[2] == nil && | ||
_clipCornerLayers[3] == nil) { | ||
return; | ||
} | ||
|
||
CGSize boundsSize = self.bounds.size; | ||
for (int idx = 0; idx < NUM_CLIP_CORNER_LAYERS; idx++) { | ||
BOOL isTop = (idx == 0 || idx == 1); | ||
BOOL isRight = (idx == 1 || idx == 2); | ||
BOOL isRight = (idx == 1 || idx == 3); | ||
if (_clipCornerLayers[idx]) { | ||
// Note the Core Animation coordinates are reversed for y; 0 is at the bottom. | ||
_clipCornerLayers[idx].position = CGPointMake(isRight ? boundsSize.width : 0.0, isTop ? boundsSize.height : 0.0); | ||
_clipCornerLayers[idx].position = CGPointMake(isRight ? boundsSize.width : 0.0, isTop ? 0.0 : boundsSize.height); | ||
[_layer addSublayer:_clipCornerLayers[idx]]; | ||
} | ||
} | ||
|
@@ -1546,78 +1547,87 @@ - (void)_updateClipCornerLayerContentsWithRadius:(CGFloat)radius backgroundColor | |
{ | ||
ASPerformBlockOnMainThread(^{ | ||
for (int idx = 0; idx < NUM_CLIP_CORNER_LAYERS; idx++) { | ||
// Layers are, in order: Top Left, Top Right, Bottom Right, Bottom Left. | ||
// Skip corners that aren't clipped (we have already set up & torn down layers based on maskedCorners.) | ||
if (_clipCornerLayers[idx] == nil) { | ||
continue; | ||
} | ||
|
||
// Layers are, in order: Top Left, Top Right, Bottom Left, Bottom Right, which mirrors CACornerMask. | ||
// anchorPoint is Bottom Left at 0,0 and Top Right at 1,1. | ||
BOOL isTop = (idx == 0 || idx == 1); | ||
BOOL isRight = (idx == 1 || idx == 2); | ||
BOOL isRight = (idx == 1 || idx == 3); | ||
|
||
CGSize size = CGSizeMake(radius + 1, radius + 1); | ||
ASGraphicsBeginImageContextWithOptions(size, NO, self.contentsScaleForDisplay); | ||
|
||
CGContextRef ctx = UIGraphicsGetCurrentContext(); | ||
if (isRight == YES) { | ||
CGContextTranslateCTM(ctx, -radius + 1, 0); | ||
} | ||
if (isTop == YES) { | ||
if (isTop == NO) { | ||
CGContextTranslateCTM(ctx, 0, -radius + 1); | ||
} | ||
|
||
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, radius * 2, radius * 2) cornerRadius:radius]; | ||
[roundedRect setUsesEvenOddFillRule:YES]; | ||
[roundedRect appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(-1, -1, radius * 2 + 1, radius * 2 + 1)]]; | ||
[backgroundColor setFill]; | ||
[roundedRect fill]; | ||
|
||
// No lock needed, as _clipCornerLayers is only modified on the main thread. | ||
CALayer *clipCornerLayer = _clipCornerLayers[idx]; | ||
unowned CALayer *clipCornerLayer = _clipCornerLayers[idx]; | ||
clipCornerLayer.contents = (id)(ASGraphicsGetImageAndEndCurrentContext().CGImage); | ||
clipCornerLayer.bounds = CGRectMake(0.0, 0.0, size.width, size.height); | ||
clipCornerLayer.anchorPoint = CGPointMake(isRight ? 1.0 : 0.0, isTop ? 1.0 : 0.0); | ||
clipCornerLayer.anchorPoint = CGPointMake(isRight ? 1.0 : 0.0, isTop ? 0.0 : 1.0); | ||
} | ||
[self _layoutClipCornersIfNeeded]; | ||
}); | ||
} | ||
|
||
- (void)_setClipCornerLayersVisible:(BOOL)visible | ||
- (void)_setClipCornerLayersVisible:(CACornerMask)visibleCornerLayers | ||
{ | ||
ASPerformBlockOnMainThread(^{ | ||
ASDisplayNodeAssertMainThread(); | ||
if (visible) { | ||
for (int idx = 0; idx < NUM_CLIP_CORNER_LAYERS; idx++) { | ||
if (_clipCornerLayers[idx] == nil) { | ||
static ASDisplayNodeCornerLayerDelegate *clipCornerLayers; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
clipCornerLayers = [[ASDisplayNodeCornerLayerDelegate alloc] init]; | ||
}); | ||
_clipCornerLayers[idx] = [[CALayer alloc] init]; | ||
_clipCornerLayers[idx].zPosition = 99999; | ||
_clipCornerLayers[idx].delegate = clipCornerLayers; | ||
} | ||
} | ||
[self _updateClipCornerLayerContentsWithRadius:_cornerRadius backgroundColor:self.backgroundColor]; | ||
} else { | ||
for (int idx = 0; idx < NUM_CLIP_CORNER_LAYERS; idx++) { | ||
for (int idx = 0; idx < NUM_CLIP_CORNER_LAYERS; idx++) { | ||
BOOL visible = (0 != (visibleCornerLayers & (1 << idx))); | ||
if (visible == (_clipCornerLayers[idx] != nil)) { | ||
continue; | ||
} else if (visible) { | ||
static ASDisplayNodeCornerLayerDelegate *clipCornerLayers; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should rename that variable at some point: |
||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
clipCornerLayers = [[ASDisplayNodeCornerLayerDelegate alloc] init]; | ||
}); | ||
_clipCornerLayers[idx] = [[CALayer alloc] init]; | ||
_clipCornerLayers[idx].zPosition = 99999; | ||
_clipCornerLayers[idx].delegate = clipCornerLayers; | ||
} else { | ||
[_clipCornerLayers[idx] removeFromSuperlayer]; | ||
_clipCornerLayers[idx] = nil; | ||
} | ||
} | ||
[self _updateClipCornerLayerContentsWithRadius:_cornerRadius backgroundColor:self.backgroundColor]; | ||
}); | ||
} | ||
|
||
- (void)updateCornerRoundingWithType:(ASCornerRoundingType)newRoundingType cornerRadius:(CGFloat)newCornerRadius | ||
- (void)updateCornerRoundingWithType:(ASCornerRoundingType)newRoundingType | ||
cornerRadius:(CGFloat)newCornerRadius | ||
maskedCorners:(CACornerMask)newMaskedCorners | ||
{ | ||
__instanceLock__.lock(); | ||
CGFloat oldCornerRadius = _cornerRadius; | ||
ASCornerRoundingType oldRoundingType = _cornerRoundingType; | ||
CACornerMask oldMaskedCorners = _maskedCorners; | ||
|
||
_cornerRadius = newCornerRadius; | ||
_cornerRoundingType = newRoundingType; | ||
_maskedCorners = newMaskedCorners; | ||
__instanceLock__.unlock(); | ||
|
||
ASPerformBlockOnMainThread(^{ | ||
ASDisplayNodeAssertMainThread(); | ||
|
||
if (oldRoundingType != newRoundingType || oldCornerRadius != newCornerRadius) { | ||
if (oldRoundingType != newRoundingType || oldCornerRadius != newCornerRadius || oldMaskedCorners != newMaskedCorners) { | ||
if (oldRoundingType == ASCornerRoundingTypeDefaultSlowCALayer) { | ||
if (newRoundingType == ASCornerRoundingTypePrecomposited) { | ||
self.layerCornerRadius = 0.0; | ||
|
@@ -1629,14 +1639,16 @@ - (void)updateCornerRoundingWithType:(ASCornerRoundingType)newRoundingType corne | |
} | ||
else if (newRoundingType == ASCornerRoundingTypeClipping) { | ||
self.layerCornerRadius = 0.0; | ||
[self _setClipCornerLayersVisible:YES]; | ||
[self _setClipCornerLayersVisible:newMaskedCorners]; | ||
} else if (newRoundingType == ASCornerRoundingTypeDefaultSlowCALayer) { | ||
self.layerCornerRadius = newCornerRadius; | ||
self.layerMaskedCorners = newMaskedCorners; | ||
} | ||
} | ||
else if (oldRoundingType == ASCornerRoundingTypePrecomposited) { | ||
if (newRoundingType == ASCornerRoundingTypeDefaultSlowCALayer) { | ||
self.layerCornerRadius = newCornerRadius; | ||
self.layerMaskedCorners = newMaskedCorners; | ||
[self setNeedsDisplay]; | ||
} | ||
else if (newRoundingType == ASCornerRoundingTypePrecomposited) { | ||
|
@@ -1645,22 +1657,23 @@ - (void)updateCornerRoundingWithType:(ASCornerRoundingType)newRoundingType corne | |
[self setNeedsDisplay]; | ||
} | ||
else if (newRoundingType == ASCornerRoundingTypeClipping) { | ||
[self _setClipCornerLayersVisible:YES]; | ||
[self _setClipCornerLayersVisible:newMaskedCorners]; | ||
[self setNeedsDisplay]; | ||
} | ||
} | ||
else if (oldRoundingType == ASCornerRoundingTypeClipping) { | ||
if (newRoundingType == ASCornerRoundingTypeDefaultSlowCALayer) { | ||
self.layerCornerRadius = newCornerRadius; | ||
[self _setClipCornerLayersVisible:NO]; | ||
[self _setClipCornerLayersVisible:kNilOptions]; | ||
} | ||
else if (newRoundingType == ASCornerRoundingTypePrecomposited) { | ||
[self _setClipCornerLayersVisible:NO]; | ||
[self _setClipCornerLayersVisible:kNilOptions]; | ||
[self displayImmediately]; | ||
} | ||
else if (newRoundingType == ASCornerRoundingTypeClipping) { | ||
// Clip corners already exist, but the radius has changed. | ||
[self _updateClipCornerLayerContentsWithRadius:newCornerRadius backgroundColor:self.backgroundColor]; | ||
// Clip corners already exist, but the radius and/or maskedCorners have changed. | ||
// This method will add & remove them, and subsequently redraw them. | ||
[self _setClipCornerLayersVisible:newMaskedCorners]; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason some of these bits flipped is (1) I changed our NUM_CLIP_CORNERS array order to match the CACornerMask order and (2) the original comments about CA being inverted in this context are incorrect.