forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASYogaUtilities.mm
249 lines (218 loc) · 8.66 KB
/
ASYogaUtilities.mm
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// ASYogaUtilities.mm
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <AsyncDisplayKit/ASYogaUtilities.h>
#import <AsyncDisplayKit/ASLayoutElementStylePrivate.h>
#if YOGA /* YOGA */
@implementation ASDisplayNode (YogaHelpers)
+ (ASDisplayNode *)yogaNode
{
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;
[node.style yogaNodeCreateIfNeeded];
return node;
}
+ (ASDisplayNode *)yogaSpacerNode
{
ASDisplayNode *node = [ASDisplayNode yogaNode];
node.style.flexGrow = 1.0f;
return node;
}
+ (ASDisplayNode *)yogaVerticalStack
{
ASDisplayNode *node = [self yogaNode];
node.style.flexDirection = ASStackLayoutDirectionVertical;
return node;
}
+ (ASDisplayNode *)yogaHorizontalStack
{
ASDisplayNode *node = [self yogaNode];
node.style.flexDirection = ASStackLayoutDirectionHorizontal;
return node;
}
@end
void ASDisplayNodePerformBlockOnEveryYogaChild(ASDisplayNode *node, void(^block)(ASDisplayNode *node))
{
if (node == nil) {
return;
}
block(node);
// We use the accessor here despite the copy, because the block may modify the yoga tree e.g.
// replacing a node.
for (ASDisplayNode *child in node.yogaChildren) {
ASDisplayNodePerformBlockOnEveryYogaChild(child, block);
}
}
#pragma mark - Yoga Type Conversion Helpers
YGAlign yogaAlignItems(ASStackLayoutAlignItems alignItems)
{
switch (alignItems) {
case ASStackLayoutAlignItemsNotSet: return YGAlignAuto;
case ASStackLayoutAlignItemsStart: return YGAlignFlexStart;
case ASStackLayoutAlignItemsEnd: return YGAlignFlexEnd;
case ASStackLayoutAlignItemsCenter: return YGAlignCenter;
case ASStackLayoutAlignItemsStretch: return YGAlignStretch;
case ASStackLayoutAlignItemsBaselineFirst: return YGAlignBaseline;
// FIXME: WARNING, Yoga does not currently support last-baseline item alignment.
case ASStackLayoutAlignItemsBaselineLast: return YGAlignBaseline;
}
}
YGJustify yogaJustifyContent(ASStackLayoutJustifyContent justifyContent)
{
switch (justifyContent) {
case ASStackLayoutJustifyContentStart: return YGJustifyFlexStart;
case ASStackLayoutJustifyContentCenter: return YGJustifyCenter;
case ASStackLayoutJustifyContentEnd: return YGJustifyFlexEnd;
case ASStackLayoutJustifyContentSpaceBetween: return YGJustifySpaceBetween;
case ASStackLayoutJustifyContentSpaceAround: return YGJustifySpaceAround;
}
}
YGAlign yogaAlignSelf(ASStackLayoutAlignSelf alignSelf)
{
switch (alignSelf) {
case ASStackLayoutAlignSelfStart: return YGAlignFlexStart;
case ASStackLayoutAlignSelfCenter: return YGAlignCenter;
case ASStackLayoutAlignSelfEnd: return YGAlignFlexEnd;
case ASStackLayoutAlignSelfStretch: return YGAlignStretch;
case ASStackLayoutAlignSelfAuto: return YGAlignAuto;
}
}
YGFlexDirection yogaFlexDirection(ASStackLayoutDirection direction)
{
switch (direction) {
case ASStackLayoutDirectionVertical:
return YGFlexDirectionColumn;
case ASStackLayoutDirectionVerticalReverse:
return YGFlexDirectionColumnReverse;
case ASStackLayoutDirectionHorizontal:
return YGFlexDirectionRow;
case ASStackLayoutDirectionHorizontalReverse:
return YGFlexDirectionRowReverse;
}
}
float yogaFloatForCGFloat(CGFloat value)
{
if (value < CGFLOAT_MAX / 2) {
return value;
} else {
return YGUndefined;
}
}
CGFloat cgFloatForYogaFloat(float yogaFloat, CGFloat undefinedDefault)
{
return YGFloatIsUndefined(yogaFloat) ? undefinedDefault : yogaFloat;
}
float yogaDimensionToPoints(ASDimension dimension)
{
ASDisplayNodeCAssert(dimension.unit == ASDimensionUnitPoints,
@"Dimensions should not be type Fraction for this method: %f", dimension.value);
return yogaFloatForCGFloat(dimension.value);
}
float yogaDimensionToPercent(ASDimension dimension)
{
ASDisplayNodeCAssert(dimension.unit == ASDimensionUnitFraction,
@"Dimensions should not be type Points for this method: %f", dimension.value);
return 100.0 * yogaFloatForCGFloat(dimension.value);
}
ASDimension dimensionForEdgeWithEdgeInsets(YGEdge edge, ASEdgeInsets insets)
{
switch (edge) {
case YGEdgeLeft: return insets.left;
case YGEdgeTop: return insets.top;
case YGEdgeRight: return insets.right;
case YGEdgeBottom: return insets.bottom;
case YGEdgeStart: return insets.start;
case YGEdgeEnd: return insets.end;
case YGEdgeHorizontal: return insets.horizontal;
case YGEdgeVertical: return insets.vertical;
case YGEdgeAll: return insets.all;
default: ASDisplayNodeCAssert(NO, @"YGEdge other than ASEdgeInsets is not supported.");
return ASDimensionAuto;
}
}
void ASLayoutElementYogaUpdateMeasureFunc(YGNodeRef yogaNode, id <ASLayoutElement> layoutElement)
{
if (yogaNode == NULL) {
return;
}
BOOL shouldHaveMeasureFunc = [layoutElement implementsLayoutMethod];
// How expensive is it to set a baselineFunc on all (leaf) nodes?
BOOL shouldHaveBaselineFunc = YES;
if (layoutElement != nil) {
if (shouldHaveMeasureFunc || shouldHaveBaselineFunc) {
// Retain the Context object. This must be explicitly released with a
// __bridge_transfer - YGNodeFree() is not sufficient.
YGNodeSetContext(yogaNode, (__bridge_retained void *)layoutElement);
}
if (shouldHaveMeasureFunc) {
YGNodeSetMeasureFunc(yogaNode, &ASLayoutElementYogaMeasureFunc);
}
if (shouldHaveBaselineFunc) {
YGNodeSetBaselineFunc(yogaNode, &ASLayoutElementYogaBaselineFunc);
}
ASDisplayNodeCAssert(YGNodeGetContext(yogaNode) == (__bridge void *)layoutElement,
@"Yoga node context should contain layoutElement: %@", layoutElement);
} else {
// If we lack any of the conditions above, and currently have a measureFn/baselineFn/context,
// get rid of it.
// Release the __bridge_retained Context object.
__unused id<ASLayoutElement> element = (__bridge_transfer id)YGNodeGetContext(yogaNode);
YGNodeSetContext(yogaNode, NULL);
YGNodeSetMeasureFunc(yogaNode, NULL);
YGNodeSetBaselineFunc(yogaNode, NULL);
}
}
float ASLayoutElementYogaBaselineFunc(YGNodeRef yogaNode, const float width, const float height)
{
id<ASLayoutElement> layoutElement = (__bridge id<ASLayoutElement>)YGNodeGetContext(yogaNode);
ASDisplayNodeCAssert([layoutElement conformsToProtocol:@protocol(ASLayoutElement)],
@"Yoga context must be <ASLayoutElement>");
ASDisplayNode *displayNode = ASDynamicCast(layoutElement, ASDisplayNode);
switch (displayNode.style.parentAlignStyle) {
case ASStackLayoutAlignItemsBaselineFirst:
return layoutElement.style.ascender;
case ASStackLayoutAlignItemsBaselineLast:
return height + layoutElement.style.descender;
default:
return 0;
}
}
YGSize ASLayoutElementYogaMeasureFunc(YGNodeRef yogaNode, float width, YGMeasureMode widthMode,
float height, YGMeasureMode heightMode)
{
id <ASLayoutElement> layoutElement = (__bridge id <ASLayoutElement>)YGNodeGetContext(yogaNode);
ASDisplayNodeCAssert([layoutElement conformsToProtocol:@protocol(ASLayoutElement)], @"Yoga context must be <ASLayoutElement>");
width = cgFloatForYogaFloat(width, CGFLOAT_MAX);
height = cgFloatForYogaFloat(height, CGFLOAT_MAX);
ASSizeRange sizeRange;
sizeRange.min = CGSizeZero;
sizeRange.max = CGSizeMake(width, height);
if (widthMode == YGMeasureModeExactly) {
sizeRange.min.width = sizeRange.max.width;
} else {
// Mode is (YGMeasureModeAtMost | YGMeasureModeUndefined)
ASDimension minWidth = layoutElement.style.minWidth;
sizeRange.min.width = (minWidth.unit == ASDimensionUnitPoints ? yogaDimensionToPoints(minWidth) : 0.0);
}
if (heightMode == YGMeasureModeExactly) {
sizeRange.min.height = sizeRange.max.height;
} else {
// Mode is (YGMeasureModeAtMost | YGMeasureModeUndefined)
ASDimension minHeight = layoutElement.style.minHeight;
sizeRange.min.height = (minHeight.unit == ASDimensionUnitPoints ? yogaDimensionToPoints(minHeight) : 0.0);
}
ASDisplayNodeCAssert(isnan(sizeRange.min.width) == NO && isnan(sizeRange.min.height) == NO, @"Yoga size range for measurement should not have NaN in minimum");
if (isnan(sizeRange.max.width)) {
sizeRange.max.width = CGFLOAT_MAX;
}
if (isnan(sizeRange.max.height)) {
sizeRange.max.height = CGFLOAT_MAX;
}
CGSize size = [[layoutElement layoutThatFits:sizeRange] size];
return (YGSize){ .width = (float)size.width, .height = (float)size.height };
}
#endif /* YOGA */