Skip to content

Commit

Permalink
Cleanup from recent changes (#1647)
Browse files Browse the repository at this point in the history
- Fix availability warnings for usage of userInterfaceLevel
- Deprecate `asyncTraitCollectionDidChange` in favor of the new method
- Remove old compiler warning flag added for Xcode 9 building
- Access `textColorFollowsTintColor` with a locked scope
  • Loading branch information
rahul-malik authored Aug 28, 2019
1 parent dfe0f57 commit 6464153
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 81 deletions.
6 changes: 4 additions & 2 deletions Source/ASDisplayNode+Subclasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,17 @@ AS_CATEGORY_IMPLEMENTABLE
* @discussion Subclasses can override this method to react to a trait collection change.
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)asyncTraitCollectionDidChange ASDISPLAYNODE_REQUIRES_SUPER;
- (void)asyncTraitCollectionDidChange ASDISPLAYNODE_REQUIRES_SUPER ASDISPLAYNODE_DEPRECATED_MSG("Use asyncTraitCollectionDidChangeWithPreviousTraitCollection: instead.");


/**
* @abstract Called when the node's ASTraitCollection changes
*
* @discussion Subclasses can override this method to react to a trait collection change. Use `ASExperimentalTraitCollectionDidChangeWithPreviousCollection` to have this method called instead of `asyncTraitCollectionDidChange`.
* @discussion Subclasses can override this method to react to a trait collection change.
*
* @param previousTraitCollection The ASPrimitiveTraitCollection object before the interface environment changed.
*
* @note Enable `ASExperimentalTraitCollectionDidChangeWithPreviousCollection` experiment to have this method called instead of `asyncTraitCollectionDidChange`.
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)asyncTraitCollectionDidChangeWithPreviousTraitCollection:(ASPrimitiveTraitCollection)previousTraitCollection ASDISPLAYNODE_REQUIRES_SUPER;
Expand Down
98 changes: 46 additions & 52 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ @interface ASImageNodeDrawParameters : NSObject {
ASDisplayNodeContextModifier _willDisplayNodeContentWithRenderingContext;
ASDisplayNodeContextModifier _didDisplayNodeContentWithRenderingContext;
ASImageNodeDrawParametersBlock _didDrawBlock;
#if AS_BUILD_UIUSERINTERFACESTYLE
UIUserInterfaceStyle userInterfaceStyle;
#endif
UIUserInterfaceStyle userInterfaceStyle API_AVAILABLE(tvos(10.0), ios(12.0));
}

@end
Expand All @@ -77,9 +75,7 @@ @interface ASImageNodeContentsKey : NSObject
@property (nonatomic) ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext;
@property (nonatomic) ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext;
@property (nonatomic) asimagenode_modification_block_t imageModificationBlock;
#if AS_BUILD_UIUSERINTERFACESTYLE
@property UIUserInterfaceStyle userInterfaceStyle API_AVAILABLE(tvos(10.0), ios(12.0));
#endif
@end

@implementation ASImageNodeContentsKey
Expand All @@ -96,18 +92,20 @@ - (BOOL)isEqual:(id)object
// overheard of our caching, so it's likely not high-impact.
if ([object isKindOfClass:[ASImageNodeContentsKey class]]) {
ASImageNodeContentsKey *other = (ASImageNodeContentsKey *)object;
return [_image isEqual:other.image]
BOOL areKeysEqual = [_image isEqual:other.image]
&& CGSizeEqualToSize(_backingSize, other.backingSize)
&& CGRectEqualToRect(_imageDrawRect, other.imageDrawRect)
&& _isOpaque == other.isOpaque
&& [_backgroundColor isEqual:other.backgroundColor]
&& [_tintColor isEqual:other.tintColor]
&& _willDisplayNodeContentWithRenderingContext == other.willDisplayNodeContentWithRenderingContext
&& _didDisplayNodeContentWithRenderingContext == other.didDisplayNodeContentWithRenderingContext
#if AS_BUILD_UIUSERINTERFACESTYLE
&& _userInterfaceStyle == other.userInterfaceStyle
#endif
&& _imageModificationBlock == other.imageModificationBlock;
if (AS_AVAILABLE_IOS_TVOS(12, 10)) {
// iOS 12, tvOS 10 and later (userInterfaceStyle only available in iOS12+)
areKeysEqual = areKeysEqual && _userInterfaceStyle == other.userInterfaceStyle;
}
return areKeysEqual;
} else {
return NO;
}
Expand Down Expand Up @@ -150,13 +148,12 @@ @implementation ASImageNode
UIImage *_image;
ASWeakMapEntry *_weakCacheEntry; // Holds a reference that keeps our contents in cache.
UIColor *_placeholderColor;
UIColor *_tintColor; // Used to cache color information for layer backed nodes. View backed will use UIViewBridge

void (^_displayCompletionBlock)(BOOL canceled);

// Drawing
ASTextNode *_debugLabelNode;

// Cropping.
CGSize _forcedSize; //Defaults to CGSizeZero, indicating no forced size.
CGRect _cropRect; // Defaults to CGRectMake(0.5, 0.5, 0, 0)
Expand Down Expand Up @@ -190,7 +187,7 @@ - (instancetype)init
_cropDisplayBounds = CGRectNull;
_placeholderColor = ASDisplayNodeDefaultPlaceholderColor();
_animatedImageRunLoopMode = ASAnimatedImageDefaultRunLoopMode;

return self;
}

Expand All @@ -210,7 +207,7 @@ - (UIImage *)placeholderImage
if ((size.width * size.height) < CGFLOAT_EPSILON) {
return nil;
}

return ASGraphicsCreateImageWithOptions(size, NO, 1, nil, nil, ^{
AS::MutexLocker l(__instanceLock__);
[_placeholderColor setFill];
Expand Down Expand Up @@ -248,12 +245,12 @@ - (void)_locked_setImage:(UIImage *)image

UIImage *oldImage = _image;
_image = image;

if (image != nil) {
// We explicitly call setNeedsDisplay in this case, although we know setNeedsDisplay will be called with lock held.
// Therefore we have to be careful in methods that are involved with setNeedsDisplay to not run into a deadlock
[self setNeedsDisplay];

// For debugging purposes we don't care about locking for now
if ([ASImageNode shouldShowImageScalingOverlay] && _debugLabelNode == nil) {
// do not use ASPerformBlockOnMainThread here, if it performs the block synchronously it will continue
Expand Down Expand Up @@ -302,7 +299,7 @@ - (void)setPlaceholderColor:(UIColor *)placeholderColor
- (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
{
ASLockScopeSelf();

ASImageNodeDrawParameters *drawParameters = [[ASImageNodeDrawParameters alloc] init];
drawParameters->_image = _image;
drawParameters->_bounds = [self threadSafeBounds];
Expand All @@ -319,17 +316,17 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
drawParameters->_imageModificationBlock = _imageModificationBlock;
drawParameters->_willDisplayNodeContentWithRenderingContext = _willDisplayNodeContentWithRenderingContext;
drawParameters->_didDisplayNodeContentWithRenderingContext = _didDisplayNodeContentWithRenderingContext;
#if AS_BUILD_UIUSERINTERFACESTYLE
drawParameters->userInterfaceStyle = self.primitiveTraitCollection.userInterfaceStyle;
#endif
if (AS_AVAILABLE_IOS_TVOS(12, 10)) {
drawParameters->userInterfaceStyle = self.primitiveTraitCollection.userInterfaceStyle;
}


// Hack for now to retain the weak entry that was created while this drawing happened
drawParameters->_didDrawBlock = ^(ASWeakMapEntry *entry){
ASLockScopeSelf();
_weakCacheEntry = entry;
};

return drawParameters;
}

Expand All @@ -341,7 +338,7 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(NS_NOESC
if (image == nil) {
return nil;
}

CGRect drawParameterBounds = drawParameter->_bounds;
BOOL forceUpscaling = drawParameter->_forceUpscaling;
CGSize forcedSize = drawParameter->_forcedSize;
Expand All @@ -356,13 +353,13 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(NS_NOESC
asimagenode_modification_block_t imageModificationBlock = drawParameter->_imageModificationBlock;
ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext = drawParameter->_willDisplayNodeContentWithRenderingContext;
ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext = drawParameter->_didDisplayNodeContentWithRenderingContext;

BOOL hasValidCropBounds = cropEnabled && !CGRectIsEmpty(cropDisplayBounds);
CGRect bounds = (hasValidCropBounds ? cropDisplayBounds : drawParameterBounds);


ASDisplayNodeAssert(contentsScale > 0, @"invalid contentsScale at display time");

// if the image is resizable, bail early since the image has likely already been configured
BOOL stretchable = !UIEdgeInsetsEqualToEdgeInsets(image.capInsets, UIEdgeInsetsZero);
if (stretchable) {
Expand All @@ -371,24 +368,24 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(NS_NOESC
}
return image;
}

CGSize imageSize = image.size;
CGSize imageSizeInPixels = CGSizeMake(imageSize.width * image.scale, imageSize.height * image.scale);
CGSize boundsSizeInPixels = CGSizeMake(std::floor(bounds.size.width * contentsScale), std::floor(bounds.size.height * contentsScale));

BOOL contentModeSupported = contentMode == UIViewContentModeScaleAspectFill ||
contentMode == UIViewContentModeScaleAspectFit ||
contentMode == UIViewContentModeCenter;

CGSize backingSize = CGSizeZero;
CGRect imageDrawRect = CGRectZero;

if (boundsSizeInPixels.width * contentsScale < 1.0f || boundsSizeInPixels.height * contentsScale < 1.0f ||
imageSizeInPixels.width < 1.0f || imageSizeInPixels.height < 1.0f) {
return nil;
}


// If we're not supposed to do any cropping, just decode image at original size
if (!cropEnabled || !contentModeSupported) {
backingSize = imageSizeInPixels;
Expand All @@ -408,18 +405,12 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(NS_NOESC
&backingSize,
&imageDrawRect);
}

if (backingSize.width <= 0.0f || backingSize.height <= 0.0f ||
imageDrawRect.size.width <= 0.0f || imageDrawRect.size.height <= 0.0f) {
return nil;
}

#if AS_BUILD_UIUSERINTERFACESTYLE
UIUserInterfaceStyle userInterfaceStyle = drawParameter->userInterfaceStyle;
#endif



ASImageNodeContentsKey *contentsKey = [[ASImageNodeContentsKey alloc] init];
contentsKey.image = image;
contentsKey.backingSize = backingSize;
Expand All @@ -430,9 +421,12 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(NS_NOESC
contentsKey.willDisplayNodeContentWithRenderingContext = willDisplayNodeContentWithRenderingContext;
contentsKey.didDisplayNodeContentWithRenderingContext = didDisplayNodeContentWithRenderingContext;
contentsKey.imageModificationBlock = imageModificationBlock;
#if AS_BUILD_UIUSERINTERFACESTYLE
contentsKey.userInterfaceStyle = userInterfaceStyle;
#endif

if (AS_AVAILABLE_IOS_TVOS(12, 10)) {
UIUserInterfaceStyle userInterfaceStyle = drawParameter->userInterfaceStyle;
contentsKey.userInterfaceStyle = userInterfaceStyle;
}

if (isCancelled()) {
return nil;
}
Expand All @@ -444,7 +438,7 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(NS_NOESC
if (entry == nil) {
return nil;
}

if (drawParameter->_didDrawBlock) {
drawParameter->_didDrawBlock(entry);
}
Expand All @@ -461,7 +455,7 @@ + (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key drawParameters:
dispatch_once(&onceToken, ^{
cacheLock = new AS::Mutex();
});

{
AS::MutexLocker l(*cacheLock);
if (!cache) {
Expand Down Expand Up @@ -550,7 +544,7 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key drawParameters:(
if (key.imageModificationBlock) {
result = key.imageModificationBlock(result);
}

return result;
}

Expand Down Expand Up @@ -587,7 +581,7 @@ - (void)displayDidFinish
_debugLabelNode.attributedText = nil;
}
}

// If we've got a block to perform after displaying, do it.
if (shouldPerformDisplayCompletionBlock) {
displayCompletionBlock(NO);
Expand Down Expand Up @@ -626,7 +620,7 @@ - (void)tintColorDidChange
- (void)clearContents
{
[super clearContents];

AS::MutexLocker l(__instanceLock__);
_weakCacheEntry = nil; // release contents from the cache.
}
Expand Down Expand Up @@ -654,7 +648,7 @@ - (void)setCropEnabled:(BOOL)cropEnabled recropImmediately:(BOOL)recropImmediate

_imageNodeFlags.cropEnabled = cropEnabled;
_cropDisplayBounds = cropBounds;

UIImage *image = _image;
__instanceLock__.unlock();

Expand Down Expand Up @@ -740,7 +734,7 @@ - (void)setImageModificationBlock:(asimagenode_modification_block_t)imageModific
- (void)layout
{
[super layout];

if (_debugLabelNode) {
CGSize boundsSize = self.bounds.size;
CGSize debugLabelSize = [_debugLabelNode layoutThatFits:ASSizeRangeMake(CGSizeZero, boundsSize)].size;
Expand All @@ -758,10 +752,9 @@ - (NSDictionary *)debugLabelAttributes
};
}

#if AS_BUILD_UIUSERINTERFACESTYLE
- (void)asyncTraitCollectionDidChangeWithPreviousTraitCollection:(ASPrimitiveTraitCollection)previousTraitCollection {
[super asyncTraitCollectionDidChangeWithPreviousTraitCollection:previousTraitCollection];

if (AS_AVAILABLE_IOS_TVOS(12, 10)) {
// update image if userInterfaceStyle was changed (dark mode)
if (_image != nil && self.primitiveTraitCollection.userInterfaceStyle != previousTraitCollection.userInterfaceStyle) {
Expand All @@ -776,7 +769,8 @@ - (void)asyncTraitCollectionDidChangeWithPreviousTraitCollection:(ASPrimitiveTra
}
}
}
#endif


@end

#pragma mark - Extras
Expand Down
5 changes: 5 additions & 0 deletions Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,11 @@ - (NSUInteger)lineCount
return ASLockedSelf([[self _locked_renderer] lineCount]);
}

- (BOOL)textColorFollowsTintColor
{
return ASLockedSelf(_textColorFollowsTintColor);
}

#pragma mark - Truncation Message

- (void)_invalidateTruncationText
Expand Down
12 changes: 0 additions & 12 deletions Source/Base/ASBaseDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@
#define AS_EXTERN FOUNDATION_EXTERN
#define unowned __unsafe_unretained

/**
* Hack to support building for iOS with Xcode 9. UIUserInterfaceStyle was previously tvOS-only,
* and it was added to iOS 12. Xcode 9 (iOS 11 SDK) will flat-out refuse to build anything that
* references this enum targeting iOS, even if it's guarded with the right availability macros,
* because it thinks the entire platform isn't compatible with the enum.
*/
#if TARGET_OS_TV || (defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0)
#define AS_BUILD_UIUSERINTERFACESTYLE 1
#else
#define AS_BUILD_UIUSERINTERFACESTYLE 0
#endif

/**
* Decorates methods that clients can implement in categories on our base class. These methods
* will be stubbed with an empty implementation if no implementation is provided.
Expand Down
5 changes: 1 addition & 4 deletions Source/Details/ASTraitCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ typedef struct {
UIUserInterfaceIdiom userInterfaceIdiom;
UIForceTouchCapability forceTouchCapability;
UITraitEnvironmentLayoutDirection layoutDirection API_AVAILABLE(ios(10.0));
#if AS_BUILD_UIUSERINTERFACESTYLE
UIUserInterfaceStyle userInterfaceStyle API_AVAILABLE(tvos(10.0), ios(12.0));
#endif


// NOTE: This must be a constant. We will assert.
unowned UIContentSizeCategory preferredContentSizeCategory API_AVAILABLE(ios(10.0));
Expand Down Expand Up @@ -139,9 +138,7 @@ AS_SUBCLASSING_RESTRICTED
@property (readonly) UIUserInterfaceIdiom userInterfaceIdiom;
@property (readonly) UIForceTouchCapability forceTouchCapability;
@property (readonly) UITraitEnvironmentLayoutDirection layoutDirection API_AVAILABLE(ios(10.0));
#if AS_BUILD_UIUSERINTERFACESTYLE
@property (readonly) UIUserInterfaceStyle userInterfaceStyle API_AVAILABLE(tvos(10.0), ios(12.0));
#endif
@property (readonly) UIContentSizeCategory preferredContentSizeCategory API_AVAILABLE(ios(10.0));

@property (readonly) CGSize containerSize;
Expand Down
Loading

0 comments on commit 6464153

Please sign in to comment.