Add Sizing API that doesn't change view's frame.

Summary: This exposes an API so people can find out the resulting size of a layout calculation without changing the frame of the view.

Reviewed By: emilsjolander

Differential Revision: D4124630

fbshipit-source-id: f2b28d8a5474857cb1c92e021a1f161806826cda
This commit is contained in:
Dustin Shahidehpour
2016-11-03 13:00:02 -07:00
committed by Facebook Github Bot
parent 630ae0972b
commit b938017ccf
3 changed files with 40 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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);
}