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:
committed by
Facebook Github Bot
parent
630ae0972b
commit
b938017ccf
@@ -58,4 +58,24 @@
|
|||||||
XCTAssertFalse([view css_usesFlexbox]);
|
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
|
@end
|
||||||
|
@@ -45,7 +45,10 @@
|
|||||||
// Get the resolved direction of this node. This won't be CSSDirectionInherit
|
// Get the resolved direction of this node. This won't be CSSDirectionInherit
|
||||||
- (CSSDirection)css_resolvedDirection;
|
- (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;
|
- (void)css_applyLayout;
|
||||||
|
|
||||||
|
//! @abstract Compute the size of a layout with a constrained size.
|
||||||
|
- (CGSize)css_sizeThatFits:(CGSize)constrainedSize;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@@ -180,20 +180,29 @@
|
|||||||
return CSSNodeLayoutGetDirection([self cssNode]);
|
return CSSNodeLayoutGetDirection([self cssNode]);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)css_applyLayout
|
- (CGSize)css_sizeThatFits:(CGSize)constrainedSize
|
||||||
{
|
{
|
||||||
NSAssert([NSThread isMainThread], @"Method called using a thread other than main!");
|
NSAssert([NSThread isMainThread], @"CSS Layout calculation must be done on main.");
|
||||||
NSAssert([self css_usesFlexbox], @"css_applyLayout must be called on a node using css styling!");
|
NSAssert([self css_usesFlexbox], @"CSS Layout is not enabled for this view.");
|
||||||
|
|
||||||
_attachNodesRecursive(self);
|
_attachNodesRecursive(self);
|
||||||
|
|
||||||
CSSNodeRef node = [self cssNode];
|
const CSSNodeRef node = [self cssNode];
|
||||||
CSSNodeCalculateLayout(
|
CSSNodeCalculateLayout(
|
||||||
[self cssNode],
|
node,
|
||||||
CSSNodeStyleGetWidth(node),
|
constrainedSize.width,
|
||||||
CSSNodeStyleGetHeight(node),
|
constrainedSize.height,
|
||||||
CSSNodeStyleGetDirection(node));
|
CSSNodeStyleGetDirection(node));
|
||||||
|
|
||||||
|
return (CGSize) {
|
||||||
|
.width = CSSNodeLayoutGetWidth(node),
|
||||||
|
.height = CSSNodeLayoutGetHeight(node),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)css_applyLayout
|
||||||
|
{
|
||||||
|
[self css_sizeThatFits:self.bounds.size];
|
||||||
_updateFrameRecursive(self);
|
_updateFrameRecursive(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user