Skip to content

Commit e9bb99c

Browse files
committed
Move ASButtonNode Yoga logic to Category
1 parent 05479b3 commit e9bb99c

File tree

5 files changed

+177
-110
lines changed

5 files changed

+177
-110
lines changed

AsyncDisplayKit.xcodeproj/project.pbxproj

+12
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@
207207
9C8898BD1C738BB800D6B02E /* ASTextKitFontSizeAdjuster.h in Headers */ = {isa = PBXBuildFile; fileRef = A32FEDD31C501B6A004F642A /* ASTextKitFontSizeAdjuster.h */; settings = {ATTRIBUTES = (Private, ); }; };
208208
9CC606651D24DF9E006581A0 /* NSIndexSet+ASHelpers.mm in Sources */ = {isa = PBXBuildFile; fileRef = CC4981BB1D1C7F65004E13CC /* NSIndexSet+ASHelpers.mm */; };
209209
9CDC18CD1B910E12004965E2 /* ASLayoutElementPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 9CDC18CB1B910E12004965E2 /* ASLayoutElementPrivate.h */; settings = {ATTRIBUTES = (Public, ); }; };
210+
9D302F9B2231B07E005739C3 /* ASButtonNode+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 9D302F9A2231B07E005739C3 /* ASButtonNode+Private.h */; };
211+
9D302F9E2231B373005739C3 /* ASButtonNode+Yoga.h in Headers */ = {isa = PBXBuildFile; fileRef = 9D302F9C2231B373005739C3 /* ASButtonNode+Yoga.h */; };
212+
9D302F9F2231B373005739C3 /* ASButtonNode+Yoga.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9D302F9D2231B373005739C3 /* ASButtonNode+Yoga.mm */; };
210213
9D9AA56921E23EE200172C09 /* ASDisplayNode+LayoutSpec.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9D9AA56721E23EE200172C09 /* ASDisplayNode+LayoutSpec.mm */; };
211214
9D9AA56B21E254B800172C09 /* ASDisplayNode+Yoga.h in Headers */ = {isa = PBXBuildFile; fileRef = 9D9AA56A21E254B800172C09 /* ASDisplayNode+Yoga.h */; };
212215
9D9AA56D21E2568500172C09 /* ASDisplayNode+LayoutSpec.h in Headers */ = {isa = PBXBuildFile; fileRef = 9D9AA56C21E2568500172C09 /* ASDisplayNode+LayoutSpec.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -777,6 +780,9 @@
777780
9CDC18CB1B910E12004965E2 /* ASLayoutElementPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASLayoutElementPrivate.h; sourceTree = "<group>"; };
778781
9CFFC6BF1CCAC73C006A6476 /* ASViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASViewController.mm; sourceTree = "<group>"; };
779782
9CFFC6C11CCAC768006A6476 /* ASTableNode.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASTableNode.mm; sourceTree = "<group>"; };
783+
9D302F9A2231B07E005739C3 /* ASButtonNode+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ASButtonNode+Private.h"; sourceTree = "<group>"; };
784+
9D302F9C2231B373005739C3 /* ASButtonNode+Yoga.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ASButtonNode+Yoga.h"; sourceTree = "<group>"; };
785+
9D302F9D2231B373005739C3 /* ASButtonNode+Yoga.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = "ASButtonNode+Yoga.mm"; sourceTree = "<group>"; };
780786
9D9AA56721E23EE200172C09 /* ASDisplayNode+LayoutSpec.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = "ASDisplayNode+LayoutSpec.mm"; sourceTree = "<group>"; };
781787
9D9AA56A21E254B800172C09 /* ASDisplayNode+Yoga.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ASDisplayNode+Yoga.h"; sourceTree = "<group>"; };
782788
9D9AA56C21E2568500172C09 /* ASDisplayNode+LayoutSpec.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ASDisplayNode+LayoutSpec.h"; sourceTree = "<group>"; };
@@ -1282,6 +1288,9 @@
12821288
8021EC1B1D2B00B100799119 /* UIImage+ASConvenience.mm */,
12831289
CC55A70B1E529FA200594372 /* UIResponder+AsyncDisplayKit.h */,
12841290
CC55A70C1E529FA200594372 /* UIResponder+AsyncDisplayKit.mm */,
1291+
9D302F9A2231B07E005739C3 /* ASButtonNode+Private.h */,
1292+
9D302F9C2231B373005739C3 /* ASButtonNode+Yoga.h */,
1293+
9D302F9D2231B373005739C3 /* ASButtonNode+Yoga.mm */,
12851294
);
12861295
path = Source;
12871296
sourceTree = "<group>";
@@ -1926,6 +1935,7 @@
19261935
CCA282B81E9EA8E40037E8B7 /* AsyncDisplayKit+Tips.h in Headers */,
19271936
B35062571B010F070018CF92 /* ASAssert.h in Headers */,
19281937
CCBBBF5D1EB161760069AA91 /* ASRangeManagingNode.h in Headers */,
1938+
9D302F9B2231B07E005739C3 /* ASButtonNode+Private.h in Headers */,
19291939
B35062581B010F070018CF92 /* ASAvailability.h in Headers */,
19301940
9019FBBF1ED8061D00C45F72 /* ASYogaUtilities.h in Headers */,
19311941
DE84918D1C8FFF2B003D89E9 /* ASRunLoopQueue.h in Headers */,
@@ -1979,6 +1989,7 @@
19791989
34EFC7691B701CE100AD841F /* ASLayoutElement.h in Headers */,
19801990
698DFF471E36B7E9002891F1 /* ASLayoutSpecUtilities.h in Headers */,
19811991
9C70F20D1CDBE9CB007D6C76 /* ASDefaultPlayButton.h in Headers */,
1992+
9D302F9E2231B373005739C3 /* ASButtonNode+Yoga.h in Headers */,
19821993
CCCCCCD51EC3EF060087FE10 /* ASTextDebugOption.h in Headers */,
19831994
CC034A091E60BEB400626263 /* ASDisplayNode+Convenience.h in Headers */,
19841995
254C6B7E1BF94DF4003EC431 /* ASTextKitTailTruncater.h in Headers */,
@@ -2433,6 +2444,7 @@
24332444
CCB1F95A1EFB60A5009C7475 /* ASLog.mm in Sources */,
24342445
767E7F8E1C90191D0066C000 /* AsyncDisplayKit+Debug.mm in Sources */,
24352446
CCEDDDCB200C2AC300FFCD0A /* ASConfigurationInternal.mm in Sources */,
2447+
9D302F9F2231B373005739C3 /* ASButtonNode+Yoga.mm in Sources */,
24362448
CCCCCCD61EC3EF060087FE10 /* ASTextDebugOption.mm in Sources */,
24372449
34EFC75C1B701BD200AD841F /* ASDimension.mm in Sources */,
24382450
B350624E1B010EFD0018CF92 /* ASDisplayNode+AsyncDisplay.mm in Sources */,

Source/ASButtonNode+Private.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// ASButtonNode+Private.h
3+
// AsyncDisplayKit
4+
//
5+
// Created by Michael Schneider on 3/7/19.
6+
// Copyright © 2019 Pinterest. All rights reserved.
7+
//
8+
9+
#import <AsyncDisplayKit/ASButtonNode.h>
10+
#import <AsyncDisplayKit/ASTextNode.h>
11+
#import <AsyncDisplayKit/ASImageNode.h>
12+
#import <AsyncDisplayKit/ASStackLayoutDefines.h>
13+
14+
@interface ASButtonNode() {
15+
NSAttributedString *_normalAttributedTitle;
16+
NSAttributedString *_highlightedAttributedTitle;
17+
NSAttributedString *_selectedAttributedTitle;
18+
NSAttributedString *_selectedHighlightedAttributedTitle;
19+
NSAttributedString *_disabledAttributedTitle;
20+
21+
UIImage *_normalImage;
22+
UIImage *_highlightedImage;
23+
UIImage *_selectedImage;
24+
UIImage *_selectedHighlightedImage;
25+
UIImage *_disabledImage;
26+
27+
UIImage *_normalBackgroundImage;
28+
UIImage *_highlightedBackgroundImage;
29+
UIImage *_selectedBackgroundImage;
30+
UIImage *_selectedHighlightedBackgroundImage;
31+
UIImage *_disabledBackgroundImage;
32+
33+
CGFloat _contentSpacing;
34+
BOOL _laysOutHorizontally;
35+
ASVerticalAlignment _contentVerticalAlignment;
36+
ASHorizontalAlignment _contentHorizontalAlignment;
37+
UIEdgeInsets _contentEdgeInsets;
38+
ASButtonNodeImageAlignment _imageAlignment;
39+
ASTextNode *_titleNode;
40+
ASImageNode *_imageNode;
41+
ASImageNode *_backgroundImageNode;
42+
}
43+
44+
@end

Source/ASButtonNode+Yoga.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// ASButtonNode+Yoga.h
3+
// AsyncDisplayKit
4+
//
5+
// Created by Michael Schneider on 3/7/19.
6+
// Copyright © 2019 Pinterest. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <AsyncDisplayKit/ASButtonNode.h>
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
@interface ASButtonNode (Yoga)
15+
16+
- (void)updateYogaLayoutIfNeeded;
17+
18+
@end
19+
20+
NS_ASSUME_NONNULL_END

Source/ASButtonNode+Yoga.mm

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// ASButtonNode+Yoga.m
3+
// AsyncDisplayKit
4+
//
5+
// Created by Michael Schneider on 3/7/19.
6+
// Copyright © 2019 Pinterest. All rights reserved.
7+
//
8+
9+
#import <AsyncDisplayKit/ASAvailability.h>
10+
#import "ASButtonNode+Yoga.h"
11+
#import <AsyncDisplayKit/ASButtonNode+Private.h>
12+
#import <AsyncDisplayKit/ASDisplayNodeInternal.h>
13+
#import <AsyncDisplayKit/ASStackLayoutSpecUtilities.h>
14+
#import <AsyncDisplayKit/ASThread.h>
15+
16+
#if YOGA
17+
static void ASButtonNodeResolveHorizontalAlignmentForStyle(ASLayoutElementStyle *style, ASStackLayoutDirection _direction, ASHorizontalAlignment _horizontalAlignment, ASStackLayoutJustifyContent _justifyContent, ASStackLayoutAlignItems _alignItems) {
18+
if (_direction == ASStackLayoutDirectionHorizontal) {
19+
style.justifyContent = justifyContent(_horizontalAlignment, _justifyContent);
20+
} else {
21+
style.alignItems = alignment(_horizontalAlignment, _alignItems);
22+
}
23+
}
24+
25+
static void ASButtonNodeResolveVerticalAlignmentForStyle(ASLayoutElementStyle *style, ASStackLayoutDirection _direction, ASVerticalAlignment _verticalAlignment, ASStackLayoutJustifyContent _justifyContent, ASStackLayoutAlignItems _alignItems) {
26+
if (_direction == ASStackLayoutDirectionHorizontal) {
27+
style.alignItems = alignment(_verticalAlignment, _alignItems);
28+
} else {
29+
style.justifyContent = justifyContent(_verticalAlignment, _justifyContent);
30+
}
31+
}
32+
#endif
33+
34+
@implementation ASButtonNode (Yoga)
35+
36+
- (void)updateYogaLayoutIfNeeded
37+
{
38+
#if YOGA
39+
NSMutableArray<ASDisplayNode *> *children = [[NSMutableArray alloc] initWithCapacity:2];
40+
{
41+
ASLockScopeSelf();
42+
43+
// Build up yoga children for button node again
44+
unowned ASLayoutElementStyle *style = [self _locked_style];
45+
[style yogaNodeCreateIfNeeded];
46+
47+
// Setup stack layout values
48+
style.flexDirection = _laysOutHorizontally ? ASStackLayoutDirectionHorizontal : ASStackLayoutDirectionVertical;
49+
50+
// Resolve horizontal and vertical alignment
51+
ASButtonNodeResolveHorizontalAlignmentForStyle(style, style.flexDirection, _contentHorizontalAlignment, style.justifyContent, style.alignItems);
52+
ASButtonNodeResolveVerticalAlignmentForStyle(style, style.flexDirection, _contentVerticalAlignment, style.justifyContent, style.alignItems);
53+
54+
// Setup new yoga children
55+
if (_imageNode.image != nil) {
56+
[_imageNode.style yogaNodeCreateIfNeeded];
57+
[children addObject:_imageNode];
58+
}
59+
60+
if (_titleNode.attributedText.length > 0) {
61+
[_titleNode.style yogaNodeCreateIfNeeded];
62+
if (_imageAlignment == ASButtonNodeImageAlignmentBeginning) {
63+
[children addObject:_titleNode];
64+
} else {
65+
[children insertObject:_titleNode atIndex:0];
66+
}
67+
}
68+
69+
// Add spacing between title and button
70+
if (children.count == 2) {
71+
unowned ASLayoutElementStyle *firstChildStyle = children.firstObject.style;
72+
if (_laysOutHorizontally) {
73+
firstChildStyle.margin = ASEdgeInsetsMake(UIEdgeInsetsMake(0, 0, 0, _contentSpacing));
74+
} else {
75+
firstChildStyle.margin = ASEdgeInsetsMake(UIEdgeInsetsMake(0, 0, _contentSpacing, 0));
76+
}
77+
}
78+
79+
// Add padding to button
80+
if (UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _contentEdgeInsets) == NO) {
81+
style.padding = ASEdgeInsetsMake(_contentEdgeInsets);
82+
}
83+
84+
// Add background node
85+
if (_backgroundImageNode.image) {
86+
[_backgroundImageNode.style yogaNodeCreateIfNeeded];
87+
[children insertObject:_backgroundImageNode atIndex:0];
88+
89+
_backgroundImageNode.style.positionType = YGPositionTypeAbsolute;
90+
_backgroundImageNode.style.position = ASEdgeInsetsMake(UIEdgeInsetsZero);
91+
}
92+
}
93+
94+
// Update new children
95+
[self setYogaChildren:children];
96+
#endif
97+
}
98+
99+
@end

Source/ASButtonNode.mm

+2-110
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,16 @@
77
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
88
//
99

10-
#import <AsyncDisplayKit/ASButtonNode.h>
11-
#import <AsyncDisplayKit/ASAvailability.h>
12-
#import <AsyncDisplayKit/ASDisplayNodeInternal.h>
10+
#import <AsyncDisplayKit/ASButtonNode+Private.h>
11+
#import <AsyncDisplayKit/ASButtonNode+Yoga.h>
1312
#import <AsyncDisplayKit/ASStackLayoutSpec.h>
14-
#import <AsyncDisplayKit/ASStackLayoutSpecUtilities.h>
1513
#import <AsyncDisplayKit/ASThread.h>
1614
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
1715
#import <AsyncDisplayKit/ASBackgroundLayoutSpec.h>
1816
#import <AsyncDisplayKit/ASInsetLayoutSpec.h>
1917
#import <AsyncDisplayKit/ASAbsoluteLayoutSpec.h>
20-
#import <AsyncDisplayKit/ASTextNode.h>
21-
#import <AsyncDisplayKit/ASImageNode.h>
2218
#import <AsyncDisplayKit/ASInternalHelpers.h>
2319

24-
#if YOGA
25-
static void ASButtonNodeResolveHorizontalAlignmentForStyle(ASLayoutElementStyle *style, ASStackLayoutDirection _direction, ASHorizontalAlignment _horizontalAlignment, ASStackLayoutJustifyContent _justifyContent, ASStackLayoutAlignItems _alignItems) {
26-
if (_direction == ASStackLayoutDirectionHorizontal) {
27-
style.justifyContent = justifyContent(_horizontalAlignment, _justifyContent);
28-
} else {
29-
style.alignItems = alignment(_horizontalAlignment, _alignItems);
30-
}
31-
}
32-
33-
static void ASButtonNodeResolveVerticalAlignmentForStyle(ASLayoutElementStyle *style, ASStackLayoutDirection _direction, ASVerticalAlignment _verticalAlignment, ASStackLayoutJustifyContent _justifyContent, ASStackLayoutAlignItems _alignItems) {
34-
if (_direction == ASStackLayoutDirectionHorizontal) {
35-
style.alignItems = alignment(_verticalAlignment, _alignItems);
36-
} else {
37-
style.justifyContent = justifyContent(_verticalAlignment, _justifyContent);
38-
}
39-
}
40-
#endif
41-
42-
@interface ASButtonNode ()
43-
{
44-
NSAttributedString *_normalAttributedTitle;
45-
NSAttributedString *_highlightedAttributedTitle;
46-
NSAttributedString *_selectedAttributedTitle;
47-
NSAttributedString *_selectedHighlightedAttributedTitle;
48-
NSAttributedString *_disabledAttributedTitle;
49-
50-
UIImage *_normalImage;
51-
UIImage *_highlightedImage;
52-
UIImage *_selectedImage;
53-
UIImage *_selectedHighlightedImage;
54-
UIImage *_disabledImage;
55-
56-
UIImage *_normalBackgroundImage;
57-
UIImage *_highlightedBackgroundImage;
58-
UIImage *_selectedBackgroundImage;
59-
UIImage *_selectedHighlightedBackgroundImage;
60-
UIImage *_disabledBackgroundImage;
61-
}
62-
63-
@end
64-
6520
@implementation ASButtonNode
6621

6722
@synthesize contentSpacing = _contentSpacing;
@@ -590,69 +545,6 @@ - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
590545
}
591546
#endif
592547

593-
- (void)updateYogaLayoutIfNeeded
594-
{
595-
#if YOGA
596-
NSMutableArray<ASDisplayNode *> *children = [[NSMutableArray alloc] initWithCapacity:2];
597-
{
598-
ASLockScopeSelf();
599-
600-
// Build up yoga children for button node again
601-
unowned ASLayoutElementStyle *style = [self _locked_style];
602-
[style yogaNodeCreateIfNeeded];
603-
604-
// Setup stack layout values
605-
style.flexDirection = _laysOutHorizontally ? ASStackLayoutDirectionHorizontal : ASStackLayoutDirectionVertical;
606-
607-
// Resolve horizontal and vertical alignment
608-
ASButtonNodeResolveHorizontalAlignmentForStyle(style, style.flexDirection, _contentHorizontalAlignment, style.justifyContent, style.alignItems);
609-
ASButtonNodeResolveVerticalAlignmentForStyle(style, style.flexDirection, _contentVerticalAlignment, style.justifyContent, style.alignItems);
610-
611-
// Setup new yoga children
612-
if (_imageNode.image != nil) {
613-
[_imageNode.style yogaNodeCreateIfNeeded];
614-
[children addObject:_imageNode];
615-
}
616-
617-
if (_titleNode.attributedText.length > 0) {
618-
[_titleNode.style yogaNodeCreateIfNeeded];
619-
if (_imageAlignment == ASButtonNodeImageAlignmentBeginning) {
620-
[children addObject:_titleNode];
621-
} else {
622-
[children insertObject:_titleNode atIndex:0];
623-
}
624-
}
625-
626-
// Add spacing between title and button
627-
if (children.count == 2) {
628-
unowned ASLayoutElementStyle *firstChildStyle = children.firstObject.style;
629-
if (_laysOutHorizontally) {
630-
firstChildStyle.margin = ASEdgeInsetsMake(UIEdgeInsetsMake(0, 0, 0, _contentSpacing));
631-
} else {
632-
firstChildStyle.margin = ASEdgeInsetsMake(UIEdgeInsetsMake(0, 0, _contentSpacing, 0));
633-
}
634-
}
635-
636-
// Add padding to button
637-
if (UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _contentEdgeInsets) == NO) {
638-
style.padding = ASEdgeInsetsMake(_contentEdgeInsets);
639-
}
640-
641-
// Add background node
642-
if (_backgroundImageNode.image) {
643-
[_backgroundImageNode.style yogaNodeCreateIfNeeded];
644-
[children insertObject:_backgroundImageNode atIndex:0];
645-
646-
_backgroundImageNode.style.positionType = YGPositionTypeAbsolute;
647-
_backgroundImageNode.style.position = ASEdgeInsetsMake(UIEdgeInsetsZero);
648-
}
649-
}
650-
651-
// Update new children
652-
[self setYogaChildren:children];
653-
#endif
654-
}
655-
656548
- (void)layout
657549
{
658550
[super layout];

0 commit comments

Comments
 (0)