diff --git a/React/Fabric/Mounting/RCTParagraphComponentView.mm b/React/Fabric/Mounting/RCTParagraphComponentView.mm index 29175e9f5..770913113 100644 --- a/React/Fabric/Mounting/RCTParagraphComponentView.mm +++ b/React/Fabric/Mounting/RCTParagraphComponentView.mm @@ -39,7 +39,7 @@ using namespace facebook::react; [super updateProps:props oldProps:oldProps]; auto paragraphProps = std::static_pointer_cast(props); assert(paragraphProps); - _paragraphAttributes = paragraphProps->getParagraphAttributes(); + _paragraphAttributes = paragraphProps->paragraphAttributes; } - (void)updateLocalData:(SharedLocalData)localData diff --git a/React/Fabric/Mounting/RCTViewComponentView.mm b/React/Fabric/Mounting/RCTViewComponentView.mm index 26fff3a50..9183968b5 100644 --- a/React/Fabric/Mounting/RCTViewComponentView.mm +++ b/React/Fabric/Mounting/RCTViewComponentView.mm @@ -24,8 +24,8 @@ using namespace facebook::react; auto oldViewProps = *std::dynamic_pointer_cast(oldProps); auto newViewProps = *std::dynamic_pointer_cast(props); - if (oldViewProps.getBackgroundColor() != newViewProps.getBackgroundColor()) { - self.backgroundColor = [UIColor colorWithCGColor:newViewProps.getBackgroundColor().get()]; + if (oldViewProps.backgroundColor != newViewProps.backgroundColor) { + self.backgroundColor = [UIColor colorWithCGColor:newViewProps.backgroundColor.get()]; } // TODO: Implement all sutable non-layout props. diff --git a/ReactCommon/fabric/core/shadownode/ConcreteShadowNode.h b/ReactCommon/fabric/core/shadownode/ConcreteShadowNode.h index 06a1afa7c..93663a325 100644 --- a/ReactCommon/fabric/core/shadownode/ConcreteShadowNode.h +++ b/ReactCommon/fabric/core/shadownode/ConcreteShadowNode.h @@ -28,22 +28,12 @@ public: using SharedConcreteProps = std::shared_ptr; using SharedConcreteShadowNode = std::shared_ptr; - static const SharedConcreteProps Props(const RawProps &rawProps, const SharedProps &baseProps = nullptr) { - if (!baseProps) { - auto props = std::make_shared(); - props->apply(rawProps); - return props; - } - - auto concreteBaseProps = std::dynamic_pointer_cast(baseProps); - assert(concreteBaseProps); - auto props = std::make_shared(*concreteBaseProps); - props->apply(rawProps); - return props; + static SharedConcreteProps Props(const RawProps &rawProps, const SharedProps &baseProps = nullptr) { + return std::make_shared(baseProps ? *std::static_pointer_cast(baseProps) : PropsT(), rawProps); } - static const SharedConcreteProps defaultSharedProps() { - static const SharedConcreteProps defaultSharedProps = std::make_shared(); + static SharedConcreteProps defaultSharedProps() { + static const SharedConcreteProps defaultSharedProps = std::make_shared(); return defaultSharedProps; } diff --git a/ReactCommon/fabric/core/shadownode/Props.cpp b/ReactCommon/fabric/core/shadownode/Props.cpp index 688b288fd..f70632d47 100644 --- a/ReactCommon/fabric/core/shadownode/Props.cpp +++ b/ReactCommon/fabric/core/shadownode/Props.cpp @@ -13,15 +13,8 @@ namespace facebook { namespace react { -void Props::apply(const RawProps &rawProps) { - ensureUnsealed(); - - applyRawProp(rawProps, "nativeID", nativeId_); -} - -const std::string &Props::getNativeId() const { - return nativeId_; -} +Props::Props(const Props &sourceProps, const RawProps &rawProps): + nativeId(convertRawProp(rawProps, "nativeID", sourceProps.nativeId)) {}; } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/core/shadownode/Props.h b/ReactCommon/fabric/core/shadownode/Props.h index d7ecea742..1e349020a 100644 --- a/ReactCommon/fabric/core/shadownode/Props.h +++ b/ReactCommon/fabric/core/shadownode/Props.h @@ -28,12 +28,10 @@ class Props: public virtual DebugStringConvertible { public: - virtual void apply(const RawProps &rawProps); + Props() = default; + Props(const Props &sourceProps, const RawProps &rawProps); - const std::string &getNativeId() const; - -private: - std::string nativeId_ {""}; + const std::string nativeId; }; } // namespace react diff --git a/ReactCommon/fabric/core/shadownode/propsConversions.h b/ReactCommon/fabric/core/shadownode/propsConversions.h index 689caf0ed..89d1808fe 100644 --- a/ReactCommon/fabric/core/shadownode/propsConversions.h +++ b/ReactCommon/fabric/core/shadownode/propsConversions.h @@ -20,33 +20,37 @@ inline int intFromDynamic(const folly::dynamic &value) { return value.getInt(); inline Float floatFromDynamic(const folly::dynamic &value) { return value.getDouble(); } inline std::string stringFromDynamic(const folly::dynamic &value) { return value.getString(); } -#define APPLY_RAW_PROP_TEMPLATE(type, converter) \ -inline static void applyRawProp(const RawProps &rawProps, const std::string &name, type &property) { \ +#define CONVERT_RAW_PROP_TEMPLATE(type, converter) \ +inline static type convertRawProp(const RawProps &rawProps, const std::string &name, const type &defaultValue) { \ auto &&iterator = rawProps.find(name); \ if (iterator != rawProps.end()) { \ - property = converter(iterator->second); \ + return converter(iterator->second); \ + } else { \ + return defaultValue; \ } \ } \ \ -inline static void applyRawProp(const RawProps &rawProps, const std::string &name, folly::Optional &property) { \ +inline static folly::Optional convertRawProp(const RawProps &rawProps, const std::string &name, const folly::Optional &defaultValue) { \ auto &&iterator = rawProps.find(name); \ if (iterator != rawProps.end()) { \ auto &&value = iterator->second; \ if (value.isNull()) { \ - property = {}; \ + return {}; \ } else { \ - property = converter(value); \ + return converter(value); \ } \ + } else { \ + return defaultValue; \ } \ } -APPLY_RAW_PROP_TEMPLATE(bool, boolFromDynamic) -APPLY_RAW_PROP_TEMPLATE(int, intFromDynamic) -APPLY_RAW_PROP_TEMPLATE(Float, floatFromDynamic) -APPLY_RAW_PROP_TEMPLATE(std::string, stringFromDynamic) -APPLY_RAW_PROP_TEMPLATE(SharedColor, colorFromDynamic) -APPLY_RAW_PROP_TEMPLATE(Point, pointFromDynamic) -APPLY_RAW_PROP_TEMPLATE(Size, sizeFromDynamic) +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) } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp b/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp index 460a3124b..f297d3800 100644 --- a/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp +++ b/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp @@ -26,7 +26,7 @@ TEST(ComponentDescriptorTest, createShadowNode) { ASSERT_STREQ(node->getComponentName().c_str(), "Test"); ASSERT_EQ(node->getTag(), 9); ASSERT_EQ(node->getRootTag(), 1); - ASSERT_STREQ(node->getProps()->getNativeId().c_str(), "abc"); + ASSERT_STREQ(node->getProps()->nativeId.c_str(), "abc"); } TEST(ComponentDescriptorTest, cloneShadowNode) { @@ -42,7 +42,7 @@ TEST(ComponentDescriptorTest, cloneShadowNode) { ASSERT_STREQ(cloned->getComponentName().c_str(), "Test"); ASSERT_EQ(cloned->getTag(), 9); ASSERT_EQ(cloned->getRootTag(), 1); - ASSERT_STREQ(cloned->getProps()->getNativeId().c_str(), "abc"); + ASSERT_STREQ(cloned->getProps()->nativeId.c_str(), "abc"); } TEST(ComponentDescriptorTest, appendChild) { diff --git a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp index 7a945bf40..3395763d9 100644 --- a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp +++ b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp @@ -19,13 +19,12 @@ TEST(ShadowNodeTest, handleProps) { RawProps raw; raw["nativeID"] = "abc"; - auto props = std::make_shared(); - props->apply(raw); + auto props = std::make_shared(Props(), raw); // Props are not sealed after applying raw props. ASSERT_FALSE(props->getSealed()); - ASSERT_STREQ(props->getNativeId().c_str(), "abc"); + ASSERT_STREQ(props->nativeId.c_str(), "abc"); } TEST(ShadowNodeTest, handleShadowNodeCreation) { @@ -41,7 +40,7 @@ TEST(ShadowNodeTest, handleShadowNodeCreation) { ASSERT_EQ(node->getSourceNode(), nullptr); ASSERT_EQ(node->getChildren()->size(), 0); - ASSERT_STREQ(node->getProps()->getNativeId().c_str(), "testNativeID"); + ASSERT_STREQ(node->getProps()->nativeId.c_str(), "testNativeID"); node->sealRecursive(); ASSERT_TRUE(node->getSealed()); diff --git a/ReactCommon/fabric/core/tests/TestComponent.h b/ReactCommon/fabric/core/tests/TestComponent.h index 87372ffea..b7257caab 100644 --- a/ReactCommon/fabric/core/tests/TestComponent.h +++ b/ReactCommon/fabric/core/tests/TestComponent.h @@ -13,6 +13,7 @@ #include #include #include +#include using namespace facebook::react; @@ -37,11 +38,9 @@ private: class TestProps : public Props { public: - TestProps() { - RawProps raw; - raw["nativeID"] = "testNativeID"; - apply(raw); - } + using Props::Props; + TestProps(): + Props(Props(), {{"nativeID", "testNativeID"}}) {} }; using SharedTestProps = std::shared_ptr; diff --git a/ReactCommon/fabric/text/basetext/BaseTextProps.cpp b/ReactCommon/fabric/text/basetext/BaseTextProps.cpp index 5b03c924d..736474355 100644 --- a/ReactCommon/fabric/text/basetext/BaseTextProps.cpp +++ b/ReactCommon/fabric/text/basetext/BaseTextProps.cpp @@ -16,52 +16,53 @@ namespace facebook { namespace react { -void BaseTextProps::apply(const RawProps &rawProps) { +static TextAttributes convertRawProp(const RawProps &rawProps, const TextAttributes defaultTextAttributes) { + TextAttributes textAttributes; + // Color - applyRawProp(rawProps, "color", textAttributes_.foregroundColor); - applyRawProp(rawProps, "backgroundColor", textAttributes_.backgroundColor); - applyRawProp(rawProps, "opacity", textAttributes_.opacity); + textAttributes.foregroundColor = convertRawProp(rawProps, "color", defaultTextAttributes.foregroundColor); + textAttributes.backgroundColor = convertRawProp(rawProps, "backgroundColor", defaultTextAttributes.backgroundColor); + textAttributes.opacity = convertRawProp(rawProps, "opacity", defaultTextAttributes.opacity); // Font - applyRawProp(rawProps, "fontFamily", textAttributes_.fontFamily); - applyRawProp(rawProps, "fontSize", textAttributes_.fontSize); - applyRawProp(rawProps, "fontSizeMultiplier", textAttributes_.fontSizeMultiplier); - applyRawProp(rawProps, "fontWeight", textAttributes_.fontWeight); - applyRawProp(rawProps, "fontStyle", textAttributes_.fontStyle); - applyRawProp(rawProps, "fontVariant", textAttributes_.fontVariant); - applyRawProp(rawProps, "allowFontScaling", textAttributes_.allowFontScaling); - applyRawProp(rawProps, "letterSpacing", textAttributes_.letterSpacing); + textAttributes.fontFamily = convertRawProp(rawProps, "fontFamily", defaultTextAttributes.fontFamily); + textAttributes.fontSize = convertRawProp(rawProps, "fontSize", defaultTextAttributes.fontSize); + textAttributes.fontSizeMultiplier = convertRawProp(rawProps, "fontSizeMultiplier", defaultTextAttributes.fontSizeMultiplier); + textAttributes.fontWeight = convertRawProp(rawProps, "fontWeight", defaultTextAttributes.fontWeight); + textAttributes.fontStyle = convertRawProp(rawProps, "fontStyle", defaultTextAttributes.fontStyle); + textAttributes.fontVariant = convertRawProp(rawProps, "fontVariant", defaultTextAttributes.fontVariant); + textAttributes.allowFontScaling = convertRawProp(rawProps, "allowFontScaling", defaultTextAttributes.allowFontScaling); + textAttributes.letterSpacing = convertRawProp(rawProps, "letterSpacing", defaultTextAttributes.letterSpacing); // Paragraph - applyRawProp(rawProps, "lineHeight", textAttributes_.lineHeight); - applyRawProp(rawProps, "alignment", textAttributes_.alignment); - applyRawProp(rawProps, "baseWritingDirection", textAttributes_.baseWritingDirection); + textAttributes.lineHeight = convertRawProp(rawProps, "lineHeight", defaultTextAttributes.lineHeight); + textAttributes.alignment = convertRawProp(rawProps, "alignment", defaultTextAttributes.alignment); + textAttributes.baseWritingDirection = convertRawProp(rawProps, "baseWritingDirection", defaultTextAttributes.baseWritingDirection); // Decoration - applyRawProp(rawProps, "textDecorationColor", textAttributes_.textDecorationColor); - applyRawProp(rawProps, "textDecorationLineType", textAttributes_.textDecorationLineType); - applyRawProp(rawProps, "textDecorationLineStyle", textAttributes_.textDecorationLineStyle); - applyRawProp(rawProps, "textDecorationLinePattern", textAttributes_.textDecorationLinePattern); + textAttributes.textDecorationColor = convertRawProp(rawProps, "textDecorationColor", defaultTextAttributes.textDecorationColor); + textAttributes.textDecorationLineType = convertRawProp(rawProps, "textDecorationLineType", defaultTextAttributes.textDecorationLineType); + textAttributes.textDecorationLineStyle = convertRawProp(rawProps, "textDecorationLineStyle", defaultTextAttributes.textDecorationLineStyle); + textAttributes.textDecorationLinePattern = convertRawProp(rawProps, "textDecorationLinePattern", defaultTextAttributes.textDecorationLinePattern); // Shadow - applyRawProp(rawProps, "textShadowOffset", textAttributes_.textShadowOffset); - applyRawProp(rawProps, "textShadowRadius", textAttributes_.textShadowRadius); - applyRawProp(rawProps, "textShadowColor", textAttributes_.textShadowColor); + textAttributes.textShadowOffset = convertRawProp(rawProps, "textShadowOffset", defaultTextAttributes.textShadowOffset); + textAttributes.textShadowRadius = convertRawProp(rawProps, "textShadowRadius", defaultTextAttributes.textShadowRadius); + textAttributes.textShadowColor = convertRawProp(rawProps, "textShadowColor", defaultTextAttributes.textShadowColor); // Special - applyRawProp(rawProps, "isHighlighted", textAttributes_.isHighlighted); + textAttributes.isHighlighted = convertRawProp(rawProps, "isHighlighted", defaultTextAttributes.isHighlighted); + + return textAttributes; } -#pragma mark - Getters - -TextAttributes BaseTextProps::getTextAttributes() const { - return textAttributes_; -} +BaseTextProps::BaseTextProps(const BaseTextProps &sourceProps, const RawProps &rawProps): + textAttributes(convertRawProp(rawProps, sourceProps.textAttributes)) {}; #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList BaseTextProps::getDebugProps() const { - return textAttributes_.getDebugProps(); + return textAttributes.getDebugProps(); } } // namespace react diff --git a/ReactCommon/fabric/text/basetext/BaseTextProps.h b/ReactCommon/fabric/text/basetext/BaseTextProps.h index 9d3909a3b..6e5026a59 100644 --- a/ReactCommon/fabric/text/basetext/BaseTextProps.h +++ b/ReactCommon/fabric/text/basetext/BaseTextProps.h @@ -22,27 +22,17 @@ namespace react { class BaseTextProps { public: - /* - * Same semantic as `Props::apply(...)`. - */ - void apply(const RawProps &rawProps); + BaseTextProps() = default; + BaseTextProps(const BaseTextProps &sourceProps, const RawProps &rawProps); -#pragma mark - Getters +#pragma mark - Props - /* - * Returns all props values as `TextAttributes` object. - */ - TextAttributes getTextAttributes() const; + const TextAttributes textAttributes {}; #pragma mark - DebugStringConvertible (partially) SharedDebugStringConvertibleList getDebugProps() const; - -private: - - TextAttributes textAttributes_; }; } // namespace react } // namespace facebook - diff --git a/ReactCommon/fabric/text/basetext/BaseTextShadowNode.cpp b/ReactCommon/fabric/text/basetext/BaseTextShadowNode.cpp index f37f534b8..d812d242c 100644 --- a/ReactCommon/fabric/text/basetext/BaseTextShadowNode.cpp +++ b/ReactCommon/fabric/text/basetext/BaseTextShadowNode.cpp @@ -30,7 +30,7 @@ AttributedString BaseTextShadowNode::getAttributedString( SharedRawTextShadowNode rawTextShadowNode = std::dynamic_pointer_cast(childNode); if (rawTextShadowNode) { AttributedString::Fragment fragment; - fragment.string = rawTextShadowNode->getProps()->getText(); + fragment.string = rawTextShadowNode->getProps()->text; fragment.textAttributes = textAttributes; attributedString.appendFragment(fragment); continue; @@ -40,7 +40,7 @@ AttributedString BaseTextShadowNode::getAttributedString( SharedTextShadowNode textShadowNode = std::dynamic_pointer_cast(childNode); if (textShadowNode) { TextAttributes localTextAttributes = textAttributes; - localTextAttributes.apply(textShadowNode->getProps()->getTextAttributes()); + localTextAttributes.apply(textShadowNode->getProps()->textAttributes); attributedString.appendAttributedString(textShadowNode->getAttributedString(localTextAttributes, textShadowNode->getChildren())); continue; } diff --git a/ReactCommon/fabric/text/paragraph/ParagraphProps.cpp b/ReactCommon/fabric/text/paragraph/ParagraphProps.cpp index a4481e138..628ee1eff 100644 --- a/ReactCommon/fabric/text/paragraph/ParagraphProps.cpp +++ b/ReactCommon/fabric/text/paragraph/ParagraphProps.cpp @@ -15,38 +15,34 @@ namespace facebook { namespace react { -void ParagraphProps::apply(const RawProps &rawProps) { - ViewProps::apply(rawProps); - BaseTextProps::apply(rawProps); +static ParagraphAttributes convertRawProp(const RawProps &rawProps, const ParagraphAttributes &defaultParagraphAttributes) { + ParagraphAttributes paragraphAttributes; - // Paragraph Attributes - applyRawProp(rawProps, "numberOfLines", paragraphAttributes_.maximumNumberOfLines); - applyRawProp(rawProps, "ellipsizeMode", paragraphAttributes_.ellipsizeMode); - applyRawProp(rawProps, "adjustsFontSizeToFit", paragraphAttributes_.adjustsFontSizeToFit); - applyRawProp(rawProps, "minimumFontSize", paragraphAttributes_.minimumFontSize); - applyRawProp(rawProps, "maximumFontSize", paragraphAttributes_.maximumFontSize); + paragraphAttributes.maximumNumberOfLines = convertRawProp(rawProps, "numberOfLines", defaultParagraphAttributes.maximumNumberOfLines); + paragraphAttributes.ellipsizeMode = convertRawProp(rawProps, "ellipsizeMode", defaultParagraphAttributes.ellipsizeMode); + paragraphAttributes.adjustsFontSizeToFit = convertRawProp(rawProps, "adjustsFontSizeToFit", defaultParagraphAttributes.adjustsFontSizeToFit); + paragraphAttributes.minimumFontSize = convertRawProp(rawProps, "minimumFontSize", defaultParagraphAttributes.minimumFontSize); + paragraphAttributes.maximumFontSize = convertRawProp(rawProps, "maximumFontSize", defaultParagraphAttributes.maximumFontSize); - // Other Props - applyRawProp(rawProps, "selectable", isSelectable_); + return paragraphAttributes; } -#pragma mark - Getters - -ParagraphAttributes ParagraphProps::getParagraphAttributes() const { - return paragraphAttributes_; -} - -bool ParagraphProps::getIsSelectable() const { - return isSelectable_; -} +ParagraphProps::ParagraphProps(const ParagraphProps &sourceProps, const RawProps &rawProps): + ViewProps(sourceProps, rawProps), + BaseTextProps(sourceProps, rawProps), + paragraphAttributes(convertRawProp(rawProps, sourceProps.paragraphAttributes)), + isSelectable(convertRawProp(rawProps, "selectable", sourceProps.isSelectable)) {}; #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList ParagraphProps::getDebugProps() const { return ViewProps::getDebugProps() + - paragraphAttributes_.getDebugProps() + - BaseTextProps::getDebugProps(); + BaseTextProps::getDebugProps() + + paragraphAttributes.getDebugProps() + + SharedDebugStringConvertibleList { + debugStringConvertibleItem("isSelectable", isSelectable) + }; } } // namespace react diff --git a/ReactCommon/fabric/text/paragraph/ParagraphProps.h b/ReactCommon/fabric/text/paragraph/ParagraphProps.h index 4918f47b9..a31e1c358 100644 --- a/ReactCommon/fabric/text/paragraph/ParagraphProps.h +++ b/ReactCommon/fabric/text/paragraph/ParagraphProps.h @@ -32,30 +32,25 @@ class ParagraphProps: public BaseTextProps { public: + ParagraphProps() = default; + ParagraphProps(const ParagraphProps &sourceProps, const RawProps &rawProps); - void apply(const RawProps &rawProps) override; - -#pragma mark - Getters +#pragma mark - Props /* - * Returns `ParagraphAttributes` object which has all prop values that affect - * visual representation of the paragraph. + * Contains all prop values that affect visual representation of the paragraph. */ - ParagraphAttributes getParagraphAttributes() const; + const ParagraphAttributes paragraphAttributes {}; /* * Defines can the text be selected (and copied) or not. */ - bool getIsSelectable() const; + const bool isSelectable {false}; #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList getDebugProps() const override; -private: - - ParagraphAttributes paragraphAttributes_ {}; - bool isSelectable_ {false}; }; } // namespace react diff --git a/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.cpp b/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.cpp index 57d13d7ed..e7b6513ba 100644 --- a/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.cpp +++ b/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.cpp @@ -19,7 +19,7 @@ ComponentName ParagraphShadowNode::getComponentName() const { } AttributedString ParagraphShadowNode::getAttributedString() const { - return BaseTextShadowNode::getAttributedString(getProps()->getTextAttributes(), getChildren()); + return BaseTextShadowNode::getAttributedString(getProps()->textAttributes, getChildren()); } void ParagraphShadowNode::setTextLayoutManager(SharedTextLayoutManager textLayoutManager) { @@ -41,7 +41,7 @@ void ParagraphShadowNode::updateLocalData() { Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const { return textLayoutManager_->measure( getAttributedString(), - getProps()->getParagraphAttributes(), + getProps()->paragraphAttributes, layoutConstraints ); } diff --git a/ReactCommon/fabric/text/propsConversions.h b/ReactCommon/fabric/text/propsConversions.h index 1656e82af..b8362947f 100644 --- a/ReactCommon/fabric/text/propsConversions.h +++ b/ReactCommon/fabric/text/propsConversions.h @@ -15,15 +15,15 @@ namespace facebook { namespace react { -APPLY_RAW_PROP_TEMPLATE(EllipsizeMode, ellipsizeModeFromDynamic) -APPLY_RAW_PROP_TEMPLATE(FontWeight, fontWeightFromDynamic) -APPLY_RAW_PROP_TEMPLATE(FontStyle, fontStyleFromDynamic) -APPLY_RAW_PROP_TEMPLATE(FontVariant, fontVariantFromDynamic) -APPLY_RAW_PROP_TEMPLATE(WritingDirection, writingDirectionFromDynamic) -APPLY_RAW_PROP_TEMPLATE(TextAlignment, textAlignmentFromDynamic) -APPLY_RAW_PROP_TEMPLATE(TextDecorationLineType, textDecorationLineTypeFromDynamic) -APPLY_RAW_PROP_TEMPLATE(TextDecorationLineStyle, textDecorationLineStyleFromDynamic) -APPLY_RAW_PROP_TEMPLATE(TextDecorationLinePattern, textDecorationLinePatternFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(EllipsizeMode, ellipsizeModeFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(FontWeight, fontWeightFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(FontStyle, fontStyleFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(FontVariant, fontVariantFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(WritingDirection, writingDirectionFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(TextAlignment, textAlignmentFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(TextDecorationLineType, textDecorationLineTypeFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(TextDecorationLineStyle, textDecorationLineStyleFromDynamic) +CONVERT_RAW_PROP_TEMPLATE(TextDecorationLinePattern, textDecorationLinePatternFromDynamic) } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/text/rawtext/RawTextProps.cpp b/ReactCommon/fabric/text/rawtext/RawTextProps.cpp index d985188c9..93f64c86f 100644 --- a/ReactCommon/fabric/text/rawtext/RawTextProps.cpp +++ b/ReactCommon/fabric/text/rawtext/RawTextProps.cpp @@ -9,28 +9,21 @@ #include #include +#include namespace facebook { namespace react { -void RawTextProps::apply(const RawProps &rawProps) { - Props::apply(rawProps); - - applyRawProp(rawProps, "text", text_); -} - -#pragma mark - Getters - -std::string RawTextProps::getText() const { - return text_; -} +RawTextProps::RawTextProps(const RawTextProps &sourceProps, const RawProps &rawProps): + Props(sourceProps, rawProps), + text(convertRawProp(rawProps, "text", sourceProps.text)) {}; #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList RawTextProps::getDebugProps() const { - SharedDebugStringConvertibleList list = {}; - list.push_back(std::make_shared("text", text_)); - return list; + return { + debugStringConvertibleItem("text", text) + }; } } // namespace react diff --git a/ReactCommon/fabric/text/rawtext/RawTextProps.h b/ReactCommon/fabric/text/rawtext/RawTextProps.h index 4dd84f8fb..37ba902ee 100644 --- a/ReactCommon/fabric/text/rawtext/RawTextProps.h +++ b/ReactCommon/fabric/text/rawtext/RawTextProps.h @@ -24,19 +24,16 @@ class RawTextProps: public: - void apply(const RawProps &rawProps) override; + RawTextProps() = default; + RawTextProps(const RawTextProps &sourceProps, const RawProps &rawProps); -#pragma mark - Getters +#pragma mark - Props - std::string getText() const; + const std::string text {""}; #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList getDebugProps() const override; - -private: - - std::string text_ {""}; }; } // namespace react diff --git a/ReactCommon/fabric/text/text/TextProps.cpp b/ReactCommon/fabric/text/text/TextProps.cpp index c752a5f0e..5e8a3e1c6 100644 --- a/ReactCommon/fabric/text/text/TextProps.cpp +++ b/ReactCommon/fabric/text/text/TextProps.cpp @@ -16,10 +16,8 @@ namespace facebook { namespace react { -void TextProps::apply(const RawProps &rawProps) { - Props::apply(rawProps); - BaseTextProps::apply(rawProps); -} +TextProps::TextProps(const TextProps &sourceProps, const RawProps &rawProps): + BaseTextProps::BaseTextProps(sourceProps, rawProps) {}; #pragma mark - DebugStringConvertible diff --git a/ReactCommon/fabric/text/text/TextProps.h b/ReactCommon/fabric/text/text/TextProps.h index 8762642bb..b42050731 100644 --- a/ReactCommon/fabric/text/text/TextProps.h +++ b/ReactCommon/fabric/text/text/TextProps.h @@ -25,7 +25,9 @@ class TextProps: public BaseTextProps { public: - void apply(const RawProps &rawProps) override; + + TextProps() = default; + TextProps(const TextProps &sourceProps, const RawProps &rawProps); #pragma mark - DebugStringConvertible diff --git a/ReactCommon/fabric/uimanager/ShadowTree.cpp b/ReactCommon/fabric/uimanager/ShadowTree.cpp index 66d2e2306..32a7870d9 100644 --- a/ReactCommon/fabric/uimanager/ShadowTree.cpp +++ b/ReactCommon/fabric/uimanager/ShadowTree.cpp @@ -44,9 +44,7 @@ void ShadowTree::constraintLayout(const LayoutConstraints &layoutConstraints, co UnsharedRootShadowNode ShadowTree::cloneRootShadowNode(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const { auto oldRootShadowNode = rootShadowNode_; - auto &&props = std::make_shared(*oldRootShadowNode->getProps()); - props->applyLayoutConstraints(layoutConstraints); - props->applyLayoutContext(layoutContext); + auto &&props = std::make_shared(*oldRootShadowNode->getProps(), layoutConstraints, layoutContext); auto newRootShadowNode = std::make_shared(oldRootShadowNode, props, nullptr); return newRootShadowNode; } diff --git a/ReactCommon/fabric/view/ViewProps.cpp b/ReactCommon/fabric/view/ViewProps.cpp index e49de1a6f..ecd119272 100644 --- a/ReactCommon/fabric/view/ViewProps.cpp +++ b/ReactCommon/fabric/view/ViewProps.cpp @@ -7,59 +7,37 @@ #include "ViewProps.h" -#include -#include #include +#include +#include +#include namespace facebook { namespace react { -void ViewProps::apply(const RawProps &rawProps) { - Props::apply(rawProps); - YogaStylableProps::apply(rawProps); +ViewProps::ViewProps(const YGStyle &yogaStyle): + YogaStylableProps(yogaStyle) {} - applyRawProp(rawProps, "zIndex", zIndex_); - applyRawProp(rawProps, "opacity", opacity_); - applyRawProp(rawProps, "color", foregroundColor_); - applyRawProp(rawProps, "backgroundColor", backgroundColor_); -} - -#pragma mark - Getters - -SharedColor ViewProps::getForegroundColor() const { - return foregroundColor_; -} - -SharedColor ViewProps::getBackgroundColor() const { - return backgroundColor_; -} +ViewProps::ViewProps(const ViewProps &sourceProps, const RawProps &rawProps): + Props(sourceProps, rawProps), + YogaStylableProps(sourceProps, rawProps), + zIndex(convertRawProp(rawProps, "zIndex", sourceProps.zIndex)), + opacity(convertRawProp(rawProps, "opacity", sourceProps.opacity)), + foregroundColor(convertRawProp(rawProps, "color", sourceProps.foregroundColor)), + backgroundColor(convertRawProp(rawProps, "backgroundColor", sourceProps.backgroundColor)) {}; #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList ViewProps::getDebugProps() const { - ViewProps defaultProps = {}; - - SharedDebugStringConvertibleList list = {}; - -#define VIEW_PROPS_ADD_TO_SET(stringName, propertyName, accessor, convertor) \ - if (propertyName != defaultProps.propertyName) { \ - list.push_back(std::make_shared(#stringName, convertor(propertyName accessor))); \ - } - - VIEW_PROPS_ADD_TO_SET(zIndex, zIndex_, , std::to_string) - VIEW_PROPS_ADD_TO_SET(opacity, opacity_, , std::to_string) - - VIEW_PROPS_ADD_TO_SET(backgroundColor, backgroundColor_, , colorNameFromColor) - VIEW_PROPS_ADD_TO_SET(foregroundColor, foregroundColor_, , colorNameFromColor) - - // Accessibility Props - auto accessibilityPropsList = AccessibilityProps::getDebugProps(); - std::move(accessibilityPropsList.begin(), accessibilityPropsList.end(), std::back_inserter(list)); - - // Yoga styles - list.push_back(std::make_shared("style", "", YogaStylableProps::getDebugProps())); - - return list; + return + AccessibilityProps::getDebugProps() + + YogaStylableProps::getDebugProps() + + SharedDebugStringConvertibleList { + debugStringConvertibleItem("zIndex", zIndex), + debugStringConvertibleItem("opacity", opacity), + debugStringConvertibleItem("foregroundColor", foregroundColor), + debugStringConvertibleItem("backgroundColor", backgroundColor), + }; } } // namespace react diff --git a/ReactCommon/fabric/view/ViewProps.h b/ReactCommon/fabric/view/ViewProps.h index 4aceb7f11..b0fa20369 100644 --- a/ReactCommon/fabric/view/ViewProps.h +++ b/ReactCommon/fabric/view/ViewProps.h @@ -26,28 +26,26 @@ class ViewProps: public AccessibilityProps { public: - void apply(const RawProps &rawProps) override; -#pragma mark - Getters + ViewProps() = default; + ViewProps(const YGStyle &yogaStyle); + ViewProps(const ViewProps &sourceProps, const RawProps &rawProps); - SharedColor getForegroundColor() const; - SharedColor getBackgroundColor() const; +#pragma mark - Props + + const int zIndex {0}; + const Float opacity {1}; + + const SharedColor foregroundColor {nullptr}; + const SharedColor backgroundColor {nullptr}; + + const SharedColor shadowColor {nullptr}; + const Point shadowOffset {}; #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList getDebugProps() const override; - -private: - int zIndex_ {0}; - Float opacity_ {1.0}; - - SharedColor foregroundColor_ {nullptr}; - SharedColor backgroundColor_ {nullptr}; - - SharedColor shadowColor_ {nullptr}; - Point shadowOffset_ {0, 0}; }; } // namespace react } // namespace facebook - diff --git a/ReactCommon/fabric/view/accessibility/AccessibilityProps.cpp b/ReactCommon/fabric/view/accessibility/AccessibilityProps.cpp index 373b4d941..f8833ac7d 100644 --- a/ReactCommon/fabric/view/accessibility/AccessibilityProps.cpp +++ b/ReactCommon/fabric/view/accessibility/AccessibilityProps.cpp @@ -12,20 +12,7 @@ namespace facebook { namespace react { -void AccessibilityProps::apply(const RawProps &rawProps) { - for (auto const &pair : rawProps) { - auto const &name = pair.first; - auto const &value = pair.second; - -#define ACCESSIBILITY_PROPERTY(stringName, variableName, accessor, convertor) \ - if (name == #stringName) { \ - variableName = convertor(value accessor); \ - continue; \ - } - - ACCESSIBILITY_PROPERTY(accessibilityLabel, accessibilityLabel_, .asString(),) - } -} +AccessibilityProps::AccessibilityProps(const AccessibilityProps &sourceProps, const RawProps &rawProps) {} } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/view/accessibility/AccessibilityProps.h b/ReactCommon/fabric/view/accessibility/AccessibilityProps.h index 12d1dbaf0..f807c5b55 100644 --- a/ReactCommon/fabric/view/accessibility/AccessibilityProps.h +++ b/ReactCommon/fabric/view/accessibility/AccessibilityProps.h @@ -20,22 +20,24 @@ class AccessibilityProps; typedef std::shared_ptr SharedAccessibilityProps; class AccessibilityProps: - public virtual DebugStringConvertible -{ + public virtual DebugStringConvertible { public: - void apply(const RawProps &rawProps); -protected: - bool accessible_ {true}; - std::string accessibilityActions_ {""}; - std::string accessibilityLabel_ {""}; - AccessibilityTraits accessibilityTraits_ {AccessibilityTraits::None}; - bool accessibilityViewIsModal_ {false}; - bool accessibilityElementsHidden_ {false}; - SharedDirectEventHandler onAccessibilityAction_ {nullptr}; - SharedDirectEventHandler onAccessibilityTap_ {nullptr}; - SharedDirectEventHandler onMagicTap_ {nullptr}; + AccessibilityProps() = default; + AccessibilityProps(const AccessibilityProps &sourceProps, const RawProps &rawProps); + +#pragma mark - Props + + const bool accessible {true}; + const std::string accessibilityActions {""}; + const std::string accessibilityLabel {""}; + const AccessibilityTraits accessibilityTraits {AccessibilityTraits::None}; + const bool accessibilityViewIsModal {false}; + const bool accessibilityElementsHidden {false}; + const SharedDirectEventHandler onAccessibilityAction {nullptr}; + const SharedDirectEventHandler onAccessibilityTap {nullptr}; + const SharedDirectEventHandler onMagicTap {nullptr}; }; } // namespace react diff --git a/ReactCommon/fabric/view/root/RootProps.cpp b/ReactCommon/fabric/view/root/RootProps.cpp index e8f0beced..4040ed3a7 100644 --- a/ReactCommon/fabric/view/root/RootProps.cpp +++ b/ReactCommon/fabric/view/root/RootProps.cpp @@ -7,39 +7,35 @@ #include "RootProps.h" +#include "YogaLayoutableShadowNode.h" #include "yogaValuesConversions.h" namespace facebook { namespace react { -void RootProps::applyLayoutConstraints(const LayoutConstraints &layoutConstraints) { - ensureUnsealed(); - - layoutConstraints_ = layoutConstraints; - - yogaStyle_.minDimensions[YGDimensionWidth] = +static YGStyle yogaStyleFromLayoutConstraints(const LayoutConstraints &layoutConstraints) { + YGStyle yogaStyle; + yogaStyle.minDimensions[YGDimensionWidth] = yogaStyleValueFromFloat(layoutConstraints.minimumSize.width); - yogaStyle_.minDimensions[YGDimensionHeight] = + yogaStyle.minDimensions[YGDimensionHeight] = yogaStyleValueFromFloat(layoutConstraints.minimumSize.height); - yogaStyle_.maxDimensions[YGDimensionWidth] = + yogaStyle.maxDimensions[YGDimensionWidth] = yogaStyleValueFromFloat(layoutConstraints.maximumSize.width); - yogaStyle_.maxDimensions[YGDimensionHeight] = + yogaStyle.maxDimensions[YGDimensionHeight] = yogaStyleValueFromFloat(layoutConstraints.maximumSize.height); + + return yogaStyle; } -void RootProps::applyLayoutContext(const LayoutContext &layoutContext) { - ensureUnsealed(); - layoutContext_ = layoutContext; -} - -LayoutConstraints RootProps::getLayoutConstraints() const { - return layoutConstraints_; -} - -LayoutContext RootProps::getLayoutContext() const { - return layoutContext_; -} +RootProps::RootProps( + const RootProps &sourceProps, + const LayoutConstraints &layoutConstraints, + const LayoutContext &layoutContext +): + ViewProps(yogaStyleFromLayoutConstraints(layoutConstraints)), + layoutConstraints(layoutConstraints), + layoutContext(layoutContext) {}; } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/view/root/RootProps.h b/ReactCommon/fabric/view/root/RootProps.h index f8c40f361..f26c53c71 100644 --- a/ReactCommon/fabric/view/root/RootProps.h +++ b/ReactCommon/fabric/view/root/RootProps.h @@ -25,19 +25,17 @@ class RootProps final: public: - /* - * Same semantic as `apply()` but LayoutConstraints & LayoutContext specific. - */ - void applyLayoutConstraints(const LayoutConstraints &layoutConstraints); - void applyLayoutContext(const LayoutContext &layoutContext); + RootProps() = default; + RootProps( + const RootProps &sourceProps, + const LayoutConstraints &layoutConstraints, + const LayoutContext &layoutContext + ); - LayoutConstraints getLayoutConstraints() const; - LayoutContext getLayoutContext() const; +#pragma mark - Props -private: - - LayoutConstraints layoutConstraints_; - LayoutContext layoutContext_; + const LayoutConstraints layoutConstraints {}; + const LayoutContext layoutContext {}; }; } // namespace react diff --git a/ReactCommon/fabric/view/root/RootShadowNode.cpp b/ReactCommon/fabric/view/root/RootShadowNode.cpp index 97dc7dd01..7cefd4b0a 100644 --- a/ReactCommon/fabric/view/root/RootShadowNode.cpp +++ b/ReactCommon/fabric/view/root/RootShadowNode.cpp @@ -16,7 +16,7 @@ ComponentName RootShadowNode::getComponentName() const { void RootShadowNode::layout() { ensureUnsealed(); - layout(getProps()->getLayoutContext()); + layout(getProps()->layoutContext); } } // namespace react diff --git a/ReactCommon/fabric/view/yoga/YogaLayoutableShadowNode.cpp b/ReactCommon/fabric/view/yoga/YogaLayoutableShadowNode.cpp index bce88ce68..10d1a2aa7 100644 --- a/ReactCommon/fabric/view/yoga/YogaLayoutableShadowNode.cpp +++ b/ReactCommon/fabric/view/yoga/YogaLayoutableShadowNode.cpp @@ -10,10 +10,10 @@ #include #include -#include #include #include #include +#include #include "yogaValuesConversions.h" @@ -40,7 +40,7 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode( auto yogaNode = std::make_shared(); yogaNode->setConfig(suitableYogaConfig().get()); - yogaNode->setStyle(props->getYogaStyle()); + yogaNode->setStyle(props->yogaStyle); yogaNode->setContext(this); yogaNode->setDirty(true); YogaLayoutableShadowNode::setYogaNodeChildrenBasedOnShadowNodeChildren(*yogaNode, children); @@ -59,7 +59,7 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode( yogaNode->setDirty(true); if (props) { - yogaNode->setStyle(props->getYogaStyle()); + yogaNode->setStyle(props->yogaStyle); } if (children) { diff --git a/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp b/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp index c13e3462d..2deec5126 100644 --- a/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp +++ b/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp @@ -17,18 +17,16 @@ namespace facebook { namespace react { -const YGStyle &YogaStylableProps::getYogaStyle() const { - return yogaStyle_; -} +static YGStyle convertRawProp(const RawProps &rawProps, const YGStyle &defaultValue) { + YGStyle yogaStyle = defaultValue; -void YogaStylableProps::apply(const RawProps &rawProps) { 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); \ + yogaStyle.yogaName = convertor(value accessor); \ continue; \ } @@ -97,15 +95,23 @@ void YogaStylableProps::apply(const RawProps &rawProps) { YOGA_STYLE_OPTIONAL_FLOAT_PROPERTY(aspectRatio) } + + return yogaStyle; } +YogaStylableProps::YogaStylableProps(const YGStyle &yogaStyle): + yogaStyle(yogaStyle) {} + +YogaStylableProps::YogaStylableProps(const YogaStylableProps &sourceProps, const RawProps &rawProps): + yogaStyle(convertRawProp(rawProps, sourceProps.yogaStyle)) {}; + #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList YogaStylableProps::getDebugProps() const { SharedDebugStringConvertibleList list = {}; YGStyle defaultYogaStyle = YGStyle(); - YGStyle currentYogaStyle = yogaStyle_; + YGStyle currentYogaStyle = yogaStyle; #define YOGA_STYLE_PROPS_ADD_TO_SET(stringName, propertyName, accessor, convertor) \ { \ diff --git a/ReactCommon/fabric/view/yoga/YogaStylableProps.h b/ReactCommon/fabric/view/yoga/YogaStylableProps.h index 14f721c05..3dd023584 100644 --- a/ReactCommon/fabric/view/yoga/YogaStylableProps.h +++ b/ReactCommon/fabric/view/yoga/YogaStylableProps.h @@ -23,16 +23,18 @@ class YogaStylableProps: public virtual DebugStringConvertible { public: - const YGStyle &getYogaStyle() const; - void apply(const RawProps &rawProps); + YogaStylableProps() = default; + YogaStylableProps(const YGStyle &yogaStyle); + YogaStylableProps(const YogaStylableProps &sourceProps, const RawProps &rawProps); + +#pragma mark - Props + + const YGStyle yogaStyle {}; #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList getDebugProps() const override; - -protected: - YGStyle yogaStyle_ {}; }; } // namespace react