From 1d65244376bfa33cf39dbc104b19b8ed1a6e29e2 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 30 Apr 2019 16:34:59 -0700 Subject: [PATCH] Changes in float-family conversion infra Summary: Different frameworks use different kinds of floats, optional floats, and floats with assigned unit names. All those approaches use different ways to represent undefined and empty values. To deal with it we need to have some helper functions. So, this diff changes some ways that we convert some corner values (like NaN and empty value). That change is motivated by recent personal discoveries in this field that shifted my vision on that. E.g. ComponentKit does not use `CGFloatMax` value as `Infinite` value. UIKit is also (surprisingly to me) okay with using `Infitite` instead of `CGFloatMax`. And, in general, seems using really conceptually appropriate values (instead of UIKit-inspired ones) it's the right thing to do. Reviewed By: mdvacca Differential Revision: D15155189 fbshipit-source-id: 33e15141f1ca3efb400a7160811224335de34ba1 --- .../fabric/components/view/conversions.h | 49 +++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/ReactCommon/fabric/components/view/conversions.h b/ReactCommon/fabric/components/view/conversions.h index c8d9b9a035f..fd1969d62f3 100644 --- a/ReactCommon/fabric/components/view/conversions.h +++ b/ReactCommon/fabric/components/view/conversions.h @@ -24,44 +24,75 @@ namespace facebook { namespace react { +/* + * Yoga's `float` <-> React Native's `Float` (can be `double` or `float`) + * + * Regular Yoga `float` values represent some onscreen-position-related values. + * They can be real numbers or special value `YGUndefined` (which actually is + * `NaN`). Conceptually, layout computation process inside Yoga should never + * produce `NaN` values from non-`NaN` values. At the same time, ` YGUndefined` + * values have special "no limit" meaning in Yoga, therefore ` YGUndefined` + * usually corresponds to `Infinity` value. + */ inline Float floatFromYogaFloat(float value) { - if (value == YGUndefined) { - return kFloatUndefined; + static_assert( + YGUndefined != YGUndefined, + "The code of this function assumes that YGUndefined is NaN."); + if (std::isnan(value) /* means: `value == YGUndefined` */) { + return std::numeric_limits::infinity(); } return (Float)value; } inline float yogaFloatFromFloat(Float value) { - if (value == kFloatUndefined) { + if (std::isinf(value)) { return YGUndefined; } return (float)value; } +/* + * `YGFloatOptional` <-> React Native's `Float` + * + * `YGFloatOptional` represents optional dimensionless float values in Yoga + * Style object (e.g. `flex`). The most suitable analogy to empty + * `YGFloatOptional` is `NaN` value. + * `YGFloatOptional` values are usually parsed from some outside data source + * which usually has some special corresponding representation for an empty + * value. + */ inline Float floatFromYogaOptionalFloat(YGFloatOptional value) { if (value.isUndefined()) { - return kFloatUndefined; + return std::numeric_limits::quiet_NaN(); } return floatFromYogaFloat(value.unwrap()); } inline YGFloatOptional yogaOptionalFloatFromFloat(Float value) { - if (value == kFloatUndefined) { + if (std::isnan(value)) { return YGFloatOptional(); } - return YGFloatOptional(yogaFloatFromFloat(value)); + return YGFloatOptional((float)value); } -inline YGValue yogaStyleValueFromFloat(const Float &value) { - if (std::isnan(value) || value == kFloatUndefined) { +/* + * `YGValue` <-> `React Native's `Float` + * + * `YGValue` represents optional dimensionful (a real number and some unit, e.g. + * pixels). + */ +inline YGValue yogaStyleValueFromFloat( + const Float &value, + YGUnit unit = YGUnitPoint) { + if (std::isnan(value)) { return YGValueUndefined; } - return {(float)value, YGUnitPoint}; + return {(float)value, unit}; } inline folly::Optional optionalFloatFromYogaValue(