From 9f85873c9fa010ddb903ef5e15f47b8e1685b1db Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 14 May 2018 15:43:48 -0700 Subject: [PATCH] Fabric: New props treatment in `graphics` module Summary: Same as previous one. Adopting template-generated `convertRawProp` and `debugStringConvertibleItem` functions in `graphics` module. Note, to do so we have to change signatures of some conversions functions to make them more overloading-friendly. Reviewed By: fkgozali Differential Revision: D7958252 fbshipit-source-id: 0f33a2e6aad60befacee31486acdb9b6114d3e07 --- .../attributedstring/TextAttributes.cpp | 3 +- .../core/layout/LayoutableShadowNode.cpp | 8 +-- .../fabric/core/shadownode/propsConversions.h | 11 +--- ...hicValuesConversions.cpp => conversions.h} | 64 +++++++++++-------- .../graphics/debugStringConvertibleUtils.h | 22 ------- .../graphics/graphicValuesConversions.h | 34 ---------- .../fabric/graphics/tests/GraphicsTest.cpp | 3 - .../fabric/text/basetext/BaseTextProps.cpp | 2 +- .../fabric/text/rawtext/RawTextProps.cpp | 1 - ReactCommon/fabric/view/ViewProps.cpp | 3 +- 10 files changed, 44 insertions(+), 107 deletions(-) rename ReactCommon/fabric/graphics/{graphicValuesConversions.cpp => conversions.h} (62%) delete mode 100644 ReactCommon/fabric/graphics/debugStringConvertibleUtils.h delete mode 100644 ReactCommon/fabric/graphics/graphicValuesConversions.h diff --git a/ReactCommon/fabric/attributedstring/TextAttributes.cpp b/ReactCommon/fabric/attributedstring/TextAttributes.cpp index fe6967749047f6..66e1f02b90210c 100644 --- a/ReactCommon/fabric/attributedstring/TextAttributes.cpp +++ b/ReactCommon/fabric/attributedstring/TextAttributes.cpp @@ -8,9 +8,8 @@ #include "TextAttributes.h" #include +#include #include -#include -#include namespace facebook { namespace react { diff --git a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp index a1fd9623fb86eb..5db399f373f78e 100644 --- a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp +++ b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include namespace facebook { namespace react { @@ -110,14 +110,14 @@ SharedDebugStringConvertibleList LayoutableShadowNode::getDebugProps() const { LayoutMetrics layoutMetrics = getLayoutMetrics(); LayoutMetrics defaultLayoutMetrics = LayoutMetrics(); - list.push_back(std::make_shared("frame", stringFromRect(layoutMetrics.frame))); + list.push_back(std::make_shared("frame", toString(layoutMetrics.frame))); if (layoutMetrics.borderWidth != defaultLayoutMetrics.borderWidth) { - list.push_back(std::make_shared("borderWidth", stringFromEdgeInsets(layoutMetrics.borderWidth))); + list.push_back(std::make_shared("borderWidth", toString(layoutMetrics.borderWidth))); } if (layoutMetrics.contentInsets != defaultLayoutMetrics.contentInsets) { - list.push_back(std::make_shared("contentInsets", stringFromEdgeInsets(layoutMetrics.contentInsets))); + list.push_back(std::make_shared("contentInsets", toString(layoutMetrics.contentInsets))); } if (layoutMetrics.displayType == DisplayType::None) { diff --git a/ReactCommon/fabric/core/shadownode/propsConversions.h b/ReactCommon/fabric/core/shadownode/propsConversions.h index b03bdc1387b775..240f70ce126716 100644 --- a/ReactCommon/fabric/core/shadownode/propsConversions.h +++ b/ReactCommon/fabric/core/shadownode/propsConversions.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include namespace facebook { namespace react { @@ -44,17 +44,8 @@ inline static folly::Optional convertRawProp(const RawProps &rawProps, con } \ } -CONVERT_RAW_PROP_TEMPLATE(bool, boolFromDynamic) -CONVERT_RAW_PROP_TEMPLATE(int, intFromDynamic) -CONVERT_RAW_PROP_TEMPLATE(Float, floatFromDynamic) -CONVERT_RAW_PROP_TEMPLATE(std::string, stringFromDynamic) -CONVERT_RAW_PROP_TEMPLATE(SharedColor, colorFromDynamic) -CONVERT_RAW_PROP_TEMPLATE(Point, pointFromDynamic) -CONVERT_RAW_PROP_TEMPLATE(Size, sizeFromDynamic) - inline void fromDynamic(const folly::dynamic &value, bool &result) { result = value.getBool(); } inline void fromDynamic(const folly::dynamic &value, int &result) { result = value.getInt(); } -inline void fromDynamic(const folly::dynamic &value, Float &result) { result = value.getDouble(); } inline void fromDynamic(const folly::dynamic &value, std::string &result) { result = value.getString(); } template diff --git a/ReactCommon/fabric/graphics/graphicValuesConversions.cpp b/ReactCommon/fabric/graphics/conversions.h similarity index 62% rename from ReactCommon/fabric/graphics/graphicValuesConversions.cpp rename to ReactCommon/fabric/graphics/conversions.h index 874c70b69045c0..7d0cd5b1cef5c3 100644 --- a/ReactCommon/fabric/graphics/graphicValuesConversions.cpp +++ b/ReactCommon/fabric/graphics/conversions.h @@ -5,14 +5,18 @@ * LICENSE file in the root directory of this source tree. */ -#include "graphicValuesConversions.h" +#pragma once -#include +#include +#include +#include namespace facebook { namespace react { -SharedColor colorFromDynamic(const folly::dynamic &value) { +#pragma mark - Color + +inline void fromDynamic(const folly::dynamic &value, SharedColor &result) { float red; float green; float blue; @@ -36,10 +40,10 @@ SharedColor colorFromDynamic(const folly::dynamic &value) { abort(); } - return colorFromComponents({red, green, blue, alpha}); + result = colorFromComponents({red, green, blue, alpha}); } -std::string colorNameFromColor(const SharedColor &value) { +inline std::string toString(const SharedColor &value) { ColorComponents components = colorComponentsFromColor(value); const float ratio = 256; return "rgba(" + @@ -49,19 +53,41 @@ std::string colorNameFromColor(const SharedColor &value) { folly::to(round(components.alpha * ratio)) + ")"; } -std::string stringFromPoint(const Point &point) { +#pragma mark - Geometry + +inline void fromDynamic(const folly::dynamic &value, Float &result) { + result = value.asDouble(); +} + +inline void fromDynamic(const folly::dynamic &value, Point &result) { + if (value.isArray()) { + result = Point {(Float)value[0].asDouble(), (Float)value[1].asDouble()}; + return; + } + abort(); +} + +inline void fromDynamic(const folly::dynamic &value, Size &result) { + if (value.isArray()) { + result = Size {(Float)value[0].asDouble(), (Float)value[1].asDouble()}; + return; + } + abort(); +} + +inline std::string toString(const Point &point) { return "{" + folly::to(point.x) + ", " + folly::to(point.y) + "}"; } -std::string stringFromSize(const Size &size) { +inline std::string toString(const Size &size) { return "{" + folly::to(size.width) + ", " + folly::to(size.height) + "}"; } -std::string stringFromRect(const Rect &rect) { - return "{" + stringFromPoint(rect.origin) + ", " + stringFromSize(rect.size) + "}"; +inline std::string toString(const Rect &rect) { + return "{" + toString(rect.origin) + ", " + toString(rect.size) + "}"; } -std::string stringFromEdgeInsets(const EdgeInsets &edgeInsets) { +inline std::string toString(const EdgeInsets &edgeInsets) { return "{" + folly::to(edgeInsets.left) + ", " + folly::to(edgeInsets.top) + ", " + @@ -69,23 +95,5 @@ std::string stringFromEdgeInsets(const EdgeInsets &edgeInsets) { folly::to(edgeInsets.bottom) + "}"; } -Float floatFromDynamic(const folly::dynamic &value) { - return value.asDouble(); -} - -Point pointFromDynamic(const folly::dynamic &value) { - if (value.isArray()) { - return Point {(Float)value[0].asDouble(), (Float)value[1].asDouble()}; - } - abort(); -} - -Size sizeFromDynamic(const folly::dynamic &value) { - if (value.isArray()) { - return Size {(Float)value[0].asDouble(), (Float)value[1].asDouble()}; - } - abort(); -} - } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/graphics/debugStringConvertibleUtils.h b/ReactCommon/fabric/graphics/debugStringConvertibleUtils.h deleted file mode 100644 index d67dd14cce13d0..00000000000000 --- a/ReactCommon/fabric/graphics/debugStringConvertibleUtils.h +++ /dev/null @@ -1,22 +0,0 @@ -/** - * 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 { - -DEBUG_STRING_CONVERTIBLE_TEMPLATE(Point, stringFromPoint) -DEBUG_STRING_CONVERTIBLE_TEMPLATE(Size, stringFromSize) -DEBUG_STRING_CONVERTIBLE_TEMPLATE(Rect, stringFromRect) -DEBUG_STRING_CONVERTIBLE_TEMPLATE(SharedColor, colorNameFromColor) - -} // namespace react -} // namespace facebook diff --git a/ReactCommon/fabric/graphics/graphicValuesConversions.h b/ReactCommon/fabric/graphics/graphicValuesConversions.h deleted file mode 100644 index 7d49cdf970589a..00000000000000 --- a/ReactCommon/fabric/graphics/graphicValuesConversions.h +++ /dev/null @@ -1,34 +0,0 @@ -/** - * 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 -#include - -namespace facebook { -namespace react { - -#pragma mark - Color - -SharedColor colorFromDynamic(const folly::dynamic &value); -std::string colorNameFromColor(const SharedColor &value); - -#pragma mark - Geometry - -std::string stringFromPoint(const Point &point); -std::string stringFromSize(const Size &size); -std::string stringFromRect(const Rect &rect); -std::string stringFromEdgeInsets(const EdgeInsets &edgeInsets); - -Float floatFromDynamic(const folly::dynamic &value); -Point pointFromDynamic(const folly::dynamic &value); -Size sizeFromDynamic(const folly::dynamic &value); - -} // namespace react -} // namespace facebook diff --git a/ReactCommon/fabric/graphics/tests/GraphicsTest.cpp b/ReactCommon/fabric/graphics/tests/GraphicsTest.cpp index 3b88ac8a423dee..4684d4c5007955 100644 --- a/ReactCommon/fabric/graphics/tests/GraphicsTest.cpp +++ b/ReactCommon/fabric/graphics/tests/GraphicsTest.cpp @@ -7,11 +7,8 @@ #include -#include #include -using namespace facebook::react; - TEST(GraphicsTest, testSomething) { // TODO } diff --git a/ReactCommon/fabric/text/basetext/BaseTextProps.cpp b/ReactCommon/fabric/text/basetext/BaseTextProps.cpp index d900d0ab2f5eba..2b3a1e9c79b182 100644 --- a/ReactCommon/fabric/text/basetext/BaseTextProps.cpp +++ b/ReactCommon/fabric/text/basetext/BaseTextProps.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include namespace facebook { namespace react { diff --git a/ReactCommon/fabric/text/rawtext/RawTextProps.cpp b/ReactCommon/fabric/text/rawtext/RawTextProps.cpp index 93f64c86f34977..f3945701befcd3 100644 --- a/ReactCommon/fabric/text/rawtext/RawTextProps.cpp +++ b/ReactCommon/fabric/text/rawtext/RawTextProps.cpp @@ -8,7 +8,6 @@ #include "RawTextProps.h" #include -#include #include namespace facebook { diff --git a/ReactCommon/fabric/view/ViewProps.cpp b/ReactCommon/fabric/view/ViewProps.cpp index ecd11927238dd8..0d0f3efdef41b7 100644 --- a/ReactCommon/fabric/view/ViewProps.cpp +++ b/ReactCommon/fabric/view/ViewProps.cpp @@ -9,8 +9,7 @@ #include #include -#include -#include +#include namespace facebook { namespace react {