Skip to content

Commit

Permalink
Setting min=max dimension is treated as setting dimension
Browse files Browse the repository at this point in the history
Summary: If a node has minDimension and maxDimension set at the same value, yoga will treat it as having a set dimension, doing less calculations.

Reviewed By: emilsjolander

Differential Revision: D4492395

fbshipit-source-id: 3f4293548399e006aa808b9586d24e77c7df1f21
  • Loading branch information
Antonio Corrado authored and facebook-github-bot committed Feb 6, 2017
1 parent 52f3471 commit 46817a3
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 93 deletions.
5 changes: 4 additions & 1 deletion java/jni/YGJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ void jni_YGNodeRemoveChild(alias_ref<jobject>, jlong nativePointer, jlong childP
YGNodeRemoveChild(_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer));
}

void jni_YGNodeCalculateLayout(alias_ref<jobject>, jlong nativePointer, jfloat width, jfloat height) {
void jni_YGNodeCalculateLayout(alias_ref<jobject>,
jlong nativePointer,
jfloat width,
jfloat height) {
const YGNodeRef root = _jlong2YGNodeRef(nativePointer);
YGNodeCalculateLayout(root,
static_cast<float>(width),
Expand Down
112 changes: 112 additions & 0 deletions tests/YGMeasureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,118 @@ TEST(YogaTest, measure_absolute_child_with_no_constraints) {
YGNodeFreeRecursive(root);
}

TEST(YogaTest, dont_measure_when_min_equals_max) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);

int measureCount = 0;

const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
YGNodeStyleSetMinWidth(root_child0, 10);
YGNodeStyleSetMaxWidth(root_child0, 10);
YGNodeStyleSetMinHeight(root_child0, 10);
YGNodeStyleSetMaxHeight(root_child0, 10);
YGNodeInsertChild(root, root_child0, 0);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

ASSERT_EQ(0, measureCount);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(10, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(10, YGNodeLayoutGetHeight(root_child0));

YGNodeFreeRecursive(root);
}

TEST(YogaTest, dont_measure_when_min_equals_max_percentages) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);

int measureCount = 0;

const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
YGNodeStyleSetMinWidthPercent(root_child0, 10);
YGNodeStyleSetMaxWidthPercent(root_child0, 10);
YGNodeStyleSetMinHeightPercent(root_child0, 10);
YGNodeStyleSetMaxHeightPercent(root_child0, 10);
YGNodeInsertChild(root, root_child0, 0);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

ASSERT_EQ(0, measureCount);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(10, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(10, YGNodeLayoutGetHeight(root_child0));

YGNodeFreeRecursive(root);
}

TEST(YogaTest, dont_measure_when_min_equals_max_mixed) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);

int measureCount = 0;

const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
YGNodeStyleSetMinWidthPercent(root_child0, 10);
YGNodeStyleSetMaxWidthPercent(root_child0, 10);
YGNodeStyleSetMinHeight(root_child0, 10);
YGNodeStyleSetMaxHeight(root_child0, 10);
YGNodeInsertChild(root, root_child0, 0);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

ASSERT_EQ(0, measureCount);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(10, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(10, YGNodeLayoutGetHeight(root_child0));

YGNodeFreeRecursive(root);
}

TEST(YogaTest, dont_measure_when_min_equals_max_mixed_2) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);

int measureCount = 0;

const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
YGNodeStyleSetMinWidth(root_child0, 10);
YGNodeStyleSetMaxWidth(root_child0, 10);
YGNodeStyleSetMinHeightPercent(root_child0, 10);
YGNodeStyleSetMaxHeightPercent(root_child0, 10);
YGNodeInsertChild(root, root_child0, 0);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

ASSERT_EQ(0, measureCount);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(10, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(10, YGNodeLayoutGetHeight(root_child0));

YGNodeFreeRecursive(root);
}

#if GTEST_HAS_DEATH_TEST
TEST(YogaTest, cannot_add_child_to_node_with_measure_func) {
const YGNodeRef root = YGNodeNew();
Expand Down
111 changes: 45 additions & 66 deletions yoga/YGEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,21 @@ YG_EXTERN_C_BEGIN

#define YGAlignCount 6
typedef YG_ENUM_BEGIN(YGAlign) {
YGAlignAuto,
YGAlignFlexStart,
YGAlignCenter,
YGAlignFlexEnd,
YGAlignStretch,
YGAlignBaseline,
} YG_ENUM_END(YGAlign);
YGAlignAuto, YGAlignFlexStart, YGAlignCenter, YGAlignFlexEnd, YGAlignStretch, YGAlignBaseline,
}
YG_ENUM_END(YGAlign);

#define YGDimensionCount 2
typedef YG_ENUM_BEGIN(YGDimension) {
YGDimensionWidth,
YGDimensionHeight,
} YG_ENUM_END(YGDimension);
YGDimensionWidth, YGDimensionHeight,
}
YG_ENUM_END(YGDimension);

#define YGDirectionCount 3
typedef YG_ENUM_BEGIN(YGDirection) {
YGDirectionInherit,
YGDirectionLTR,
YGDirectionRTL,
} YG_ENUM_END(YGDirection);
YGDirectionInherit, YGDirectionLTR, YGDirectionRTL,
}
YG_ENUM_END(YGDirection);

#define YGDisplayCount 2
typedef YG_ENUM_BEGIN(YGDisplay) {
Expand All @@ -44,87 +39,71 @@ typedef YG_ENUM_BEGIN(YGDisplay) {

#define YGEdgeCount 9
typedef YG_ENUM_BEGIN(YGEdge) {
YGEdgeLeft,
YGEdgeTop,
YGEdgeRight,
YGEdgeBottom,
YGEdgeStart,
YGEdgeEnd,
YGEdgeHorizontal,
YGEdgeVertical,
YGEdgeAll,
} YG_ENUM_END(YGEdge);
YGEdgeLeft, YGEdgeTop, YGEdgeRight, YGEdgeBottom, YGEdgeStart, YGEdgeEnd, YGEdgeHorizontal,
YGEdgeVertical, YGEdgeAll,
}
YG_ENUM_END(YGEdge);

#define YGExperimentalFeatureCount 2
typedef YG_ENUM_BEGIN(YGExperimentalFeature) {
YGExperimentalFeatureRounding,
YGExperimentalFeatureWebFlexBasis,
} YG_ENUM_END(YGExperimentalFeature);
YGExperimentalFeatureRounding, YGExperimentalFeatureWebFlexBasis,
}
YG_ENUM_END(YGExperimentalFeature);

#define YGFlexDirectionCount 4
typedef YG_ENUM_BEGIN(YGFlexDirection) {
YGFlexDirectionColumn,
YGFlexDirectionColumnReverse,
YGFlexDirectionRow,
YGFlexDirectionRowReverse,
} YG_ENUM_END(YGFlexDirection);
YGFlexDirectionColumn, YGFlexDirectionColumnReverse, YGFlexDirectionRow,
YGFlexDirectionRowReverse,
}
YG_ENUM_END(YGFlexDirection);

#define YGJustifyCount 5
typedef YG_ENUM_BEGIN(YGJustify) {
YGJustifyFlexStart,
YGJustifyCenter,
YGJustifyFlexEnd,
YGJustifySpaceBetween,
YGJustifySpaceAround,
} YG_ENUM_END(YGJustify);
YGJustifyFlexStart, YGJustifyCenter, YGJustifyFlexEnd, YGJustifySpaceBetween,
YGJustifySpaceAround,
}
YG_ENUM_END(YGJustify);

#define YGLogLevelCount 5
typedef YG_ENUM_BEGIN(YGLogLevel) {
YGLogLevelError,
YGLogLevelWarn,
YGLogLevelInfo,
YGLogLevelDebug,
YGLogLevelVerbose,
} YG_ENUM_END(YGLogLevel);
YGLogLevelError, YGLogLevelWarn, YGLogLevelInfo, YGLogLevelDebug, YGLogLevelVerbose,
}
YG_ENUM_END(YGLogLevel);

#define YGMeasureModeCount 3
typedef YG_ENUM_BEGIN(YGMeasureMode) {
YGMeasureModeUndefined,
YGMeasureModeExactly,
YGMeasureModeAtMost,
} YG_ENUM_END(YGMeasureMode);
YGMeasureModeUndefined, YGMeasureModeExactly, YGMeasureModeAtMost,
}
YG_ENUM_END(YGMeasureMode);

#define YGOverflowCount 3
typedef YG_ENUM_BEGIN(YGOverflow) {
YGOverflowVisible,
YGOverflowHidden,
YGOverflowScroll,
} YG_ENUM_END(YGOverflow);
YGOverflowVisible, YGOverflowHidden, YGOverflowScroll,
}
YG_ENUM_END(YGOverflow);

#define YGPositionTypeCount 2
typedef YG_ENUM_BEGIN(YGPositionType) {
YGPositionTypeRelative,
YGPositionTypeAbsolute,
} YG_ENUM_END(YGPositionType);
YGPositionTypeRelative, YGPositionTypeAbsolute,
}
YG_ENUM_END(YGPositionType);

#define YGPrintOptionsCount 3
typedef YG_ENUM_BEGIN(YGPrintOptions) {
YGPrintOptionsLayout = 1,
YGPrintOptionsStyle = 2,
YGPrintOptionsChildren = 4,
} YG_ENUM_END(YGPrintOptions);
YGPrintOptionsLayout = 1, YGPrintOptionsStyle = 2, YGPrintOptionsChildren = 4,
}
YG_ENUM_END(YGPrintOptions);

#define YGUnitCount 3
typedef YG_ENUM_BEGIN(YGUnit) {
YGUnitUndefined,
YGUnitPixel,
YGUnitPercent,
} YG_ENUM_END(YGUnit);
YGUnitUndefined, YGUnitPixel, YGUnitPercent,
}
YG_ENUM_END(YGUnit);

#define YGWrapCount 2
typedef YG_ENUM_BEGIN(YGWrap) {
YGWrapNoWrap,
YGWrapWrap,
} YG_ENUM_END(YGWrap);
YGWrapNoWrap, YGWrapWrap,
}
YG_ENUM_END(YGWrap);

YG_EXTERN_C_END
Loading

0 comments on commit 46817a3

Please sign in to comment.