Refactor ReactViewGroup to reuse ReactViewBackgroundManager

Reviewed By: achen1

Differential Revision: D6704701

fbshipit-source-id: 14bb155f2eb0951307674e257e729a1c72b08d8b
This commit is contained in:
David Vacca 2018-01-12 16:54:29 -08:00 коммит произвёл Facebook Github Bot
Родитель 164f6b6afd
Коммит e8aa60430c
3 изменённых файлов: 27 добавлений и 72 удалений

Просмотреть файл

@ -4,6 +4,7 @@ package com.facebook.react.views.common;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.view.View;
/** Helper class for Views */
@ -18,7 +19,7 @@ public class ViewHelper {
* @param drawable {@link Drawable} The Drawable to use as the background, or null to remove the
* background
*/
public static void setBackground(View view, Drawable drawable) {
public static void setBackground(View view, @Nullable Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
} else {

Просмотреть файл

@ -57,6 +57,10 @@ public class ReactViewBackgroundManager {
getOrCreateReactViewBackground().setRadius(borderRadius);
}
public boolean hasRoundedBorders() {
return getOrCreateReactViewBackground().hasRoundedBorders();
}
public void setBorderRadius(float borderRadius, int position) {
getOrCreateReactViewBackground().setRadius(borderRadius, position);
}

Просмотреть файл

@ -35,6 +35,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.react.views.common.ViewHelper;
import com.facebook.yoga.YogaConstants;
import javax.annotation.Nullable;
@ -51,6 +52,7 @@ public class ReactViewGroup extends ViewGroup implements
private static final LayoutParams sDefaultLayoutParam = new ViewGroup.LayoutParams(0, 0);
/* should only be used in {@link #updateClippingToRect} */
private static final Rect sHelperRect = new Rect();
private ReactViewBackgroundManager mReactBackgroundManager;
/**
* This listener will be set for child views when removeClippedSubview property is enabled. When
@ -111,6 +113,7 @@ public class ReactViewGroup extends ViewGroup implements
public ReactViewGroup(Context context) {
super(context);
mDrawingOrderHelper = new ViewGroupDrawingOrderHelper(this);
mReactBackgroundManager = new ReactViewBackgroundManager(this);
}
@Override
@ -144,17 +147,19 @@ public class ReactViewGroup extends ViewGroup implements
@Override
public void setBackgroundColor(int color) {
if (color == Color.TRANSPARENT && mReactBackgroundDrawable == null) {
// don't do anything, no need to allocate ReactBackgroundDrawable for transparent background
} else {
getOrCreateReactViewBackground().setColor(color);
}
mReactBackgroundManager.setBackgroundColor(color);
}
@Override
public void setBackground(Drawable drawable) {
throw new UnsupportedOperationException(
"This method is not supported for ReactViewGroup instances");
public void setBorderWidth(int position, float width) {
mReactBackgroundManager.setBorderWidth(position, width);
}
public void setBorderColor(int position, float color, float alpha) {
mReactBackgroundManager.setBorderColor(position, color, alpha);
}
public void setBorderStyle(@Nullable String style) {
mReactBackgroundManager.setBorderStyle(style);
}
public void setTranslucentBackgroundDrawable(@Nullable Drawable background) {
@ -162,13 +167,13 @@ public class ReactViewGroup extends ViewGroup implements
// background to be a layer drawable that contains a drawable that has been previously setup
// as a background previously. This will not work correctly as the drawable callback logic is
// messed up in AOSP
updateBackgroundDrawable(null);
ViewHelper.setBackground(this, null);
if (mReactBackgroundDrawable != null && background != null) {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, background});
updateBackgroundDrawable(layerDrawable);
ViewHelper.setBackground(this, layerDrawable);
} else if (background != null) {
updateBackgroundDrawable(background);
ViewHelper.setBackground(this, background);
}
}
@ -220,22 +225,13 @@ public class ReactViewGroup extends ViewGroup implements
mNeedsOffscreenAlphaCompositing = needsOffscreenAlphaCompositing;
}
public void setBorderWidth(int position, float width) {
getOrCreateReactViewBackground().setBorderWidth(position, width);
}
public void setBorderColor(int position, float rgb, float alpha) {
getOrCreateReactViewBackground().setBorderColor(position, rgb, alpha);
}
public void setBorderRadius(float borderRadius) {
ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground();
backgroundDrawable.setRadius(borderRadius);
mReactBackgroundManager.setBorderRadius(borderRadius);
if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
final int UPDATED_LAYER_TYPE =
backgroundDrawable.hasRoundedBorders()
mReactBackgroundManager.hasRoundedBorders()
? View.LAYER_TYPE_SOFTWARE
: View.LAYER_TYPE_HARDWARE;
@ -246,13 +242,12 @@ public class ReactViewGroup extends ViewGroup implements
}
public void setBorderRadius(float borderRadius, int position) {
ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground();
backgroundDrawable.setRadius(borderRadius, position);
mReactBackgroundManager.setBorderRadius(borderRadius, position);
if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
final int UPDATED_LAYER_TYPE =
backgroundDrawable.hasRoundedBorders()
mReactBackgroundManager.hasRoundedBorders()
? View.LAYER_TYPE_SOFTWARE
: View.LAYER_TYPE_HARDWARE;
@ -262,10 +257,6 @@ public class ReactViewGroup extends ViewGroup implements
}
}
public void setBorderStyle(@Nullable String style) {
getOrCreateReactViewBackground().setBorderStyle(style);
}
@Override
public void setRemoveClippedSubviews(boolean removeClippedSubviews) {
if (removeClippedSubviews == mRemoveClippedSubviews) {
@ -600,32 +591,6 @@ public class ReactViewGroup extends ViewGroup implements
return DEFAULT_BACKGROUND_COLOR;
}
private ReactViewBackgroundDrawable getOrCreateReactViewBackground() {
if (mReactBackgroundDrawable == null) {
mReactBackgroundDrawable = new ReactViewBackgroundDrawable(getContext());
Drawable backgroundDrawable = getBackground();
updateBackgroundDrawable(
null); // required so that drawable callback is cleared before we add the
// drawable back as a part of LayerDrawable
if (backgroundDrawable == null) {
updateBackgroundDrawable(mReactBackgroundDrawable);
} else {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, backgroundDrawable});
updateBackgroundDrawable(layerDrawable);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mLayoutDirection =
I18nUtil.getInstance().isRTL(getContext())
? LAYOUT_DIRECTION_RTL
: LAYOUT_DIRECTION_LTR;
mReactBackgroundDrawable.setResolvedLayoutDirection(mLayoutDirection);
}
}
return mReactBackgroundDrawable;
}
@Override
public @Nullable Rect getHitSlopRect() {
return mHitSlopRect;
@ -640,21 +605,6 @@ public class ReactViewGroup extends ViewGroup implements
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.
*
* @param drawable {@link Drawable} The Drawable to use as the background, or null to remove the
* background
*/
private void updateBackgroundDrawable(Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
super.setBackground(drawable);
} else {
super.setBackgroundDrawable(drawable);
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
if (mOverflow != null) {