forked from facebookarchive/AsyncDisplayKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASLayout.mm
More file actions
101 lines (85 loc) · 3.28 KB
/
ASLayout.mm
File metadata and controls
101 lines (85 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* 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 "ASLayout.h"
#import "ASAssert.h"
#import "ASLayoutSpecUtilities.h"
#import "ASInternalHelpers.h"
#import <stack>
CGPoint const CGPointNull = {NAN, NAN};
extern BOOL CGPointIsNull(CGPoint point)
{
return isnan(point.x) && isnan(point.y);
}
@implementation ASLayout
+ (instancetype)layoutWithLayoutableObject:(id<ASLayoutable>)layoutableObject
size:(CGSize)size
position:(CGPoint)position
sublayouts:(NSArray *)sublayouts
{
ASDisplayNodeAssert(layoutableObject, @"layoutableObject is required.");
#if DEBUG
for (ASLayout *sublayout in sublayouts) {
ASDisplayNodeAssert(!CGPointIsNull(sublayout.position), @"Invalid position is not allowed in sublayout.");
}
#endif
ASLayout *l = [super new];
if (l) {
l->_layoutableObject = layoutableObject;
l->_size = CGSizeMake(ASCeilPixelValue(size.width), ASCeilPixelValue(size.height));
if (CGPointIsNull(position) == NO) {
l->_position = CGPointMake(ASCeilPixelValue(position.x), ASCeilPixelValue(position.y));
} else {
l->_position = position;
}
l->_sublayouts = [sublayouts copy];
}
return l;
}
+ (instancetype)layoutWithLayoutableObject:(id<ASLayoutable>)layoutableObject
size:(CGSize)size
sublayouts:(NSArray *)sublayouts
{
return [self layoutWithLayoutableObject:layoutableObject size:size position:CGPointNull sublayouts:sublayouts];
}
+ (instancetype)layoutWithLayoutableObject:(id<ASLayoutable>)layoutableObject size:(CGSize)size
{
return [self layoutWithLayoutableObject:layoutableObject size:size sublayouts:nil];
}
- (ASLayout *)flattenedLayoutUsingPredicateBlock:(BOOL (^)(ASLayout *))predicateBlock
{
NSMutableArray *flattenedSublayouts = [NSMutableArray array];
struct Context {
ASLayout *layout;
CGPoint absolutePosition;
BOOL visited;
};
// Stack of Contexts, used to keep track of sublayouts while traversing this layout in a DFS fashion.
std::stack<Context> stack;
stack.push({self, CGPointMake(0, 0), NO});
while (!stack.empty()) {
Context &context = stack.top();
if (context.visited) {
stack.pop();
} else {
context.visited = YES;
if (predicateBlock(context.layout)) {
[flattenedSublayouts addObject:[ASLayout layoutWithLayoutableObject:context.layout.layoutableObject
size:context.layout.size
position:context.absolutePosition
sublayouts:nil]];
}
for (ASLayout *sublayout in context.layout.sublayouts) {
stack.push({sublayout, context.absolutePosition + sublayout.position, NO});
}
}
}
return [ASLayout layoutWithLayoutableObject:_layoutableObject size:_size sublayouts:flattenedSublayouts];
}
@end