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
This commit is contained in:
Lukas Wöhrl 2017-03-03 10:47:42 -08:00 committed by Facebook Github Bot
parent 495fb54058
commit e596217d99
2 changed files with 28 additions and 5 deletions

View File

@ -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) ||

View File

@ -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);