[ASRelativeLayoutSpec] Fix ASRelatativeLayoutSpec behavior for ASRelativeLayoutSpecPositionStart (#2393)

* Fix ASRelatativeLayoutSpec behavior for ASRelativeLayoutSpecPositionStart

* Add updated images for snapshot tests
This commit is contained in:
Michael Schneider 2016-10-18 12:00:01 -07:00 committed by Adlai Holler
parent 5a8d0037ec
commit ba80b83695
35 changed files with 55 additions and 55 deletions

View File

@ -49,9 +49,7 @@ NS_ASSUME_NONNULL_BEGIN
* Initializer. * Initializer.
* *
* @param centeringOptions How the child is centered. * @param centeringOptions How the child is centered.
*
* @param sizingOptions How much space will be taken up. * @param sizingOptions How much space will be taken up.
*
* @param child The child to center. * @param child The child to center.
*/ */
+ (instancetype)centerLayoutSpecWithCenteringOptions:(ASCenterLayoutSpecCenteringOptions)centeringOptions + (instancetype)centerLayoutSpecWithCenteringOptions:(ASCenterLayoutSpecCenteringOptions)centeringOptions

View File

@ -58,21 +58,19 @@
- (ASRelativeLayoutSpecPosition)horizontalPositionFromCenteringOptions:(ASCenterLayoutSpecCenteringOptions)centeringOptions - (ASRelativeLayoutSpecPosition)horizontalPositionFromCenteringOptions:(ASCenterLayoutSpecCenteringOptions)centeringOptions
{ {
BOOL centerX = (centeringOptions & ASCenterLayoutSpecCenteringX) != 0; if ((centeringOptions & ASCenterLayoutSpecCenteringX) != 0) {
if (centerX) {
return ASRelativeLayoutSpecPositionCenter; return ASRelativeLayoutSpecPositionCenter;
} else { } else {
return ASRelativeLayoutSpecPositionStart; return ASRelativeLayoutSpecPositionNone;
} }
} }
- (ASRelativeLayoutSpecPosition)verticalPositionFromCenteringOptions:(ASCenterLayoutSpecCenteringOptions)centeringOptions - (ASRelativeLayoutSpecPosition)verticalPositionFromCenteringOptions:(ASCenterLayoutSpecCenteringOptions)centeringOptions
{ {
BOOL centerY = (centeringOptions & ASCenterLayoutSpecCenteringY) != 0; if ((centeringOptions & ASCenterLayoutSpecCenteringY) != 0) {
if (centerY) {
return ASRelativeLayoutSpecPositionCenter; return ASRelativeLayoutSpecPositionCenter;
} else { } else {
return ASRelativeLayoutSpecPositionStart; return ASRelativeLayoutSpecPositionNone;
} }
} }

View File

@ -60,15 +60,15 @@
- (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize - (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
{ {
std::vector<CGSize> sizeOptions; 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, { sizeOptions.push_back(ASSizeRangeClamp(constrainedSize, {
constrainedSize.max.width, constrainedSize.max.width,
ASFloorPixelValue(_ratio * 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, { sizeOptions.push_back(ASSizeRangeClamp(constrainedSize, {
ASFloorPixelValue(constrainedSize.max.height / _ratio), ASFloorPixelValue(constrainedSize.max.height / _ratio),
constrainedSize.max.height constrainedSize.max.height

View File

@ -13,13 +13,15 @@
#import <AsyncDisplayKit/ASLayoutSpec.h> #import <AsyncDisplayKit/ASLayoutSpec.h>
/** How the child is positioned within the spec. */ /** 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) */ /** 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 */ /** 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) */ /** 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. */ /** How much space the spec will take up. */

View File

@ -54,31 +54,21 @@
- (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize - (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
{ {
// If we have a finite size in any direction, pass this so that the child can // If we have a finite size in any direction, pass this so that the child can resolve percentages against it.
// resolve percentages against it. Otherwise pass ASLayoutElementParentDimensionUndefined // Otherwise pass ASLayoutElementParentDimensionUndefined as the size will depend on the content
// as the size will depend on the content
// TODO: layout: isValidForLayout() call should not be necessary if INFINITY is used
CGSize size = { CGSize size = {
isinf(constrainedSize.max.width) || !ASPointsValidForLayout(constrainedSize.max.width) ? ASLayoutElementParentDimensionUndefined : constrainedSize.max.width, ASPointsValidForSize(constrainedSize.max.width) == NO ? ASLayoutElementParentDimensionUndefined : constrainedSize.max.width,
isinf(constrainedSize.max.height) || !ASPointsValidForLayout(constrainedSize.max.height) ? ASLayoutElementParentDimensionUndefined : constrainedSize.max.height 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 // Layout the child
const CGSize minChildSize = { const CGSize minChildSize = {
reduceWidth ? 0 : constrainedSize.min.width, (_horizontalPosition != ASRelativeLayoutSpecPositionNone) ? 0 : constrainedSize.min.width,
reduceHeight ? 0 : constrainedSize.min.height, (_verticalPosition != ASRelativeLayoutSpecPositionNone) ? 0 : constrainedSize.min.height,
}; };
ASLayout *sublayout = [self.child layoutThatFits:ASSizeRangeMake(minChildSize, constrainedSize.max) parentSize:size]; 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 // If we have an undetermined height or width, use the child size to define the layout size
// size
size = ASSizeRangeClamp(constrainedSize, { size = ASSizeRangeClamp(constrainedSize, {
isfinite(size.width) == NO ? sublayout.size.width : size.width, isfinite(size.width) == NO ? sublayout.size.width : size.width,
isfinite(size.height) == NO ? sublayout.size.height : size.height isfinite(size.height) == NO ? sublayout.size.height : size.height
@ -104,9 +94,9 @@
- (CGFloat)proportionOfAxisForAxisPosition:(ASRelativeLayoutSpecPosition)position - (CGFloat)proportionOfAxisForAxisPosition:(ASRelativeLayoutSpecPosition)position
{ {
if ((position & ASRelativeLayoutSpecPositionCenter) != 0) { if (position == ASRelativeLayoutSpecPositionCenter) {
return 0.5f; return 0.5f;
} else if ((position & ASRelativeLayoutSpecPositionEnd) != 0) { } else if (position == ASRelativeLayoutSpecPositionEnd) {
return 1.0f; return 1.0f;
} else { } else {
return 0.0f; return 0.0f;

View File

@ -63,7 +63,8 @@
ASLayoutSpec *layoutSpec = ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec backgroundLayoutSpecWithChild: [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:
[ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:children] [ASAbsoluteLayoutSpec
absoluteLayoutSpecWithChildren:children]
background:backgroundNode]; background:backgroundNode];
[self testLayoutSpec:layoutSpec sizeRange:sizeRange subnodes:subnodes identifier:identifier]; [self testLayoutSpec:layoutSpec sizeRange:sizeRange subnodes:subnodes identifier:identifier];

View File

@ -21,6 +21,16 @@ static const ASSizeRange kSize = {{100, 120}, {320, 160}};
@implementation ASRelativeLayoutSpecSnapshotTests @implementation ASRelativeLayoutSpecSnapshotTests
#pragma mark - XCTestCase
- (void)setUp
{
[super setUp];
self.recordMode = NO;
}
- (void)testWithOptions - (void)testWithOptions
{ {
[self testAllVerticalPositionsForHorizontalPosition:ASRelativeLayoutSpecPositionStart]; [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:ASRelativeLayoutSpecPositionStart sizingOptions:{}];
[self testWithHorizontalPosition:horizontalPosition verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOptions:{}]; [self testWithHorizontalPosition:horizontalPosition verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOptions:{}];
[self testWithHorizontalPosition:horizontalPosition verticalPosition:ASRelativeLayoutSpecPositionEnd sizingOptions:{}]; [self testWithHorizontalPosition:horizontalPosition verticalPosition:ASRelativeLayoutSpecPositionEnd sizingOptions:{}];
@ -80,15 +91,15 @@ static NSString *suffixForPositionOptions(ASRelativeLayoutSpecPosition horizonta
{ {
NSMutableString *suffix = [NSMutableString string]; NSMutableString *suffix = [NSMutableString string];
if ((horizontalPosition & ASRelativeLayoutSpecPositionCenter) != 0) { if (horizontalPosition == ASRelativeLayoutSpecPositionCenter) {
[suffix appendString:@"CenterX"]; [suffix appendString:@"CenterX"];
} else if ((horizontalPosition & ASRelativeLayoutSpecPositionEnd) != 0) { } else if (horizontalPosition == ASRelativeLayoutSpecPositionEnd) {
[suffix appendString:@"EndX"]; [suffix appendString:@"EndX"];
} }
if ((verticalPosition & ASRelativeLayoutSpecPositionCenter) != 0) { if (verticalPosition == ASRelativeLayoutSpecPositionCenter) {
[suffix appendString:@"CenterY"]; [suffix appendString:@"CenterY"];
} else if ((verticalPosition & ASRelativeLayoutSpecPositionEnd) != 0) { } else if (verticalPosition == ASRelativeLayoutSpecPositionEnd) {
[suffix appendString:@"EndY"]; [suffix appendString:@"EndY"];
} }
@ -122,8 +133,8 @@ static NSString *suffixForPositionOptions(ASRelativeLayoutSpecPosition horizonta
ASRelativeLayoutSpec *layoutSpec = ASRelativeLayoutSpec *layoutSpec =
[ASRelativeLayoutSpec [ASRelativeLayoutSpec
relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionNone
verticalPosition:ASRelativeLayoutSpecPositionStart verticalPosition:ASRelativeLayoutSpecPositionNone
sizingOption:{} sizingOption:{}
child:childSpec]; child:childSpec];

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB