This commit is contained in:
ricky cancro
2015-08-19 16:59:23 -07:00
parent 11896904a2
commit 774d91653b
16 changed files with 172 additions and 134 deletions

View File

@@ -8,9 +8,9 @@
*
*/
#import <AsyncDisplayKit/ASLayoutable.h>
#import <AsyncDisplayKit/ASStackLayoutable.h>
/** A layout spec is an immutable object that describes a layout, loosely inspired by React. */
@interface ASLayoutSpec : NSObject <ASLayoutable>
@interface ASLayoutSpec : NSObject <ASStackLayoutable>
@end

View File

@@ -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

View File

@@ -20,53 +20,6 @@
*/
@protocol ASLayoutable <NSObject>
/**
* @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.
*

View File

@@ -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 */
};

View File

@@ -9,49 +9,7 @@
*/
#import <AsyncDisplayKit/ASLayoutSpec.h>
/** 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 <AsyncDisplayKit/ASStackLayoutDefines.h>
typedef struct {
/** Specifies the direction children are stacked in. */

View File

@@ -25,8 +25,9 @@
@implementation ASStackLayoutSpec
{
ASStackLayoutSpecStyle _style;
std::vector<id<ASLayoutable>> _children;
std::vector<id<ASStackLayoutable>> _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<id<ASLayoutable>>();
for (id<ASLayoutable> child in children) {
spec->_children = std::vector<id<ASStackLayoutable>>();
for (id<ASStackLayoutable> 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

View File

@@ -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 <AsyncDisplayKit/ASLayoutable.h>
@protocol ASStackLayoutable <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 in stack spec. The distance from the top of the object to its baseline.
*/
- (CGFloat)distanceToBaseline:(ASStackLayoutAlignItems)baselineAlignmentType;
@end