Add support for multiple children to ASWrapperLayoutSpec (#2469)

This commit is contained in:
Michael Schneider
2016-10-24 09:59:44 -07:00
committed by GitHub
parent 97e23776e4
commit 4e580b96dc
2 changed files with 44 additions and 7 deletions

View File

@@ -63,19 +63,31 @@ NS_ASSUME_NONNULL_BEGIN
@end
/**
* An ASLayoutSpec subclass that can wrap a ASLayoutElement and calculates the layout of the child.
* An ASLayoutSpec subclass that can wrap one or more ASLayoutElement and calculates the layout based on the
* sizes of the children. If multiple children are provided the size of the biggest child will be used to for
* size of this layout spec.
*/
@interface ASWrapperLayoutSpec : ASLayoutSpec
/*
* Returns an ASWrapperLayoutSpec object with the given layoutElement as child
* Returns an ASWrapperLayoutSpec object with the given layoutElement as child.
*/
+ (instancetype)wrapperWithLayoutElement:(id<ASLayoutElement>)layoutElement AS_WARN_UNUSED_RESULT;
/*
* Returns an ASWrapperLayoutSpec object initialized with the given layoutElement as child
* Returns an ASWrapperLayoutSpec object with the given layoutElements as children.
*/
- (instancetype)initWithLayoutElement:(id<ASLayoutElement>)layoutElement NS_DESIGNATED_INITIALIZER;
+ (instancetype)wrapperWithLayoutElements:(NSArray<id<ASLayoutElement>> *)layoutElements AS_WARN_UNUSED_RESULT;
/*
* Returns an ASWrapperLayoutSpec object initialized with the given layoutElement as child.
*/
- (instancetype)initWithLayoutElement:(id<ASLayoutElement>)layoutElement AS_WARN_UNUSED_RESULT;
/*
* Returns an ASWrapperLayoutSpec object initialized with the given layoutElements as children.
*/
- (instancetype)initWithLayoutElements:(NSArray<id<ASLayoutElement>> *)layoutElements AS_WARN_UNUSED_RESULT;
/*
* Init not available for ASWrapperLayoutSpec

View File

@@ -315,11 +315,36 @@ ASLayoutElementStyleForwarding
return self;
}
+ (instancetype)wrapperWithLayoutElements:(NSArray<id<ASLayoutElement>> *)layoutElements
{
return [[self alloc] initWithLayoutElements:layoutElements];
}
- (instancetype)initWithLayoutElements:(NSArray<id<ASLayoutElement>> *)layoutElements
{
self = [super init];
if (self) {
self.children = layoutElements;
}
return self;
}
- (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
{
ASLayout *sublayout = [self.child layoutThatFits:constrainedSize parentSize:constrainedSize.max];
sublayout.position = CGPointZero;
return [ASLayout layoutWithLayoutElement:self size:sublayout.size sublayouts:@[sublayout]];
NSArray *children = self.children;
NSMutableArray *sublayouts = [NSMutableArray arrayWithCapacity:children.count];
CGSize size = constrainedSize.min;
for (id<ASLayoutElement> child in children) {
ASLayout *sublayout = [child layoutThatFits:constrainedSize parentSize:constrainedSize.max];
size.width = MAX(size.width, sublayout.size.width);
size.height = MAX(size.height, sublayout.size.height);
[sublayouts addObject:sublayout];
}
return [ASLayout layoutWithLayoutElement:self size:size sublayouts:sublayouts];
}
@end