mirror of
https://github.com/status-im/react-native.git
synced 2025-02-11 08:56:44 +00:00
Refactor ReactViewGroup to reuse ReactViewBackgroundManager
Reviewed By: achen1 Differential Revision: D6704701 fbshipit-source-id: 14bb155f2eb0951307674e257e729a1c72b08d8b
This commit is contained in:
parent
164f6b6afd
commit
e8aa60430c
@ -4,6 +4,7 @@ package com.facebook.react.views.common;
|
|||||||
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
/** Helper class for Views */
|
/** 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
|
* @param drawable {@link Drawable} The Drawable to use as the background, or null to remove the
|
||||||
* background
|
* 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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
||||||
view.setBackground(drawable);
|
view.setBackground(drawable);
|
||||||
} else {
|
} else {
|
||||||
|
@ -57,6 +57,10 @@ public class ReactViewBackgroundManager {
|
|||||||
getOrCreateReactViewBackground().setRadius(borderRadius);
|
getOrCreateReactViewBackground().setRadius(borderRadius);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasRoundedBorders() {
|
||||||
|
return getOrCreateReactViewBackground().hasRoundedBorders();
|
||||||
|
}
|
||||||
|
|
||||||
public void setBorderRadius(float borderRadius, int position) {
|
public void setBorderRadius(float borderRadius, int position) {
|
||||||
getOrCreateReactViewBackground().setRadius(borderRadius, 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.ReactPointerEventsView;
|
||||||
import com.facebook.react.uimanager.ReactZIndexedViewGroup;
|
import com.facebook.react.uimanager.ReactZIndexedViewGroup;
|
||||||
import com.facebook.react.uimanager.ViewGroupDrawingOrderHelper;
|
import com.facebook.react.uimanager.ViewGroupDrawingOrderHelper;
|
||||||
|
import com.facebook.react.views.common.ViewHelper;
|
||||||
import com.facebook.yoga.YogaConstants;
|
import com.facebook.yoga.YogaConstants;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
@ -51,6 +52,7 @@ public class ReactViewGroup extends ViewGroup implements
|
|||||||
private static final LayoutParams sDefaultLayoutParam = new ViewGroup.LayoutParams(0, 0);
|
private static final LayoutParams sDefaultLayoutParam = new ViewGroup.LayoutParams(0, 0);
|
||||||
/* should only be used in {@link #updateClippingToRect} */
|
/* should only be used in {@link #updateClippingToRect} */
|
||||||
private static final Rect sHelperRect = new Rect();
|
private static final Rect sHelperRect = new Rect();
|
||||||
|
private ReactViewBackgroundManager mReactBackgroundManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This listener will be set for child views when removeClippedSubview property is enabled. When
|
* 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) {
|
public ReactViewGroup(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
mDrawingOrderHelper = new ViewGroupDrawingOrderHelper(this);
|
mDrawingOrderHelper = new ViewGroupDrawingOrderHelper(this);
|
||||||
|
mReactBackgroundManager = new ReactViewBackgroundManager(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -144,17 +147,19 @@ public class ReactViewGroup extends ViewGroup implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBackgroundColor(int color) {
|
public void setBackgroundColor(int color) {
|
||||||
if (color == Color.TRANSPARENT && mReactBackgroundDrawable == null) {
|
mReactBackgroundManager.setBackgroundColor(color);
|
||||||
// don't do anything, no need to allocate ReactBackgroundDrawable for transparent background
|
|
||||||
} else {
|
|
||||||
getOrCreateReactViewBackground().setColor(color);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void setBorderWidth(int position, float width) {
|
||||||
public void setBackground(Drawable drawable) {
|
mReactBackgroundManager.setBorderWidth(position, width);
|
||||||
throw new UnsupportedOperationException(
|
}
|
||||||
"This method is not supported for ReactViewGroup instances");
|
|
||||||
|
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) {
|
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
|
// 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
|
// as a background previously. This will not work correctly as the drawable callback logic is
|
||||||
// messed up in AOSP
|
// messed up in AOSP
|
||||||
updateBackgroundDrawable(null);
|
ViewHelper.setBackground(this, null);
|
||||||
if (mReactBackgroundDrawable != null && background != null) {
|
if (mReactBackgroundDrawable != null && background != null) {
|
||||||
LayerDrawable layerDrawable =
|
LayerDrawable layerDrawable =
|
||||||
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, background});
|
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, background});
|
||||||
updateBackgroundDrawable(layerDrawable);
|
ViewHelper.setBackground(this, layerDrawable);
|
||||||
} else if (background != null) {
|
} else if (background != null) {
|
||||||
updateBackgroundDrawable(background);
|
ViewHelper.setBackground(this, background);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,22 +225,13 @@ public class ReactViewGroup extends ViewGroup implements
|
|||||||
mNeedsOffscreenAlphaCompositing = needsOffscreenAlphaCompositing;
|
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) {
|
public void setBorderRadius(float borderRadius) {
|
||||||
ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground();
|
mReactBackgroundManager.setBorderRadius(borderRadius);
|
||||||
backgroundDrawable.setRadius(borderRadius);
|
|
||||||
|
|
||||||
if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT
|
if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT
|
||||||
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
|
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
|
||||||
final int UPDATED_LAYER_TYPE =
|
final int UPDATED_LAYER_TYPE =
|
||||||
backgroundDrawable.hasRoundedBorders()
|
mReactBackgroundManager.hasRoundedBorders()
|
||||||
? View.LAYER_TYPE_SOFTWARE
|
? View.LAYER_TYPE_SOFTWARE
|
||||||
: View.LAYER_TYPE_HARDWARE;
|
: View.LAYER_TYPE_HARDWARE;
|
||||||
|
|
||||||
@ -246,13 +242,12 @@ public class ReactViewGroup extends ViewGroup implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setBorderRadius(float borderRadius, int position) {
|
public void setBorderRadius(float borderRadius, int position) {
|
||||||
ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground();
|
mReactBackgroundManager.setBorderRadius(borderRadius, position);
|
||||||
backgroundDrawable.setRadius(borderRadius, position);
|
|
||||||
|
|
||||||
if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT
|
if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT
|
||||||
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
|
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
|
||||||
final int UPDATED_LAYER_TYPE =
|
final int UPDATED_LAYER_TYPE =
|
||||||
backgroundDrawable.hasRoundedBorders()
|
mReactBackgroundManager.hasRoundedBorders()
|
||||||
? View.LAYER_TYPE_SOFTWARE
|
? View.LAYER_TYPE_SOFTWARE
|
||||||
: View.LAYER_TYPE_HARDWARE;
|
: View.LAYER_TYPE_HARDWARE;
|
||||||
|
|
||||||
@ -262,10 +257,6 @@ public class ReactViewGroup extends ViewGroup implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBorderStyle(@Nullable String style) {
|
|
||||||
getOrCreateReactViewBackground().setBorderStyle(style);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setRemoveClippedSubviews(boolean removeClippedSubviews) {
|
public void setRemoveClippedSubviews(boolean removeClippedSubviews) {
|
||||||
if (removeClippedSubviews == mRemoveClippedSubviews) {
|
if (removeClippedSubviews == mRemoveClippedSubviews) {
|
||||||
@ -600,32 +591,6 @@ public class ReactViewGroup extends ViewGroup implements
|
|||||||
return DEFAULT_BACKGROUND_COLOR;
|
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
|
@Override
|
||||||
public @Nullable Rect getHitSlopRect() {
|
public @Nullable Rect getHitSlopRect() {
|
||||||
return mHitSlopRect;
|
return mHitSlopRect;
|
||||||
@ -640,21 +605,6 @@ public class ReactViewGroup extends ViewGroup implements
|
|||||||
invalidate();
|
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
|
@Override
|
||||||
protected void dispatchDraw(Canvas canvas) {
|
protected void dispatchDraw(Canvas canvas) {
|
||||||
if (mOverflow != null) {
|
if (mOverflow != null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user