diff --git a/uikit/CSSLayout/Tests/CSSLayoutTests.m b/uikit/CSSLayout/Tests/CSSLayoutTests.m index cd1e775a..1dfc5339 100644 --- a/uikit/CSSLayout/Tests/CSSLayoutTests.m +++ b/uikit/CSSLayout/Tests/CSSLayoutTests.m @@ -58,4 +58,24 @@ XCTAssertFalse([view css_usesFlexbox]); } +- (void)testSizeThatFitsAsserts +{ + UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; + XCTAssertThrows([view css_sizeThatFits:CGSizeZero]); + + dispatch_sync(dispatch_queue_create("com.facebook.CSSLayout.testing", DISPATCH_QUEUE_SERIAL), ^(void){ + XCTAssertThrows([view css_sizeThatFits:CGSizeZero]); + }); +} + +- (void)testSizeThatFitsSmoke +{ + UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; + [view css_setUsesFlexbox:YES]; + + const CGSize constrainedSize = CGSizeMake(50, 50); + const CGSize actualSize = [view css_sizeThatFits:constrainedSize]; + XCTAssertTrue(CGSizeEqualToSize(constrainedSize, actualSize), @"Actual Size: %@", NSStringFromCGSize(actualSize)); +} + @end diff --git a/uikit/CSSLayout/UIView+CSSLayout.h b/uikit/CSSLayout/UIView+CSSLayout.h index e17948b5..17bd97ca 100644 --- a/uikit/CSSLayout/UIView+CSSLayout.h +++ b/uikit/CSSLayout/UIView+CSSLayout.h @@ -45,7 +45,10 @@ // Get the resolved direction of this node. This won't be CSSDirectionInherit - (CSSDirection)css_resolvedDirection; -// Perform a layout calculation and update the frames of the views in the hierarchy with th results +//! @abstract Perform a layout calculation and update the frames of the views in the hierarchy with th results - (void)css_applyLayout; +//! @abstract Compute the size of a layout with a constrained size. +- (CGSize)css_sizeThatFits:(CGSize)constrainedSize; + @end diff --git a/uikit/CSSLayout/UIView+CSSLayout.m b/uikit/CSSLayout/UIView+CSSLayout.m index 9a8eb645..1bc408d2 100644 --- a/uikit/CSSLayout/UIView+CSSLayout.m +++ b/uikit/CSSLayout/UIView+CSSLayout.m @@ -180,20 +180,29 @@ return CSSNodeLayoutGetDirection([self cssNode]); } -- (void)css_applyLayout +- (CGSize)css_sizeThatFits:(CGSize)constrainedSize { - NSAssert([NSThread isMainThread], @"Method called using a thread other than main!"); - NSAssert([self css_usesFlexbox], @"css_applyLayout must be called on a node using css styling!"); + NSAssert([NSThread isMainThread], @"CSS Layout calculation must be done on main."); + NSAssert([self css_usesFlexbox], @"CSS Layout is not enabled for this view."); _attachNodesRecursive(self); - CSSNodeRef node = [self cssNode]; + const CSSNodeRef node = [self cssNode]; CSSNodeCalculateLayout( - [self cssNode], - CSSNodeStyleGetWidth(node), - CSSNodeStyleGetHeight(node), + node, + constrainedSize.width, + constrainedSize.height, CSSNodeStyleGetDirection(node)); + return (CGSize) { + .width = CSSNodeLayoutGetWidth(node), + .height = CSSNodeLayoutGetHeight(node), + }; +} + +- (void)css_applyLayout +{ + [self css_sizeThatFits:self.bounds.size]; _updateFrameRecursive(self); }