Make direction-aware borders work with APIs >= 17

Reviewed By: achen1

Differential Revision: D6189497

fbshipit-source-id: 848ca35540c5a4eb1581e0b7c39f8fa880540317
This commit is contained in:
Ramanpreet Nara 2017-11-08 10:27:39 -08:00 committed by Facebook Github Bot
parent 0bbd9f042a
commit 7170543e80
2 changed files with 34 additions and 14 deletions

View File

@ -109,6 +109,7 @@ public class ReactViewBackgroundDrawable extends Drawable {
private @Nullable float[] mBorderCornerRadii;
private final Context mContext;
private int mLayoutDirection;
public enum BorderRadiusLocation {
TOP_LEFT,
@ -290,6 +291,25 @@ public class ReactViewBackgroundDrawable extends Drawable {
invalidateSelf();
}
/** Similar to Drawable.getLayoutDirection, but available in APIs < 23. */
public int getResolvedLayoutDirection() {
return mLayoutDirection;
}
/** Similar to Drawable.setLayoutDirection, but available in APIs < 23. */
public boolean setResolvedLayoutDirection(int layoutDirection) {
if (mLayoutDirection != layoutDirection) {
mLayoutDirection = layoutDirection;
return onResolvedLayoutDirectionChanged(layoutDirection);
}
return false;
}
/** Similar to Drawable.onLayoutDirectionChanged, but available in APIs < 23. */
public boolean onResolvedLayoutDirectionChanged(int layoutDirection) {
return false;
}
@VisibleForTesting
public int getColor() {
return mColor;
@ -323,8 +343,8 @@ public class ReactViewBackgroundDrawable extends Drawable {
int colorRight = getBorderColor(Spacing.RIGHT);
int colorBottom = getBorderColor(Spacing.BOTTOM);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
int colorStart = getBorderColor(Spacing.START);
int colorEnd = getBorderColor(Spacing.END);
@ -478,13 +498,13 @@ public class ReactViewBackgroundDrawable extends Drawable {
float bottomRightRadius =
getBorderRadiusOrDefaultTo(borderRadius, BorderRadiusLocation.BOTTOM_RIGHT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
float topStartRadius = getBorderRadius(BorderRadiusLocation.TOP_START);
float topEndRadius = getBorderRadius(BorderRadiusLocation.TOP_END);
float bottomStartRadius = getBorderRadius(BorderRadiusLocation.BOTTOM_START);
float bottomEndRadius = getBorderRadius(BorderRadiusLocation.BOTTOM_END);
if (I18nUtil.getInstance().doLeftAndRightSwapInRTL(mContext)) {
if (YogaConstants.isUndefined(topStartRadius)) {
topStartRadius = topLeftRadius;
@ -930,8 +950,8 @@ public class ReactViewBackgroundDrawable extends Drawable {
int colorRight = getBorderColor(Spacing.RIGHT);
int colorBottom = getBorderColor(Spacing.BOTTOM);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
int colorStart = getBorderColor(Spacing.START);
int colorEnd = getBorderColor(Spacing.END);
@ -1140,8 +1160,8 @@ public class ReactViewBackgroundDrawable extends Drawable {
float borderLeftWidth = getBorderWidthOrDefaultTo(borderWidth, Spacing.LEFT);
float borderRightWidth = getBorderWidthOrDefaultTo(borderWidth, Spacing.RIGHT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mBorderWidth != null) {
final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mBorderWidth != null) {
final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
float borderStartWidth = mBorderWidth.getRaw(Spacing.START);
float borderEndWidth = mBorderWidth.getRaw(Spacing.END);

View File

@ -129,9 +129,9 @@ public class ReactViewGroup extends ViewGroup implements
@Override
public void onRtlPropertiesChanged(int layoutDirection) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (mReactBackgroundDrawable != null) {
mReactBackgroundDrawable.setLayoutDirection(mLayoutDirection);
mReactBackgroundDrawable.setResolvedLayoutDirection(mLayoutDirection);
}
}
}
@ -589,12 +589,12 @@ public class ReactViewGroup extends ViewGroup implements
updateBackgroundDrawable(layerDrawable);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mLayoutDirection =
I18nUtil.getInstance().isRTL(getContext())
? LAYOUT_DIRECTION_RTL
: LAYOUT_DIRECTION_LTR;
mReactBackgroundDrawable.setLayoutDirection(mLayoutDirection);
mReactBackgroundDrawable.setResolvedLayoutDirection(mLayoutDirection);
}
}
return mReactBackgroundDrawable;
@ -671,7 +671,7 @@ public class ReactViewGroup extends ViewGroup implements
mReactBackgroundDrawable.getBorderRadiusOrDefaultTo(
borderRadius, ReactViewBackgroundDrawable.BorderRadiusLocation.BOTTOM_RIGHT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
final boolean isRTL = mLayoutDirection == View.LAYOUT_DIRECTION_RTL;
float topStartBorderRadius =
mReactBackgroundDrawable.getBorderRadius(