Skip to content

Commit ca8fcdf

Browse files
authored
[ASScrollNode] Add .scrollDirection property so that internal content sizing can be easily "unlimited" in the direction of scrolling. (facebookarchive#3001)
1 parent ada553c commit ca8fcdf

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

AsyncDisplayKit/ASScrollNode.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//
1010

1111
#import <AsyncDisplayKit/ASDisplayNode.h>
12+
#import <AsyncDisplayKit/ASScrollDirection.h>
1213

1314
NS_ASSUME_NONNULL_BEGIN
1415

@@ -33,6 +34,18 @@ NS_ASSUME_NONNULL_BEGIN
3334
*/
3435
@property (nonatomic, assign) BOOL automaticallyManagesContentSize;
3536

37+
/**
38+
* @abstract This property controls how the constrainedSize is interpreted when sizing the content.
39+
* if you are using automaticallyManagesContentSize, it can be crucial to ensure that the sizing is
40+
* done how you expect.
41+
* Vertical: The constrainedSize is interpreted as having unbounded .height (CGFLOAT_MAX), allowing
42+
* stacks and other content in the layout spec to expand and result in scrollable content.
43+
* Horizontal: The constrainedSize is interpreted as having unbounded .width (CGFLOAT_MAX), ...
44+
* Vertical & Horizontal: the constrainedSize is interpreted as unbounded in both directions.
45+
* @default ASScrollDirectionVerticalDirections
46+
*/
47+
@property (nonatomic, assign) ASScrollDirection scrollableDirections;
48+
3649
@end
3750

3851
NS_ASSUME_NONNULL_END

AsyncDisplayKit/ASScrollNode.mm

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ - (void)didMoveToWindow
5656

5757
@implementation ASScrollNode
5858
{
59+
ASScrollDirection _scrollableDirections;
5960
BOOL _automaticallyManagesContentSize;
6061
CGSize _contentCalculatedSizeFromLayout;
6162
}
@@ -70,12 +71,20 @@ - (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
7071
restrictedToSize:(ASLayoutElementSize)size
7172
relativeToParentSize:(CGSize)parentSize
7273
{
73-
ASLayout *layout = [super calculateLayoutThatFits:constrainedSize
74+
ASDN::MutexLocker l(__instanceLock__); // Lock for using our instance variables.
75+
76+
ASSizeRange contentConstrainedSize = constrainedSize;
77+
if (ASScrollDirectionContainsVerticalDirection(_scrollableDirections)) {
78+
contentConstrainedSize.max.height = CGFLOAT_MAX;
79+
}
80+
if (ASScrollDirectionContainsHorizontalDirection(_scrollableDirections)) {
81+
contentConstrainedSize.max.width = CGFLOAT_MAX;
82+
}
83+
84+
ASLayout *layout = [super calculateLayoutThatFits:contentConstrainedSize
7485
restrictedToSize:size
7586
relativeToParentSize:parentSize];
76-
77-
ASDN::MutexLocker l(__instanceLock__); // Lock for using our two instance variables.
78-
87+
7988
if (_automaticallyManagesContentSize) {
8089
// To understand this code, imagine we're containing a horizontal stack set within a vertical table node.
8190
// Our parentSize is fixed ~375pt width, but 0 - INF height. Our stack measures 1000pt width, 50pt height.
@@ -124,6 +133,25 @@ - (void)setAutomaticallyManagesContentSize:(BOOL)automaticallyManagesContentSize
124133
{
125134
ASDN::MutexLocker l(__instanceLock__);
126135
_automaticallyManagesContentSize = automaticallyManagesContentSize;
136+
if (_automaticallyManagesContentSize == YES
137+
&& ASScrollDirectionContainsVerticalDirection(_scrollableDirections) == NO
138+
&& ASScrollDirectionContainsHorizontalDirection(_scrollableDirections) == NO) {
139+
// Set the @default value, for more user-friendly behavior of the most
140+
// common use cases of .automaticallyManagesContentSize.
141+
_scrollableDirections = ASScrollDirectionVerticalDirections;
142+
}
143+
}
144+
145+
- (ASScrollDirection)scrollableDirections
146+
{
147+
ASDN::MutexLocker l(__instanceLock__);
148+
return _scrollableDirections;
149+
}
150+
151+
- (void)setScrollableDirections:(ASScrollDirection)scrollableDirections
152+
{
153+
ASDN::MutexLocker l(__instanceLock__);
154+
_scrollableDirections = scrollableDirections;
127155
}
128156

129157
@end

0 commit comments

Comments
 (0)