From 774d91653bcd0e16fcbc02b646d0fbb645aea086 Mon Sep 17 00:00:00 2001 From: ricky cancro <@pinterest.com> Date: Wed, 19 Aug 2015 16:59:23 -0700 Subject: [PATCH] flailing --- AsyncDisplayKit.xcodeproj/project.pbxproj | 6 ++ AsyncDisplayKit/ASDisplayNode.h | 4 +- AsyncDisplayKit/ASDisplayNode.mm | 9 +-- AsyncDisplayKit/ASTextNode.mm | 24 +++++++- AsyncDisplayKit/Layout/ASLayoutSpec.h | 4 +- AsyncDisplayKit/Layout/ASLayoutSpec.mm | 7 ++- AsyncDisplayKit/Layout/ASLayoutable.h | 47 --------------- AsyncDisplayKit/Layout/ASStackLayoutDefines.h | 49 ++++++++++++++-- AsyncDisplayKit/Layout/ASStackLayoutSpec.h | 44 +------------- AsyncDisplayKit/Layout/ASStackLayoutSpec.mm | 17 ++++-- AsyncDisplayKit/Layout/ASStackLayoutable.h | 57 +++++++++++++++++++ .../Private/ASStackLayoutSpecUtilities.h | 4 -- .../Private/ASStackPositionedLayout.h | 3 +- .../Private/ASStackPositionedLayout.mm | 15 ++--- .../Private/ASStackUnpositionedLayout.h | 4 +- .../Private/ASStackUnpositionedLayout.mm | 12 ++-- 16 files changed, 172 insertions(+), 134 deletions(-) create mode 100644 AsyncDisplayKit/Layout/ASStackLayoutable.h diff --git a/AsyncDisplayKit.xcodeproj/project.pbxproj b/AsyncDisplayKit.xcodeproj/project.pbxproj index aee3b1867a..7cff23c102 100644 --- a/AsyncDisplayKit.xcodeproj/project.pbxproj +++ b/AsyncDisplayKit.xcodeproj/project.pbxproj @@ -217,6 +217,8 @@ 509E68651B3AEDC5009B9150 /* CGRect+ASConvenience.h in Headers */ = {isa = PBXBuildFile; fileRef = 205F0E1F1B376416007741D0 /* CGRect+ASConvenience.h */; }; 509E68661B3AEDD7009B9150 /* CGRect+ASConvenience.m in Sources */ = {isa = PBXBuildFile; fileRef = 205F0E201B376416007741D0 /* CGRect+ASConvenience.m */; }; 6BDC61F61979037800E50D21 /* AsyncDisplayKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 6BDC61F51978FEA400E50D21 /* AsyncDisplayKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 9C49C36F1B853957000B0DD5 /* ASStackLayoutable.h in Headers */ = {isa = PBXBuildFile; fileRef = 9C49C36E1B853957000B0DD5 /* ASStackLayoutable.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 9C49C3701B853961000B0DD5 /* ASStackLayoutable.h in Headers */ = {isa = PBXBuildFile; fileRef = 9C49C36E1B853957000B0DD5 /* ASStackLayoutable.h */; settings = {ATTRIBUTES = (Public, ); }; }; 9F06E5CD1B4CAF4200F015D8 /* ASCollectionViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9F06E5CC1B4CAF4200F015D8 /* ASCollectionViewTests.m */; }; AC21EC101B3D0BF600C8B19A /* ASStackLayoutDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = AC21EC0F1B3D0BF600C8B19A /* ASStackLayoutDefines.h */; settings = {ATTRIBUTES = (Public, ); }; }; AC3C4A511A1139C100143C57 /* ASCollectionView.h in Headers */ = {isa = PBXBuildFile; fileRef = AC3C4A4F1A1139C100143C57 /* ASCollectionView.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -556,6 +558,7 @@ 4640521E1A3F83C40061C0BA /* ASMultidimensionalArrayUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASMultidimensionalArrayUtils.h; sourceTree = ""; }; 4640521F1A3F83C40061C0BA /* ASMultidimensionalArrayUtils.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASMultidimensionalArrayUtils.mm; sourceTree = ""; }; 6BDC61F51978FEA400E50D21 /* AsyncDisplayKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AsyncDisplayKit.h; sourceTree = ""; }; + 9C49C36E1B853957000B0DD5 /* ASStackLayoutable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASStackLayoutable.h; path = AsyncDisplayKit/Layout/ASStackLayoutable.h; sourceTree = ""; }; 9F06E5CC1B4CAF4200F015D8 /* ASCollectionViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASCollectionViewTests.m; sourceTree = ""; }; AC21EC0F1B3D0BF600C8B19A /* ASStackLayoutDefines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASStackLayoutDefines.h; path = AsyncDisplayKit/Layout/ASStackLayoutDefines.h; sourceTree = ""; }; AC3C4A4F1A1139C100143C57 /* ASCollectionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASCollectionView.h; sourceTree = ""; }; @@ -958,6 +961,7 @@ ACF6ED131B17843500DA7C62 /* ASOverlayLayoutSpec.mm */, ACF6ED141B17843500DA7C62 /* ASRatioLayoutSpec.h */, ACF6ED151B17843500DA7C62 /* ASRatioLayoutSpec.mm */, + 9C49C36E1B853957000B0DD5 /* ASStackLayoutable.h */, AC21EC0F1B3D0BF600C8B19A /* ASStackLayoutDefines.h */, ACF6ED161B17843500DA7C62 /* ASStackLayoutSpec.h */, ACF6ED171B17843500DA7C62 /* ASStackLayoutSpec.mm */, @@ -1004,6 +1008,7 @@ files = ( AC21EC101B3D0BF600C8B19A /* ASStackLayoutDefines.h in Headers */, AC47D9451B3BB41900AAEE9D /* ASRelativeSize.h in Headers */, + 9C49C36F1B853957000B0DD5 /* ASStackLayoutable.h in Headers */, ACF6ED511B17847A00DA7C62 /* ASStackUnpositionedLayout.h in Headers */, ACF6ED2D1B17843500DA7C62 /* ASRatioLayoutSpec.h in Headers */, ACF6ED261B17843500DA7C62 /* ASLayoutSpec.h in Headers */, @@ -1170,6 +1175,7 @@ B350620C1B010EFD0018CF92 /* ASTableViewProtocols.h in Headers */, B35062481B010EFD0018CF92 /* _AS-objc-internal.h in Headers */, B350623F1B010EFD0018CF92 /* _ASAsyncTransactionContainer.h in Headers */, + 9C49C3701B853961000B0DD5 /* ASStackLayoutable.h in Headers */, 34EFC7731B701D0700AD841F /* ASStaticLayoutSpec.h in Headers */, B35062081B010EFD0018CF92 /* ASScrollNode.h in Headers */, B35061F51B010EFD0018CF92 /* ASCollectionView.h in Headers */, diff --git a/AsyncDisplayKit/ASDisplayNode.h b/AsyncDisplayKit/ASDisplayNode.h index 7fcfbd5999..974bbe1272 100644 --- a/AsyncDisplayKit/ASDisplayNode.h +++ b/AsyncDisplayKit/ASDisplayNode.h @@ -13,7 +13,7 @@ #import #import -#import +#import /** * UIView creation block. Used to create the backing view of a new display node. @@ -40,7 +40,7 @@ typedef CALayer *(^ASDisplayNodeLayerBlock)(); * */ -@interface ASDisplayNode : ASDealloc2MainObject +@interface ASDisplayNode : ASDealloc2MainObject /** @name Initializing a node object */ diff --git a/AsyncDisplayKit/ASDisplayNode.mm b/AsyncDisplayKit/ASDisplayNode.mm index e860b1bfc6..e14f256b5a 100644 --- a/AsyncDisplayKit/ASDisplayNode.mm +++ b/AsyncDisplayKit/ASDisplayNode.mm @@ -48,8 +48,6 @@ @synthesize flexBasis = _flexBasis; @synthesize alignSelf = _alignSelf; @synthesize preferredFrameSize = _preferredFrameSize; -@synthesize ascender = _ascender; -@synthesize descender = _descender; BOOL ASDisplayNodeSubclassOverridesSelector(Class subclass, SEL selector) { @@ -159,8 +157,6 @@ void ASDisplayNodeRespectThreadAffinityOfNode(ASDisplayNode *node, void (^block) _flexBasis = ASRelativeDimensionUnconstrained; _preferredFrameSize = CGSizeZero; - _ascender = 0; - _descender = 0; } - (id)init @@ -457,6 +453,11 @@ void ASDisplayNodeRespectThreadAffinityOfNode(ASDisplayNode *node, void (^block) #pragma mark - +- (CGFloat)distanceToBaseline:(ASStackLayoutAlignItems)baselineAlignmentType +{ + return 0; +} + - (CGSize)measure:(CGSize)constrainedSize { return [self measureWithSizeRange:ASSizeRangeMake(CGSizeZero, constrainedSize)].size; diff --git a/AsyncDisplayKit/ASTextNode.mm b/AsyncDisplayKit/ASTextNode.mm index b46620d565..f15ab5f786 100644 --- a/AsyncDisplayKit/ASTextNode.mm +++ b/AsyncDisplayKit/ASTextNode.mm @@ -17,6 +17,7 @@ #import #import +#import "ASInternalHelpers.h" #import "ASTextNodeRenderer.h" #import "ASTextNodeShadower.h" @@ -105,6 +106,9 @@ ASDISPLAYNODE_INLINE CGFloat ceilPixelValue(CGFloat f) ASTextNodeShadower *_shadower; UILongPressGestureRecognizer *_longPressGestureRecognizer; + + CGFloat _topBaseline; + CGFloat _bottomBaseline; } #pragma mark - NSObject @@ -356,8 +360,24 @@ ASDISPLAYNODE_INLINE CGFloat ceilPixelValue(CGFloat f) } }); - self.ascender = round([[attributedString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL] ascender] * [UIScreen mainScreen].scale)/[UIScreen mainScreen].scale; - self.descender = round([[attributedString attribute:NSFontAttributeName atIndex:attributedString.length - 1 effectiveRange:NULL] descender] * [UIScreen mainScreen].scale)/[UIScreen mainScreen].scale; + _topBaseline = round([[attributedString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL] ascender] * ASScreenScale())/ASScreenScale(); + _bottomBaseline = round([[attributedString attribute:NSFontAttributeName atIndex:attributedString.length - 1 effectiveRange:NULL] descender] * ASScreenScale())/ASScreenScale(); +} + +#pragma mark - Baseline computation + +- (CGFloat)distanceToBaseline:(ASStackLayoutAlignItems)baselineAlignmentType +{ + switch (baselineAlignmentType) { + case ASStackLayoutAlignItemsLastBaseline: + return self.calculatedSize.height + _bottomBaseline; + + case ASStackLayoutAlignItemsFirstBaseline: + return _topBaseline; + + default: + return 0; + } } #pragma mark - Text Layout diff --git a/AsyncDisplayKit/Layout/ASLayoutSpec.h b/AsyncDisplayKit/Layout/ASLayoutSpec.h index c2602824e7..e92bbf75cc 100644 --- a/AsyncDisplayKit/Layout/ASLayoutSpec.h +++ b/AsyncDisplayKit/Layout/ASLayoutSpec.h @@ -8,9 +8,9 @@ * */ -#import +#import /** A layout spec is an immutable object that describes a layout, loosely inspired by React. */ -@interface ASLayoutSpec : NSObject +@interface ASLayoutSpec : NSObject @end diff --git a/AsyncDisplayKit/Layout/ASLayoutSpec.mm b/AsyncDisplayKit/Layout/ASLayoutSpec.mm index 377fbe144b..34f6206e1b 100644 --- a/AsyncDisplayKit/Layout/ASLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASLayoutSpec.mm @@ -24,8 +24,6 @@ @synthesize flexShrink = _flexShrink; @synthesize flexBasis = _flexBasis; @synthesize alignSelf = _alignSelf; -@synthesize ascender = _ascender; -@synthesize descender = _descender; + (instancetype)new { @@ -36,6 +34,11 @@ return spec; } +- (CGFloat)distanceToBaseline:(ASStackLayoutAlignItems)baselineAlignmentType +{ + return 0; +} + #pragma mark - Layout - (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize diff --git a/AsyncDisplayKit/Layout/ASLayoutable.h b/AsyncDisplayKit/Layout/ASLayoutable.h index e6f7df7bdc..4ac01ee05f 100644 --- a/AsyncDisplayKit/Layout/ASLayoutable.h +++ b/AsyncDisplayKit/Layout/ASLayoutable.h @@ -20,53 +20,6 @@ */ @protocol ASLayoutable -/** - * @abstract Additional space to place before this object in the stacking direction. - * Used when attached to a stack layout. - */ -@property (nonatomic, readwrite) CGFloat spacingBefore; - -/** - * @abstract Additional space to place after this object in the stacking direction. - * Used when attached to a stack layout. - */ -@property (nonatomic, readwrite) CGFloat spacingAfter; - -/** - * @abstract If the sum of childrens' stack dimensions is less than the minimum size, should this object grow? - * Used when attached to a stack layout. - */ -@property (nonatomic, readwrite) BOOL flexGrow; - -/** - * @abstract If the sum of childrens' stack dimensions is greater than the maximum size, should this object shrink? - * Used when attached to a stack layout. - */ -@property (nonatomic, readwrite) BOOL flexShrink; - -/** - * @abstract Specifies the initial size in the stack dimension for this object. - * Default to ASRelativeDimensionUnconstrained. - * Used when attached to a stack layout. - */ -@property (nonatomic, readwrite) ASRelativeDimension flexBasis; - -/** - * @abstract Orientation of the object along cross axis, overriding alignItems - * Used when attached to a stack layout. - */ -@property (nonatomic, readwrite) ASStackLayoutAlignSelf alignSelf; - -/** - * @abstract Used for baseline alignment. The distance from the top of the object to its baseline. - */ -@property (nonatomic, readwrite) CGFloat ascender; - -/** - * @abstract Used for baseline alignment. The distance from the baseline of the object to its bottom. - */ -@property (nonatomic, readwrite) CGFloat descender; - /** * @abstract Calculate a layout based on given size range. * diff --git a/AsyncDisplayKit/Layout/ASStackLayoutDefines.h b/AsyncDisplayKit/Layout/ASStackLayoutDefines.h index 0e6a600ec3..39e51b65c8 100644 --- a/AsyncDisplayKit/Layout/ASStackLayoutDefines.h +++ b/AsyncDisplayKit/Layout/ASStackLayoutDefines.h @@ -8,6 +8,49 @@ * */ +/** The direction children are stacked in */ +typedef NS_ENUM(NSUInteger, ASStackLayoutDirection) { + /** Children are stacked vertically */ + ASStackLayoutDirectionVertical, + /** Children are stacked horizontally */ + ASStackLayoutDirectionHorizontal, +}; + +/** If no children are flexible, how should this spec justify its children in the available space? */ +typedef NS_ENUM(NSUInteger, ASStackLayoutJustifyContent) { + /** + On overflow, children overflow out of this spec's bounds on the right/bottom side. + On underflow, children are left/top-aligned within this spec's bounds. + */ + ASStackLayoutJustifyContentStart, + /** + On overflow, children are centered and overflow on both sides. + On underflow, children are centered within this spec's bounds in the stacking direction. + */ + ASStackLayoutJustifyContentCenter, + /** + On overflow, children overflow out of this spec's bounds on the left/top side. + On underflow, children are right/bottom-aligned within this spec's bounds. + */ + ASStackLayoutJustifyContentEnd, +}; + +/** Orientation of children along cross axis */ +typedef NS_ENUM(NSUInteger, ASStackLayoutAlignItems) { + /** Align children to start of cross axis */ + ASStackLayoutAlignItemsStart, + /** Align children with end of cross axis */ + ASStackLayoutAlignItemsEnd, + /** Center children on cross axis */ + ASStackLayoutAlignItemsCenter, + /** Expand children to fill cross axis */ + ASStackLayoutAlignItemsStretch, + /** Children align along the first baseline of the stack. Only available for horizontal stack nodes */ + ASStackLayoutAlignItemsFirstBaseline, + /** Children align along the last baseline of the stack. Only available for horizontal stack nodes */ + ASStackLayoutAlignItemsLastBaseline, +}; + /** Each child may override their parent stack's cross axis alignment. @see ASStackLayoutAlignItems @@ -23,8 +66,6 @@ typedef NS_ENUM(NSUInteger, ASStackLayoutAlignSelf) { ASStackLayoutAlignSelfCenter, /** Expand to fill cross axis */ ASStackLayoutAlignSelfStretch, - /** Children align to their first baseline. Only available for horizontal stack nodes */ - ASStackLayoutAlignSelfBaselineFirst, - /** Children align to their last baseline. Only available for horizontal stack nodes */ - ASStackLayoutAlignSelfBaselineLast, + + /** Note: All children in a stack must have the same baseline align type */ }; diff --git a/AsyncDisplayKit/Layout/ASStackLayoutSpec.h b/AsyncDisplayKit/Layout/ASStackLayoutSpec.h index 77ce335891..242d6e3122 100644 --- a/AsyncDisplayKit/Layout/ASStackLayoutSpec.h +++ b/AsyncDisplayKit/Layout/ASStackLayoutSpec.h @@ -9,49 +9,7 @@ */ #import - -/** The direction children are stacked in */ -typedef NS_ENUM(NSUInteger, ASStackLayoutDirection) { - /** Children are stacked vertically */ - ASStackLayoutDirectionVertical, - /** Children are stacked horizontally */ - ASStackLayoutDirectionHorizontal, -}; - -/** If no children are flexible, how should this spec justify its children in the available space? */ -typedef NS_ENUM(NSUInteger, ASStackLayoutJustifyContent) { - /** - On overflow, children overflow out of this spec's bounds on the right/bottom side. - On underflow, children are left/top-aligned within this spec's bounds. - */ - ASStackLayoutJustifyContentStart, - /** - On overflow, children are centered and overflow on both sides. - On underflow, children are centered within this spec's bounds in the stacking direction. - */ - ASStackLayoutJustifyContentCenter, - /** - On overflow, children overflow out of this spec's bounds on the left/top side. - On underflow, children are right/bottom-aligned within this spec's bounds. - */ - ASStackLayoutJustifyContentEnd, -}; - -/** Orientation of children along cross axis */ -typedef NS_ENUM(NSUInteger, ASStackLayoutAlignItems) { - /** Align children to start of cross axis */ - ASStackLayoutAlignItemsStart, - /** Align children with end of cross axis */ - ASStackLayoutAlignItemsEnd, - /** Center children on cross axis */ - ASStackLayoutAlignItemsCenter, - /** Expand children to fill cross axis */ - ASStackLayoutAlignItemsStretch, - /** Children align to their first baseline. Only available for horizontal stack nodes */ - ASStackLayoutAlignItemsBaselineFirst, - /** Children align to their last baseline. Only available for horizontal stack nodes */ - ASStackLayoutAlignItemsBaselineLast, -}; +#import typedef struct { /** Specifies the direction children are stacked in. */ diff --git a/AsyncDisplayKit/Layout/ASStackLayoutSpec.mm b/AsyncDisplayKit/Layout/ASStackLayoutSpec.mm index 5e36099d17..90d2cc7fd8 100644 --- a/AsyncDisplayKit/Layout/ASStackLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASStackLayoutSpec.mm @@ -25,8 +25,9 @@ @implementation ASStackLayoutSpec { ASStackLayoutSpecStyle _style; - std::vector> _children; + std::vector> _children; ASDN::RecursiveMutex _propertyLock; + CGFloat _distanceToBaseline; } + (instancetype)newWithStyle:(ASStackLayoutSpecStyle)style children:(NSArray *)children @@ -34,8 +35,10 @@ ASStackLayoutSpec *spec = [super new]; if (spec) { spec->_style = style; - spec->_children = std::vector>(); - for (id child in children) { + spec->_children = std::vector>(); + for (id child in children) { + ASDisplayNodeAssert([child conformsToProtocol:@protocol(ASStackLayoutable)], @"child must conform to ASStackLayoutable"); + spec->_children.push_back(child); } } @@ -55,12 +58,16 @@ NSArray *sublayouts = [NSArray arrayWithObjects:&positionedLayout.sublayouts[0] count:positionedLayout.sublayouts.size()]; ASDN::MutexLocker l(_propertyLock); - self.ascender = positionedLayout.ascender; - self.descender = positionedLayout.descender; + _distanceToBaseline = positionedLayout.distanceToBaseline; return [ASLayout newWithLayoutableObject:self size:ASSizeRangeClamp(constrainedSize, finalSize) sublayouts:sublayouts]; } +- (CGFloat)distanceToBaseline:(ASStackLayoutAlignItems)baselineAlignmentType +{ + return _distanceToBaseline; +} + @end diff --git a/AsyncDisplayKit/Layout/ASStackLayoutable.h b/AsyncDisplayKit/Layout/ASStackLayoutable.h new file mode 100644 index 0000000000..643bed0382 --- /dev/null +++ b/AsyncDisplayKit/Layout/ASStackLayoutable.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#import + +@protocol ASStackLayoutable + +/** + * @abstract Additional space to place before this object in the stacking direction. + * Used when attached to a stack layout. + */ +@property (nonatomic, readwrite) CGFloat spacingBefore; + +/** + * @abstract Additional space to place after this object in the stacking direction. + * Used when attached to a stack layout. + */ +@property (nonatomic, readwrite) CGFloat spacingAfter; + +/** + * @abstract If the sum of childrens' stack dimensions is less than the minimum size, should this object grow? + * Used when attached to a stack layout. + */ +@property (nonatomic, readwrite) BOOL flexGrow; + +/** + * @abstract If the sum of childrens' stack dimensions is greater than the maximum size, should this object shrink? + * Used when attached to a stack layout. + */ +@property (nonatomic, readwrite) BOOL flexShrink; + +/** + * @abstract Specifies the initial size in the stack dimension for this object. + * Default to ASRelativeDimensionUnconstrained. + * Used when attached to a stack layout. + */ +@property (nonatomic, readwrite) ASRelativeDimension flexBasis; + +/** + * @abstract Orientation of the object along cross axis, overriding alignItems + * Used when attached to a stack layout. + */ +@property (nonatomic, readwrite) ASStackLayoutAlignSelf alignSelf; + +/** + * @abstract Used for baseline alignment in stack spec. The distance from the top of the object to its baseline. + */ +- (CGFloat)distanceToBaseline:(ASStackLayoutAlignItems)baselineAlignmentType; + +@end diff --git a/AsyncDisplayKit/Private/ASStackLayoutSpecUtilities.h b/AsyncDisplayKit/Private/ASStackLayoutSpecUtilities.h index 8a6f7f5126..4f5eff0aeb 100644 --- a/AsyncDisplayKit/Private/ASStackLayoutSpecUtilities.h +++ b/AsyncDisplayKit/Private/ASStackLayoutSpecUtilities.h @@ -55,10 +55,6 @@ inline ASStackLayoutAlignItems alignment(ASStackLayoutAlignSelf childAlignment, return ASStackLayoutAlignItemsStart; case ASStackLayoutAlignSelfStretch: return ASStackLayoutAlignItemsStretch; - case ASStackLayoutAlignSelfBaselineFirst: - return ASStackLayoutAlignItemsBaselineFirst; - case ASStackLayoutAlignSelfBaselineLast: - return ASStackLayoutAlignItemsBaselineLast; case ASStackLayoutAlignSelfAuto: default: return stackAlignment; diff --git a/AsyncDisplayKit/Private/ASStackPositionedLayout.h b/AsyncDisplayKit/Private/ASStackPositionedLayout.h index b8664a3855..ea9f56fd26 100644 --- a/AsyncDisplayKit/Private/ASStackPositionedLayout.h +++ b/AsyncDisplayKit/Private/ASStackPositionedLayout.h @@ -17,8 +17,7 @@ struct ASStackPositionedLayout { const std::vector sublayouts; const CGFloat crossSize; - const CGFloat ascender; - const CGFloat descender; + const CGFloat distanceToBaseline; /** Given an unpositioned layout, computes the positions each child should be placed at. */ static ASStackPositionedLayout compute(const ASStackUnpositionedLayout &unpositionedLayout, diff --git a/AsyncDisplayKit/Private/ASStackPositionedLayout.mm b/AsyncDisplayKit/Private/ASStackPositionedLayout.mm index 4a78594cee..1e667abd73 100644 --- a/AsyncDisplayKit/Private/ASStackPositionedLayout.mm +++ b/AsyncDisplayKit/Private/ASStackPositionedLayout.mm @@ -18,10 +18,8 @@ static CGFloat baselineForItem(const ASStackLayoutSpecStyle &style, const ASStackUnpositionedItem &item) { const ASStackLayoutAlignItems alignItems = alignment(item.child.alignSelf, style.alignItems); - if (alignItems == ASStackLayoutAlignItemsBaselineFirst) { - return item.child.ascender; - } else if (alignItems == ASStackLayoutAlignItemsBaselineLast) { - return item.layout.size.height + item.child.descender; + if (alignItems == ASStackLayoutAlignItemsFirstBaseline || alignItems == ASStackLayoutAlignItemsLastBaseline) { + return [item.child distanceToBaseline:alignItems]; } return 0; } @@ -39,9 +37,8 @@ static CGFloat crossOffset(const ASStackLayoutSpecStyle &style, case ASStackLayoutAlignItemsStart: case ASStackLayoutAlignItemsStretch: return 0; - case ASStackLayoutAlignItemsBaselineFirst: - return maxBaseline - l.child.ascender; - case ASStackLayoutAlignItemsBaselineLast: + case ASStackLayoutAlignItemsLastBaseline: + case ASStackLayoutAlignItemsFirstBaseline: return maxBaseline - baselineForItem(style, l); } } @@ -78,11 +75,11 @@ static ASStackPositionedLayout stackedLayout(const ASStackLayoutSpecStyle &style first = NO; l.layout.position = p + directionPoint(style.direction, 0, crossOffset(style, l, crossSize, maxBaseline)); - CGFloat spacingAfterBaseline = (style.direction == ASStackLayoutDirectionVertical && style.baselineRelativeArrangement) ? l.child.descender : 0; + CGFloat spacingAfterBaseline = (style.direction == ASStackLayoutDirectionVertical && style.baselineRelativeArrangement) ? l.layout.size.height - [l.child distanceToBaseline:style.alignItems] : 0; p = p + directionPoint(style.direction, stackDimension(style.direction, l.layout.size) + l.child.spacingAfter + spacingAfterBaseline, 0); return l.layout; }); - return {stackedChildren, crossSize, maxBaseline, maxBaseline == 0 ? 0 : maxBaseline - crossSize}; + return {stackedChildren, crossSize, maxBaseline}; } ASStackPositionedLayout ASStackPositionedLayout::compute(const ASStackUnpositionedLayout &unpositionedLayout, diff --git a/AsyncDisplayKit/Private/ASStackUnpositionedLayout.h b/AsyncDisplayKit/Private/ASStackUnpositionedLayout.h index 45f648192e..44b7770ccf 100644 --- a/AsyncDisplayKit/Private/ASStackUnpositionedLayout.h +++ b/AsyncDisplayKit/Private/ASStackUnpositionedLayout.h @@ -15,7 +15,7 @@ struct ASStackUnpositionedItem { /** The original source child. */ - id child; + id child; /** The proposed layout. */ ASLayout *layout; }; @@ -30,7 +30,7 @@ struct ASStackUnpositionedLayout { const CGFloat violation; /** Given a set of children, computes the unpositioned layouts for those children. */ - static ASStackUnpositionedLayout compute(const std::vector> &children, + static ASStackUnpositionedLayout compute(const std::vector> &children, const ASStackLayoutSpecStyle &style, const ASSizeRange &sizeRange); }; diff --git a/AsyncDisplayKit/Private/ASStackUnpositionedLayout.mm b/AsyncDisplayKit/Private/ASStackUnpositionedLayout.mm index 9eb83a5b0d..0c094bedfd 100644 --- a/AsyncDisplayKit/Private/ASStackUnpositionedLayout.mm +++ b/AsyncDisplayKit/Private/ASStackUnpositionedLayout.mm @@ -18,7 +18,7 @@ /** Sizes the child given the parameters specified, and returns the computed layout. */ -static ASLayout *crossChildLayout(const id child, +static ASLayout *crossChildLayout(const id child, const ASStackLayoutSpecStyle style, const CGFloat stackMin, const CGFloat stackMax, @@ -186,7 +186,7 @@ static std::function isFlexibleInViolatio } } -ASDISPLAYNODE_INLINE BOOL isFlexibleInBothDirections(id child) +ASDISPLAYNODE_INLINE BOOL isFlexibleInBothDirections(id child) { return child.flexGrow && child.flexShrink; } @@ -195,7 +195,7 @@ ASDISPLAYNODE_INLINE BOOL isFlexibleInBothDirections(id child) If we have a single flexible (both shrinkable and growable) child, and our allowed size range is set to a specific number then we may avoid the first "intrinsic" size calculation. */ -ASDISPLAYNODE_INLINE BOOL useOptimizedFlexing(const std::vector> &children, +ASDISPLAYNODE_INLINE BOOL useOptimizedFlexing(const std::vector> &children, const ASStackLayoutSpecStyle &style, const ASSizeRange &sizeRange) { @@ -283,7 +283,7 @@ static void flexChildrenAlongStackDimension(std::vector Performs the first unconstrained layout of the children, generating the unpositioned items that are then flexed and stretched. */ -static std::vector layoutChildrenAlongUnconstrainedStackDimension(const std::vector> &children, +static std::vector layoutChildrenAlongUnconstrainedStackDimension(const std::vector> &children, const ASStackLayoutSpecStyle &style, const ASSizeRange &sizeRange, const CGSize size, @@ -292,7 +292,7 @@ static std::vector layoutChildrenAlongUnconstrainedStac const CGFloat minCrossDimension = crossDimension(style.direction, sizeRange.min); const CGFloat maxCrossDimension = crossDimension(style.direction, sizeRange.max); - return AS::map(children, [&](id child) -> ASStackUnpositionedItem { + return AS::map(children, [&](id child) -> ASStackUnpositionedItem { const BOOL isUnconstrainedFlexBasis = ASRelativeDimensionEqualToRelativeDimension(ASRelativeDimensionUnconstrained, child.flexBasis); const CGFloat exactStackDimension = ASRelativeDimensionResolve(child.flexBasis, stackDimension(style.direction, size)); @@ -312,7 +312,7 @@ static std::vector layoutChildrenAlongUnconstrainedStac }); } -ASStackUnpositionedLayout ASStackUnpositionedLayout::compute(const std::vector> &children, +ASStackUnpositionedLayout ASStackUnpositionedLayout::compute(const std::vector> &children, const ASStackLayoutSpecStyle &style, const ASSizeRange &sizeRange) {