Skip to content

Commit

Permalink
Add Yoga support to ASButtonNode (TextureGroup#1381)
Browse files Browse the repository at this point in the history
* Add Yoga support to ASButtonNode

* Drop unowned ASLayoutElementStyle parameter

* Fix access of Yoga properties

* Move ASButtonNode Yoga logic to Category

* Update header
  • Loading branch information
maicki authored and hebertialmeida committed May 10, 2019
1 parent 46e2a81 commit 17f9b00
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 71 deletions.
12 changes: 12 additions & 0 deletions AsyncDisplayKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@
9C8898BD1C738BB800D6B02E /* ASTextKitFontSizeAdjuster.h in Headers */ = {isa = PBXBuildFile; fileRef = A32FEDD31C501B6A004F642A /* ASTextKitFontSizeAdjuster.h */; settings = {ATTRIBUTES = (Private, ); }; };
9CC606651D24DF9E006581A0 /* NSIndexSet+ASHelpers.mm in Sources */ = {isa = PBXBuildFile; fileRef = CC4981BB1D1C7F65004E13CC /* NSIndexSet+ASHelpers.mm */; };
9CDC18CD1B910E12004965E2 /* ASLayoutElementPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 9CDC18CB1B910E12004965E2 /* ASLayoutElementPrivate.h */; settings = {ATTRIBUTES = (Public, ); }; };
9D302F9B2231B07E005739C3 /* ASButtonNode+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 9D302F9A2231B07E005739C3 /* ASButtonNode+Private.h */; };
9D302F9E2231B373005739C3 /* ASButtonNode+Yoga.h in Headers */ = {isa = PBXBuildFile; fileRef = 9D302F9C2231B373005739C3 /* ASButtonNode+Yoga.h */; };
9D302F9F2231B373005739C3 /* ASButtonNode+Yoga.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9D302F9D2231B373005739C3 /* ASButtonNode+Yoga.mm */; };
9D9AA56921E23EE200172C09 /* ASDisplayNode+LayoutSpec.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9D9AA56721E23EE200172C09 /* ASDisplayNode+LayoutSpec.mm */; };
9D9AA56B21E254B800172C09 /* ASDisplayNode+Yoga.h in Headers */ = {isa = PBXBuildFile; fileRef = 9D9AA56A21E254B800172C09 /* ASDisplayNode+Yoga.h */; };
9D9AA56D21E2568500172C09 /* ASDisplayNode+LayoutSpec.h in Headers */ = {isa = PBXBuildFile; fileRef = 9D9AA56C21E2568500172C09 /* ASDisplayNode+LayoutSpec.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -777,6 +780,9 @@
9CDC18CB1B910E12004965E2 /* ASLayoutElementPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASLayoutElementPrivate.h; sourceTree = "<group>"; };
9CFFC6BF1CCAC73C006A6476 /* ASViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASViewController.mm; sourceTree = "<group>"; };
9CFFC6C11CCAC768006A6476 /* ASTableNode.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASTableNode.mm; sourceTree = "<group>"; };
9D302F9A2231B07E005739C3 /* ASButtonNode+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ASButtonNode+Private.h"; sourceTree = "<group>"; };
9D302F9C2231B373005739C3 /* ASButtonNode+Yoga.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ASButtonNode+Yoga.h"; sourceTree = "<group>"; };
9D302F9D2231B373005739C3 /* ASButtonNode+Yoga.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = "ASButtonNode+Yoga.mm"; sourceTree = "<group>"; };
9D9AA56721E23EE200172C09 /* ASDisplayNode+LayoutSpec.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = "ASDisplayNode+LayoutSpec.mm"; sourceTree = "<group>"; };
9D9AA56A21E254B800172C09 /* ASDisplayNode+Yoga.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ASDisplayNode+Yoga.h"; sourceTree = "<group>"; };
9D9AA56C21E2568500172C09 /* ASDisplayNode+LayoutSpec.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ASDisplayNode+LayoutSpec.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1282,6 +1288,9 @@
8021EC1B1D2B00B100799119 /* UIImage+ASConvenience.mm */,
CC55A70B1E529FA200594372 /* UIResponder+AsyncDisplayKit.h */,
CC55A70C1E529FA200594372 /* UIResponder+AsyncDisplayKit.mm */,
9D302F9A2231B07E005739C3 /* ASButtonNode+Private.h */,
9D302F9C2231B373005739C3 /* ASButtonNode+Yoga.h */,
9D302F9D2231B373005739C3 /* ASButtonNode+Yoga.mm */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -1926,6 +1935,7 @@
CCA282B81E9EA8E40037E8B7 /* AsyncDisplayKit+Tips.h in Headers */,
B35062571B010F070018CF92 /* ASAssert.h in Headers */,
CCBBBF5D1EB161760069AA91 /* ASRangeManagingNode.h in Headers */,
9D302F9B2231B07E005739C3 /* ASButtonNode+Private.h in Headers */,
B35062581B010F070018CF92 /* ASAvailability.h in Headers */,
9019FBBF1ED8061D00C45F72 /* ASYogaUtilities.h in Headers */,
DE84918D1C8FFF2B003D89E9 /* ASRunLoopQueue.h in Headers */,
Expand Down Expand Up @@ -1979,6 +1989,7 @@
34EFC7691B701CE100AD841F /* ASLayoutElement.h in Headers */,
698DFF471E36B7E9002891F1 /* ASLayoutSpecUtilities.h in Headers */,
9C70F20D1CDBE9CB007D6C76 /* ASDefaultPlayButton.h in Headers */,
9D302F9E2231B373005739C3 /* ASButtonNode+Yoga.h in Headers */,
CCCCCCD51EC3EF060087FE10 /* ASTextDebugOption.h in Headers */,
CC034A091E60BEB400626263 /* ASDisplayNode+Convenience.h in Headers */,
254C6B7E1BF94DF4003EC431 /* ASTextKitTailTruncater.h in Headers */,
Expand Down Expand Up @@ -2433,6 +2444,7 @@
CCB1F95A1EFB60A5009C7475 /* ASLog.mm in Sources */,
767E7F8E1C90191D0066C000 /* AsyncDisplayKit+Debug.mm in Sources */,
CCEDDDCB200C2AC300FFCD0A /* ASConfigurationInternal.mm in Sources */,
9D302F9F2231B373005739C3 /* ASButtonNode+Yoga.mm in Sources */,
CCCCCCD61EC3EF060087FE10 /* ASTextDebugOption.mm in Sources */,
34EFC75C1B701BD200AD841F /* ASDimension.mm in Sources */,
B350624E1B010EFD0018CF92 /* ASDisplayNode+AsyncDisplay.mm in Sources */,
Expand Down
44 changes: 44 additions & 0 deletions Source/ASButtonNode+Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// ASButtonNode+Private.h
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//

#import <AsyncDisplayKit/ASButtonNode.h>
#import <AsyncDisplayKit/ASTextNode.h>
#import <AsyncDisplayKit/ASImageNode.h>
#import <AsyncDisplayKit/ASStackLayoutDefines.h>

@interface ASButtonNode() {
NSAttributedString *_normalAttributedTitle;
NSAttributedString *_highlightedAttributedTitle;
NSAttributedString *_selectedAttributedTitle;
NSAttributedString *_selectedHighlightedAttributedTitle;
NSAttributedString *_disabledAttributedTitle;

UIImage *_normalImage;
UIImage *_highlightedImage;
UIImage *_selectedImage;
UIImage *_selectedHighlightedImage;
UIImage *_disabledImage;

UIImage *_normalBackgroundImage;
UIImage *_highlightedBackgroundImage;
UIImage *_selectedBackgroundImage;
UIImage *_selectedHighlightedBackgroundImage;
UIImage *_disabledBackgroundImage;

CGFloat _contentSpacing;
BOOL _laysOutHorizontally;
ASVerticalAlignment _contentVerticalAlignment;
ASHorizontalAlignment _contentHorizontalAlignment;
UIEdgeInsets _contentEdgeInsets;
ASButtonNodeImageAlignment _imageAlignment;
ASTextNode *_titleNode;
ASImageNode *_imageNode;
ASImageNode *_backgroundImageNode;
}

@end
20 changes: 20 additions & 0 deletions Source/ASButtonNode+Yoga.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ASButtonNode+Yoga.h
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//

#import <Foundation/Foundation.h>
#import <AsyncDisplayKit/ASButtonNode.h>

NS_ASSUME_NONNULL_BEGIN

@interface ASButtonNode (Yoga)

- (void)updateYogaLayoutIfNeeded;

@end

NS_ASSUME_NONNULL_END
106 changes: 106 additions & 0 deletions Source/ASButtonNode+Yoga.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
Loading

0 comments on commit 17f9b00

Please sign in to comment.