Don't call sizeThatFits: if measure modes are exact.

Summary: `sizeThatFits:` can be expensive, this optimizes our measuring function so that we do not call it if we know that we are going to use the exact height and width that were passed-in.

Reviewed By: rnystrom

Differential Revision: D4023715

fbshipit-source-id: dc02703b50bafd168ffab62ed98a7f6342100cc9
This commit is contained in:
Dustin Shahidehpour
2016-10-14 14:49:42 -07:00
committed by Facebook Github Bot
parent 05ba3a2565
commit 0254e3e97f

View File

@@ -11,25 +11,6 @@
#import <objc/runtime.h>
static CSSSize _measure(
void *context,
float width,
CSSMeasureMode widthMode,
float height,
CSSMeasureMode heightMode)
{
UIView *view = (__bridge UIView*) context;
CGSize sizeThatFits = [view sizeThatFits:(CGSize) {
.width = widthMode == CSSMeasureModeUndefined ? CGFLOAT_MAX : width,
.height = heightMode == CSSMeasureModeUndefined ? CGFLOAT_MAX : height,
}];
return (CSSSize) {
.width = widthMode == CSSMeasureModeExactly ? width : sizeThatFits.width,
.height = heightMode == CSSMeasureModeExactly ? height : sizeThatFits.height,
};
}
static void _attachNodesRecursive(UIView *view);
static void _updateFrameRecursive(UIView *view);
@@ -223,6 +204,37 @@ static void _updateFrameRecursive(UIView *view);
_updateFrameRecursive(self);
}
#pragma mark - Private
static CSSSize _measure(
void *context,
float width,
CSSMeasureMode widthMode,
float height,
CSSMeasureMode heightMode)
{
const BOOL useExactWidth = (widthMode == CSSMeasureModeExactly);
const BOOL useExactHeight = (heightMode == CSSMeasureModeExactly);
if (useExactHeight && useExactWidth) {
return (CSSSize) {
.width = width,
.height = height,
};
}
UIView *view = (__bridge UIView*) context;
const CGSize sizeThatFits = [view sizeThatFits:(CGSize) {
.width = widthMode == CSSMeasureModeUndefined ? CGFLOAT_MAX : width,
.height = heightMode == CSSMeasureModeUndefined ? CGFLOAT_MAX : height,
}];
return (CSSSize) {
.width = useExactWidth ? width : sizeThatFits.width,
.height = useExactHeight ? height : sizeThatFits.height,
};
}
@end
static void _attachNodesRecursive(UIView *view) {