Add preferredSize as getter to ASLayoutElementStyle (#2427)

This commit is contained in:
Michael Schneider
2016-10-19 12:55:19 -07:00
committed by Adlai Holler
parent 67489492ee
commit 4b914f8e15
3 changed files with 34 additions and 3 deletions

View File

@@ -251,9 +251,10 @@ extern NSString * const ASLayoutElementStyleLayoutPositionProperty;
* for nodes that either have no intrinsic content size or * for nodes that either have no intrinsic content size or
* should be laid out at a different size than its intrinsic content size. For example, this property could be * should be laid out at a different size than its intrinsic content size. For example, this property could be
* set on an ASImageNode to display at a size different from the underlying image size. * set on an ASImageNode to display at a size different from the underlying image size.
*
* @warning Calling the getter when the size's width or height are relative will cause an assert.
*/ */
@property (nonatomic, assign) CGSize preferredSize; @property (nonatomic, assign) CGSize preferredSize;
- (CGSize)preferredSize UNAVAILABLE_ATTRIBUTE;
/** /**
* @abstract An optional property that provides a minimum size bound for a layout element. If provided, this restriction will * @abstract An optional property that provides a minimum size bound for a layout element. If provided, this restriction will

View File

@@ -254,6 +254,22 @@ do {\
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleHeightProperty); ASLayoutElementStyleCallDelegate(ASLayoutElementStyleHeightProperty);
} }
- (CGSize)preferredSize
{
ASDN::MutexLocker l(__instanceLock__);
if (_size.width.unit != ASDimensionUnitPoints) {
NSCAssert(NO, @"Cannot get preferredSize of element with fractional width. Width: %@.", NSStringFromASDimension(_size.width));
return CGSizeZero;
}
if (_size.height.unit != ASDimensionUnitPoints) {
NSCAssert(NO, @"Cannot get preferredSize of element with fractional height. Height: %@.", NSStringFromASDimension(_size.height));
return CGSizeZero;
}
return CGSizeMake(_size.width.value, _size.height.value);
}
- (void)setMinSize:(CGSize)minSize - (void)setMinSize:(CGSize)minSize
{ {
ASDN::MutexLocker l(__instanceLock__); ASDN::MutexLocker l(__instanceLock__);

View File

@@ -8,7 +8,7 @@
// of patent rights can be found in the PATENTS file in the same directory. // of patent rights can be found in the PATENTS file in the same directory.
// //
#import <XCTest/XCTest.h> #import "ASXCTExtensions.h"
#import "ASLayoutElement.h" #import "ASLayoutElement.h"
#pragma mark - ASLayoutElementStyleTestsDelegate #pragma mark - ASLayoutElementStyleTestsDelegate
@@ -61,6 +61,7 @@
CGSize size = CGSizeMake(100, 100); CGSize size = CGSizeMake(100, 100);
style.preferredSize = size; style.preferredSize = size;
ASXCTAssertEqualSizes(style.preferredSize, size);
XCTAssertTrue(ASDimensionEqualToDimension(style.width, ASDimensionMakeWithPoints(size.width))); XCTAssertTrue(ASDimensionEqualToDimension(style.width, ASDimensionMakeWithPoints(size.width)));
XCTAssertTrue(ASDimensionEqualToDimension(style.height, ASDimensionMakeWithPoints(size.height))); XCTAssertTrue(ASDimensionEqualToDimension(style.height, ASDimensionMakeWithPoints(size.height)));
@@ -73,6 +74,19 @@
XCTAssertTrue(ASDimensionEqualToDimension(style.maxHeight, ASDimensionMakeWithPoints(size.height))); XCTAssertTrue(ASDimensionEqualToDimension(style.maxHeight, ASDimensionMakeWithPoints(size.height)));
} }
- (void)testReadingInvalidSizeForPreferredSize
{
ASLayoutElementStyle *style = [ASLayoutElementStyle new];
XCTAssertThrows(style.preferredSize);
style.width = ASDimensionMake(ASDimensionUnitFraction, 0.5);
XCTAssertThrows(style.preferredSize);
style.preferredSize = CGSizeMake(100, 100);
XCTAssertNoThrow(style.preferredSize);
}
- (void)testSettingSizeViaLayoutSize - (void)testSettingSizeViaLayoutSize
{ {
ASLayoutElementStyle *style = [ASLayoutElementStyle new]; ASLayoutElementStyle *style = [ASLayoutElementStyle new];