Skip to content

Commit e8aa604

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Refactor ReactViewGroup to reuse ReactViewBackgroundManager
Reviewed By: achen1 Differential Revision: D6704701 fbshipit-source-id: 14bb155f2eb0951307674e257e729a1c72b08d8b
1 parent 164f6b6 commit e8aa604

File tree

3 files changed

+27
-72
lines changed

3 files changed

+27
-72
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/common/ViewHelper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import android.graphics.drawable.Drawable;
66
import android.os.Build;
7+
import android.support.annotation.Nullable;
78
import android.view.View;
89

910
/** Helper class for Views */
@@ -18,7 +19,7 @@ public class ViewHelper {
1819
* @param drawable {@link Drawable} The Drawable to use as the background, or null to remove the
1920
* background
2021
*/
21-
public static void setBackground(View view, Drawable drawable) {
22+
public static void setBackground(View view, @Nullable Drawable drawable) {
2223
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
2324
view.setBackground(drawable);
2425
} else {

ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundManager.java

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public void setBorderRadius(float borderRadius) {
5757
getOrCreateReactViewBackground().setRadius(borderRadius);
5858
}
5959

60+
public boolean hasRoundedBorders() {
61+
return getOrCreateReactViewBackground().hasRoundedBorders();
62+
}
63+
6064
public void setBorderRadius(float borderRadius, int position) {
6165
getOrCreateReactViewBackground().setRadius(borderRadius, position);
6266
}

ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java

+21-71
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.facebook.react.uimanager.ReactPointerEventsView;
3636
import com.facebook.react.uimanager.ReactZIndexedViewGroup;
3737
import com.facebook.react.uimanager.ViewGroupDrawingOrderHelper;
38+
import com.facebook.react.views.common.ViewHelper;
3839
import com.facebook.yoga.YogaConstants;
3940
import javax.annotation.Nullable;
4041

@@ -51,6 +52,7 @@ public class ReactViewGroup extends ViewGroup implements
5152
private static final LayoutParams sDefaultLayoutParam = new ViewGroup.LayoutParams(0, 0);
5253
/* should only be used in {@link #updateClippingToRect} */
5354
private static final Rect sHelperRect = new Rect();
55+
private ReactViewBackgroundManager mReactBackgroundManager;
5456

5557
/**
5658
* This listener will be set for child views when removeClippedSubview property is enabled. When
@@ -111,6 +113,7 @@ public void onLayoutChange(
111113
public ReactViewGroup(Context context) {
112114
super(context);
113115
mDrawingOrderHelper = new ViewGroupDrawingOrderHelper(this);
116+
mReactBackgroundManager = new ReactViewBackgroundManager(this);
114117
}
115118

116119
@Override
@@ -144,31 +147,33 @@ public void requestLayout() {
144147

145148
@Override
146149
public void setBackgroundColor(int color) {
147-
if (color == Color.TRANSPARENT && mReactBackgroundDrawable == null) {
148-
// don't do anything, no need to allocate ReactBackgroundDrawable for transparent background
149-
} else {
150-
getOrCreateReactViewBackground().setColor(color);
151-
}
150+
mReactBackgroundManager.setBackgroundColor(color);
152151
}
153152

154-
@Override
155-
public void setBackground(Drawable drawable) {
156-
throw new UnsupportedOperationException(
157-
"This method is not supported for ReactViewGroup instances");
153+
public void setBorderWidth(int position, float width) {
154+
mReactBackgroundManager.setBorderWidth(position, width);
155+
}
156+
157+
public void setBorderColor(int position, float color, float alpha) {
158+
mReactBackgroundManager.setBorderColor(position, color, alpha);
159+
}
160+
161+
public void setBorderStyle(@Nullable String style) {
162+
mReactBackgroundManager.setBorderStyle(style);
158163
}
159164

160165
public void setTranslucentBackgroundDrawable(@Nullable Drawable background) {
161166
// it's required to call setBackground to null, as in some of the cases we may set new
162167
// background to be a layer drawable that contains a drawable that has been previously setup
163168
// as a background previously. This will not work correctly as the drawable callback logic is
164169
// messed up in AOSP
165-
updateBackgroundDrawable(null);
170+
ViewHelper.setBackground(this, null);
166171
if (mReactBackgroundDrawable != null && background != null) {
167172
LayerDrawable layerDrawable =
168173
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, background});
169-
updateBackgroundDrawable(layerDrawable);
174+
ViewHelper.setBackground(this, layerDrawable);
170175
} else if (background != null) {
171-
updateBackgroundDrawable(background);
176+
ViewHelper.setBackground(this, background);
172177
}
173178
}
174179

@@ -220,22 +225,13 @@ public void setNeedsOffscreenAlphaCompositing(boolean needsOffscreenAlphaComposi
220225
mNeedsOffscreenAlphaCompositing = needsOffscreenAlphaCompositing;
221226
}
222227

223-
public void setBorderWidth(int position, float width) {
224-
getOrCreateReactViewBackground().setBorderWidth(position, width);
225-
}
226-
227-
public void setBorderColor(int position, float rgb, float alpha) {
228-
getOrCreateReactViewBackground().setBorderColor(position, rgb, alpha);
229-
}
230-
231228
public void setBorderRadius(float borderRadius) {
232-
ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground();
233-
backgroundDrawable.setRadius(borderRadius);
229+
mReactBackgroundManager.setBorderRadius(borderRadius);
234230

235231
if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT
236232
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
237233
final int UPDATED_LAYER_TYPE =
238-
backgroundDrawable.hasRoundedBorders()
234+
mReactBackgroundManager.hasRoundedBorders()
239235
? View.LAYER_TYPE_SOFTWARE
240236
: View.LAYER_TYPE_HARDWARE;
241237

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

248244
public void setBorderRadius(float borderRadius, int position) {
249-
ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground();
250-
backgroundDrawable.setRadius(borderRadius, position);
245+
mReactBackgroundManager.setBorderRadius(borderRadius, position);
251246

252247
if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT
253248
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
254249
final int UPDATED_LAYER_TYPE =
255-
backgroundDrawable.hasRoundedBorders()
250+
mReactBackgroundManager.hasRoundedBorders()
256251
? View.LAYER_TYPE_SOFTWARE
257252
: View.LAYER_TYPE_HARDWARE;
258253

@@ -262,10 +257,6 @@ public void setBorderRadius(float borderRadius, int position) {
262257
}
263258
}
264259

265-
public void setBorderStyle(@Nullable String style) {
266-
getOrCreateReactViewBackground().setBorderStyle(style);
267-
}
268-
269260
@Override
270261
public void setRemoveClippedSubviews(boolean removeClippedSubviews) {
271262
if (removeClippedSubviews == mRemoveClippedSubviews) {
@@ -600,32 +591,6 @@ public int getBackgroundColor() {
600591
return DEFAULT_BACKGROUND_COLOR;
601592
}
602593

603-
private ReactViewBackgroundDrawable getOrCreateReactViewBackground() {
604-
if (mReactBackgroundDrawable == null) {
605-
mReactBackgroundDrawable = new ReactViewBackgroundDrawable(getContext());
606-
Drawable backgroundDrawable = getBackground();
607-
updateBackgroundDrawable(
608-
null); // required so that drawable callback is cleared before we add the
609-
// drawable back as a part of LayerDrawable
610-
if (backgroundDrawable == null) {
611-
updateBackgroundDrawable(mReactBackgroundDrawable);
612-
} else {
613-
LayerDrawable layerDrawable =
614-
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, backgroundDrawable});
615-
updateBackgroundDrawable(layerDrawable);
616-
}
617-
618-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
619-
mLayoutDirection =
620-
I18nUtil.getInstance().isRTL(getContext())
621-
? LAYOUT_DIRECTION_RTL
622-
: LAYOUT_DIRECTION_LTR;
623-
mReactBackgroundDrawable.setResolvedLayoutDirection(mLayoutDirection);
624-
}
625-
}
626-
return mReactBackgroundDrawable;
627-
}
628-
629594
@Override
630595
public @Nullable Rect getHitSlopRect() {
631596
return mHitSlopRect;
@@ -640,21 +605,6 @@ public void setOverflow(String overflow) {
640605
invalidate();
641606
}
642607

643-
/**
644-
* Set the background for the view or remove the background. It calls {@link
645-
* #setBackground(Drawable)} or {@link #setBackgroundDrawable(Drawable)} based on the sdk version.
646-
*
647-
* @param drawable {@link Drawable} The Drawable to use as the background, or null to remove the
648-
* background
649-
*/
650-
private void updateBackgroundDrawable(Drawable drawable) {
651-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
652-
super.setBackground(drawable);
653-
} else {
654-
super.setBackgroundDrawable(drawable);
655-
}
656-
}
657-
658608
@Override
659609
protected void dispatchDraw(Canvas canvas) {
660610
if (mOverflow != null) {

0 commit comments

Comments
 (0)