Skip to content

Commit

Permalink
Revert D6704701: Refactor ReactViewGroup to reuse ReactViewBackground…
Browse files Browse the repository at this point in the history
…Manager

Differential Revision: D6704701

fbshipit-source-id: 01feb61a9c2c263f88cca4fea5978957c5e4767f
  • Loading branch information
mdvacca authored and facebook-github-bot committed Jan 16, 2018
1 parent 2b80cdf commit 1f5bb37
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.view.View;

/** Helper class for Views */
Expand All @@ -19,7 +18,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, @Nullable Drawable drawable) {
public static void setBackground(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ public void setBorderRadius(float borderRadius) {
getOrCreateReactViewBackground().setRadius(borderRadius);
}

public boolean hasRoundedBorders() {
return getOrCreateReactViewBackground().hasRoundedBorders();
}

public void setBorderRadius(float borderRadius, int position) {
getOrCreateReactViewBackground().setRadius(borderRadius, position);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
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;

Expand All @@ -52,7 +51,6 @@ 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
Expand Down Expand Up @@ -113,7 +111,6 @@ public void onLayoutChange(
public ReactViewGroup(Context context) {
super(context);
mDrawingOrderHelper = new ViewGroupDrawingOrderHelper(this);
mReactBackgroundManager = new ReactViewBackgroundManager(this);
}

@Override
Expand Down Expand Up @@ -147,33 +144,31 @@ public void requestLayout() {

@Override
public void setBackgroundColor(int color) {
mReactBackgroundManager.setBackgroundColor(color);
}

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);
if (color == Color.TRANSPARENT && mReactBackgroundDrawable == null) {
// don't do anything, no need to allocate ReactBackgroundDrawable for transparent background
} else {
getOrCreateReactViewBackground().setColor(color);
}
}

public void setBorderStyle(@Nullable String style) {
mReactBackgroundManager.setBorderStyle(style);
@Override
public void setBackground(Drawable drawable) {
throw new UnsupportedOperationException(
"This method is not supported for ReactViewGroup instances");
}

public void setTranslucentBackgroundDrawable(@Nullable Drawable background) {
// it's required to call setBackground to null, as in some of the cases we may set new
// 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
ViewHelper.setBackground(this, null);
updateBackgroundDrawable(null);
if (mReactBackgroundDrawable != null && background != null) {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, background});
ViewHelper.setBackground(this, layerDrawable);
updateBackgroundDrawable(layerDrawable);
} else if (background != null) {
ViewHelper.setBackground(this, background);
updateBackgroundDrawable(background);
}
}

Expand Down Expand Up @@ -225,13 +220,22 @@ public void setNeedsOffscreenAlphaCompositing(boolean needsOffscreenAlphaComposi
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) {
mReactBackgroundManager.setBorderRadius(borderRadius);
ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground();
backgroundDrawable.setRadius(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 =
mReactBackgroundManager.hasRoundedBorders()
backgroundDrawable.hasRoundedBorders()
? View.LAYER_TYPE_SOFTWARE
: View.LAYER_TYPE_HARDWARE;

Expand All @@ -242,12 +246,13 @@ public void setBorderRadius(float borderRadius) {
}

public void setBorderRadius(float borderRadius, int position) {
mReactBackgroundManager.setBorderRadius(borderRadius, position);
ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground();
backgroundDrawable.setRadius(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 =
mReactBackgroundManager.hasRoundedBorders()
backgroundDrawable.hasRoundedBorders()
? View.LAYER_TYPE_SOFTWARE
: View.LAYER_TYPE_HARDWARE;

Expand All @@ -257,6 +262,10 @@ public void setBorderRadius(float borderRadius, int position) {
}
}

public void setBorderStyle(@Nullable String style) {
getOrCreateReactViewBackground().setBorderStyle(style);
}

@Override
public void setRemoveClippedSubviews(boolean removeClippedSubviews) {
if (removeClippedSubviews == mRemoveClippedSubviews) {
Expand Down Expand Up @@ -591,6 +600,32 @@ public int getBackgroundColor() {
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;
Expand All @@ -605,6 +640,21 @@ public void setOverflow(String 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.
*
* @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) {
Expand Down

0 comments on commit 1f5bb37

Please sign in to comment.