From 6ba9ad8ece02b526eb52641103b5f12c14cc214e Mon Sep 17 00:00:00 2001 From: Aaron Chiu Date: Tue, 3 Oct 2017 00:03:50 -0700 Subject: [PATCH] redo D5917111 Reviewed By: fkgozali Differential Revision: D5962566 fbshipit-source-id: 5f92890a08b6e900b8d54f35c48e783166ce5897 --- .../view/ReactViewBackgroundDrawable.java | 17 +++--- .../react/views/view/ReactViewGroup.java | 52 +++++++++++++++++++ .../react/views/view/ReactViewManager.java | 5 ++ 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java index 2362a697f..496007c8b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java @@ -9,11 +9,6 @@ package com.facebook.react.views.view; -import javax.annotation.Nullable; - -import java.util.Arrays; -import java.util.Locale; - import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; @@ -26,10 +21,13 @@ import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.os.Build; -import com.facebook.yoga.YogaConstants; import com.facebook.react.common.annotations.VisibleForTesting; import com.facebook.react.uimanager.FloatUtil; import com.facebook.react.uimanager.Spacing; +import com.facebook.yoga.YogaConstants; +import java.util.Arrays; +import java.util.Locale; +import javax.annotation.Nullable; /** * A subclass of {@link Drawable} used for background of {@link ReactViewGroup}. It supports @@ -230,6 +228,10 @@ public class ReactViewBackgroundDrawable extends Drawable { } } + public float getRadius() { + return mBorderRadius; + } + public void setColor(int color) { mColor = color; invalidateSelf(); @@ -333,9 +335,8 @@ public class ReactViewBackgroundDrawable extends Drawable { mPaint.setPathEffect(mPathEffectForBorderStyle); } - /** For rounded borders we use default "borderWidth" property. */ - private float getFullBorderWidth() { + public float getFullBorderWidth() { return (mBorderWidth != null && !YogaConstants.isUndefined(mBorderWidth.getRaw(Spacing.ALL))) ? mBorderWidth.getRaw(Spacing.ALL) : 0f; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java index adb0e4860..828b928d4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java @@ -10,8 +10,11 @@ package com.facebook.react.views.view; import android.content.Context; +import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Path; import android.graphics.Rect; +import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.os.Build; @@ -31,6 +34,7 @@ import com.facebook.react.uimanager.ReactClippingViewGroupHelper; import com.facebook.react.uimanager.ReactPointerEventsView; import com.facebook.react.uimanager.ReactZIndexedViewGroup; import com.facebook.react.uimanager.ViewGroupDrawingOrderHelper; +import com.facebook.yoga.YogaConstants; import javax.annotation.Nullable; /** @@ -100,6 +104,7 @@ public class ReactViewGroup extends ViewGroup implements private @Nullable OnInterceptTouchEventListener mOnInterceptTouchEventListener; private boolean mNeedsOffscreenAlphaCompositing = false; private final ViewGroupDrawingOrderHelper mDrawingOrderHelper; + private @Nullable Path mPath; public ReactViewGroup(Context context) { super(context); @@ -585,6 +590,11 @@ public class ReactViewGroup extends ViewGroup implements mHitSlopRect = rect; } + public void setOverflow(String overflow) { + mOverflow = overflow; + invalidate(); + } + /** * Set the background for the view or remove the background. It calls {@link * #setBackground(Drawable)} or {@link #setBackgroundDrawable(Drawable)} based on the sdk version. @@ -599,4 +609,46 @@ public class ReactViewGroup extends ViewGroup implements super.setBackgroundDrawable(drawable); } } + + @Override + protected void dispatchDraw(Canvas canvas) { + if (mOverflow != null) { + switch (mOverflow) { + case "visible": + if (mPath != null) { + mPath.rewind(); + } + break; + case "hidden": + if (mReactBackgroundDrawable != null) { + float left = 0f; + float top = 0f; + float right = getWidth(); + float bottom = getHeight(); + if (mReactBackgroundDrawable.getFullBorderWidth() != 0f) { + float borderWidth = mReactBackgroundDrawable.getFullBorderWidth(); + left += borderWidth; + top += borderWidth; + right -= borderWidth; + bottom -= borderWidth; + } + float radius = mReactBackgroundDrawable.getRadius(); + + if (!YogaConstants.isUndefined(radius) && radius > 0.5f) { + if (mPath == null) { + mPath = new Path(); + } + mPath.rewind(); + mPath.addRoundRect( + new RectF(left, top, right, bottom), radius, radius, Path.Direction.CW); + canvas.clipPath(mPath); + } + } + break; + default: + break; + } + } + super.dispatchDraw(canvas); + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java index 59404c672..77104de53 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java @@ -163,6 +163,11 @@ public class ReactViewManager extends ViewGroupManager { // handled in NativeViewHierarchyOptimizer } + @ReactProp(name = ViewProps.OVERFLOW) + public void setOverflow(ReactViewGroup view, String overflow) { + view.setOverflow(overflow); + } + @Override public String getName() { return REACT_CLASS;