Skip to content

Commit

Permalink
Fabric: New props treatment in graphics module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shergin authored and facebook-github-bot committed May 14, 2018
1 parent 9d21e66 commit 9f85873
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 107 deletions.
3 changes: 1 addition & 2 deletions ReactCommon/fabric/attributedstring/TextAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
#include "TextAttributes.h"

#include <fabric/attributedstring/conversions.h>
#include <fabric/graphics/conversions.h>
#include <fabric/core/debugStringConvertibleUtils.h>
#include <fabric/graphics/debugStringConvertibleUtils.h>
#include <fabric/graphics/graphicValuesConversions.h>

namespace facebook {
namespace react {
Expand Down
8 changes: 4 additions & 4 deletions ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <fabric/core/LayoutContext.h>
#include <fabric/core/LayoutMetrics.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include <fabric/graphics/conversions.h>

namespace facebook {
namespace react {
Expand Down Expand Up @@ -110,14 +110,14 @@ SharedDebugStringConvertibleList LayoutableShadowNode::getDebugProps() const {
LayoutMetrics layoutMetrics = getLayoutMetrics();
LayoutMetrics defaultLayoutMetrics = LayoutMetrics();

list.push_back(std::make_shared<DebugStringConvertibleItem>("frame", stringFromRect(layoutMetrics.frame)));
list.push_back(std::make_shared<DebugStringConvertibleItem>("frame", toString(layoutMetrics.frame)));

if (layoutMetrics.borderWidth != defaultLayoutMetrics.borderWidth) {
list.push_back(std::make_shared<DebugStringConvertibleItem>("borderWidth", stringFromEdgeInsets(layoutMetrics.borderWidth)));
list.push_back(std::make_shared<DebugStringConvertibleItem>("borderWidth", toString(layoutMetrics.borderWidth)));
}

if (layoutMetrics.contentInsets != defaultLayoutMetrics.contentInsets) {
list.push_back(std::make_shared<DebugStringConvertibleItem>("contentInsets", stringFromEdgeInsets(layoutMetrics.contentInsets)));
list.push_back(std::make_shared<DebugStringConvertibleItem>("contentInsets", toString(layoutMetrics.contentInsets)));
}

if (layoutMetrics.displayType == DisplayType::None) {
Expand Down
11 changes: 1 addition & 10 deletions ReactCommon/fabric/core/shadownode/propsConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <folly/dynamic.h>
#include <fabric/graphics/Color.h>
#include <fabric/graphics/Geometry.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include <fabric/graphics/conversions.h>

namespace facebook {
namespace react {
Expand Down Expand Up @@ -44,17 +44,8 @@ inline static folly::Optional<type> 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 <typename T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
* LICENSE file in the root directory of this source tree.
*/

#include "graphicValuesConversions.h"
#pragma once

#include <folly/Conv.h>
#include <folly/dynamic.h>
#include <fabric/graphics/Color.h>
#include <fabric/graphics/Geometry.h>

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;
Expand All @@ -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(" +
Expand All @@ -49,43 +53,47 @@ std::string colorNameFromColor(const SharedColor &value) {
folly::to<std::string>(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<std::string>(point.x) + ", " + folly::to<std::string>(point.y) + "}";
}

std::string stringFromSize(const Size &size) {
inline std::string toString(const Size &size) {
return "{" + folly::to<std::string>(size.width) + ", " + folly::to<std::string>(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<std::string>(edgeInsets.left) + ", " +
folly::to<std::string>(edgeInsets.top) + ", " +
folly::to<std::string>(edgeInsets.right) + ", " +
folly::to<std::string>(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
22 changes: 0 additions & 22 deletions ReactCommon/fabric/graphics/debugStringConvertibleUtils.h

This file was deleted.

34 changes: 0 additions & 34 deletions ReactCommon/fabric/graphics/graphicValuesConversions.h

This file was deleted.

3 changes: 0 additions & 3 deletions ReactCommon/fabric/graphics/tests/GraphicsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

#include <memory>

#include <fabric/graphics/graphicValuesConversions.h>
#include <gtest/gtest.h>

using namespace facebook::react;

TEST(GraphicsTest, testSomething) {
// TODO
}
2 changes: 1 addition & 1 deletion ReactCommon/fabric/text/basetext/BaseTextProps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <fabric/attributedstring/conversions.h>
#include <fabric/core/propsConversions.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include <fabric/graphics/conversions.h>

namespace facebook {
namespace react {
Expand Down
1 change: 0 additions & 1 deletion ReactCommon/fabric/text/rawtext/RawTextProps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "RawTextProps.h"

#include <fabric/core/propsConversions.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/debug/debugStringConvertibleUtils.h>

namespace facebook {
Expand Down
3 changes: 1 addition & 2 deletions ReactCommon/fabric/view/ViewProps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

#include <fabric/core/propsConversions.h>
#include <fabric/debug/debugStringConvertibleUtils.h>
#include <fabric/graphics/debugStringConvertibleUtils.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include <fabric/graphics/conversions.h>

namespace facebook {
namespace react {
Expand Down

0 comments on commit 9f85873

Please sign in to comment.