forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASCornerLayoutSpec.mm
More file actions
169 lines (138 loc) · 5.27 KB
/
Copy pathASCornerLayoutSpec.mm
File metadata and controls
169 lines (138 loc) · 5.27 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// ASCornerLayoutSpec.mm
// Texture
//
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
#import <AsyncDisplayKit/ASCornerLayoutSpec.h>
#import <AsyncDisplayKit/ASLayout.h>
#import <AsyncDisplayKit/ASLayoutSpec+Subclasses.h>
#import <AsyncDisplayKit/ASDisplayNode.h>
CGPoint as_calculatedCornerOriginIn(CGRect baseFrame, CGSize cornerSize, ASCornerLayoutLocation cornerLocation, CGPoint offset)
{
CGPoint cornerOrigin = CGPointZero;
CGPoint baseOrigin = baseFrame.origin;
CGSize baseSize = baseFrame.size;
switch (cornerLocation) {
case ASCornerLayoutLocationTopLeft:
cornerOrigin.x = baseOrigin.x - cornerSize.width / 2;
cornerOrigin.y = baseOrigin.y - cornerSize.height / 2;
break;
case ASCornerLayoutLocationTopRight:
cornerOrigin.x = baseOrigin.x + baseSize.width - cornerSize.width / 2;
cornerOrigin.y = baseOrigin.y - cornerSize.height / 2;
break;
case ASCornerLayoutLocationBottomLeft:
cornerOrigin.x = baseOrigin.x - cornerSize.width / 2;
cornerOrigin.y = baseOrigin.y + baseSize.height - cornerSize.height / 2;
break;
case ASCornerLayoutLocationBottomRight:
cornerOrigin.x = baseOrigin.x + baseSize.width - cornerSize.width / 2;
cornerOrigin.y = baseOrigin.y + baseSize.height - cornerSize.height / 2;
break;
}
cornerOrigin.x += offset.x;
cornerOrigin.y += offset.y;
return cornerOrigin;
}
static NSUInteger const kBaseChildIndex = 0;
static NSUInteger const kCornerChildIndex = 1;
@interface ASCornerLayoutSpec()
@end
@implementation ASCornerLayoutSpec
- (instancetype)initWithChild:(id <ASLayoutElement>)child corner:(id <ASLayoutElement>)corner location:(ASCornerLayoutLocation)location
{
self = [super init];
if (self) {
self.child = child;
self.corner = corner;
self.cornerLocation = location;
}
return self;
}
+ (instancetype)cornerLayoutSpecWithChild:(id <ASLayoutElement>)child corner:(id <ASLayoutElement>)corner location:(ASCornerLayoutLocation)location NS_RETURNS_RETAINED
{
return [[self alloc] initWithChild:child corner:corner location:location];
}
#pragma mark - Children
- (void)setChild:(id<ASLayoutElement>)child
{
ASDisplayNodeAssertNotNil(child, @"Child shouldn't be nil.");
[super setChild:child atIndex:kBaseChildIndex];
}
- (id<ASLayoutElement>)child
{
return [super childAtIndex:kBaseChildIndex];
}
- (void)setCorner:(id<ASLayoutElement>)corner
{
ASDisplayNodeAssertNotNil(corner, @"Corner element cannot be nil.");
[super setChild:corner atIndex:kCornerChildIndex];
}
- (id<ASLayoutElement>)corner
{
return [super childAtIndex:kCornerChildIndex];
}
#pragma mark - Calculation
- (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
{
CGSize size = {
ASPointsValidForSize(constrainedSize.max.width) == NO ? ASLayoutElementParentDimensionUndefined : constrainedSize.max.width,
ASPointsValidForSize(constrainedSize.max.height) == NO ? ASLayoutElementParentDimensionUndefined : constrainedSize.max.height
};
id <ASLayoutElement> child = self.child;
id <ASLayoutElement> corner = self.corner;
// Element validation
[self _validateElement:child];
[self _validateElement:corner];
CGRect childFrame = CGRectZero;
CGRect cornerFrame = CGRectZero;
// Layout child
ASLayout *childLayout = [child layoutThatFits:constrainedSize parentSize:size];
childFrame.size = childLayout.size;
// Layout corner
ASLayout *cornerLayout = [corner layoutThatFits:constrainedSize parentSize:size];
cornerFrame.size = cornerLayout.size;
// Calculate corner's position
CGPoint relativePosition = as_calculatedCornerOriginIn(childFrame, cornerFrame.size, _cornerLocation, _offset);
// Update corner's position
cornerFrame.origin = relativePosition;
// Calculate size
CGRect frame = childFrame;
if (_wrapsCorner) {
frame = CGRectUnion(childFrame, cornerFrame);
frame.size = ASSizeRangeClamp(constrainedSize, frame.size);
}
// Shift sublayouts' positions if they are off the bounds.
if (frame.origin.x != 0) {
CGFloat deltaX = frame.origin.x;
childFrame.origin.x -= deltaX;
cornerFrame.origin.x -= deltaX;
}
if (frame.origin.y != 0) {
CGFloat deltaY = frame.origin.y;
childFrame.origin.y -= deltaY;
cornerFrame.origin.y -= deltaY;
}
childLayout.position = childFrame.origin;
cornerLayout.position = cornerFrame.origin;
return [ASLayout layoutWithLayoutElement:self size:frame.size sublayouts:@[childLayout, cornerLayout]];
}
- (void)_validateElement:(id <ASLayoutElement>)element
{
// Validate non-nil element
if (element == nil) {
ASDisplayNodeAssertNotNil(element, @"[%@]: Must have a non-nil child/corner for layout calculation.", self.class);
}
// Validate preferredSize if needed
CGSize size = element.style.preferredSize;
if (!CGSizeEqualToSize(size, CGSizeZero) && !ASIsCGSizeValidForSize(size) && (size.width < 0 || (size.height < 0))) {
ASDisplayNodeFailAssert(@"[%@]: Should give a valid preferredSize value for %@ before corner's position calculation.", self.class, element);
}
}
@end