35
35
import com .facebook .react .uimanager .ReactPointerEventsView ;
36
36
import com .facebook .react .uimanager .ReactZIndexedViewGroup ;
37
37
import com .facebook .react .uimanager .ViewGroupDrawingOrderHelper ;
38
+ import com .facebook .react .views .common .ViewHelper ;
38
39
import com .facebook .yoga .YogaConstants ;
39
40
import javax .annotation .Nullable ;
40
41
@@ -51,6 +52,7 @@ public class ReactViewGroup extends ViewGroup implements
51
52
private static final LayoutParams sDefaultLayoutParam = new ViewGroup .LayoutParams (0 , 0 );
52
53
/* should only be used in {@link #updateClippingToRect} */
53
54
private static final Rect sHelperRect = new Rect ();
55
+ private ReactViewBackgroundManager mReactBackgroundManager ;
54
56
55
57
/**
56
58
* This listener will be set for child views when removeClippedSubview property is enabled. When
@@ -111,6 +113,7 @@ public void onLayoutChange(
111
113
public ReactViewGroup (Context context ) {
112
114
super (context );
113
115
mDrawingOrderHelper = new ViewGroupDrawingOrderHelper (this );
116
+ mReactBackgroundManager = new ReactViewBackgroundManager (this );
114
117
}
115
118
116
119
@ Override
@@ -144,31 +147,33 @@ public void requestLayout() {
144
147
145
148
@ Override
146
149
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 );
152
151
}
153
152
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 );
158
163
}
159
164
160
165
public void setTranslucentBackgroundDrawable (@ Nullable Drawable background ) {
161
166
// it's required to call setBackground to null, as in some of the cases we may set new
162
167
// background to be a layer drawable that contains a drawable that has been previously setup
163
168
// as a background previously. This will not work correctly as the drawable callback logic is
164
169
// messed up in AOSP
165
- updateBackgroundDrawable ( null );
170
+ ViewHelper . setBackground ( this , null );
166
171
if (mReactBackgroundDrawable != null && background != null ) {
167
172
LayerDrawable layerDrawable =
168
173
new LayerDrawable (new Drawable [] {mReactBackgroundDrawable , background });
169
- updateBackgroundDrawable ( layerDrawable );
174
+ ViewHelper . setBackground ( this , layerDrawable );
170
175
} else if (background != null ) {
171
- updateBackgroundDrawable ( background );
176
+ ViewHelper . setBackground ( this , background );
172
177
}
173
178
}
174
179
@@ -220,22 +225,13 @@ public void setNeedsOffscreenAlphaCompositing(boolean needsOffscreenAlphaComposi
220
225
mNeedsOffscreenAlphaCompositing = needsOffscreenAlphaCompositing ;
221
226
}
222
227
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
-
231
228
public void setBorderRadius (float borderRadius ) {
232
- ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground ();
233
- backgroundDrawable .setRadius (borderRadius );
229
+ mReactBackgroundManager .setBorderRadius (borderRadius );
234
230
235
231
if (Build .VERSION_CODES .HONEYCOMB < Build .VERSION .SDK_INT
236
232
&& Build .VERSION .SDK_INT < Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
237
233
final int UPDATED_LAYER_TYPE =
238
- backgroundDrawable .hasRoundedBorders ()
234
+ mReactBackgroundManager .hasRoundedBorders ()
239
235
? View .LAYER_TYPE_SOFTWARE
240
236
: View .LAYER_TYPE_HARDWARE ;
241
237
@@ -246,13 +242,12 @@ public void setBorderRadius(float borderRadius) {
246
242
}
247
243
248
244
public void setBorderRadius (float borderRadius , int position ) {
249
- ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground ();
250
- backgroundDrawable .setRadius (borderRadius , position );
245
+ mReactBackgroundManager .setBorderRadius (borderRadius , position );
251
246
252
247
if (Build .VERSION_CODES .HONEYCOMB < Build .VERSION .SDK_INT
253
248
&& Build .VERSION .SDK_INT < Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
254
249
final int UPDATED_LAYER_TYPE =
255
- backgroundDrawable .hasRoundedBorders ()
250
+ mReactBackgroundManager .hasRoundedBorders ()
256
251
? View .LAYER_TYPE_SOFTWARE
257
252
: View .LAYER_TYPE_HARDWARE ;
258
253
@@ -262,10 +257,6 @@ public void setBorderRadius(float borderRadius, int position) {
262
257
}
263
258
}
264
259
265
- public void setBorderStyle (@ Nullable String style ) {
266
- getOrCreateReactViewBackground ().setBorderStyle (style );
267
- }
268
-
269
260
@ Override
270
261
public void setRemoveClippedSubviews (boolean removeClippedSubviews ) {
271
262
if (removeClippedSubviews == mRemoveClippedSubviews ) {
@@ -600,32 +591,6 @@ public int getBackgroundColor() {
600
591
return DEFAULT_BACKGROUND_COLOR ;
601
592
}
602
593
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
-
629
594
@ Override
630
595
public @ Nullable Rect getHitSlopRect () {
631
596
return mHitSlopRect ;
@@ -640,21 +605,6 @@ public void setOverflow(String overflow) {
640
605
invalidate ();
641
606
}
642
607
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
-
658
608
@ Override
659
609
protected void dispatchDraw (Canvas canvas ) {
660
610
if (mOverflow != null ) {
0 commit comments