From f3f378ad3d0c213f8a8e1d2a4d0460cc79cfc33f Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 14 May 2018 15:43:31 -0700 Subject: [PATCH] Fabric: Using modern props-conversion appraoch for YogaStylableProps Summary: This a natiral continue of previous/ongoing work towards modernizing props pipeline. Less defines, less code, more obvious code. Reviewed By: fkgozali Differential Revision: D7901246 fbshipit-source-id: 3387b6d13e21e6ec48a38c9e3708762dfe536105 --- ReactCommon/fabric/view/propsConversions.h | 87 ++++++++++++++++++ .../fabric/view/yoga/YogaStylableProps.cpp | 89 +------------------ 2 files changed, 91 insertions(+), 85 deletions(-) create mode 100644 ReactCommon/fabric/view/propsConversions.h diff --git a/ReactCommon/fabric/view/propsConversions.h b/ReactCommon/fabric/view/propsConversions.h new file mode 100644 index 00000000000..76d19228a91 --- /dev/null +++ b/ReactCommon/fabric/view/propsConversions.h @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include +#include + +namespace facebook { +namespace react { + +CONVERT_RAW_PROP_TEMPLATE(YGDirection, yogaStyleDirectionFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(YGFlexDirection, yogaStyleFlexDirectionFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(YGJustify, yogaStyleJustifyFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(YGAlign, yogaStyleAlignFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(YGPositionType, yogaStylePositionTypeFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(YGWrap, yogaStyleWrapFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(YGOverflow, yogaStyleOverflowFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(YGDisplay, yogaStyleDisplayFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(YGValue, yogaStyleValueFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(YGFloatOptional, yogaStyleOptionalFloatFromDynamic) + +static std::array convertRawProp(const RawProps &rawProps, const std::string &widthName, const std::string &heightName, const std::array &defaultValue) { + std::array dimentions; + dimentions[YGDimensionWidth] = convertRawProp(rawProps, widthName, defaultValue[YGDimensionWidth]); + dimentions[YGDimensionHeight] = convertRawProp(rawProps, heightName, defaultValue[YGDimensionHeight]); + return dimentions; +} + +static std::array convertRawProp(const RawProps &rawProps, const std::string &prefix, const std::array &defaultValue) { + std::array result = defaultValue; + result[YGEdgeLeft] = convertRawProp(rawProps, prefix + "Left", defaultValue[YGEdgeLeft]); + result[YGEdgeTop] = convertRawProp(rawProps, prefix + "Top", defaultValue[YGEdgeTop]); + result[YGEdgeRight] = convertRawProp(rawProps, prefix + "Right", defaultValue[YGEdgeRight]); + result[YGEdgeBottom] = convertRawProp(rawProps, prefix + "Bottom", defaultValue[YGEdgeBottom]); + result[YGEdgeStart] = convertRawProp(rawProps, prefix + "Start", defaultValue[YGEdgeStart]); + result[YGEdgeEnd] = convertRawProp(rawProps, prefix + "End", defaultValue[YGEdgeEnd]); + result[YGEdgeHorizontal] = convertRawProp(rawProps, prefix + "Horizontal", defaultValue[YGEdgeHorizontal]); + result[YGEdgeVertical] = convertRawProp(rawProps, prefix + "Vertical", defaultValue[YGEdgeVertical]); + result[YGEdgeAll] = convertRawProp(rawProps, prefix, defaultValue[YGEdgeAll]); + return result; +} + +static std::array convertRawProp(const RawProps &rawProps, const std::array &defaultValue) { + std::array result = defaultValue; + result[YGEdgeLeft] = convertRawProp(rawProps, "left", defaultValue[YGEdgeLeft]); + result[YGEdgeTop] = convertRawProp(rawProps, "top", defaultValue[YGEdgeTop]); + result[YGEdgeRight] = convertRawProp(rawProps, "right", defaultValue[YGEdgeRight]); + result[YGEdgeBottom] = convertRawProp(rawProps, "bottom", defaultValue[YGEdgeBottom]); + result[YGEdgeStart] = convertRawProp(rawProps, "start", defaultValue[YGEdgeStart]); + result[YGEdgeEnd] = convertRawProp(rawProps, "end", defaultValue[YGEdgeEnd]); + return result; +} + +static YGStyle convertRawProp(const RawProps &rawProps, const YGStyle &defaultValue) { + YGStyle yogaStyle; + yogaStyle.direction = convertRawProp(rawProps, "direction", defaultValue.direction); + yogaStyle.flexDirection = convertRawProp(rawProps, "flexDirection", defaultValue.flexDirection); + yogaStyle.justifyContent = convertRawProp(rawProps, "justifyContent", defaultValue.justifyContent); + yogaStyle.alignContent = convertRawProp(rawProps, "alignContent", defaultValue.alignContent); + yogaStyle.alignItems = convertRawProp(rawProps, "alignItems", defaultValue.alignItems); + yogaStyle.alignSelf = convertRawProp(rawProps, "alignSelf", defaultValue.alignSelf); + yogaStyle.positionType = convertRawProp(rawProps, "positionType", defaultValue.positionType); + yogaStyle.flexWrap = convertRawProp(rawProps, "flexWrap", defaultValue.flexWrap); + yogaStyle.overflow = convertRawProp(rawProps, "overflow", defaultValue.overflow); + yogaStyle.display = convertRawProp(rawProps, "display", defaultValue.display); + yogaStyle.flex = convertRawProp(rawProps, "flex", defaultValue.flex); + yogaStyle.flexGrow = convertRawProp(rawProps, "flexGrow", defaultValue.flexGrow); + yogaStyle.flexShrink = convertRawProp(rawProps, "flexShrink", defaultValue.flexShrink); + yogaStyle.flexBasis = convertRawProp(rawProps, "flexBasis", defaultValue.flexBasis); + yogaStyle.margin = convertRawProp(rawProps, "margin", defaultValue.margin); + yogaStyle.position = convertRawProp(rawProps, defaultValue.position); + yogaStyle.padding = convertRawProp(rawProps, "padding", defaultValue.padding); + yogaStyle.border = convertRawProp(rawProps, "border", defaultValue.border); + yogaStyle.dimensions = convertRawProp(rawProps, "width", "height", defaultValue.dimensions); + yogaStyle.minDimensions = convertRawProp(rawProps, "minWidth", "minHeight", defaultValue.minDimensions); + yogaStyle.maxDimensions = convertRawProp(rawProps, "maxWidth", "maxHeight", defaultValue.maxDimensions); + yogaStyle.aspectRatio = convertRawProp(rawProps, "aspectRatio", defaultValue.aspectRatio); + return yogaStyle; +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp b/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp index 2deec512699..88471868b6d 100644 --- a/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp +++ b/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp @@ -7,98 +7,17 @@ #include "YogaStylableProps.h" -#include -#include - +#include #include +#include +#include +#include #include "yogaValuesConversions.h" namespace facebook { namespace react { -static YGStyle convertRawProp(const RawProps &rawProps, const YGStyle &defaultValue) { - YGStyle yogaStyle = defaultValue; - - for (auto const &pair : rawProps) { - auto const &name = pair.first; - auto const &value = pair.second; - -#define YOGA_STYLE_PROPERTY(stringName, yogaName, accessor, convertor) \ - if (name == #stringName) { \ - yogaStyle.yogaName = convertor(value accessor); \ - continue; \ - } - -#define YOGA_STYLE_SIMPLE_FLOAT_PROPERTY(name) \ - YOGA_STYLE_PROPERTY(name, name, .asDouble(), ) - -#define YOGA_STYLE_OPTIONAL_FLOAT_PROPERTY(name) \ - YOGA_STYLE_PROPERTY(name, name, .asDouble(), yogaOptionalFloatFromFabricFloat) - -#define YOGA_STYLE_SIMPLE_INTEGER_PROPERTY(name) \ - YOGA_STYLE_PROPERTY(name, name, .asInt(), ) - -// Dimension Properties -#define YOGA_STYLE_DIMENSION_PROPERTY() \ - YOGA_STYLE_PROPERTY(width, dimensions[YGDimensionWidth], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(height, dimensions[YGDimensionHeight], , yogaStyleValueFromDynamic) - -#define YOGA_STYLE_PREFIXED_DIMENSION_PROPERTY(prefix) \ - YOGA_STYLE_PROPERTY(prefix##Width, prefix##Dimensions[YGDimensionWidth], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(prefix##Height, prefix##Dimensions[YGDimensionHeight], , yogaStyleValueFromDynamic) - -// Edge Properties -#define YOGA_STYLE_POSITION_EDGE_PROPERTY() \ - YOGA_STYLE_PROPERTY(left, position[YGEdgeLeft], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(top, position[YGEdgeTop], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(right, position[YGEdgeRight], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(bottom, position[YGEdgeBottom], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(start, position[YGEdgeStart], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(end, position[YGEdgeEnd], , yogaStyleValueFromDynamic) - -#define YOGA_STYLE_PREFIXED_EDGE_PROPERTY(prefix) \ - YOGA_STYLE_PROPERTY(prefix##Left, prefix[YGEdgeLeft], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(prefix##Top, prefix[YGEdgeTop], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(prefix##Right, prefix[YGEdgeRight], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(prefix##Bottom, prefix[YGEdgeBottom], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(prefix##Start, prefix[YGEdgeStart], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(prefix##End, prefix[YGEdgeEnd], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(prefix##Horizontal, prefix[YGEdgeHorizontal], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(prefix##Vertical, prefix[YGEdgeVertical], , yogaStyleValueFromDynamic) \ - YOGA_STYLE_PROPERTY(prefix, prefix[YGEdgeAll], , yogaStyleValueFromDynamic) - - YOGA_STYLE_PROPERTY(direction, direction, , yogaStyleDirectionFromDynamic) - YOGA_STYLE_PROPERTY(flexDirection, flexDirection, , yogaStyleFlexDirectionFromDynamic) - YOGA_STYLE_PROPERTY(justifyContent, justifyContent, , yogaStyleJustifyFromDynamic) - YOGA_STYLE_PROPERTY(alignContent, alignContent, , yogaStyleAlignFromDynamic) - YOGA_STYLE_PROPERTY(alignItems, alignItems, , yogaStyleAlignFromDynamic) - YOGA_STYLE_PROPERTY(alignSelf, alignSelf, , yogaStyleAlignFromDynamic) - YOGA_STYLE_PROPERTY(positionType, positionType, , yogaStylePositionTypeFromDynamic) - YOGA_STYLE_PROPERTY(flexWrap, flexWrap, , yogaStyleWrapFromDynamic) - YOGA_STYLE_PROPERTY(overflow, overflow, , yogaStyleOverflowFromDynamic) - YOGA_STYLE_PROPERTY(display, display, , yogaStyleDisplayFromDynamic) - - YOGA_STYLE_OPTIONAL_FLOAT_PROPERTY(flex) - YOGA_STYLE_OPTIONAL_FLOAT_PROPERTY(flexGrow) - YOGA_STYLE_OPTIONAL_FLOAT_PROPERTY(flexShrink) - YOGA_STYLE_PROPERTY(flexBasis, flexBasis, , yogaStyleValueFromDynamic) - - YOGA_STYLE_DIMENSION_PROPERTY() - YOGA_STYLE_PREFIXED_DIMENSION_PROPERTY(min) - YOGA_STYLE_PREFIXED_DIMENSION_PROPERTY(max) - - YOGA_STYLE_POSITION_EDGE_PROPERTY() - YOGA_STYLE_PREFIXED_EDGE_PROPERTY(margin) - YOGA_STYLE_PREFIXED_EDGE_PROPERTY(padding) - YOGA_STYLE_PREFIXED_EDGE_PROPERTY(border) - - YOGA_STYLE_OPTIONAL_FLOAT_PROPERTY(aspectRatio) - } - - return yogaStyle; -} - YogaStylableProps::YogaStylableProps(const YGStyle &yogaStyle): yogaStyle(yogaStyle) {}