Make YGNode as c++ struct with properties exposed through accessors

Summary: Moved c implementation of `YGNode` to C++ struct. Not moving to C++ class as the React Classes dependent on `Yoga.h` assume it to be C. Thats why keeping `Yoga.h` C compatible. Sorry for the long diff, didn't thought that it will turn out to be this much big.Will keep an eye on number of lines next time 😉

Reviewed By: emilsjolander

Differential Revision: D6592257

fbshipit-source-id: 641e8b9462ad00731a094511f9f5608b23a6bb21
This commit is contained in:
Pritesh Nandgaonkar
2017-12-19 11:18:00 -08:00
committed by Facebook Github Bot
parent dbf6a12134
commit fbd332dee8
16 changed files with 1957 additions and 1091 deletions

1
BUCK
View File

@@ -14,7 +14,6 @@ GMOCK_OVERRIDE_FLAGS = [
COMPILER_FLAGS = LIBRARY_COMPILER_FLAGS + [ COMPILER_FLAGS = LIBRARY_COMPILER_FLAGS + [
"-std=c++1y", "-std=c++1y",
"-Wno-global-constructors",
] ]
TEST_COMPILER_FLAGS = BASE_COMPILER_FLAGS + GMOCK_OVERRIDE_FLAGS + [ TEST_COMPILER_FLAGS = BASE_COMPILER_FLAGS + GMOCK_OVERRIDE_FLAGS + [

View File

@@ -8,8 +8,9 @@
*/ */
#include <fb/fbjni.h> #include <fb/fbjni.h>
#include <iostream> #include <yoga/YGNode.h>
#include <yoga/Yoga.h> #include <yoga/Yoga.h>
#include <iostream>
using namespace facebook::jni; using namespace facebook::jni;
using namespace std; using namespace std;
@@ -19,7 +20,7 @@ struct JYogaNode : public JavaClass<JYogaNode> {
}; };
static inline weak_ref<JYogaNode> *YGNodeJobject(YGNodeRef node) { static inline weak_ref<JYogaNode> *YGNodeJobject(YGNodeRef node) {
return reinterpret_cast<weak_ref<JYogaNode> *>(YGNodeGetContext(node)); return reinterpret_cast<weak_ref<JYogaNode>*>(node->getContext());
} }
static void YGTransferLayoutDirection(YGNodeRef node, alias_ref<jobject> javaNode) { static void YGTransferLayoutDirection(YGNodeRef node, alias_ref<jobject> javaNode) {
@@ -28,7 +29,7 @@ static void YGTransferLayoutDirection(YGNodeRef node, alias_ref<jobject> javaNod
} }
static void YGTransferLayoutOutputsRecursive(YGNodeRef root) { static void YGTransferLayoutOutputsRecursive(YGNodeRef root) {
if (YGNodeGetHasNewLayout(root)) { if (root->getHasNewLayout()) {
if (auto obj = YGNodeJobject(root)->lockLocal()) { if (auto obj = YGNodeJobject(root)->lockLocal()) {
static auto widthField = obj->getClass()->getField<jfloat>("mWidth"); static auto widthField = obj->getClass()->getField<jfloat>("mWidth");
static auto heightField = obj->getClass()->getField<jfloat>("mHeight"); static auto heightField = obj->getClass()->getField<jfloat>("mHeight");
@@ -88,7 +89,7 @@ static void YGTransferLayoutOutputsRecursive(YGNodeRef root) {
obj->setFieldValue<jboolean>(hasNewLayoutField, true); obj->setFieldValue<jboolean>(hasNewLayoutField, true);
YGTransferLayoutDirection(root, obj); YGTransferLayoutDirection(root, obj);
YGNodeSetHasNewLayout(root, false); root->setHasNewLayout(false);
for (uint32_t i = 0; i < YGNodeGetChildCount(root); i++) { for (uint32_t i = 0; i < YGNodeGetChildCount(root); i++) {
YGTransferLayoutOutputsRecursive(YGNodeGetChild(root, i)); YGTransferLayoutOutputsRecursive(YGNodeGetChild(root, i));
@@ -188,15 +189,17 @@ static inline YGConfigRef _jlong2YGConfigRef(jlong addr) {
jlong jni_YGNodeNew(alias_ref<jobject> thiz) { jlong jni_YGNodeNew(alias_ref<jobject> thiz) {
const YGNodeRef node = YGNodeNew(); const YGNodeRef node = YGNodeNew();
YGNodeSetContext(node, new weak_ref<jobject>(make_weak(thiz))); node->setContext(new weak_ref<jobject>(make_weak(thiz)));
YGNodeSetPrintFunc(node, YGPrint); // YGNodeSetContext(node, new weak_ref<jobject>(make_weak(thiz)));
node->setPrintFunc(YGPrint);
// YGNodeSetPrintFunc(node, YGPrint);
return reinterpret_cast<jlong>(node); return reinterpret_cast<jlong>(node);
} }
jlong jni_YGNodeNewWithConfig(alias_ref<jobject> thiz, jlong configPointer) { jlong jni_YGNodeNewWithConfig(alias_ref<jobject> thiz, jlong configPointer) {
const YGNodeRef node = YGNodeNewWithConfig(_jlong2YGConfigRef(configPointer)); const YGNodeRef node = YGNodeNewWithConfig(_jlong2YGConfigRef(configPointer));
YGNodeSetContext(node, new weak_ref<jobject>(make_weak(thiz))); node->setContext(new weak_ref<jobject>(make_weak(thiz)));
YGNodeSetPrintFunc(node, YGPrint); node->setPrintFunc(YGPrint);
return reinterpret_cast<jlong>(node); return reinterpret_cast<jlong>(node);
} }
@@ -208,10 +211,10 @@ void jni_YGNodeFree(alias_ref<jobject> thiz, jlong nativePointer) {
void jni_YGNodeReset(alias_ref<jobject> thiz, jlong nativePointer) { void jni_YGNodeReset(alias_ref<jobject> thiz, jlong nativePointer) {
const YGNodeRef node = _jlong2YGNodeRef(nativePointer); const YGNodeRef node = _jlong2YGNodeRef(nativePointer);
void *context = YGNodeGetContext(node); void* context = node->getContext();
YGNodeReset(node); YGNodeReset(node);
YGNodeSetContext(node, context); node->setContext(context);
YGNodeSetPrintFunc(node, YGPrint); node->setPrintFunc(YGPrint);
} }
void jni_YGNodePrint(alias_ref<jobject> thiz, jlong nativePointer) { void jni_YGNodePrint(alias_ref<jobject> thiz, jlong nativePointer) {
@@ -246,18 +249,19 @@ void jni_YGNodeMarkDirty(alias_ref<jobject>, jlong nativePointer) {
} }
jboolean jni_YGNodeIsDirty(alias_ref<jobject>, jlong nativePointer) { jboolean jni_YGNodeIsDirty(alias_ref<jobject>, jlong nativePointer) {
return (jboolean) YGNodeIsDirty(_jlong2YGNodeRef(nativePointer)); return (jboolean)_jlong2YGNodeRef(nativePointer)->isDirty();
} }
void jni_YGNodeSetHasMeasureFunc(alias_ref<jobject>, jlong nativePointer, jboolean hasMeasureFunc) { void jni_YGNodeSetHasMeasureFunc(alias_ref<jobject>, jlong nativePointer, jboolean hasMeasureFunc) {
YGNodeSetMeasureFunc(_jlong2YGNodeRef(nativePointer), hasMeasureFunc ? YGJNIMeasureFunc : NULL); _jlong2YGNodeRef(nativePointer)
->setMeasureFunc(hasMeasureFunc ? YGJNIMeasureFunc : nullptr);
} }
void jni_YGNodeSetHasBaselineFunc(alias_ref<jobject>, void jni_YGNodeSetHasBaselineFunc(alias_ref<jobject>,
jlong nativePointer, jlong nativePointer,
jboolean hasBaselineFunc) { jboolean hasBaselineFunc) {
YGNodeSetBaselineFunc(_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(nativePointer)
hasBaselineFunc ? YGJNIBaselineFunc : NULL); ->setBaseLineFunc(hasBaselineFunc ? YGJNIBaselineFunc : nullptr);
} }
void jni_YGNodeCopyStyle(alias_ref<jobject>, jlong dstNativePointer, jlong srcNativePointer) { void jni_YGNodeCopyStyle(alias_ref<jobject>, jlong dstNativePointer, jlong srcNativePointer) {

View File

@@ -8,6 +8,7 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <yoga/Yoga.h> #include <yoga/Yoga.h>
static YGSize _measure(YGNodeRef node, static YGSize _measure(YGNodeRef node,
@@ -449,7 +450,7 @@ TEST(YogaTest, aspect_ratio_with_measure_func) {
YGNodeStyleSetHeight(root, 100); YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeStyleSetAspectRatio(root_child0, 1); YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);

View File

@@ -8,10 +8,11 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <yoga/Yoga.h> #include <yoga/Yoga.h>
static float _baseline(YGNodeRef node, const float width, const float height) { static float _baseline(YGNodeRef node, const float width, const float height) {
float *baseline = (float *) YGNodeGetContext(node); float* baseline = (float*)node->getContext();
return *baseline; return *baseline;
} }
@@ -34,9 +35,9 @@ TEST(YogaTest, align_baseline_customer_func) {
float baselineValue = 10; float baselineValue = 10;
const YGNodeRef root_child1_child0 = YGNodeNew(); const YGNodeRef root_child1_child0 = YGNodeNew();
YGNodeSetContext(root_child1_child0, &baselineValue); root_child1_child0->setContext(&baselineValue);
YGNodeStyleSetWidth(root_child1_child0, 50); YGNodeStyleSetWidth(root_child1_child0, 50);
YGNodeSetBaselineFunc(root_child1_child0, _baseline); root_child1_child0->setBaseLineFunc(_baseline);
YGNodeStyleSetHeight(root_child1_child0, 20); YGNodeStyleSetHeight(root_child1_child0, 20);
YGNodeInsertChild(root_child1, root_child1_child0, 0); YGNodeInsertChild(root_child1, root_child1_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

View File

@@ -8,7 +8,7 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <yoga/Yoga.h> #include <yoga/YGNode.h>
TEST(YogaTest, dirty_propagation) { TEST(YogaTest, dirty_propagation) {
const YGNodeRef root = YGNodeNew(); const YGNodeRef root = YGNodeNew();
@@ -30,15 +30,15 @@ TEST(YogaTest, dirty_propagation) {
YGNodeStyleSetWidth(root_child0, 20); YGNodeStyleSetWidth(root_child0, 20);
EXPECT_TRUE(YGNodeIsDirty(root_child0)); EXPECT_TRUE(root_child0->isDirty());
EXPECT_FALSE(YGNodeIsDirty(root_child1)); EXPECT_FALSE(root_child1->isDirty());
EXPECT_TRUE(YGNodeIsDirty(root)); EXPECT_TRUE(root->isDirty());
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
EXPECT_FALSE(YGNodeIsDirty(root_child0)); EXPECT_FALSE(root_child0->isDirty());
EXPECT_FALSE(YGNodeIsDirty(root_child1)); EXPECT_FALSE(root_child1->isDirty());
EXPECT_FALSE(YGNodeIsDirty(root)); EXPECT_FALSE(root->isDirty());
YGNodeFreeRecursive(root); YGNodeFreeRecursive(root);
} }
@@ -63,9 +63,9 @@ TEST(YogaTest, dirty_propagation_only_if_prop_changed) {
YGNodeStyleSetWidth(root_child0, 50); YGNodeStyleSetWidth(root_child0, 50);
EXPECT_FALSE(YGNodeIsDirty(root_child0)); EXPECT_FALSE(root_child0->isDirty());
EXPECT_FALSE(YGNodeIsDirty(root_child1)); EXPECT_FALSE(root_child1->isDirty());
EXPECT_FALSE(YGNodeIsDirty(root)); EXPECT_FALSE(root->isDirty());
YGNodeFreeRecursive(root); YGNodeFreeRecursive(root);
} }
@@ -133,11 +133,11 @@ TEST(YogaTest, dirty_node_only_if_children_are_actually_removed) {
const YGNodeRef child1 = YGNodeNew(); const YGNodeRef child1 = YGNodeNew();
YGNodeRemoveChild(root, child1); YGNodeRemoveChild(root, child1);
EXPECT_FALSE(YGNodeIsDirty(root)); EXPECT_FALSE(root->isDirty());
YGNodeFree(child1); YGNodeFree(child1);
YGNodeRemoveChild(root, child0); YGNodeRemoveChild(root, child0);
EXPECT_TRUE(YGNodeIsDirty(root)); EXPECT_TRUE(root->isDirty());
YGNodeFree(child0); YGNodeFree(child0);
YGNodeFreeRecursive(root); YGNodeFreeRecursive(root);
@@ -151,11 +151,11 @@ TEST(YogaTest, dirty_node_only_if_undefined_values_gets_set_to_undefined) {
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
EXPECT_FALSE(YGNodeIsDirty(root)); EXPECT_FALSE(root->isDirty());
YGNodeStyleSetMinWidth(root, YGUndefined); YGNodeStyleSetMinWidth(root, YGUndefined);
EXPECT_FALSE(YGNodeIsDirty(root)); EXPECT_FALSE(root->isDirty());
YGNodeFreeRecursive(root); YGNodeFreeRecursive(root);
} }

View File

@@ -8,6 +8,7 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <yoga/Yoga.h> #include <yoga/Yoga.h>
static YGSize _measureMax(YGNodeRef node, static YGSize _measureMax(YGNodeRef node,
@@ -15,7 +16,7 @@ static YGSize _measureMax(YGNodeRef node,
YGMeasureMode widthMode, YGMeasureMode widthMode,
float height, float height,
YGMeasureMode heightMode) { YGMeasureMode heightMode) {
int *measureCount = (int *) YGNodeGetContext(node); int* measureCount = (int*)node->getContext();
(*measureCount)++; (*measureCount)++;
return YGSize{ return YGSize{
@@ -29,7 +30,7 @@ static YGSize _measureMin(YGNodeRef node,
YGMeasureMode widthMode, YGMeasureMode widthMode,
float height, float height,
YGMeasureMode heightMode) { YGMeasureMode heightMode) {
int *measureCount = (int *) YGNodeGetContext(node); int* measureCount = (int*)node->getContext();
*measureCount = *measureCount + 1; *measureCount = *measureCount + 1;
return YGSize{ return YGSize{
.width = .width =
@@ -48,7 +49,7 @@ static YGSize _measure_84_49(YGNodeRef node,
YGMeasureMode widthMode, YGMeasureMode widthMode,
float height, float height,
YGMeasureMode heightMode) { YGMeasureMode heightMode) {
int *measureCount = (int *) YGNodeGetContext(node); int* measureCount = (int*)node->getContext();
if (measureCount) { if (measureCount) {
(*measureCount)++; (*measureCount)++;
} }
@@ -67,8 +68,8 @@ TEST(YogaTest, measure_once_single_flexible_child) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
int measureCount = 0; int measureCount = 0;
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measureMax); root_child0->setMeasureFunc(_measureMax);
YGNodeStyleSetFlexGrow(root_child0, 1); YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -84,8 +85,8 @@ TEST(YogaTest, remeasure_with_same_exact_width_larger_than_needed_height) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
int measureCount = 0; int measureCount = 0;
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measureMin); root_child0->setMeasureFunc(_measureMin);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR); YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
@@ -102,8 +103,8 @@ TEST(YogaTest, remeasure_with_same_atmost_width_larger_than_needed_height) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
int measureCount = 0; int measureCount = 0;
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measureMin); root_child0->setMeasureFunc(_measureMin);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR); YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
@@ -120,8 +121,8 @@ TEST(YogaTest, remeasure_with_computed_width_larger_than_needed_height) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
int measureCount = 0; int measureCount = 0;
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measureMin); root_child0->setMeasureFunc(_measureMin);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR); YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
@@ -139,8 +140,8 @@ TEST(YogaTest, remeasure_with_atmost_computed_width_undefined_height) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
int measureCount = 0; int measureCount = 0;
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measureMin); root_child0->setMeasureFunc(_measureMin);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, 100, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, 100, YGUndefined, YGDirectionLTR);
@@ -165,8 +166,8 @@ TEST(YogaTest, remeasure_with_already_measured_value_smaller_but_still_float_equ
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child0_child0 = YGNodeNew(); const YGNodeRef root_child0_child0 = YGNodeNew();
YGNodeSetContext(root_child0_child0, &measureCount); root_child0_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0_child0, _measure_84_49); root_child0_child0->setMeasureFunc(_measure_84_49);
YGNodeInsertChild(root_child0, root_child0_child0, 0); YGNodeInsertChild(root_child0, root_child0_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

View File

@@ -8,6 +8,7 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <yoga/Yoga.h> #include <yoga/Yoga.h>
struct _MeasureConstraint { struct _MeasureConstraint {
@@ -28,7 +29,7 @@ static YGSize _measure(YGNodeRef node,
float height, float height,
YGMeasureMode heightMode) { YGMeasureMode heightMode) {
struct _MeasureConstraintList* constraintList = struct _MeasureConstraintList* constraintList =
(struct _MeasureConstraintList *) YGNodeGetContext(node); (struct _MeasureConstraintList*)node->getContext();
struct _MeasureConstraint *constraints = constraintList->constraints; struct _MeasureConstraint *constraints = constraintList->constraints;
uint32_t currentIndex = constraintList->length; uint32_t currentIndex = constraintList->length;
(&constraints[currentIndex])->width = width; (&constraints[currentIndex])->width = width;
@@ -54,8 +55,10 @@ TEST(YogaTest, exactly_measure_stretched_child_column) {
YGNodeStyleSetHeight(root, 100); YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &constraintList); // root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setContext(&constraintList);
root_child0->setMeasureFunc(_measure);
// root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -81,8 +84,9 @@ TEST(YogaTest, exactly_measure_stretched_child_row) {
YGNodeStyleSetHeight(root, 100); YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &constraintList); // root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setContext(&constraintList);
root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -107,8 +111,8 @@ TEST(YogaTest, at_most_main_axis_column) {
YGNodeStyleSetHeight(root, 100); YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &constraintList); root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -134,8 +138,8 @@ TEST(YogaTest, at_most_cross_axis_column) {
YGNodeStyleSetHeight(root, 100); YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &constraintList); root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -161,8 +165,8 @@ TEST(YogaTest, at_most_main_axis_row) {
YGNodeStyleSetHeight(root, 100); YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &constraintList); root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -189,8 +193,8 @@ TEST(YogaTest, at_most_cross_axis_row) {
YGNodeStyleSetHeight(root, 100); YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &constraintList); root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -215,8 +219,8 @@ TEST(YogaTest, flex_child) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetFlexGrow(root_child0, 1); YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeSetContext(root_child0, &constraintList); root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -245,8 +249,8 @@ TEST(YogaTest, flex_child_with_flex_basis) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetFlexGrow(root_child0, 1); YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetFlexBasis(root_child0, 0); YGNodeStyleSetFlexBasis(root_child0, 0);
YGNodeSetContext(root_child0, &constraintList); root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -273,8 +277,8 @@ TEST(YogaTest, overflow_scroll_column) {
YGNodeStyleSetWidth(root, 100); YGNodeStyleSetWidth(root, 100);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &constraintList); root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -305,8 +309,8 @@ TEST(YogaTest, overflow_scroll_row) {
YGNodeStyleSetWidth(root, 100); YGNodeStyleSetWidth(root, 100);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &constraintList); root_child0->setContext(&constraintList);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

View File

@@ -8,6 +8,7 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <yoga/Yoga.h> #include <yoga/Yoga.h>
static YGSize _measure(YGNodeRef node, static YGSize _measure(YGNodeRef node,
@@ -15,7 +16,7 @@ static YGSize _measure(YGNodeRef node,
YGMeasureMode widthMode, YGMeasureMode widthMode,
float height, float height,
YGMeasureMode heightMode) { YGMeasureMode heightMode) {
int *measureCount = (int *) YGNodeGetContext(node); int* measureCount = (int*)node->getContext();
if (measureCount) { if (measureCount) {
(*measureCount)++; (*measureCount)++;
} }
@@ -60,8 +61,8 @@ TEST(YogaTest, dont_measure_single_grow_shrink_child) {
int measureCount = 0; int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeStyleSetFlexGrow(root_child0, 1); YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetFlexShrink(root_child0, 1); YGNodeStyleSetFlexShrink(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -83,8 +84,8 @@ TEST(YogaTest, measure_absolute_child_with_no_constraints) {
const YGNodeRef root_child0_child0 = YGNodeNew(); const YGNodeRef root_child0_child0 = YGNodeNew();
YGNodeStyleSetPositionType(root_child0_child0, YGPositionTypeAbsolute); YGNodeStyleSetPositionType(root_child0_child0, YGPositionTypeAbsolute);
YGNodeSetContext(root_child0_child0, &measureCount); root_child0_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0_child0, _measure); root_child0_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root_child0, root_child0_child0, 0); YGNodeInsertChild(root_child0, root_child0_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -103,8 +104,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max) {
int measureCount = 0; int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMinWidth(root_child0, 10); YGNodeStyleSetMinWidth(root_child0, 10);
YGNodeStyleSetMaxWidth(root_child0, 10); YGNodeStyleSetMaxWidth(root_child0, 10);
YGNodeStyleSetMinHeight(root_child0, 10); YGNodeStyleSetMinHeight(root_child0, 10);
@@ -131,8 +132,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max_percentages) {
int measureCount = 0; int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMinWidthPercent(root_child0, 10); YGNodeStyleSetMinWidthPercent(root_child0, 10);
YGNodeStyleSetMaxWidthPercent(root_child0, 10); YGNodeStyleSetMaxWidthPercent(root_child0, 10);
YGNodeStyleSetMinHeightPercent(root_child0, 10); YGNodeStyleSetMinHeightPercent(root_child0, 10);
@@ -157,7 +158,7 @@ TEST(YogaTest, measure_nodes_with_margin_auto_and_stretch) {
YGNodeStyleSetHeight(root, 500); YGNodeStyleSetHeight(root, 500);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMarginAuto(root_child0, YGEdgeLeft); YGNodeStyleSetMarginAuto(root_child0, YGEdgeLeft);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -180,8 +181,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max_mixed_width_percent) {
int measureCount = 0; int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMinWidthPercent(root_child0, 10); YGNodeStyleSetMinWidthPercent(root_child0, 10);
YGNodeStyleSetMaxWidthPercent(root_child0, 10); YGNodeStyleSetMaxWidthPercent(root_child0, 10);
YGNodeStyleSetMinHeight(root_child0, 10); YGNodeStyleSetMinHeight(root_child0, 10);
@@ -208,8 +209,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max_mixed_height_percent) {
int measureCount = 0; int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMinWidth(root_child0, 10); YGNodeStyleSetMinWidth(root_child0, 10);
YGNodeStyleSetMaxWidth(root_child0, 10); YGNodeStyleSetMaxWidth(root_child0, 10);
YGNodeStyleSetMinHeightPercent(root_child0, 10); YGNodeStyleSetMinHeightPercent(root_child0, 10);
@@ -233,7 +234,7 @@ TEST(YogaTest, measure_enough_size_should_be_in_single_line) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetAlignSelf(root_child0, YGAlignFlexStart); YGNodeStyleSetAlignSelf(root_child0, YGAlignFlexStart);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -251,8 +252,8 @@ TEST(YogaTest, measure_not_enough_size_should_wrap) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetAlignSelf(root_child0, YGAlignFlexStart); YGNodeStyleSetAlignSelf(root_child0, YGAlignFlexStart);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); // YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -274,8 +275,8 @@ TEST(YogaTest, measure_zero_space_should_grow) {
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionColumn); YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionColumn);
YGNodeStyleSetPadding(root_child0, YGEdgeAll, 100); YGNodeStyleSetPadding(root_child0, YGEdgeAll, 100);
YGNodeSetContext(root_child0, &measureCount); root_child0->setContext(&measureCount);
YGNodeSetMeasureFunc(root_child0, _measure); root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -300,7 +301,8 @@ TEST(YogaTest, measure_flex_direction_row_and_padding) {
YGNodeStyleSetHeight(root, 50); YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); root_child0->setMeasureFunc(_simulate_wrapping_text);
// YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config); const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
@@ -339,7 +341,8 @@ TEST(YogaTest, measure_flex_direction_column_and_padding) {
YGNodeStyleSetHeight(root, 50); YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); root_child0->setMeasureFunc(_simulate_wrapping_text);
// YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config); const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
@@ -378,7 +381,8 @@ TEST(YogaTest, measure_flex_direction_row_no_padding) {
YGNodeStyleSetHeight(root, 50); YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); // YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config); const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
@@ -418,7 +422,7 @@ TEST(YogaTest, measure_flex_direction_row_no_padding_align_items_flexstart) {
YGNodeStyleSetAlignItems(root, YGAlignFlexStart); YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config); const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
@@ -457,7 +461,7 @@ TEST(YogaTest, measure_with_fixed_size) {
YGNodeStyleSetHeight(root, 50); YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeStyleSetWidth(root_child0, 10); YGNodeStyleSetWidth(root_child0, 10);
YGNodeStyleSetHeight(root_child0, 10); YGNodeStyleSetHeight(root_child0, 10);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -498,7 +502,7 @@ TEST(YogaTest, measure_with_flex_shrink) {
YGNodeStyleSetHeight(root, 50); YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeStyleSetFlexShrink(root_child0, 1); YGNodeStyleSetFlexShrink(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -537,7 +541,7 @@ TEST(YogaTest, measure_no_padding) {
YGNodeStyleSetHeight(root, 50); YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeStyleSetFlexShrink(root_child0, 1); YGNodeStyleSetFlexShrink(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -570,7 +574,7 @@ TEST(YogaTest, measure_no_padding) {
#if GTEST_HAS_DEATH_TEST #if GTEST_HAS_DEATH_TEST
TEST(YogaDeathTest, cannot_add_child_to_node_with_measure_func) { TEST(YogaDeathTest, cannot_add_child_to_node_with_measure_func) {
const YGNodeRef root = YGNodeNew(); const YGNodeRef root = YGNodeNew();
YGNodeSetMeasureFunc(root, _measure); root->setMeasureFunc(_measure);
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
ASSERT_DEATH(YGNodeInsertChild(root, root_child0, 0), "Cannot add child.*"); ASSERT_DEATH(YGNodeInsertChild(root, root_child0, 0), "Cannot add child.*");
@@ -582,8 +586,7 @@ TEST(YogaDeathTest, cannot_add_nonnull_measure_func_to_non_leaf_node) {
const YGNodeRef root = YGNodeNew(); const YGNodeRef root = YGNodeNew();
const YGNodeRef root_child0 = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew();
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
ASSERT_DEATH(root->setMeasureFunc(_measure), "Cannot set measure function.*");
ASSERT_DEATH(YGNodeSetMeasureFunc(root, _measure), "Cannot set measure function.*");
YGNodeFreeRecursive(root); YGNodeFreeRecursive(root);
} }
@@ -592,9 +595,8 @@ TEST(YogaDeathTest, cannot_add_nonnull_measure_func_to_non_leaf_node) {
TEST(YogaTest, can_nullify_measure_func_on_any_node) { TEST(YogaTest, can_nullify_measure_func_on_any_node) {
const YGNodeRef root = YGNodeNew(); const YGNodeRef root = YGNodeNew();
YGNodeInsertChild(root, YGNodeNew(), 0); YGNodeInsertChild(root, YGNodeNew(), 0);
root->setMeasureFunc(nullptr);
YGNodeSetMeasureFunc(root, NULL); ASSERT_TRUE(root->getMeasure() == NULL);
ASSERT_TRUE(YGNodeGetMeasureFunc(root) == NULL);
YGNodeFreeRecursive(root); YGNodeFreeRecursive(root);
} }
@@ -607,7 +609,7 @@ TEST(YogaTest, cant_call_negative_measure) {
YGNodeStyleSetHeight(root, 10); YGNodeStyleSetHeight(root, 10);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _measure_assert_negative); root_child0->setMeasureFunc(_measure_assert_negative);
YGNodeStyleSetMargin(root_child0, YGEdgeTop, 20); YGNodeStyleSetMargin(root_child0, YGEdgeTop, 20);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -626,7 +628,7 @@ TEST(YogaTest, cant_call_negative_measure_horizontal) {
YGNodeStyleSetHeight(root, 20); YGNodeStyleSetHeight(root, 20);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _measure_assert_negative); root_child0->setMeasureFunc(_measure_assert_negative);
YGNodeStyleSetMargin(root_child0, YGEdgeStart, 20); YGNodeStyleSetMargin(root_child0, YGEdgeStart, 20);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
@@ -661,8 +663,7 @@ TEST(YogaTest, percent_with_text_node) {
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config); const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
root_child1->setMeasureFunc(_measure_90_10);
YGNodeSetMeasureFunc(root_child1, _measure_90_10);
YGNodeStyleSetMaxWidthPercent(root_child1, 50); YGNodeStyleSetMaxWidthPercent(root_child1, 50);
YGNodeStyleSetPaddingPercent(root_child1, YGEdgeTop, 50); YGNodeStyleSetPaddingPercent(root_child1, YGEdgeTop, 50);
YGNodeInsertChild(root, root_child1, 1); YGNodeInsertChild(root, root_child1, 1);

View File

@@ -8,6 +8,7 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <yoga/Yoga.h> #include <yoga/Yoga.h>
static YGSize _measureFloor(YGNodeRef node, static YGSize _measureFloor(YGNodeRef node,
@@ -45,7 +46,7 @@ TEST(YogaTest, rounding_feature_with_custom_measure_func_floor) {
const YGNodeRef root = YGNodeNewWithConfig(config); const YGNodeRef root = YGNodeNewWithConfig(config);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _measureFloor); root_child0->setMeasureFunc(_measureFloor);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGConfigSetPointScaleFactor(config, 0.0f); YGConfigSetPointScaleFactor(config, 0.0f);
@@ -93,7 +94,7 @@ TEST(YogaTest, rounding_feature_with_custom_measure_func_ceil) {
const YGNodeRef root = YGNodeNewWithConfig(config); const YGNodeRef root = YGNodeNewWithConfig(config);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _measureCeil); root_child0->setMeasureFunc(_measureCeil);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGConfigSetPointScaleFactor(config, 1.0f); YGConfigSetPointScaleFactor(config, 1.0f);
@@ -114,7 +115,7 @@ TEST(YogaTest, rounding_feature_with_custom_measure_and_fractial_matching_scale)
const YGNodeRef root_child0 = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 73.625); YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 73.625);
YGNodeSetMeasureFunc(root_child0, _measureFractial); root_child0->setMeasureFunc(_measureFractial);
YGNodeInsertChild(root, root_child0, 0); YGNodeInsertChild(root, root_child0, 0);
YGConfigSetPointScaleFactor(config, 2.0f); YGConfigSetPointScaleFactor(config, 2.0f);

View File

@@ -8,15 +8,15 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <yoga/Yoga.h> #include <yoga/YGNode.h>
TEST(YogaTest, copy_style_same) { TEST(YogaTest, copy_style_same) {
const YGNodeRef node0 = YGNodeNew(); const YGNodeRef node0 = YGNodeNew();
const YGNodeRef node1 = YGNodeNew(); const YGNodeRef node1 = YGNodeNew();
ASSERT_FALSE(YGNodeIsDirty(node0)); ASSERT_FALSE(node0->isDirty());
YGNodeCopyStyle(node0, node1); YGNodeCopyStyle(node0, node1);
ASSERT_FALSE(YGNodeIsDirty(node0)); ASSERT_FALSE(node0->isDirty());
YGNodeFree(node0); YGNodeFree(node0);
YGNodeFree(node1); YGNodeFree(node1);
@@ -24,7 +24,7 @@ TEST(YogaTest, copy_style_same) {
TEST(YogaTest, copy_style_modified) { TEST(YogaTest, copy_style_modified) {
const YGNodeRef node0 = YGNodeNew(); const YGNodeRef node0 = YGNodeNew();
ASSERT_FALSE(YGNodeIsDirty(node0)); ASSERT_FALSE(node0->isDirty());
ASSERT_EQ(YGFlexDirectionColumn, YGNodeStyleGetFlexDirection(node0)); ASSERT_EQ(YGFlexDirectionColumn, YGNodeStyleGetFlexDirection(node0));
ASSERT_FALSE(YGNodeStyleGetMaxHeight(node0).unit != YGUnitUndefined); ASSERT_FALSE(YGNodeStyleGetMaxHeight(node0).unit != YGUnitUndefined);
@@ -33,7 +33,7 @@ TEST(YogaTest, copy_style_modified) {
YGNodeStyleSetMaxHeight(node1, 10); YGNodeStyleSetMaxHeight(node1, 10);
YGNodeCopyStyle(node0, node1); YGNodeCopyStyle(node0, node1);
ASSERT_TRUE(YGNodeIsDirty(node0)); ASSERT_TRUE(node0->isDirty());
ASSERT_EQ(YGFlexDirectionRow, YGNodeStyleGetFlexDirection(node0)); ASSERT_EQ(YGFlexDirectionRow, YGNodeStyleGetFlexDirection(node0));
ASSERT_FLOAT_EQ(10, YGNodeStyleGetMaxHeight(node0).value); ASSERT_FLOAT_EQ(10, YGNodeStyleGetMaxHeight(node0).value);
@@ -46,14 +46,14 @@ TEST(YogaTest, copy_style_modified_same) {
YGNodeStyleSetFlexDirection(node0, YGFlexDirectionRow); YGNodeStyleSetFlexDirection(node0, YGFlexDirectionRow);
YGNodeStyleSetMaxHeight(node0, 10); YGNodeStyleSetMaxHeight(node0, 10);
YGNodeCalculateLayout(node0, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(node0, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FALSE(YGNodeIsDirty(node0)); ASSERT_FALSE(node0->isDirty());
const YGNodeRef node1 = YGNodeNew(); const YGNodeRef node1 = YGNodeNew();
YGNodeStyleSetFlexDirection(node1, YGFlexDirectionRow); YGNodeStyleSetFlexDirection(node1, YGFlexDirectionRow);
YGNodeStyleSetMaxHeight(node1, 10); YGNodeStyleSetMaxHeight(node1, 10);
YGNodeCopyStyle(node0, node1); YGNodeCopyStyle(node0, node1);
ASSERT_FALSE(YGNodeIsDirty(node0)); ASSERT_FALSE(node0->isDirty());
YGNodeFree(node0); YGNodeFree(node0);
YGNodeFree(node1); YGNodeFree(node1);

399
yoga/YGNode.cpp Normal file
View File

@@ -0,0 +1,399 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "YGNode.h"
#include <iostream>
void* YGNode::getContext() const {
return context_;
}
YGPrintFunc YGNode::getPrintFunc() const {
return print_;
}
bool YGNode::getHasNewLayout() const {
return hasNewLayout_;
}
YGNodeType YGNode::getNodeType() const {
return nodeType_;
}
YGMeasureFunc YGNode::getMeasure() const {
return measure_;
}
YGBaselineFunc YGNode::getBaseline() const {
return baseline_;
}
YGStyle YGNode::getStyle() const {
return style_;
}
YGLayout YGNode::getLayout() const {
return layout_;
}
YGLayout& YGNode::getLayoutRef() {
return layout_;
}
uint32_t YGNode::getLineIndex() const {
return lineIndex_;
}
YGNodeRef YGNode::getParent() const {
return parent_;
}
YGVector YGNode::getChildren() const {
return children_;
}
YGNodeRef YGNode::getChild(uint32_t index) const {
return children_.at(index);
}
YGNodeRef YGNode::getNextChild() const {
return nextChild_;
}
YGConfigRef YGNode::getConfig() const {
return config_;
}
bool YGNode::isDirty() const {
return isDirty_;
}
YGValue YGNode::getResolvedDimension(int index) {
return resolvedDimensions_[index];
}
std::array<YGValue, 2> YGNode::getResolvedDimensions() const {
return resolvedDimensions_;
}
// Setters
void YGNode::setContext(void* context) {
context_ = context;
}
void YGNode::setPrintFunc(YGPrintFunc printFunc) {
print_ = printFunc;
}
void YGNode::setHasNewLayout(bool hasNewLayout) {
hasNewLayout_ = hasNewLayout;
}
void YGNode::setNodeType(YGNodeType nodeType) {
nodeType_ = nodeType;
}
void YGNode::setStyleFlexDirection(YGFlexDirection direction) {
style_.flexDirection = direction;
}
void YGNode::setStyleAlignContent(YGAlign alignContent) {
style_.alignContent = alignContent;
}
void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
if (measureFunc == nullptr) {
measure_ = nullptr;
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate
// places in Litho
nodeType_ = YGNodeTypeDefault;
} else {
YGAssertWithNode(
this,
children_.size() == 0,
"Cannot set measure function: Nodes with measure functions cannot have children.");
measure_ = measureFunc;
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate
// places in Litho
setNodeType(YGNodeTypeText);
}
measure_ = measureFunc;
}
void YGNode::setBaseLineFunc(YGBaselineFunc baseLineFunc) {
baseline_ = baseLineFunc;
}
void YGNode::setStyle(YGStyle style) {
style_ = style;
}
void YGNode::setLayout(YGLayout layout) {
layout_ = layout;
}
void YGNode::setLineIndex(uint32_t lineIndex) {
lineIndex_ = lineIndex;
}
void YGNode::setParent(YGNodeRef parent) {
parent_ = parent;
}
void YGNode::setChildren(YGVector children) {
children_ = children;
}
void YGNode::setNextChild(YGNodeRef nextChild) {
nextChild_ = nextChild;
}
void YGNode::replaceChild(YGNodeRef child, uint32_t index) {
children_[index] = child;
}
void YGNode::insertChild(YGNodeRef child, uint32_t index) {
children_.insert(children_.begin() + index, child);
}
void YGNode::setConfig(YGConfigRef config) {
config_ = config;
}
void YGNode::setDirty(bool isDirty) {
isDirty_ = isDirty;
}
bool YGNode::removeChild(YGNodeRef child) {
std::vector<YGNodeRef>::iterator p =
std::find(children_.begin(), children_.end(), child);
if (p != children_.end()) {
children_.erase(p);
return true;
}
return false;
}
void YGNode::removeChild(uint32_t index) {
children_.erase(children_.begin() + index);
}
void YGNode::setLayoutDirection(YGDirection direction) {
layout_.direction = direction;
}
void YGNode::setLayoutMargin(float margin, int index) {
layout_.margin[index] = margin;
}
void YGNode::setLayoutBorder(float border, int index) {
layout_.border[index] = border;
}
void YGNode::setLayoutPadding(float padding, int index) {
layout_.padding[index] = padding;
}
void YGNode::setLayoutLastParentDirection(YGDirection direction) {
layout_.lastParentDirection = direction;
}
void YGNode::setLayoutComputedFlexBasis(float computedFlexBasis) {
layout_.computedFlexBasis = computedFlexBasis;
}
void YGNode::setLayoutPosition(float position, int index) {
layout_.position[index] = position;
}
void YGNode::setLayoutComputedFlexBasisGeneration(
uint32_t computedFlexBasisGeneration) {
layout_.computedFlexBasisGeneration = computedFlexBasisGeneration;
}
void YGNode::setLayoutMeasuredDimension(float measuredDimension, int index) {
layout_.measuredDimensions[index] = measuredDimension;
}
void YGNode::setLayoutHadOverflow(bool hadOverflow) {
layout_.hadOverflow = hadOverflow;
}
void YGNode::setLayoutDimension(float dimension, int index) {
layout_.dimensions[index] = dimension;
}
YGNode::YGNode()
: context_(nullptr),
print_(nullptr),
hasNewLayout_(true),
nodeType_(YGNodeTypeDefault),
measure_(nullptr),
baseline_(nullptr),
style_(gYGNodeStyleDefaults),
layout_(gYGNodeLayoutDefaults),
lineIndex_(0),
parent_(nullptr),
children_(YGVector()),
nextChild_(nullptr),
config_(nullptr),
isDirty_(false),
resolvedDimensions_({{YGValueUndefined, YGValueUndefined}}) {}
YGNode::YGNode(const YGNode& node)
: context_(node.getContext()),
print_(node.getPrintFunc()),
hasNewLayout_(node.getHasNewLayout()),
nodeType_(node.getNodeType()),
measure_(node.getMeasure()),
baseline_(node.getBaseline()),
style_(node.getStyle()),
layout_(node.getLayout()),
lineIndex_(node.getLineIndex()),
parent_(node.getParent()),
children_(node.getChildren()),
nextChild_(node.getNextChild()),
config_(node.getConfig()),
isDirty_(node.isDirty()),
resolvedDimensions_(node.getResolvedDimensions()) {}
YGNode::YGNode(const YGConfigRef newConfig) : YGNode() {
config_ = newConfig;
}
YGNode::YGNode(
void* context,
YGPrintFunc print,
bool hasNewLayout,
YGNodeType nodeType,
YGMeasureFunc measure,
YGBaselineFunc baseline,
YGStyle style,
YGLayout layout,
uint32_t lineIndex,
YGNodeRef parent,
YGVector children,
YGNodeRef nextChild,
YGConfigRef config,
bool isDirty,
std::array<YGValue, 2> resolvedDimensions)
: context_(context),
print_(print),
hasNewLayout_(hasNewLayout),
nodeType_(nodeType),
measure_(measure),
baseline_(baseline),
style_(style),
layout_(layout),
lineIndex_(lineIndex),
parent_(parent),
children_(children),
nextChild_(nextChild),
config_(config),
isDirty_(isDirty),
resolvedDimensions_(resolvedDimensions) {}
YGNode& YGNode::operator=(const YGNode& node) {
if (&node == this) {
return *this;
}
for (auto child : children_) {
delete child;
}
context_ = node.getContext();
print_ = node.getPrintFunc();
hasNewLayout_ = node.getHasNewLayout();
nodeType_ = node.getNodeType();
measure_ = node.getMeasure();
baseline_ = node.getBaseline();
style_ = node.getStyle();
layout_ = node.getLayout();
lineIndex_ = node.getLineIndex();
parent_ = node.getParent();
children_ = node.getChildren();
nextChild_ = node.getNextChild();
config_ = node.getConfig();
isDirty_ = node.isDirty();
resolvedDimensions_ = node.getResolvedDimensions();
return *this;
}
YGValue YGNode::marginLeadingValue(const YGFlexDirection axis) const {
if (YGFlexDirectionIsRow(axis) &&
getStyle().margin[YGEdgeStart].unit != YGUnitUndefined) {
return getStyle().margin[YGEdgeStart];
} else {
return getStyle().margin[leading[axis]];
}
}
YGValue YGNode::marginTrailingValue(const YGFlexDirection axis) const {
if (YGFlexDirectionIsRow(axis) &&
getStyle().margin[YGEdgeEnd].unit != YGUnitUndefined) {
return getStyle().margin[YGEdgeEnd];
} else {
return getStyle().margin[trailing[axis]];
}
}
YGValue YGNode::resolveFlexBasisPtr() const {
YGValue flexBasis = getStyle().flexBasis;
if (flexBasis.unit != YGUnitAuto && flexBasis.unit != YGUnitUndefined) {
return flexBasis;
}
if (!YGFloatIsUndefined(getStyle().flex) && getStyle().flex > 0.0f) {
return config_->useWebDefaults ? YGValueAuto : YGValueZero;
}
return YGValueAuto;
}
void YGNode::resolveDimension() {
for (uint32_t dim = YGDimensionWidth; dim < YGDimensionCount; dim++) {
if (getStyle().maxDimensions[dim].unit != YGUnitUndefined &&
YGValueEqual(
getStyle().maxDimensions[dim], getStyle().minDimensions[dim])) {
resolvedDimensions_[dim] = getStyle().maxDimensions[dim];
} else {
resolvedDimensions_[dim] = getStyle().dimensions[dim];
}
}
}
void YGNode::clearChildren() {
children_.clear();
children_.shrink_to_fit();
}
YGNode::~YGNode() {
// All the member variables are deallocated externally, so no need to
// deallocate here
}
const YGNode& YGNode::defaultValue() {
static const YGNode n = {nullptr,
nullptr,
true,
YGNodeTypeDefault,
nullptr,
nullptr,
gYGNodeStyleDefaults,
gYGNodeLayoutDefaults,
0,
nullptr,
YGVector(),
nullptr,
nullptr,
false,
{{YGValueUndefined, YGValueUndefined}}};
return n;
}

121
yoga/YGNode.h Normal file
View File

@@ -0,0 +1,121 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include <stdio.h>
#include "Yoga-internal.h"
struct YGNode {
private:
void* context_;
YGPrintFunc print_;
bool hasNewLayout_;
YGNodeType nodeType_;
YGMeasureFunc measure_;
YGBaselineFunc baseline_;
YGStyle style_;
YGLayout layout_;
uint32_t lineIndex_;
YGNodeRef parent_;
YGVector children_;
YGNodeRef nextChild_;
YGConfigRef config_;
bool isDirty_;
std::array<YGValue, 2> resolvedDimensions_;
public:
YGNode();
~YGNode();
explicit YGNode(const YGConfigRef newConfig);
YGNode(const YGNode& node);
YGNode& operator=(const YGNode& node);
YGNode(
void* context,
YGPrintFunc print,
bool hasNewLayout,
YGNodeType nodeType,
YGMeasureFunc measure,
YGBaselineFunc baseline,
YGStyle style,
YGLayout layout,
uint32_t lineIndex,
YGNodeRef parent,
YGVector children,
YGNodeRef nextChild,
YGConfigRef config,
bool isDirty,
std::array<YGValue, 2> resolvedDimensions);
// Getters
void* getContext() const;
YGPrintFunc getPrintFunc() const;
bool getHasNewLayout() const;
YGNodeType getNodeType() const;
YGMeasureFunc getMeasure() const;
YGBaselineFunc getBaseline() const;
YGStyle getStyle() const;
YGLayout getLayout() const;
YGLayout& getLayoutRef(); // TODO remove its use
uint32_t getLineIndex() const;
YGNodeRef getParent() const;
YGVector getChildren() const;
YGNodeRef getChild(uint32_t index) const;
YGNodeRef getNextChild() const;
YGConfigRef getConfig() const;
bool isDirty() const;
std::array<YGValue, 2> getResolvedDimensions() const;
YGValue getResolvedDimension(int index);
// Setters
void setContext(void* context);
void setPrintFunc(YGPrintFunc printFunc);
void setHasNewLayout(bool hasNewLayout);
void setNodeType(YGNodeType nodeTye);
void setMeasureFunc(YGMeasureFunc measureFunc);
void setBaseLineFunc(YGBaselineFunc baseLineFunc);
void setStyle(YGStyle style);
void setStyleFlexDirection(YGFlexDirection direction);
void setStyleAlignContent(YGAlign alignContent);
void setLayout(YGLayout layout);
void setLineIndex(uint32_t lineIndex);
void setParent(YGNodeRef parent);
void setChildren(YGVector children);
void setNextChild(YGNodeRef nextChild);
void setConfig(YGConfigRef config);
void setDirty(bool isDirty);
void setLayoutLastParentDirection(YGDirection direction);
void setLayoutComputedFlexBasis(float computedFlexBasis);
void setLayoutComputedFlexBasisGeneration(
uint32_t computedFlexBasisGeneration);
void setLayoutMeasuredDimension(float measuredDimension, int index);
void setLayoutHadOverflow(bool hadOverflow);
void setLayoutDimension(float dimension, int index);
// Other methods
YGValue marginLeadingValue(const YGFlexDirection axis) const;
YGValue marginTrailingValue(const YGFlexDirection axis) const;
YGValue resolveFlexBasisPtr() const;
void resolveDimension();
void clearChildren();
void replaceChild(YGNodeRef child, uint32_t index);
void insertChild(YGNodeRef child, uint32_t index);
/// Removes the first occurrence of child
bool removeChild(YGNodeRef child);
void removeChild(uint32_t index);
void setLayoutDirection(YGDirection direction);
void setLayoutMargin(float margin, int index);
void setLayoutBorder(float border, int index);
void setLayoutPadding(float padding, int index);
void setLayoutPosition(float position, int index);
// Static methods
static const YGNode& defaultValue();
};

View File

@@ -10,6 +10,7 @@
#include "YGNodePrint.h" #include "YGNodePrint.h"
#include <stdarg.h> #include <stdarg.h>
#include "YGEnums.h" #include "YGEnums.h"
#include "YGNode.h"
#include "Yoga-internal.h" #include "Yoga-internal.h"
namespace facebook { namespace facebook {
@@ -22,7 +23,7 @@ static void indent(string* base, uint32_t level) {
} }
} }
static bool areFourValuesEqual(const YGValue four[4]) { static bool areFourValuesEqual(const std::array<YGValue, YGEdgeCount>& four) {
return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) && return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) &&
YGValueEqual(four[0], four[3]); YGValueEqual(four[0], four[3]);
} }
@@ -50,54 +51,53 @@ appendFloatIfNotUndefined(string* base, const string key, const float num) {
static void appendNumberIfNotUndefined( static void appendNumberIfNotUndefined(
string* base, string* base,
const string key, const string key,
const YGValue* const number) { const YGValue number) {
if (number->unit != YGUnitUndefined) { if (number.unit != YGUnitUndefined) {
if (number->unit == YGUnitAuto) { if (number.unit == YGUnitAuto) {
base->append(key + ": auto; "); base->append(key + ": auto; ");
} else { } else {
string unit = number->unit == YGUnitPoint ? "px" : "%%"; string unit = number.unit == YGUnitPoint ? "px" : "%%";
appendFormatedString( appendFormatedString(
base, "%s: %g%s; ", key.c_str(), number->value, unit.c_str()); base, "%s: %g%s; ", key.c_str(), number.value, unit.c_str());
} }
} }
} }
static void appendNumberIfNotAuto( static void
string* base, appendNumberIfNotAuto(string* base, const string& key, const YGValue number) {
const string key, if (number.unit != YGUnitAuto) {
const YGValue* const number) {
if (number->unit != YGUnitAuto) {
appendNumberIfNotUndefined(base, key, number); appendNumberIfNotUndefined(base, key, number);
} }
} }
static void appendNumberIfNotZero( static void
string* base, appendNumberIfNotZero(string* base, const string& str, const YGValue number) {
const string str, if (!YGFloatsEqual(number.value, 0)) {
const YGValue* const number) {
if (!YGFloatsEqual(number->value, 0)) {
appendNumberIfNotUndefined(base, str, number); appendNumberIfNotUndefined(base, str, number);
} }
} }
static void appendEdges(string* base, const string key, const YGValue* edges) { static void appendEdges(
string* base,
const string& key,
const std::array<YGValue, YGEdgeCount>& edges) {
if (areFourValuesEqual(edges)) { if (areFourValuesEqual(edges)) {
appendNumberIfNotZero(base, key, &edges[YGEdgeLeft]); appendNumberIfNotZero(base, key, edges[YGEdgeLeft]);
} else { } else {
for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) { for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) {
string str = key + "-" + YGEdgeToString(static_cast<YGEdge>(edge)); string str = key + "-" + YGEdgeToString(static_cast<YGEdge>(edge));
appendNumberIfNotZero(base, str, &edges[edge]); appendNumberIfNotZero(base, str, edges[edge]);
} }
} }
} }
static void appendEdgeIfNotUndefined( static void appendEdgeIfNotUndefined(
string* base, string* base,
const string str, const string& str,
const YGValue* edges, const std::array<YGValue, YGEdgeCount>& edges,
const YGEdge edge) { const YGEdge edge) {
appendNumberIfNotUndefined( appendNumberIfNotUndefined(
base, str, YGComputedEdgeValue(edges, edge, &YGValueUndefined)); base, str, *YGComputedEdgeValue(edges, edge, &YGValueUndefined));
} }
void YGNodeToString( void YGNodeToString(
@@ -107,105 +107,112 @@ void YGNodeToString(
uint32_t level) { uint32_t level) {
indent(str, level); indent(str, level);
appendFormatedString(str, "<div "); appendFormatedString(str, "<div ");
if (node->print != nullptr) { if (node->getPrintFunc() != nullptr) {
node->print(node); node->getPrintFunc()(node);
} }
if (options & YGPrintOptionsLayout) { if (options & YGPrintOptionsLayout) {
appendFormatedString(str, "layout=\""); appendFormatedString(str, "layout=\"");
appendFormatedString( appendFormatedString(
str, "width: %g; ", node->layout.dimensions[YGDimensionWidth]); str, "width: %g; ", node->getLayout().dimensions[YGDimensionWidth]);
appendFormatedString( appendFormatedString(
str, "height: %g; ", node->layout.dimensions[YGDimensionHeight]); str, "height: %g; ", node->getLayout().dimensions[YGDimensionHeight]);
appendFormatedString(str, "top: %g; ", node->layout.position[YGEdgeTop]); appendFormatedString(
appendFormatedString(str, "left: %g;", node->layout.position[YGEdgeLeft]); str, "top: %g; ", node->getLayout().position[YGEdgeTop]);
appendFormatedString(
str, "left: %g;", node->getLayout().position[YGEdgeLeft]);
appendFormatedString(str, "\" "); appendFormatedString(str, "\" ");
} }
if (options & YGPrintOptionsStyle) { if (options & YGPrintOptionsStyle) {
appendFormatedString(str, "style=\""); appendFormatedString(str, "style=\"");
if (node->style.flexDirection != gYGNodeDefaults.style.flexDirection) { if (node->getStyle().flexDirection != YGNode().getStyle().flexDirection) {
appendFormatedString( appendFormatedString(
str, str,
"flex-direction: %s; ", "flex-direction: %s; ",
YGFlexDirectionToString(node->style.flexDirection)); YGFlexDirectionToString(node->getStyle().flexDirection));
} }
if (node->style.justifyContent != gYGNodeDefaults.style.justifyContent) { if (node->getStyle().justifyContent != YGNode().getStyle().justifyContent) {
appendFormatedString( appendFormatedString(
str, str,
"justify-content: %s; ", "justify-content: %s; ",
YGJustifyToString(node->style.justifyContent)); YGJustifyToString(node->getStyle().justifyContent));
} }
if (node->style.alignItems != gYGNodeDefaults.style.alignItems) { if (node->getStyle().alignItems != YGNode().getStyle().alignItems) {
appendFormatedString( appendFormatedString(
str, "align-items: %s; ", YGAlignToString(node->style.alignItems)); str,
"align-items: %s; ",
YGAlignToString(node->getStyle().alignItems));
} }
if (node->style.alignContent != gYGNodeDefaults.style.alignContent) { if (node->getStyle().alignContent != YGNode().getStyle().alignContent) {
appendFormatedString( appendFormatedString(
str, str,
"align-content: %s; ", "align-content: %s; ",
YGAlignToString(node->style.alignContent)); YGAlignToString(node->getStyle().alignContent));
} }
if (node->style.alignSelf != gYGNodeDefaults.style.alignSelf) { if (node->getStyle().alignSelf != YGNode().getStyle().alignSelf) {
appendFormatedString( appendFormatedString(
str, "align-self: %s; ", YGAlignToString(node->style.alignSelf)); str, "align-self: %s; ", YGAlignToString(node->getStyle().alignSelf));
} }
appendFloatIfNotUndefined(str, "flex-grow", node->style.flexGrow); appendFloatIfNotUndefined(str, "flex-grow", node->getStyle().flexGrow);
appendFloatIfNotUndefined(str, "flex-shrink", node->style.flexShrink); appendFloatIfNotUndefined(str, "flex-shrink", node->getStyle().flexShrink);
appendNumberIfNotAuto(str, "flex-basis", &node->style.flexBasis); appendNumberIfNotAuto(str, "flex-basis", node->getStyle().flexBasis);
appendFloatIfNotUndefined(str, "flex", node->style.flex); appendFloatIfNotUndefined(str, "flex", node->getStyle().flex);
if (node->style.flexWrap != gYGNodeDefaults.style.flexWrap) { if (node->getStyle().flexWrap != YGNode().getStyle().flexWrap) {
appendFormatedString( appendFormatedString(
str, "flexWrap: %s; ", YGWrapToString(node->style.flexWrap)); str, "flexWrap: %s; ", YGWrapToString(node->getStyle().flexWrap));
} }
if (node->style.overflow != gYGNodeDefaults.style.overflow) { if (node->getStyle().overflow != YGNode().getStyle().overflow) {
appendFormatedString( appendFormatedString(
str, "overflow: %s; ", YGOverflowToString(node->style.overflow)); str, "overflow: %s; ", YGOverflowToString(node->getStyle().overflow));
} }
if (node->style.display != gYGNodeDefaults.style.display) { if (node->getStyle().display != YGNode().getStyle().display) {
appendFormatedString( appendFormatedString(
str, "display: %s; ", YGDisplayToString(node->style.display)); str, "display: %s; ", YGDisplayToString(node->getStyle().display));
} }
appendEdges(str, "margin", node->style.margin); appendEdges(str, "margin", node->getStyle().margin);
appendEdges(str, "padding", node->style.padding); appendEdges(str, "padding", node->getStyle().padding);
appendEdges(str, "border", node->style.border); appendEdges(str, "border", node->getStyle().border);
appendNumberIfNotAuto( appendNumberIfNotAuto(
str, "width", &node->style.dimensions[YGDimensionWidth]); str, "width", node->getStyle().dimensions[YGDimensionWidth]);
appendNumberIfNotAuto( appendNumberIfNotAuto(
str, "height", &node->style.dimensions[YGDimensionHeight]); str, "height", node->getStyle().dimensions[YGDimensionHeight]);
appendNumberIfNotAuto( appendNumberIfNotAuto(
str, "max-width", &node->style.maxDimensions[YGDimensionWidth]); str, "max-width", node->getStyle().maxDimensions[YGDimensionWidth]);
appendNumberIfNotAuto( appendNumberIfNotAuto(
str, "max-height", &node->style.maxDimensions[YGDimensionHeight]); str, "max-height", node->getStyle().maxDimensions[YGDimensionHeight]);
appendNumberIfNotAuto( appendNumberIfNotAuto(
str, "min-width", &node->style.minDimensions[YGDimensionWidth]); str, "min-width", node->getStyle().minDimensions[YGDimensionWidth]);
appendNumberIfNotAuto( appendNumberIfNotAuto(
str, "min-height", &node->style.minDimensions[YGDimensionHeight]); str, "min-height", node->getStyle().minDimensions[YGDimensionHeight]);
if (node->style.positionType != gYGNodeDefaults.style.positionType) { if (node->getStyle().positionType != YGNode().getStyle().positionType) {
appendFormatedString( appendFormatedString(
str, str,
"position: %s; ", "position: %s; ",
YGPositionTypeToString(node->style.positionType)); YGPositionTypeToString(node->getStyle().positionType));
} }
appendEdgeIfNotUndefined(str, "left", node->style.position, YGEdgeLeft); appendEdgeIfNotUndefined(
appendEdgeIfNotUndefined(str, "right", node->style.position, YGEdgeRight); str, "left", node->getStyle().position, YGEdgeLeft);
appendEdgeIfNotUndefined(str, "top", node->style.position, YGEdgeTop); appendEdgeIfNotUndefined(
appendEdgeIfNotUndefined(str, "bottom", node->style.position, YGEdgeBottom); str, "right", node->getStyle().position, YGEdgeRight);
appendEdgeIfNotUndefined(str, "top", node->getStyle().position, YGEdgeTop);
appendEdgeIfNotUndefined(
str, "bottom", node->getStyle().position, YGEdgeBottom);
appendFormatedString(str, "\" "); appendFormatedString(str, "\" ");
if (node->measure != nullptr) { if (node->getMeasure() != nullptr) {
appendFormatedString(str, "has-custom-measure=\"true\""); appendFormatedString(str, "has-custom-measure=\"true\"");
} }
} }
appendFormatedString(str, ">"); appendFormatedString(str, ">");
const uint32_t childCount = node->children.size(); const uint32_t childCount = node->getChildren().size();
if (options & YGPrintOptionsChildren && childCount > 0) { if (options & YGPrintOptionsChildren && childCount > 0) {
for (uint32_t i = 0; i < childCount; i++) { for (uint32_t i = 0; i < childCount; i++) {
appendFormatedString(str, "\n"); appendFormatedString(str, "\n");

View File

@@ -8,6 +8,9 @@
*/ */
#pragma once #pragma once
#include <algorithm>
#include <array>
#include <cmath>
#include <vector> #include <vector>
#include "Yoga.h" #include "Yoga.h"
@@ -23,6 +26,25 @@ WIN_EXPORT float YGRoundValueToPixelGrid(const float value,
YG_EXTERN_C_END YG_EXTERN_C_END
extern const std::array<YGEdge, 4> trailing;
extern const std::array<YGEdge, 4> leading;
extern bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection);
extern bool YGValueEqual(const YGValue a, const YGValue b);
extern const YGValue YGValueUndefined;
extern const YGValue YGValueAuto;
extern const YGValue YGValueZero;
template <std::size_t size>
bool YGValueArrayEqual(
const std::array<YGValue, size> val1,
const std::array<YGValue, size> val2) {
bool areEqual = true;
for (uint32_t i = 0; i < size && areEqual; ++i) {
areEqual = YGValueEqual(val1[i], val2[i]);
}
return areEqual;
}
typedef struct YGCachedMeasurement { typedef struct YGCachedMeasurement {
float availableWidth; float availableWidth;
float availableHeight; float availableHeight;
@@ -37,12 +59,12 @@ typedef struct YGCachedMeasurement {
// layouts should not require more than 16 entries to fit within the cache. // layouts should not require more than 16 entries to fit within the cache.
#define YG_MAX_CACHED_RESULT_COUNT 16 #define YG_MAX_CACHED_RESULT_COUNT 16
typedef struct YGLayout { struct YGLayout {
float position[4]; std::array<float, 4> position;
float dimensions[2]; std::array<float, 2> dimensions;
float margin[6]; std::array<float, 6> margin;
float border[6]; std::array<float, 6> border;
float padding[6]; std::array<float, 6> padding;
YGDirection direction; YGDirection direction;
uint32_t computedFlexBasisGeneration; uint32_t computedFlexBasisGeneration;
@@ -56,12 +78,12 @@ typedef struct YGLayout {
uint32_t nextCachedMeasurementsIndex; uint32_t nextCachedMeasurementsIndex;
YGCachedMeasurement cachedMeasurements[YG_MAX_CACHED_RESULT_COUNT]; YGCachedMeasurement cachedMeasurements[YG_MAX_CACHED_RESULT_COUNT];
float measuredDimensions[2]; std::array<float, 2> measuredDimensions;
YGCachedMeasurement cachedLayout; YGCachedMeasurement cachedLayout;
} YGLayout; };
typedef struct YGStyle { struct YGStyle {
YGDirection direction; YGDirection direction;
YGFlexDirection flexDirection; YGFlexDirection flexDirection;
YGJustify justifyContent; YGJustify justifyContent;
@@ -76,17 +98,58 @@ typedef struct YGStyle {
float flexGrow; float flexGrow;
float flexShrink; float flexShrink;
YGValue flexBasis; YGValue flexBasis;
YGValue margin[YGEdgeCount]; std::array<YGValue, YGEdgeCount> margin;
YGValue position[YGEdgeCount]; std::array<YGValue, YGEdgeCount> position;
YGValue padding[YGEdgeCount]; std::array<YGValue, YGEdgeCount> padding;
YGValue border[YGEdgeCount]; std::array<YGValue, YGEdgeCount> border;
YGValue dimensions[2]; std::array<YGValue, 2> dimensions;
YGValue minDimensions[2]; std::array<YGValue, 2> minDimensions;
YGValue maxDimensions[2]; std::array<YGValue, 2> maxDimensions;
// Yoga specific properties, not compatible with flexbox specification // Yoga specific properties, not compatible with flexbox specification
float aspectRatio; float aspectRatio;
} YGStyle; bool operator==(YGStyle style) {
bool areNonFloatValuesEqual = direction == style.direction &&
flexDirection == style.flexDirection &&
justifyContent == style.justifyContent &&
alignContent == style.alignContent && alignItems == style.alignItems &&
alignSelf == style.alignSelf && positionType == style.positionType &&
flexWrap == style.flexWrap && overflow == style.overflow &&
display == style.display && YGValueEqual(flexBasis, style.flexBasis) &&
YGValueArrayEqual(margin, style.margin) &&
YGValueArrayEqual(position, style.position) &&
YGValueArrayEqual(padding, style.padding) &&
YGValueArrayEqual(border, style.border) &&
YGValueArrayEqual(dimensions, style.dimensions) &&
YGValueArrayEqual(minDimensions, style.minDimensions) &&
YGValueArrayEqual(maxDimensions, style.maxDimensions);
if (!(std::isnan(flex) && std::isnan(style.flex))) {
areNonFloatValuesEqual = areNonFloatValuesEqual && flex == style.flex;
}
if (!(std::isnan(flexGrow) && std::isnan(style.flexGrow))) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && flexGrow == style.flexGrow;
}
if (!(std::isnan(flexShrink) && std::isnan(style.flexShrink))) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && flexShrink == style.flexShrink;
}
if (!(std::isnan(aspectRatio) && std::isnan(style.aspectRatio))) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && aspectRatio == style.aspectRatio;
}
return areNonFloatValuesEqual;
}
bool operator!=(YGStyle style) {
return !(*this == style);
}
};
typedef struct YGConfig { typedef struct YGConfig {
bool experimentalFeatures[YGExperimentalFeatureCount + 1]; bool experimentalFeatures[YGExperimentalFeatureCount + 1];
@@ -98,29 +161,6 @@ typedef struct YGConfig {
void* context; void* context;
} YGConfig; } YGConfig;
typedef struct YGNode {
YGStyle style;
YGLayout layout;
uint32_t lineIndex;
YGNodeRef parent;
YGVector children;
struct YGNode* nextChild;
YGMeasureFunc measure;
YGBaselineFunc baseline;
YGPrintFunc print;
YGConfigRef config;
void* context;
bool isDirty;
bool hasNewLayout;
YGNodeType nodeType;
YGValue const* resolvedDimensions[2];
} YGNode;
#define YG_UNDEFINED_VALUES \ #define YG_UNDEFINED_VALUES \
{ .value = YGUndefined, .unit = YGUnitUndefined } { .value = YGUndefined, .unit = YGUnitUndefined }
@@ -167,19 +207,51 @@ static const YGStyle gYGNodeStyleDefaults = {
.flexGrow = YGUndefined, .flexGrow = YGUndefined,
.flexShrink = YGUndefined, .flexShrink = YGUndefined,
.flexBasis = YG_AUTO_VALUES, .flexBasis = YG_AUTO_VALUES,
.margin = YG_DEFAULT_EDGE_VALUES_UNIT, .margin = {{YG_UNDEFINED_VALUES,
.position = YG_DEFAULT_EDGE_VALUES_UNIT, YG_UNDEFINED_VALUES,
.padding = YG_DEFAULT_EDGE_VALUES_UNIT, YG_UNDEFINED_VALUES,
.border = YG_DEFAULT_EDGE_VALUES_UNIT, YG_UNDEFINED_VALUES,
.dimensions = YG_DEFAULT_DIMENSION_VALUES_AUTO_UNIT, YG_UNDEFINED_VALUES,
.minDimensions = YG_DEFAULT_DIMENSION_VALUES_UNIT, YG_UNDEFINED_VALUES,
.maxDimensions = YG_DEFAULT_DIMENSION_VALUES_UNIT, YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES}},
.position = {{YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES}},
.padding = {{YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES}},
.border = {{YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES}},
.dimensions = {{YG_AUTO_VALUES, YG_AUTO_VALUES}},
.minDimensions = {{YG_UNDEFINED_VALUES, YG_UNDEFINED_VALUES}},
.maxDimensions = {{YG_UNDEFINED_VALUES, YG_UNDEFINED_VALUES}},
.aspectRatio = YGUndefined, .aspectRatio = YGUndefined,
}; };
static const YGLayout gYGNodeLayoutDefaults = { static const YGLayout gYGNodeLayoutDefaults = {
.position = {}, .position = {},
.dimensions = YG_DEFAULT_DIMENSION_VALUES, .dimensions = {{YGUndefined, YGUndefined}},
.margin = {}, .margin = {},
.border = {}, .border = {},
.padding = {}, .padding = {},
@@ -191,7 +263,7 @@ static const YGLayout gYGNodeLayoutDefaults = {
.lastParentDirection = (YGDirection)-1, .lastParentDirection = (YGDirection)-1,
.nextCachedMeasurementsIndex = 0, .nextCachedMeasurementsIndex = 0,
.cachedMeasurements = {}, .cachedMeasurements = {},
.measuredDimensions = YG_DEFAULT_DIMENSION_VALUES, .measuredDimensions = {{YGUndefined, YGUndefined}},
.cachedLayout = .cachedLayout =
{ {
.availableWidth = 0, .availableWidth = 0,
@@ -203,28 +275,9 @@ static const YGLayout gYGNodeLayoutDefaults = {
}, },
}; };
static const YGNode gYGNodeDefaults = {
.style = gYGNodeStyleDefaults,
.layout = gYGNodeLayoutDefaults,
.lineIndex = 0,
.parent = nullptr,
.children = YGVector(),
.nextChild = nullptr,
.measure = nullptr,
.baseline = nullptr,
.print = nullptr,
.config = nullptr,
.context = nullptr,
.isDirty = false,
.hasNewLayout = true,
.nodeType = YGNodeTypeDefault,
.resolvedDimensions = {[YGDimensionWidth] = &YGValueUndefined,
[YGDimensionHeight] = &YGValueUndefined},
};
extern bool YGFloatsEqual(const float a, const float b); extern bool YGFloatsEqual(const float a, const float b);
extern bool YGValueEqual(const YGValue a, const YGValue b); extern bool YGValueEqual(const YGValue a, const YGValue b);
extern const YGValue* YGComputedEdgeValue( extern const YGValue* YGComputedEdgeValue(
const YGValue edges[YGEdgeCount], const std::array<YGValue, YGEdgeCount>& edges,
const YGEdge edge, const YGEdge edge,
const YGValue* const defaultValue); const YGValue* const defaultValue);

File diff suppressed because it is too large Load Diff

View File

@@ -43,11 +43,13 @@ typedef struct YGValue {
YGUnit unit; YGUnit unit;
} YGValue; } YGValue;
static const YGValue YGValueUndefined = {YGUndefined, YGUnitUndefined}; extern const YGValue YGValueUndefined;
static const YGValue YGValueAuto = {YGUndefined, YGUnitAuto}; extern const YGValue YGValueAuto;
typedef struct YGConfig *YGConfigRef; typedef struct YGConfig *YGConfigRef;
typedef struct YGNode* YGNodeRef; typedef struct YGNode* YGNodeRef;
typedef YGSize (*YGMeasureFunc)(YGNodeRef node, typedef YGSize (*YGMeasureFunc)(YGNodeRef node,
float width, float width,
YGMeasureMode widthMode, YGMeasureMode widthMode,
@@ -95,7 +97,6 @@ WIN_EXPORT void YGNodeCalculateLayout(const YGNodeRef node,
// depends on information not known to YG they must perform this dirty // depends on information not known to YG they must perform this dirty
// marking manually. // marking manually.
WIN_EXPORT void YGNodeMarkDirty(const YGNodeRef node); WIN_EXPORT void YGNodeMarkDirty(const YGNodeRef node);
WIN_EXPORT bool YGNodeIsDirty(const YGNodeRef node);
WIN_EXPORT void YGNodePrint(const YGNodeRef node, const YGPrintOptions options); WIN_EXPORT void YGNodePrint(const YGNodeRef node, const YGPrintOptions options);
@@ -158,12 +159,19 @@ WIN_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode
#define YG_NODE_LAYOUT_EDGE_PROPERTY(type, name) \ #define YG_NODE_LAYOUT_EDGE_PROPERTY(type, name) \
WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node, const YGEdge edge); WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node, const YGEdge edge);
YG_NODE_PROPERTY(void *, Context, context); void* YGNodeGetContext(YGNodeRef node);
YG_NODE_PROPERTY(YGMeasureFunc, MeasureFunc, measureFunc); void YGNodeSetContext(YGNodeRef node, void* context);
YG_NODE_PROPERTY(YGBaselineFunc, BaselineFunc, baselineFunc) YGMeasureFunc YGNodeGetMeasureFunc(YGNodeRef node);
YG_NODE_PROPERTY(YGPrintFunc, PrintFunc, printFunc); void YGNodeSetMeasureFunc(YGNodeRef node, YGMeasureFunc measureFunc);
YG_NODE_PROPERTY(bool, HasNewLayout, hasNewLayout); YGBaselineFunc YGNodeGetBaselineFunc(YGNodeRef node);
YG_NODE_PROPERTY(YGNodeType, NodeType, nodeType); void YGNodeSetBaselineFunc(YGNodeRef node, YGBaselineFunc baselineFunc);
YGPrintFunc YGNodeGetPrintFunc(YGNodeRef node);
void YGNodeSetPrintFunc(YGNodeRef node, YGPrintFunc printFunc);
bool YGNodeGetHasNewLayout(YGNodeRef node);
void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout);
YGNodeType YGNodeGetNodeType(YGNodeRef node);
void YGNodeSetNodeType(YGNodeRef node, YGNodeType nodeType);
bool YGNodeIsDirty(YGNodeRef node);
YG_NODE_STYLE_PROPERTY(YGDirection, Direction, direction); YG_NODE_STYLE_PROPERTY(YGDirection, Direction, direction);
YG_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection); YG_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection);