[ASRelativeLayoutSpec] Fix ASRelatativeLayoutSpec behavior for ASRelativeLayoutSpecPositionStart (#2393)
* Fix ASRelatativeLayoutSpec behavior for ASRelativeLayoutSpecPositionStart * Add updated images for snapshot tests
@ -49,9 +49,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
* Initializer.
|
||||
*
|
||||
* @param centeringOptions How the child is centered.
|
||||
*
|
||||
* @param sizingOptions How much space will be taken up.
|
||||
*
|
||||
* @param child The child to center.
|
||||
*/
|
||||
+ (instancetype)centerLayoutSpecWithCenteringOptions:(ASCenterLayoutSpecCenteringOptions)centeringOptions
|
||||
|
||||
@ -58,21 +58,19 @@
|
||||
|
||||
- (ASRelativeLayoutSpecPosition)horizontalPositionFromCenteringOptions:(ASCenterLayoutSpecCenteringOptions)centeringOptions
|
||||
{
|
||||
BOOL centerX = (centeringOptions & ASCenterLayoutSpecCenteringX) != 0;
|
||||
if (centerX) {
|
||||
if ((centeringOptions & ASCenterLayoutSpecCenteringX) != 0) {
|
||||
return ASRelativeLayoutSpecPositionCenter;
|
||||
} else {
|
||||
return ASRelativeLayoutSpecPositionStart;
|
||||
return ASRelativeLayoutSpecPositionNone;
|
||||
}
|
||||
}
|
||||
|
||||
- (ASRelativeLayoutSpecPosition)verticalPositionFromCenteringOptions:(ASCenterLayoutSpecCenteringOptions)centeringOptions
|
||||
{
|
||||
BOOL centerY = (centeringOptions & ASCenterLayoutSpecCenteringY) != 0;
|
||||
if (centerY) {
|
||||
if ((centeringOptions & ASCenterLayoutSpecCenteringY) != 0) {
|
||||
return ASRelativeLayoutSpecPositionCenter;
|
||||
} else {
|
||||
return ASRelativeLayoutSpecPositionStart;
|
||||
return ASRelativeLayoutSpecPositionNone;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -60,15 +60,15 @@
|
||||
- (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
|
||||
{
|
||||
std::vector<CGSize> sizeOptions;
|
||||
// TODO: layout: isValidForLayout() call should not be necessary if INFINITY is used
|
||||
if (!isinf(constrainedSize.max.width) && ASPointsValidForLayout(constrainedSize.max.width)) {
|
||||
|
||||
if (ASPointsValidForSize(constrainedSize.max.width)) {
|
||||
sizeOptions.push_back(ASSizeRangeClamp(constrainedSize, {
|
||||
constrainedSize.max.width,
|
||||
ASFloorPixelValue(_ratio * constrainedSize.max.width)
|
||||
}));
|
||||
}
|
||||
// TODO: layout: isValidForLayout() call should not be necessary if INFINITY is used
|
||||
if (!isinf(constrainedSize.max.height) && ASPointsValidForLayout(constrainedSize.max.width)) {
|
||||
|
||||
if (ASPointsValidForSize(constrainedSize.max.width)) {
|
||||
sizeOptions.push_back(ASSizeRangeClamp(constrainedSize, {
|
||||
ASFloorPixelValue(constrainedSize.max.height / _ratio),
|
||||
constrainedSize.max.height
|
||||
|
||||
@ -13,13 +13,15 @@
|
||||
#import <AsyncDisplayKit/ASLayoutSpec.h>
|
||||
|
||||
/** How the child is positioned within the spec. */
|
||||
typedef NS_OPTIONS(NSUInteger, ASRelativeLayoutSpecPosition) {
|
||||
typedef NS_ENUM(NSUInteger, ASRelativeLayoutSpecPosition) {
|
||||
/** The child is positioned at point 0 */
|
||||
ASRelativeLayoutSpecPositionNone = 0,
|
||||
/** The child is positioned at point 0 relatively to the layout axis (ie left / top most) */
|
||||
ASRelativeLayoutSpecPositionStart = 0,
|
||||
ASRelativeLayoutSpecPositionStart = 1,
|
||||
/** The child is centered along the specified axis */
|
||||
ASRelativeLayoutSpecPositionCenter = 1 << 0,
|
||||
ASRelativeLayoutSpecPositionCenter = 2,
|
||||
/** The child is positioned at the maximum point of the layout axis (ie right / bottom most) */
|
||||
ASRelativeLayoutSpecPositionEnd = 1 << 1,
|
||||
ASRelativeLayoutSpecPositionEnd = 3,
|
||||
};
|
||||
|
||||
/** How much space the spec will take up. */
|
||||
|
||||
@ -54,31 +54,21 @@
|
||||
|
||||
- (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
|
||||
{
|
||||
// If we have a finite size in any direction, pass this so that the child can
|
||||
// resolve percentages against it. Otherwise pass ASLayoutElementParentDimensionUndefined
|
||||
// as the size will depend on the content
|
||||
// TODO: layout: isValidForLayout() call should not be necessary if INFINITY is used
|
||||
// If we have a finite size in any direction, pass this so that the child can resolve percentages against it.
|
||||
// Otherwise pass ASLayoutElementParentDimensionUndefined as the size will depend on the content
|
||||
CGSize size = {
|
||||
isinf(constrainedSize.max.width) || !ASPointsValidForLayout(constrainedSize.max.width) ? ASLayoutElementParentDimensionUndefined : constrainedSize.max.width,
|
||||
isinf(constrainedSize.max.height) || !ASPointsValidForLayout(constrainedSize.max.height) ? ASLayoutElementParentDimensionUndefined : constrainedSize.max.height
|
||||
ASPointsValidForSize(constrainedSize.max.width) == NO ? ASLayoutElementParentDimensionUndefined : constrainedSize.max.width,
|
||||
ASPointsValidForSize(constrainedSize.max.height) == NO ? ASLayoutElementParentDimensionUndefined : constrainedSize.max.height
|
||||
};
|
||||
|
||||
BOOL reduceWidth = (_horizontalPosition & ASRelativeLayoutSpecPositionCenter) != 0 ||
|
||||
(_horizontalPosition & ASRelativeLayoutSpecPositionEnd) != 0;
|
||||
|
||||
BOOL reduceHeight = (_verticalPosition & ASRelativeLayoutSpecPositionCenter) != 0 ||
|
||||
(_verticalPosition & ASRelativeLayoutSpecPositionEnd) != 0;
|
||||
|
||||
// Layout the child
|
||||
const CGSize minChildSize = {
|
||||
reduceWidth ? 0 : constrainedSize.min.width,
|
||||
reduceHeight ? 0 : constrainedSize.min.height,
|
||||
(_horizontalPosition != ASRelativeLayoutSpecPositionNone) ? 0 : constrainedSize.min.width,
|
||||
(_verticalPosition != ASRelativeLayoutSpecPositionNone) ? 0 : constrainedSize.min.height,
|
||||
};
|
||||
|
||||
ASLayout *sublayout = [self.child layoutThatFits:ASSizeRangeMake(minChildSize, constrainedSize.max) parentSize:size];
|
||||
|
||||
// If we have an undetermined height or width, use the child size to define the layout
|
||||
// size
|
||||
// If we have an undetermined height or width, use the child size to define the layout size
|
||||
size = ASSizeRangeClamp(constrainedSize, {
|
||||
isfinite(size.width) == NO ? sublayout.size.width : size.width,
|
||||
isfinite(size.height) == NO ? sublayout.size.height : size.height
|
||||
@ -104,9 +94,9 @@
|
||||
|
||||
- (CGFloat)proportionOfAxisForAxisPosition:(ASRelativeLayoutSpecPosition)position
|
||||
{
|
||||
if ((position & ASRelativeLayoutSpecPositionCenter) != 0) {
|
||||
if (position == ASRelativeLayoutSpecPositionCenter) {
|
||||
return 0.5f;
|
||||
} else if ((position & ASRelativeLayoutSpecPositionEnd) != 0) {
|
||||
} else if (position == ASRelativeLayoutSpecPositionEnd) {
|
||||
return 1.0f;
|
||||
} else {
|
||||
return 0.0f;
|
||||
|
||||
@ -63,7 +63,8 @@
|
||||
|
||||
ASLayoutSpec *layoutSpec =
|
||||
[ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:
|
||||
[ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:children]
|
||||
[ASAbsoluteLayoutSpec
|
||||
absoluteLayoutSpecWithChildren:children]
|
||||
background:backgroundNode];
|
||||
|
||||
[self testLayoutSpec:layoutSpec sizeRange:sizeRange subnodes:subnodes identifier:identifier];
|
||||
|
||||
@ -21,6 +21,16 @@ static const ASSizeRange kSize = {{100, 120}, {320, 160}};
|
||||
|
||||
@implementation ASRelativeLayoutSpecSnapshotTests
|
||||
|
||||
#pragma mark - XCTestCase
|
||||
|
||||
- (void)setUp
|
||||
{
|
||||
[super setUp];
|
||||
|
||||
self.recordMode = NO;
|
||||
}
|
||||
|
||||
|
||||
- (void)testWithOptions
|
||||
{
|
||||
[self testAllVerticalPositionsForHorizontalPosition:ASRelativeLayoutSpecPositionStart];
|
||||
@ -29,7 +39,8 @@ static const ASSizeRange kSize = {{100, 120}, {320, 160}};
|
||||
|
||||
}
|
||||
|
||||
- (void)testAllVerticalPositionsForHorizontalPosition:(ASRelativeLayoutSpecPosition)horizontalPosition {
|
||||
- (void)testAllVerticalPositionsForHorizontalPosition:(ASRelativeLayoutSpecPosition)horizontalPosition
|
||||
{
|
||||
[self testWithHorizontalPosition:horizontalPosition verticalPosition:ASRelativeLayoutSpecPositionStart sizingOptions:{}];
|
||||
[self testWithHorizontalPosition:horizontalPosition verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOptions:{}];
|
||||
[self testWithHorizontalPosition:horizontalPosition verticalPosition:ASRelativeLayoutSpecPositionEnd sizingOptions:{}];
|
||||
@ -80,15 +91,15 @@ static NSString *suffixForPositionOptions(ASRelativeLayoutSpecPosition horizonta
|
||||
{
|
||||
NSMutableString *suffix = [NSMutableString string];
|
||||
|
||||
if ((horizontalPosition & ASRelativeLayoutSpecPositionCenter) != 0) {
|
||||
if (horizontalPosition == ASRelativeLayoutSpecPositionCenter) {
|
||||
[suffix appendString:@"CenterX"];
|
||||
} else if ((horizontalPosition & ASRelativeLayoutSpecPositionEnd) != 0) {
|
||||
} else if (horizontalPosition == ASRelativeLayoutSpecPositionEnd) {
|
||||
[suffix appendString:@"EndX"];
|
||||
}
|
||||
|
||||
if ((verticalPosition & ASRelativeLayoutSpecPositionCenter) != 0) {
|
||||
if (verticalPosition == ASRelativeLayoutSpecPositionCenter) {
|
||||
[suffix appendString:@"CenterY"];
|
||||
} else if ((verticalPosition & ASRelativeLayoutSpecPositionEnd) != 0) {
|
||||
} else if (verticalPosition == ASRelativeLayoutSpecPositionEnd) {
|
||||
[suffix appendString:@"EndY"];
|
||||
}
|
||||
|
||||
@ -122,8 +133,8 @@ static NSString *suffixForPositionOptions(ASRelativeLayoutSpecPosition horizonta
|
||||
|
||||
ASRelativeLayoutSpec *layoutSpec =
|
||||
[ASRelativeLayoutSpec
|
||||
relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart
|
||||
verticalPosition:ASRelativeLayoutSpecPositionStart
|
||||
relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionNone
|
||||
verticalPosition:ASRelativeLayoutSpecPositionNone
|
||||
sizingOption:{}
|
||||
child:childSpec];
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 4.8 KiB |
|
Before Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 6.0 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 6.5 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 6.5 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 6.3 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 5.9 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 5.5 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 5.7 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 5.4 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 5.8 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 6.0 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 4.7 KiB |
|
Before Width: | Height: | Size: 9.3 KiB |
|
After Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 3.9 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |