From 62f47190fb2c13d47b13efacfc237f30ab312b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20W=C3=B6hrl?= Date: Fri, 3 Mar 2017 10:47:42 -0800 Subject: [PATCH] Added bool config to YGConfig to configure using web defaults Summary: Added bool config to YGConfig to configure using web defaults. See #445. Closes https://github.com/facebook/yoga/pull/449 Reviewed By: astreet Differential Revision: D4642272 Pulled By: emilsjolander fbshipit-source-id: 4f35bd17b7f764f42295052a4a8b4ae46c192d7e --- tests/YGDefaultValuesTest.cpp | 13 +++++++++++++ yoga/Yoga.c | 27 ++++++++++++++++++++++----- yoga/Yoga.h | 6 ++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/tests/YGDefaultValuesTest.cpp b/tests/YGDefaultValuesTest.cpp index a1d33eee..7e6b341f 100644 --- a/tests/YGDefaultValuesTest.cpp +++ b/tests/YGDefaultValuesTest.cpp @@ -90,3 +90,16 @@ TEST(YogaTest, assert_default_values) { YGNodeFreeRecursive(root); } + +TEST(YogaTest, assert_webdefault_values) { + YGConfig * config = YGConfigNew(); + YGConfigSetUseWebDefaults(config, true); + const YGNodeRef root = YGNodeNewWithConfig(config); + + ASSERT_EQ(YGFlexDirectionRow, YGNodeStyleGetFlexDirection(root)); + ASSERT_EQ(YGAlignStretch, YGNodeStyleGetAlignContent(root)); + ASSERT_FLOAT_EQ(1.0f, YGNodeStyleGetFlexShrink(root)); + + YGNodeFreeRecursive(root); + YGConfigFree(config); +} diff --git a/yoga/Yoga.c b/yoga/Yoga.c index d4743518..7338319e 100644 --- a/yoga/Yoga.c +++ b/yoga/Yoga.c @@ -94,7 +94,10 @@ typedef struct YGStyle { float aspectRatio; } YGStyle; -typedef struct YGConfig { bool experimentalFeatures[YGExperimentalFeatureCount + 1]; } YGConfig; +typedef struct YGConfig { + bool experimentalFeatures[YGExperimentalFeatureCount + 1]; + bool useWebDefaults; +} YGConfig; typedef struct YGNode { YGStyle style; @@ -144,6 +147,7 @@ typedef struct YGNode { static const float kDefaultFlexGrow = 0.0f; static const float kDefaultFlexShrink = 0.0f; +static const float kWebDefaultFlexShrink = 1.0f; static YGNode gYGNodeDefaults = { .parent = NULL, @@ -201,6 +205,7 @@ static YGConfig gYGConfigDefaults = { [YGExperimentalFeatureMinFlexFix] = false, [YGExperimentalFeatureWebFlexBasis] = false, }, + .useWebDefaults = false, }; static void YGNodeMarkDirtyInternal(const YGNodeRef node); @@ -309,6 +314,10 @@ WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) { gNodeInstanceCount++; memcpy(node, &gYGNodeDefaults, sizeof(YGNode)); + if (config->useWebDefaults) { + node->style.flexDirection = YGFlexDirectionRow; + node->style.alignContent = YGAlignStretch; + } node->config = config; return node; } @@ -463,17 +472,17 @@ float YGNodeStyleGetFlexGrow(const YGNodeRef node) { } float YGNodeStyleGetFlexShrink(const YGNodeRef node) { - return YGFloatIsUndefined(node->style.flexShrink) ? kDefaultFlexShrink : node->style.flexShrink; + return YGFloatIsUndefined(node->style.flexShrink) ? (node->config->useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink) : node->style.flexShrink; } static inline float YGNodeResolveFlexShrink(const YGNodeRef node) { if (!YGFloatIsUndefined(node->style.flexShrink)) { return node->style.flexShrink; } - if (!YGFloatIsUndefined(node->style.flex) && node->style.flex < 0.0f) { + if (!node->config->useWebDefaults && !YGFloatIsUndefined(node->style.flex) && node->style.flex < 0.0f) { return -node->style.flex; } - return kDefaultFlexShrink; + return node->config->useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink; } static inline const YGValue *YGNodeResolveFlexBasisPtr(const YGNodeRef node) { @@ -481,7 +490,7 @@ static inline const YGValue *YGNodeResolveFlexBasisPtr(const YGNodeRef node) { return &node->style.flexBasis; } if (!YGFloatIsUndefined(node->style.flex) && node->style.flex > 0.0f) { - return &YGValueZero; + return node->config->useWebDefaults ? &YGValueAuto : &YGValueZero; } return &YGValueAuto; } @@ -3440,6 +3449,14 @@ inline bool YGConfigIsExperimentalFeatureEnabled(const YGConfigRef config, return config->experimentalFeatures[feature]; } +void YGConfigSetUseWebDefaults(const YGConfigRef config, const bool enabled) { + config->useWebDefaults = enabled; +} + +bool YGConfigGetUseWebDefaults(const YGConfigRef config) { + return config->useWebDefaults; +} + void YGSetMemoryFuncs(YGMalloc ygmalloc, YGCalloc yccalloc, YGRealloc ygrealloc, YGFree ygfree) { YG_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first"); YG_ASSERT((ygmalloc == NULL && yccalloc == NULL && ygrealloc == NULL && ygfree == NULL) || diff --git a/yoga/Yoga.h b/yoga/Yoga.h index cbcf92ee..3b3b84a9 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -235,6 +235,12 @@ WIN_EXPORT void YGConfigSetExperimentalFeatureEnabled(const YGConfigRef config, WIN_EXPORT bool YGConfigIsExperimentalFeatureEnabled(const YGConfigRef config, const YGExperimentalFeature feature); +// Using the web defaults is the prefered configuration for new projects. +// Usage of non web defaults should be considered as legacy. +WIN_EXPORT void YGConfigSetUseWebDefaults(const YGConfigRef config, const bool enabled); + +WIN_EXPORT bool YGConfigGetUseWebDefaults(const YGConfigRef config); + WIN_EXPORT void YGSetMemoryFuncs(YGMalloc ygmalloc, YGCalloc yccalloc, YGRealloc ygrealloc, YGFree ygfree);