diff --git a/AsyncDisplayKit/Layout/ASBackgroundLayoutSpec.mm b/AsyncDisplayKit/Layout/ASBackgroundLayoutSpec.mm index e23ebaf903..fd3cfce134 100644 --- a/AsyncDisplayKit/Layout/ASBackgroundLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASBackgroundLayoutSpec.mm @@ -25,12 +25,13 @@ - (instancetype)initWithChild:(id)child background:(id)background { - self = [super init]; - if (self) { - ASDisplayNodeAssertNotNil(child, @"Child cannot be nil"); - _child = child; - _background = background; + if (!(self = [super init])) { + return nil; } + + ASDisplayNodeAssertNotNil(child, @"Child cannot be nil"); + _child = child; + _background = background; return self; } diff --git a/AsyncDisplayKit/Layout/ASCenterLayoutSpec.mm b/AsyncDisplayKit/Layout/ASCenterLayoutSpec.mm index fd52637480..1830895320 100644 --- a/AsyncDisplayKit/Layout/ASCenterLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASCenterLayoutSpec.mm @@ -24,13 +24,13 @@ sizingOptions:(ASCenterLayoutSpecSizingOptions)sizingOptions child:(id)child; { - self = [super init]; - if (self) { - ASDisplayNodeAssertNotNil(child, @"Child cannot be nil"); - _centeringOptions = centeringOptions; - _sizingOptions = sizingOptions; - _child = child; + if (!(self = [super init])) { + return nil; } + ASDisplayNodeAssertNotNil(child, @"Child cannot be nil"); + _centeringOptions = centeringOptions; + _sizingOptions = sizingOptions; + _child = child; return self; } diff --git a/AsyncDisplayKit/Layout/ASInsetLayoutSpec.mm b/AsyncDisplayKit/Layout/ASInsetLayoutSpec.mm index 01c382b663..5ca96ab848 100644 --- a/AsyncDisplayKit/Layout/ASInsetLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASInsetLayoutSpec.mm @@ -45,12 +45,12 @@ static CGFloat centerInset(CGFloat outer, CGFloat inner) - (instancetype)initWithInsets:(UIEdgeInsets)insets child:(id)child; { - self = [super init]; - if (self) { - ASDisplayNodeAssertNotNil(child, @"Child cannot be nil"); - _insets = insets; - _child = child; + if (!(self = [super init])) { + return nil; } + ASDisplayNodeAssertNotNil(child, @"Child cannot be nil"); + _insets = insets; + _child = child; return self; } diff --git a/AsyncDisplayKit/Layout/ASLayoutSpec.mm b/AsyncDisplayKit/Layout/ASLayoutSpec.mm index 6d6ab903f4..ef09aa365e 100644 --- a/AsyncDisplayKit/Layout/ASLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASLayoutSpec.mm @@ -27,11 +27,11 @@ - (instancetype)init { - self = [super init]; - if (self) { - _flexBasis = ASRelativeDimensionUnconstrained; - _isMutable = YES; + if (!(self = [super init])) { + return nil; } + _flexBasis = ASRelativeDimensionUnconstrained; + _isMutable = YES; return self; } diff --git a/AsyncDisplayKit/Layout/ASOverlayLayoutSpec.h b/AsyncDisplayKit/Layout/ASOverlayLayoutSpec.h index ec82fe00e6..35a9577dde 100644 --- a/AsyncDisplayKit/Layout/ASOverlayLayoutSpec.h +++ b/AsyncDisplayKit/Layout/ASOverlayLayoutSpec.h @@ -18,6 +18,6 @@ @property (nonatomic, strong) id child; @property (nonatomic, strong) id overlay; -+ (instancetype)overlayLayoutWithChild:(id)child overlay:(id)overlay; ++ (instancetype)overlayLayoutSpecWithChild:(id)child overlay:(id)overlay; @end diff --git a/AsyncDisplayKit/Layout/ASOverlayLayoutSpec.mm b/AsyncDisplayKit/Layout/ASOverlayLayoutSpec.mm index c9d0edb80c..f4f025573b 100644 --- a/AsyncDisplayKit/Layout/ASOverlayLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASOverlayLayoutSpec.mm @@ -22,16 +22,16 @@ - (instancetype)initWithChild:(id)child overlay:(id)overlay { - self = [super init]; - if (self) { - ASDisplayNodeAssertNotNil(child, @"Child that will be overlayed on shouldn't be nil"); - _overlay = overlay; - _child = child; + if (!(self = [super init])) { + return nil; } + ASDisplayNodeAssertNotNil(child, @"Child that will be overlayed on shouldn't be nil"); + _overlay = overlay; + _child = child; return self; } -+ (instancetype)overlayLayoutWithChild:(id)child overlay:(id)overlay ++ (instancetype)overlayLayoutSpecWithChild:(id)child overlay:(id)overlay { return [[self alloc] initWithChild:child overlay:overlay]; } diff --git a/AsyncDisplayKit/Layout/ASRatioLayoutSpec.mm b/AsyncDisplayKit/Layout/ASRatioLayoutSpec.mm index c106844c44..5e5c6f0c66 100644 --- a/AsyncDisplayKit/Layout/ASRatioLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASRatioLayoutSpec.mm @@ -32,13 +32,13 @@ - (instancetype)initWithRatio:(CGFloat)ratio child:(id)child; { - self = [super init]; - if (self) { - ASDisplayNodeAssertNotNil(child, @"Child cannot be nil"); - ASDisplayNodeAssert(ratio > 0, @"Ratio should be strictly positive, but received %f", ratio); - _ratio = ratio; - _child = child; + if (!(self = [super init])) { + return nil; } + ASDisplayNodeAssertNotNil(child, @"Child cannot be nil"); + ASDisplayNodeAssert(ratio > 0, @"Ratio should be strictly positive, but received %f", ratio); + _ratio = ratio; + _child = child; return self; } diff --git a/AsyncDisplayKit/Layout/ASStackLayoutSpec.h b/AsyncDisplayKit/Layout/ASStackLayoutSpec.h index c39522a83f..e71de22819 100644 --- a/AsyncDisplayKit/Layout/ASStackLayoutSpec.h +++ b/AsyncDisplayKit/Layout/ASStackLayoutSpec.h @@ -85,15 +85,13 @@ typedef NS_ENUM(NSUInteger, ASStackLayoutAlignItems) { - (instancetype)init; /** - @param style Specifies how children are laid out. + @param direction The direction of the stack view (horizontal or vertical) + @param spacing The spacing between the children + @param justifyContent If no children are flexible, this describes how to fill any extra space + @param alignItems Orientation of the children along the cross axis @param children ASLayoutable children to be positioned. */ -+ (instancetype)satckLayoutSpecWithDirection:(ASStackLayoutDirection)direction - spacing:(CGFloat)spacing - contentJustification:(ASStackLayoutJustifyContent)justifyContent - itemAlignment:(ASStackLayoutAlignItems)alignItems - children:(NSArray *)children; - ++ (instancetype)stackLayoutSpecWithDirection:(ASStackLayoutDirection)direction spacing:(CGFloat)spacing justifyContent:(ASStackLayoutJustifyContent)justifyContent alignItems:(ASStackLayoutAlignItems)alignItems children:(NSArray *)children; - (void)addChild:(id)child; - (void)addChildren:(NSArray *)children; diff --git a/AsyncDisplayKit/Layout/ASStackLayoutSpec.mm b/AsyncDisplayKit/Layout/ASStackLayoutSpec.mm index e712bd2d0b..9d02cecb8a 100644 --- a/AsyncDisplayKit/Layout/ASStackLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASStackLayoutSpec.mm @@ -28,43 +28,27 @@ - (instancetype)init { - return [self initWithDirection:ASStackLayoutDirectionHorizontal - spacing:0.0 - contentJustification:ASStackLayoutJustifyContentStart - itemAlignment:ASStackLayoutAlignItemsStart - children:nil]; + return [self initWithDirection:ASStackLayoutDirectionHorizontal spacing:0.0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:nil]; } -+ (instancetype)satckLayoutSpecWithDirection:(ASStackLayoutDirection)direction - spacing:(CGFloat)spacing - contentJustification:(ASStackLayoutJustifyContent)justifyContent - itemAlignment:(ASStackLayoutAlignItems)alignItems - children:(NSArray *)children ++ (instancetype)stackLayoutSpecWithDirection:(ASStackLayoutDirection)direction spacing:(CGFloat)spacing justifyContent:(ASStackLayoutJustifyContent)justifyContent alignItems:(ASStackLayoutAlignItems)alignItems children:(NSArray *)children { - return [[self alloc] initWithDirection:direction - spacing:spacing - contentJustification:justifyContent - itemAlignment:alignItems - children:children]; + return [[self alloc] initWithDirection:direction spacing:spacing justifyContent:justifyContent alignItems:alignItems children:children]; } -- (instancetype)initWithDirection:(ASStackLayoutDirection)direction - spacing:(CGFloat)spacing - contentJustification:(ASStackLayoutJustifyContent)justifyContent - itemAlignment:(ASStackLayoutAlignItems)alignItems - children:(NSArray *)children; +- (instancetype)initWithDirection:(ASStackLayoutDirection)direction spacing:(CGFloat)spacing justifyContent:(ASStackLayoutJustifyContent)justifyContent alignItems:(ASStackLayoutAlignItems)alignItems children:(NSArray *)children { - self = [super init]; - if (self) { - _direction = direction; - _alignItems = alignItems; - _spacing = spacing; - _justifyContent = justifyContent; - - _children = std::vector>(); - for (id child in children) { - _children.push_back(child); - } + if (!(self = [super init])) { + return nil; + } + _direction = direction; + _alignItems = alignItems; + _spacing = spacing; + _justifyContent = justifyContent; + + _children = std::vector>(); + for (id child in children) { + _children.push_back(child); } return self; } @@ -82,7 +66,6 @@ } } - - (void)setDirection:(ASStackLayoutDirection)direction { ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable"); @@ -109,11 +92,7 @@ - (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize { - ASStackLayoutSpecStyle style = {.direction = _direction, - .spacing = _spacing, - .justifyContent = _justifyContent, - .alignItems = _alignItems - }; + ASStackLayoutSpecStyle style = {.direction = _direction, .spacing = _spacing, .justifyContent = _justifyContent, .alignItems = _alignItems}; const auto unpositionedLayout = ASStackUnpositionedLayout::compute(_children, style, constrainedSize); const auto positionedLayout = ASStackPositionedLayout::compute(unpositionedLayout, style, constrainedSize); const CGSize finalSize = directionSize(style.direction, unpositionedLayout.stackDimensionSum, positionedLayout.crossSize); diff --git a/AsyncDisplayKit/Layout/ASStaticLayoutSpec.mm b/AsyncDisplayKit/Layout/ASStaticLayoutSpec.mm index b0becb0093..cf2647eba8 100644 --- a/AsyncDisplayKit/Layout/ASStaticLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASStaticLayoutSpec.mm @@ -46,10 +46,10 @@ - (instancetype)initWithChildren:(NSArray *)children { - self = [super init]; - if (self) { - _children = children; + if (!(self = [super init])) { + return nil; } + _children = children; return self; } diff --git a/AsyncDisplayKitTests/ASCenterLayoutSpecSnapshotTests.mm b/AsyncDisplayKitTests/ASCenterLayoutSpecSnapshotTests.mm index 388e7d0e7e..5a7cbd0a92 100644 --- a/AsyncDisplayKitTests/ASCenterLayoutSpecSnapshotTests.mm +++ b/AsyncDisplayKitTests/ASCenterLayoutSpecSnapshotTests.mm @@ -52,9 +52,9 @@ static const ASSizeRange kSize = {{100, 120}, {320, 160}}; ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec - newWithChild: + backgroundLayoutSpecWithChild: [ASCenterLayoutSpec - newWithCenteringOptions:options + centerLayoutSpecWithCenteringOptions:options sizingOptions:sizingOptions child:foregroundNode] background:backgroundNode]; @@ -98,11 +98,11 @@ static NSString *suffixForCenteringOptions(ASCenterLayoutSpecCenteringOptions ce ASCenterLayoutSpec *layoutSpec = [ASCenterLayoutSpec - newWithCenteringOptions:ASCenterLayoutSpecCenteringNone + centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringNone sizingOptions:{} child: [ASBackgroundLayoutSpec - newWithChild:[ASStackLayoutSpec newWithStyle:{} children:@[foregroundNode]] + backgroundLayoutSpecWithChild:[ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[foregroundNode]] background:backgroundNode]]; [self testLayoutSpec:layoutSpec sizeRange:kSize subnodes:@[backgroundNode, foregroundNode] identifier:nil]; diff --git a/AsyncDisplayKitTests/ASInsetLayoutSpecSnapshotTests.mm b/AsyncDisplayKitTests/ASInsetLayoutSpecSnapshotTests.mm index 9ff4bf219e..994acee8c4 100644 --- a/AsyncDisplayKitTests/ASInsetLayoutSpecSnapshotTests.mm +++ b/AsyncDisplayKitTests/ASInsetLayoutSpecSnapshotTests.mm @@ -61,7 +61,7 @@ static NSString *nameForInsets(UIEdgeInsets insets) ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec - newWithChild:[ASInsetLayoutSpec newWithInsets:insets child:foregroundNode] + backgroundLayoutSpecWithChild:[ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:foregroundNode] background:backgroundNode]; static ASSizeRange kVariableSize = {{0, 0}, {300, 300}}; @@ -82,7 +82,7 @@ static NSString *nameForInsets(UIEdgeInsets insets) ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec - newWithChild:[ASInsetLayoutSpec newWithInsets:insets child:foregroundNode] + backgroundLayoutSpecWithChild:[ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:foregroundNode] background:backgroundNode]; static ASSizeRange kFixedSize = {{300, 300}, {300, 300}}; @@ -104,7 +104,7 @@ static NSString *nameForInsets(UIEdgeInsets insets) ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec - newWithChild:[ASInsetLayoutSpec newWithInsets:insets child:foregroundNode] + backgroundLayoutSpecWithChild:[ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:foregroundNode] background:backgroundNode]; static ASSizeRange kFixedSize = {{300, 300}, {300, 300}}; diff --git a/AsyncDisplayKitTests/ASLayoutSpecSnapshotTestsHelper.m b/AsyncDisplayKitTests/ASLayoutSpecSnapshotTestsHelper.m index 0141d225e6..4cd9232f05 100644 --- a/AsyncDisplayKitTests/ASLayoutSpecSnapshotTestsHelper.m +++ b/AsyncDisplayKitTests/ASLayoutSpecSnapshotTestsHelper.m @@ -55,7 +55,7 @@ { ASLayout *layout = [layoutSpecUnderTest measureWithSizeRange:sizeRange]; layout.position = CGPointZero; - layout = [ASLayout newWithLayoutableObject:self size:layout.size sublayouts:@[layout]]; + layout = [ASLayout layoutWithLayoutableObject:self size:layout.size sublayouts:@[layout]]; _layoutUnderTest = [layout flattenedLayoutUsingPredicateBlock:^BOOL(ASLayout *evaluatedLayout) { return [self.subnodes containsObject:evaluatedLayout.layoutableObject]; }]; diff --git a/AsyncDisplayKitTests/ASOverlayLayoutSpecSnapshotTests.mm b/AsyncDisplayKitTests/ASOverlayLayoutSpecSnapshotTests.mm index 066a6c47b5..f024de9faa 100644 --- a/AsyncDisplayKitTests/ASOverlayLayoutSpecSnapshotTests.mm +++ b/AsyncDisplayKitTests/ASOverlayLayoutSpecSnapshotTests.mm @@ -34,10 +34,10 @@ static const ASSizeRange kSize = {{320, 320}, {320, 320}}; ASLayoutSpec *layoutSpec = [ASOverlayLayoutSpec - newWithChild:backgroundNode + overlayLayoutSpecWithChild:backgroundNode overlay: [ASCenterLayoutSpec - newWithCenteringOptions:ASCenterLayoutSpecCenteringXY + centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringXY sizingOptions:{} child:foregroundNode]]; diff --git a/AsyncDisplayKitTests/ASRatioLayoutSpecSnapshotTests.mm b/AsyncDisplayKitTests/ASRatioLayoutSpecSnapshotTests.mm index ffe30e0f0d..5345a84649 100644 --- a/AsyncDisplayKitTests/ASRatioLayoutSpecSnapshotTests.mm +++ b/AsyncDisplayKitTests/ASRatioLayoutSpecSnapshotTests.mm @@ -30,7 +30,7 @@ static const ASSizeRange kFixedSize = {{0, 0}, {100, 100}}; ASStaticSizeDisplayNode *subnode = ASDisplayNodeWithBackgroundColor([UIColor greenColor]); subnode.staticSize = childSize; - ASLayoutSpec *layoutSpec = [ASRatioLayoutSpec newWithRatio:ratio child:subnode]; + ASLayoutSpec *layoutSpec = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:ratio child:subnode]; [self testLayoutSpec:layoutSpec sizeRange:kFixedSize subnodes:@[subnode] identifier:identifier]; } diff --git a/AsyncDisplayKitTests/ASStackLayoutSpecSnapshotTests.mm b/AsyncDisplayKitTests/ASStackLayoutSpecSnapshotTests.mm index 21a885444e..9b6c3d655d 100644 --- a/AsyncDisplayKitTests/ASStackLayoutSpecSnapshotTests.mm +++ b/AsyncDisplayKitTests/ASStackLayoutSpecSnapshotTests.mm @@ -11,6 +11,7 @@ #import "ASLayoutSpecSnapshotTestsHelper.h" #import "ASStackLayoutSpec.h" +#import "ASStackLayoutSpecUtilities.h" #import "ASBackgroundLayoutSpec.h" #import "ASRatioLayoutSpec.h" #import "ASInsetLayoutSpec.h" @@ -79,7 +80,7 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex) ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec - newWithChild:[ASStackLayoutSpec newWithStyle:style children:children] + backgroundLayoutSpecWithChild:[ASStackLayoutSpec stackLayoutSpecWithDirection:style.direction spacing:style.spacing justifyContent:style.justifyContent alignItems:style.alignItems children:children] background:backgroundNode]; NSMutableArray *newSubnodes = [NSMutableArray arrayWithObject:backgroundNode]; @@ -179,17 +180,12 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex) ASLayoutSpec *layoutSpec = [ASInsetLayoutSpec - newWithInsets:{10, 10, 10 ,10} + insetLayoutSpecWithInsets:{10, 10, 10 ,10} child: [ASBackgroundLayoutSpec - newWithChild: + backgroundLayoutSpecWithChild: [ASStackLayoutSpec - newWithStyle:{ - .direction = ASStackLayoutDirectionVertical, - .spacing = 10, - .alignItems = ASStackLayoutAlignItemsStretch - } - children:@[]] + stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[]] background:backgroundNode]]; // width 300px; height 0-300px @@ -257,7 +253,7 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex) ASStaticSizeDisplayNode * subnode2 = ASDisplayNodeWithBackgroundColor([UIColor redColor]); subnode2.staticSize = {50, 50}; - ASRatioLayoutSpec *child1 = [ASRatioLayoutSpec newWithRatio:1.5 child:subnode1]; + ASRatioLayoutSpec *child1 = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.5 child:subnode1]; child1.flexBasis = ASRelativeDimensionMakeWithPercent(1); child1.flexGrow = YES; child1.flexShrink = YES; @@ -481,7 +477,7 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex) ((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {10, 0}; ((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {3000, 3000}; - ASRatioLayoutSpec *child2 = [ASRatioLayoutSpec newWithRatio:1.0 child:subnodes[2]]; + ASRatioLayoutSpec *child2 = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.0 child:subnodes[2]]; child2.flexGrow = YES; child2.flexShrink = YES; @@ -489,16 +485,12 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex) // Instead it should be stretched to 300 points tall, matching the red child and not overlapping the green inset. ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec - newWithChild: + backgroundLayoutSpecWithChild: [ASInsetLayoutSpec - newWithInsets:UIEdgeInsetsMake(10, 10, 10, 10) + insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 10, 10, 10) child: - [ASStackLayoutSpec - newWithStyle:{ - .direction = ASStackLayoutDirectionHorizontal, - .alignItems = ASStackLayoutAlignItemsStretch, - } - children:@[subnodes[1], child2,]]] + [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[subnodes[1], child2,]] + ] background:subnodes[0]]; static ASSizeRange kSize = {{300, 0}, {300, INFINITY}}; diff --git a/examples/Kittens/Sample/BlurbNode.m b/examples/Kittens/Sample/BlurbNode.m index a4802daf5c..693ec0cd03 100644 --- a/examples/Kittens/Sample/BlurbNode.m +++ b/examples/Kittens/Sample/BlurbNode.m @@ -81,9 +81,8 @@ static NSString *kLinkAttributeName = @"PlaceKittenNodeLinkAttributeName"; centerSpec.sizingOptions = ASCenterLayoutSpecSizingOptionMinimumY; centerSpec.child = _textNode; - - return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(kTextPadding, kTextPadding, kTextPadding, kTextPadding) - child:centerSpec]; + UIEdgeInsets padding =UIEdgeInsetsMake(kTextPadding, kTextPadding, kTextPadding, kTextPadding); + return [ASInsetLayoutSpec insetLayoutSpecWithInsets:padding child:centerSpec]; } #else - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize