Skip to content

Commit 7170543

Browse files
RSNarafacebook-github-bot
authored andcommittedNov 8, 2017
Make direction-aware borders work with APIs >= 17
Reviewed By: achen1 Differential Revision: D6189497 fbshipit-source-id: 848ca35540c5a4eb1581e0b7c39f8fa880540317
1 parent 0bbd9f0 commit 7170543

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed
 

‎ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java

+29-9
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ private static enum BorderStyle {
109109

110110
private @Nullable float[] mBorderCornerRadii;
111111
private final Context mContext;
112+
private int mLayoutDirection;
112113

113114
public enum BorderRadiusLocation {
114115
TOP_LEFT,
@@ -290,6 +291,25 @@ public void setColor(int color) {
290291
invalidateSelf();
291292
}
292293

294+
/** Similar to Drawable.getLayoutDirection, but available in APIs < 23. */
295+
public int getResolvedLayoutDirection() {
296+
return mLayoutDirection;
297+
}
298+
299+
/** Similar to Drawable.setLayoutDirection, but available in APIs < 23. */
300+
public boolean setResolvedLayoutDirection(int layoutDirection) {
301+
if (mLayoutDirection != layoutDirection) {
302+
mLayoutDirection = layoutDirection;
303+
return onResolvedLayoutDirectionChanged(layoutDirection);
304+
}
305+
return false;
306+
}
307+
308+
/** Similar to Drawable.onLayoutDirectionChanged, but available in APIs < 23. */
309+
public boolean onResolvedLayoutDirectionChanged(int layoutDirection) {
310+
return false;
311+
}
312+
293313
@VisibleForTesting
294314
public int getColor() {
295315
return mColor;
@@ -323,8 +343,8 @@ private void drawRoundedBackgroundWithBorders(Canvas canvas) {
323343
int colorRight = getBorderColor(Spacing.RIGHT);
324344
int colorBottom = getBorderColor(Spacing.BOTTOM);
325345

326-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
327-
final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
346+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
347+
final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
328348
int colorStart = getBorderColor(Spacing.START);
329349
int colorEnd = getBorderColor(Spacing.END);
330350

@@ -478,13 +498,13 @@ private void updatePath() {
478498
float bottomRightRadius =
479499
getBorderRadiusOrDefaultTo(borderRadius, BorderRadiusLocation.BOTTOM_RIGHT);
480500

481-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
482-
final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
501+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
502+
final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
483503
float topStartRadius = getBorderRadius(BorderRadiusLocation.TOP_START);
484504
float topEndRadius = getBorderRadius(BorderRadiusLocation.TOP_END);
485505
float bottomStartRadius = getBorderRadius(BorderRadiusLocation.BOTTOM_START);
486506
float bottomEndRadius = getBorderRadius(BorderRadiusLocation.BOTTOM_END);
487-
507+
488508
if (I18nUtil.getInstance().doLeftAndRightSwapInRTL(mContext)) {
489509
if (YogaConstants.isUndefined(topStartRadius)) {
490510
topStartRadius = topLeftRadius;
@@ -930,8 +950,8 @@ private void drawRectangularBackgroundWithBorders(Canvas canvas) {
930950
int colorRight = getBorderColor(Spacing.RIGHT);
931951
int colorBottom = getBorderColor(Spacing.BOTTOM);
932952

933-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
934-
final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
953+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
954+
final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
935955
int colorStart = getBorderColor(Spacing.START);
936956
int colorEnd = getBorderColor(Spacing.END);
937957

@@ -1140,8 +1160,8 @@ public RectF getDirectionAwareBorderInsets() {
11401160
float borderLeftWidth = getBorderWidthOrDefaultTo(borderWidth, Spacing.LEFT);
11411161
float borderRightWidth = getBorderWidthOrDefaultTo(borderWidth, Spacing.RIGHT);
11421162

1143-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mBorderWidth != null) {
1144-
final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
1163+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mBorderWidth != null) {
1164+
final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
11451165
float borderStartWidth = mBorderWidth.getRaw(Spacing.START);
11461166
float borderEndWidth = mBorderWidth.getRaw(Spacing.END);
11471167

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
129129

130130
@Override
131131
public void onRtlPropertiesChanged(int layoutDirection) {
132-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
132+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
133133
if (mReactBackgroundDrawable != null) {
134-
mReactBackgroundDrawable.setLayoutDirection(mLayoutDirection);
134+
mReactBackgroundDrawable.setResolvedLayoutDirection(mLayoutDirection);
135135
}
136136
}
137137
}
@@ -589,12 +589,12 @@ private ReactViewBackgroundDrawable getOrCreateReactViewBackground() {
589589
updateBackgroundDrawable(layerDrawable);
590590
}
591591

592-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
592+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
593593
mLayoutDirection =
594594
I18nUtil.getInstance().isRTL(getContext())
595595
? LAYOUT_DIRECTION_RTL
596596
: LAYOUT_DIRECTION_LTR;
597-
mReactBackgroundDrawable.setLayoutDirection(mLayoutDirection);
597+
mReactBackgroundDrawable.setResolvedLayoutDirection(mLayoutDirection);
598598
}
599599
}
600600
return mReactBackgroundDrawable;
@@ -671,7 +671,7 @@ protected void dispatchDraw(Canvas canvas) {
671671
mReactBackgroundDrawable.getBorderRadiusOrDefaultTo(
672672
borderRadius, ReactViewBackgroundDrawable.BorderRadiusLocation.BOTTOM_RIGHT);
673673

674-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
674+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
675675
final boolean isRTL = mLayoutDirection == View.LAYOUT_DIRECTION_RTL;
676676
float topStartBorderRadius =
677677
mReactBackgroundDrawable.getBorderRadius(

0 commit comments

Comments
 (0)
Please sign in to comment.