From 9ad193c35b2c030beff4e364c26700e97539423e Mon Sep 17 00:00:00 2001 From: David Vacca Date: Mon, 17 Sep 2018 18:46:08 -0700 Subject: [PATCH] Implement Local Data in Android Fabric C++ Summary: This diff introduces the concept of Local Data in Android Fabric C++ and as an example we uses it to implement Text View. Reviewed By: shergin Differential Revision: D9583970 fbshipit-source-id: ab7478b16ef4327ff574ca1467870ab9cb684ea0 --- .../facebook/react/uimanager/ViewManager.java | 7 ++++ .../views/text/ReactTextViewManager.java | 33 +++++++++++++++++++ .../react/views/text/TextAttributeProps.java | 8 ++--- .../text/paragraph/ParagraphLocalData.cpp | 5 +++ .../text/paragraph/ParagraphLocalData.h | 2 ++ .../fabric/core/shadownode/LocalData.h | 5 +++ .../platform/android/TextLayoutManager.cpp | 3 +- 7 files changed, 58 insertions(+), 5 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java index c7936b6ca..4abd015eb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java @@ -205,6 +205,13 @@ public abstract class ViewManager return ViewManagerPropertyUpdater.getNativeProps(getClass(), getShadowNodeClass()); } + /** + * + */ + public @Nullable Object updateLocalData(T view, ReactStylesDiffMap props, ReactStylesDiffMap localData) { + return null; + } + public float[] measure( ReactContext context, T view, diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManager.java index b2f813ba5..c2f4ed22b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManager.java @@ -7,12 +7,16 @@ package com.facebook.react.views.text; +import android.text.Layout; import android.text.Spannable; import com.facebook.react.common.MapBuilder; import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableNativeMap; import com.facebook.react.common.annotations.VisibleForTesting; import com.facebook.react.module.annotations.ReactModule; +import com.facebook.react.uimanager.ReactStylesDiffMap; import com.facebook.react.uimanager.ThemedReactContext; import java.util.Map; import javax.annotation.Nullable; @@ -65,6 +69,35 @@ public class ReactTextViewManager view.updateView(); } + @Override + public Object updateLocalData(ReactTextView view, ReactStylesDiffMap props, ReactStylesDiffMap localData) { + ReadableMap attributedString = localData.getMap("attributedString"); + ReadableArray fragments = attributedString.getArray("fragments"); + String string = attributedString.getString("string"); + + Spannable spanned = TextLayoutManager.spannedFromTextFragments(view.getContext(), + fragments, string); + view.setSpanned(spanned); + + TextAttributeProps textViewProps = new TextAttributeProps(props); + + // TODO add textBreakStrategy prop into local Data + int textBreakStrategy = Layout.BREAK_STRATEGY_HIGH_QUALITY; + + return + new ReactTextUpdate( + spanned, + -1, // TODO add this into local Data? + false, // TODO add this into local Data + textViewProps.getStartPadding(), + textViewProps.getTopPadding(), + textViewProps.getEndPadding(), + textViewProps.getBottomPadding(), + textViewProps.getTextAlign(), + textBreakStrategy + ); + } + @Override public @Nullable Map getExportedCustomDirectEventTypeConstants() { return MapBuilder.of("topTextLayout", MapBuilder.of("registrationName", "onTextLayout")); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java index c69faac2e..fdb7bb5cb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java @@ -241,10 +241,10 @@ public class TextAttributeProps { public void setBackgroundColor(Integer color) { //TODO: Don't apply background color to anchor TextView since it will be applied on the View directly //if (!isVirtualAnchor()) { - mIsBackgroundColorSet = (color != null); - if (mIsBackgroundColorSet) { - mBackgroundColor = color; - } + mIsBackgroundColorSet = (color != null); + if (mIsBackgroundColorSet) { + mBackgroundColor = color; + } //} } diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphLocalData.cpp b/ReactCommon/fabric/components/text/paragraph/ParagraphLocalData.cpp index ec995b3ab..69f5cdcbc 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphLocalData.cpp +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphLocalData.cpp @@ -7,6 +7,7 @@ #include "ParagraphLocalData.h" +#include #include namespace facebook { @@ -30,6 +31,10 @@ void ParagraphLocalData::setTextLayoutManager(SharedTextLayoutManager textLayout textLayoutManager_ = textLayoutManager; } +folly::dynamic ParagraphLocalData::getDynamic() const { + return toDynamic(*this); +} + #pragma mark - DebugStringConvertible std::string ParagraphLocalData::getDebugName() const { diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphLocalData.h b/ReactCommon/fabric/components/text/paragraph/ParagraphLocalData.h index e4f3296f7..b24f53d55 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphLocalData.h +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphLocalData.h @@ -41,6 +41,8 @@ public: SharedTextLayoutManager getTextLayoutManager() const; void setTextLayoutManager(SharedTextLayoutManager textLayoutManager); + folly::dynamic getDynamic() const override; + #pragma mark - DebugStringConvertible std::string getDebugName() const override; diff --git a/ReactCommon/fabric/core/shadownode/LocalData.h b/ReactCommon/fabric/core/shadownode/LocalData.h index baccf1cc2..603ee7ef6 100644 --- a/ReactCommon/fabric/core/shadownode/LocalData.h +++ b/ReactCommon/fabric/core/shadownode/LocalData.h @@ -9,6 +9,7 @@ #include #include +#include namespace facebook { namespace react { @@ -29,6 +30,10 @@ class LocalData: public Sealable, public DebugStringConvertible { + public: + virtual folly::dynamic getDynamic() const { + return folly::dynamic::object(); + }; }; } // namespace react diff --git a/ReactCommon/fabric/textlayoutmanager/platform/android/TextLayoutManager.cpp b/ReactCommon/fabric/textlayoutmanager/platform/android/TextLayoutManager.cpp index 175926748..b5ff70bb7 100644 --- a/ReactCommon/fabric/textlayoutmanager/platform/android/TextLayoutManager.cpp +++ b/ReactCommon/fabric/textlayoutmanager/platform/android/TextLayoutManager.cpp @@ -7,6 +7,7 @@ #include "TextLayoutManager.h" +#include #include using namespace facebook::jni; @@ -37,7 +38,7 @@ Size TextLayoutManager::measure( int width = (int) layoutConstraints.maximumSize.width; int height = (int) layoutConstraints.maximumSize.height; local_ref componentName = make_jstring("RCTText"); - auto values = measure(fabricUIManager, reactTag, componentName.get(), ReadableNativeMap::newObjectCxxArgs(attributedString.toDynamic()).get(), ReadableNativeMap::newObjectCxxArgs(paragraphAttributes.toDynamic()).get(), width, height); + auto values = measure(fabricUIManager, reactTag, componentName.get(), ReadableNativeMap::newObjectCxxArgs(toDynamic(attributedString)).get(), ReadableNativeMap::newObjectCxxArgs(toDynamic(paragraphAttributes)).get(), width, height); std::vector indices; indices.resize(values->size());