forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Yoga support to ASButtonNode (TextureGroup#1381)
* 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
1 parent
46e2a81
commit 17f9b00
Showing
5 changed files
with
245 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.