28
28
import com .facebook .react .modules .i18nmanager .I18nUtil ;
29
29
import com .facebook .react .uimanager .FloatUtil ;
30
30
import com .facebook .react .uimanager .Spacing ;
31
- import java .util .Arrays ;
31
+ import com .facebook .react .uimanager .style .BorderRadiusProp ;
32
+ import com .facebook .react .uimanager .style .BorderRadiusStyle ;
33
+ import com .facebook .react .uimanager .style .ComputedBorderRadius ;
32
34
import java .util .Locale ;
33
35
34
36
/**
@@ -99,7 +101,6 @@ private enum BorderStyle {
99
101
private @ Nullable PointF mInnerBottomRightCorner ;
100
102
private @ Nullable PointF mInnerBottomLeftCorner ;
101
103
private boolean mNeedUpdatePathForBorderRadius = false ;
102
- private float mBorderRadius = Float .NaN ;
103
104
104
105
/* Used by all types of background and for drawing borders */
105
106
private final Paint mPaint = new Paint (Paint .ANTI_ALIAS_FLAG );
@@ -112,25 +113,10 @@ private enum BorderStyle {
112
113
// the paths, overlapping them and closing the visible gap.
113
114
private final float mGapBetweenPaths = 0.8f ;
114
115
115
- private @ Nullable float [] mBorderCornerRadii ;
116
+ private BorderRadiusStyle mBorderRadius = new BorderRadiusStyle () ;
116
117
private final Context mContext ;
117
118
private int mLayoutDirection ;
118
119
119
- public enum BorderRadiusLocation {
120
- TOP_LEFT ,
121
- TOP_RIGHT ,
122
- BOTTOM_RIGHT ,
123
- BOTTOM_LEFT ,
124
- TOP_START ,
125
- TOP_END ,
126
- BOTTOM_START ,
127
- BOTTOM_END ,
128
- END_END ,
129
- END_START ,
130
- START_END ,
131
- START_START
132
- }
133
-
134
120
public CSSBackgroundDrawable (Context context ) {
135
121
mContext = context ;
136
122
}
@@ -146,19 +132,7 @@ public void draw(Canvas canvas) {
146
132
}
147
133
148
134
public boolean hasRoundedBorders () {
149
- if (!Float .isNaN (mBorderRadius ) && mBorderRadius > 0 ) {
150
- return true ;
151
- }
152
-
153
- if (mBorderCornerRadii != null ) {
154
- for (final float borderRadii : mBorderCornerRadii ) {
155
- if (!Float .isNaN (borderRadii ) && borderRadii > 0 ) {
156
- return true ;
157
- }
158
- }
159
- }
160
-
161
- return false ;
135
+ return mBorderRadius .hasRoundedBorders ();
162
136
}
163
137
164
138
@ Override
@@ -193,7 +167,7 @@ public int getOpacity() {
193
167
/* Android's elevation implementation requires this to be implemented to know where to draw the shadow. */
194
168
@ Override
195
169
public void getOutline (Outline outline ) {
196
- if ((! Float . isNaN ( mBorderRadius ) && mBorderRadius > 0 ) || mBorderCornerRadii != null ) {
170
+ if (hasRoundedBorders () ) {
197
171
updatePath ();
198
172
199
173
outline .setConvexPath (mPathForBorderRadiusOutline );
@@ -260,48 +234,36 @@ public void setBorderStyle(@Nullable String style) {
260
234
}
261
235
}
262
236
237
+ /**
238
+ * @deprecated Use {@link #setBorderRadius(BorderRadiusProp, Float)} instead.
239
+ */
263
240
public void setRadius (float radius ) {
264
- if (!FloatUtil .floatsEqual (mBorderRadius , radius )) {
265
- mBorderRadius = radius ;
266
- mNeedUpdatePathForBorderRadius = true ;
267
- invalidateSelf ();
268
- }
241
+ @ Nullable Float boxedRadius = Float .isNaN (radius ) ? null : Float .valueOf (radius );
242
+ setBorderRadius (BorderRadiusProp .BORDER_RADIUS , boxedRadius );
269
243
}
270
244
245
+ /**
246
+ * @deprecated Use {@link #setBorderRadius(BorderRadiusProp, Float)} instead.
247
+ */
271
248
public void setRadius (float radius , int position ) {
272
- if (mBorderCornerRadii == null ) {
273
- mBorderCornerRadii = new float [12 ];
274
- Arrays .fill (mBorderCornerRadii , Float .NaN );
275
- }
249
+ @ Nullable Float boxedRadius = Float .isNaN (radius ) ? null : Float .valueOf (radius );
250
+ setBorderRadius (BorderRadiusProp .values ()[position ], boxedRadius );
251
+ }
276
252
277
- if (!FloatUtil .floatsEqual (mBorderCornerRadii [position ], radius )) {
278
- mBorderCornerRadii [position ] = radius ;
253
+ public void setBorderRadius (BorderRadiusProp property , @ Nullable Float radius ) {
254
+ if (!FloatUtil .floatsEqual (mBorderRadius .getUniform (), radius )) {
255
+ mBorderRadius .set (property , radius );
279
256
mNeedUpdatePathForBorderRadius = true ;
280
257
invalidateSelf ();
281
258
}
282
259
}
283
260
284
- public float getFullBorderRadius () {
285
- return Float .isNaN (mBorderRadius ) ? 0 : mBorderRadius ;
286
- }
287
-
288
- public float getBorderRadius (final BorderRadiusLocation location ) {
289
- return getBorderRadiusOrDefaultTo (Float .NaN , location );
261
+ public void setBorderRadius (BorderRadiusStyle radius ) {
262
+ mBorderRadius = radius ;
290
263
}
291
264
292
- public float getBorderRadiusOrDefaultTo (
293
- final float defaultValue , final BorderRadiusLocation location ) {
294
- if (mBorderCornerRadii == null ) {
295
- return defaultValue ;
296
- }
297
-
298
- final float radius = mBorderCornerRadii [location .ordinal ()];
299
-
300
- if (Float .isNaN (radius )) {
301
- return defaultValue ;
302
- }
303
-
304
- return radius ;
265
+ public BorderRadiusStyle getBorderRadius () {
266
+ return mBorderRadius ;
305
267
}
306
268
307
269
public void setColor (int color ) {
@@ -606,95 +568,11 @@ private void updatePath() {
606
568
mTempRectForCenterDrawPath .left += borderWidth .left * 0.5f ;
607
569
mTempRectForCenterDrawPath .right -= borderWidth .right * 0.5f ;
608
570
609
- final float borderRadius = getFullBorderRadius ();
610
- float topLeftRadius = getBorderRadiusOrDefaultTo (borderRadius , BorderRadiusLocation .TOP_LEFT );
611
- float topRightRadius = getBorderRadiusOrDefaultTo (borderRadius , BorderRadiusLocation .TOP_RIGHT );
612
- float bottomLeftRadius =
613
- getBorderRadiusOrDefaultTo (borderRadius , BorderRadiusLocation .BOTTOM_LEFT );
614
- float bottomRightRadius =
615
- getBorderRadiusOrDefaultTo (borderRadius , BorderRadiusLocation .BOTTOM_RIGHT );
616
-
617
- final boolean isRTL = getResolvedLayoutDirection () == View .LAYOUT_DIRECTION_RTL ;
618
- float topStartRadius = getBorderRadius (BorderRadiusLocation .TOP_START );
619
- float topEndRadius = getBorderRadius (BorderRadiusLocation .TOP_END );
620
- float bottomStartRadius = getBorderRadius (BorderRadiusLocation .BOTTOM_START );
621
- float bottomEndRadius = getBorderRadius (BorderRadiusLocation .BOTTOM_END );
622
-
623
- float endEndRadius = getBorderRadius (BorderRadiusLocation .END_END );
624
- float endStartRadius = getBorderRadius (BorderRadiusLocation .END_START );
625
- float startEndRadius = getBorderRadius (BorderRadiusLocation .START_END );
626
- float startStartRadius = getBorderRadius (BorderRadiusLocation .START_START );
627
-
628
- if (I18nUtil .getInstance ().doLeftAndRightSwapInRTL (mContext )) {
629
- if (Float .isNaN (topStartRadius )) {
630
- topStartRadius = topLeftRadius ;
631
- }
632
-
633
- if (Float .isNaN (topEndRadius )) {
634
- topEndRadius = topRightRadius ;
635
- }
636
-
637
- if (Float .isNaN (bottomStartRadius )) {
638
- bottomStartRadius = bottomLeftRadius ;
639
- }
640
-
641
- if (Float .isNaN (bottomEndRadius )) {
642
- bottomEndRadius = bottomRightRadius ;
643
- }
644
-
645
- final float logicalTopStartRadius =
646
- Float .isNaN (topStartRadius ) ? startStartRadius : topStartRadius ;
647
- final float logicalTopEndRadius = Float .isNaN (topEndRadius ) ? startEndRadius : topEndRadius ;
648
- final float logicalBottomStartRadius =
649
- Float .isNaN (bottomStartRadius ) ? endStartRadius : bottomStartRadius ;
650
- final float logicalBottomEndRadius =
651
- Float .isNaN (bottomEndRadius ) ? endEndRadius : bottomEndRadius ;
652
-
653
- final float directionAwareTopLeftRadius = isRTL ? logicalTopEndRadius : logicalTopStartRadius ;
654
- final float directionAwareTopRightRadius =
655
- isRTL ? logicalTopStartRadius : logicalTopEndRadius ;
656
- final float directionAwareBottomLeftRadius =
657
- isRTL ? logicalBottomEndRadius : logicalBottomStartRadius ;
658
- final float directionAwareBottomRightRadius =
659
- isRTL ? logicalBottomStartRadius : logicalBottomEndRadius ;
660
-
661
- topLeftRadius = directionAwareTopLeftRadius ;
662
- topRightRadius = directionAwareTopRightRadius ;
663
- bottomLeftRadius = directionAwareBottomLeftRadius ;
664
- bottomRightRadius = directionAwareBottomRightRadius ;
665
- } else {
666
- final float logicalTopStartRadius =
667
- Float .isNaN (topStartRadius ) ? startStartRadius : topStartRadius ;
668
- final float logicalTopEndRadius = Float .isNaN (topEndRadius ) ? startEndRadius : topEndRadius ;
669
- final float logicalBottomStartRadius =
670
- Float .isNaN (bottomStartRadius ) ? endStartRadius : bottomStartRadius ;
671
- final float logicalBottomEndRadius =
672
- Float .isNaN (bottomEndRadius ) ? endEndRadius : bottomEndRadius ;
673
-
674
- final float directionAwareTopLeftRadius = isRTL ? logicalTopEndRadius : logicalTopStartRadius ;
675
- final float directionAwareTopRightRadius =
676
- isRTL ? logicalTopStartRadius : logicalTopEndRadius ;
677
- final float directionAwareBottomLeftRadius =
678
- isRTL ? logicalBottomEndRadius : logicalBottomStartRadius ;
679
- final float directionAwareBottomRightRadius =
680
- isRTL ? logicalBottomStartRadius : logicalBottomEndRadius ;
681
-
682
- if (!Float .isNaN (directionAwareTopLeftRadius )) {
683
- topLeftRadius = directionAwareTopLeftRadius ;
684
- }
685
-
686
- if (!Float .isNaN (directionAwareTopRightRadius )) {
687
- topRightRadius = directionAwareTopRightRadius ;
688
- }
689
-
690
- if (!Float .isNaN (directionAwareBottomLeftRadius )) {
691
- bottomLeftRadius = directionAwareBottomLeftRadius ;
692
- }
693
-
694
- if (!Float .isNaN (directionAwareBottomRightRadius )) {
695
- bottomRightRadius = directionAwareBottomRightRadius ;
696
- }
697
- }
571
+ ComputedBorderRadius radius = mBorderRadius .resolve (mLayoutDirection , mContext );
572
+ float topLeftRadius = radius .getTopLeft ();
573
+ float topRightRadius = radius .getTopRight ();
574
+ float bottomLeftRadius = radius .getBottomLeft ();
575
+ float bottomRightRadius = radius .getBottomRight ();
698
576
699
577
final float innerTopLeftRadiusX = Math .max (topLeftRadius - borderWidth .left , 0 );
700
578
final float innerTopLeftRadiusY = Math .max (topLeftRadius - borderWidth .top , 0 );
0 commit comments