forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASButtonNode+Yoga.mm
106 lines (85 loc) · 3.7 KB
/
ASButtonNode+Yoga.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
//
// ASButtonNode+Yoga.mm
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <AsyncDisplayKit/ASAvailability.h>
#import "ASButtonNode+Yoga.h"
#import <AsyncDisplayKit/ASButtonNode+Private.h>
#import <AsyncDisplayKit/ASDisplayNodeInternal.h>
#import <AsyncDisplayKit/ASStackLayoutSpecUtilities.h>
#import <AsyncDisplayKit/ASThread.h>
#if YOGA
static void ASButtonNodeResolveHorizontalAlignmentForStyle(ASLayoutElementStyle *style, ASStackLayoutDirection _direction, ASHorizontalAlignment _horizontalAlignment, ASStackLayoutJustifyContent _justifyContent, ASStackLayoutAlignItems _alignItems) {
if (_direction == ASStackLayoutDirectionHorizontal) {
style.justifyContent = justifyContent(_horizontalAlignment, _justifyContent);
} else {
style.alignItems = alignment(_horizontalAlignment, _alignItems);
}
}
static void ASButtonNodeResolveVerticalAlignmentForStyle(ASLayoutElementStyle *style, ASStackLayoutDirection _direction, ASVerticalAlignment _verticalAlignment, ASStackLayoutJustifyContent _justifyContent, ASStackLayoutAlignItems _alignItems) {
if (_direction == ASStackLayoutDirectionHorizontal) {
style.alignItems = alignment(_verticalAlignment, _alignItems);
} else {
style.justifyContent = justifyContent(_verticalAlignment, _justifyContent);
}
}
@implementation ASButtonNode (Yoga)
- (void)updateYogaLayoutIfNeeded
{
NSMutableArray<ASDisplayNode *> *children = [[NSMutableArray alloc] initWithCapacity:2];
{
ASLockScopeSelf();
// Build up yoga children for button node again
unowned ASLayoutElementStyle *style = [self _locked_style];
[style yogaNodeCreateIfNeeded];
// Setup stack layout values
style.flexDirection = _laysOutHorizontally ? ASStackLayoutDirectionHorizontal : ASStackLayoutDirectionVertical;
// Resolve horizontal and vertical alignment
ASButtonNodeResolveHorizontalAlignmentForStyle(style, style.flexDirection, _contentHorizontalAlignment, style.justifyContent, style.alignItems);
ASButtonNodeResolveVerticalAlignmentForStyle(style, style.flexDirection, _contentVerticalAlignment, style.justifyContent, style.alignItems);
// Setup new yoga children
if (_imageNode.image != nil) {
[_imageNode.style yogaNodeCreateIfNeeded];
[children addObject:_imageNode];
}
if (_titleNode.attributedText.length > 0) {
[_titleNode.style yogaNodeCreateIfNeeded];
if (_imageAlignment == ASButtonNodeImageAlignmentBeginning) {
[children addObject:_titleNode];
} else {
[children insertObject:_titleNode atIndex:0];
}
}
// Add spacing between title and button
if (children.count == 2) {
unowned ASLayoutElementStyle *firstChildStyle = children.firstObject.style;
if (_laysOutHorizontally) {
firstChildStyle.margin = ASEdgeInsetsMake(UIEdgeInsetsMake(0, 0, 0, _contentSpacing));
} else {
firstChildStyle.margin = ASEdgeInsetsMake(UIEdgeInsetsMake(0, 0, _contentSpacing, 0));
}
}
// Add padding to button
if (UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _contentEdgeInsets) == NO) {
style.padding = ASEdgeInsetsMake(_contentEdgeInsets);
}
// Add background node
if (_backgroundImageNode.image) {
[_backgroundImageNode.style yogaNodeCreateIfNeeded];
[children insertObject:_backgroundImageNode atIndex:0];
_backgroundImageNode.style.positionType = YGPositionTypeAbsolute;
_backgroundImageNode.style.position = ASEdgeInsetsMake(UIEdgeInsetsZero);
}
}
// Update new children
[self setYogaChildren:children];
}
@end
#else
@implementation ASButtonNode (Yoga)
- (void)updateYogaLayoutIfNeeded {}
@end
#endif