Remove ReactEditText Legacy Background Path (#46162)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/46162

## This Diff

This removes the legacy path from ReactEditText and its view manager.

## This Stack

This removes the non-Style-applicator background management paths of the different native components. There have been multiple conflicting changes, and bugs added bc harder to reason about, which motivates making this change as soon as possible. This also lets us formalize guarantees that BaseViewManager may safely manipulate background styling of all built in native components.

There is one still known issue, where BackgroundStyleApplicator does not propagate I18nManager derived layout direction to borders (compared to Android derived root direction). This is mostly an issue for apps that with LTR and RTL context, or force a layout direction, which I would guess is relatively rare, so my plan is to forward fix this later this by enabling set_android_layout_direction which will solve that problem mopre generically.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D61658080

fbshipit-source-id: 6ac7c5ed230e44fe307640a730e076b903e0674a
This commit is contained in:
Nick Gerleman
2024-09-16 19:14:53 -07:00
committed by Facebook GitHub Bot
parent b4d28293a6
commit 426b3004ac
2 changed files with 38 additions and 105 deletions
@@ -49,7 +49,6 @@ import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactSoftExceptionLogger;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags;
import com.facebook.react.uimanager.BackgroundStyleApplicator;
import com.facebook.react.uimanager.LengthPercentage;
import com.facebook.react.uimanager.LengthPercentageType;
@@ -76,7 +75,6 @@ import com.facebook.react.views.text.internal.span.ReactSpan;
import com.facebook.react.views.text.internal.span.ReactStrikethroughSpan;
import com.facebook.react.views.text.internal.span.ReactUnderlineSpan;
import com.facebook.react.views.text.internal.span.TextInlineImageSpan;
import com.facebook.react.views.view.ReactViewBackgroundManager;
import java.util.ArrayList;
import java.util.Objects;
@@ -133,8 +131,6 @@ public class ReactEditText extends AppCompatEditText {
private @Nullable String mPlaceholder = null;
private Overflow mOverflow = Overflow.VISIBLE;
private final ReactViewBackgroundManager mReactBackgroundManager;
private StateWrapper mStateWrapper = null;
protected boolean mDisableTextDiffing = false;
@@ -147,7 +143,6 @@ public class ReactEditText extends AppCompatEditText {
super(context);
setFocusableInTouchMode(false);
mReactBackgroundManager = new ReactViewBackgroundManager(this);
mInputMethodManager =
(InputMethodManager)
Assertions.assertNotNull(context.getSystemService(Context.INPUT_METHOD_SERVICE));
@@ -765,7 +760,9 @@ public class ReactEditText extends AppCompatEditText {
stripSpansOfKind(
sb,
ReactBackgroundColorSpan.class,
(span) -> span.getBackgroundColor() == mReactBackgroundManager.getBackgroundColor());
(span) ->
Objects.equals(
span.getBackgroundColor(), BackgroundStyleApplicator.getBackgroundColor(this)));
stripSpansOfKind(
sb,
@@ -827,8 +824,8 @@ public class ReactEditText extends AppCompatEditText {
workingText.setSpan(
new ReactForegroundColorSpan(getCurrentTextColor()), 0, workingText.length(), spanFlags);
int backgroundColor = mReactBackgroundManager.getBackgroundColor();
if (backgroundColor != Color.TRANSPARENT) {
@Nullable Integer backgroundColor = BackgroundStyleApplicator.getBackgroundColor(this);
if (backgroundColor != null && backgroundColor != Color.TRANSPARENT) {
workingText.setSpan(
new ReactBackgroundColorSpan(backgroundColor), 0, workingText.length(), spanFlags);
}
@@ -1087,39 +1084,23 @@ public class ReactEditText extends AppCompatEditText {
@Override
public void setBackgroundColor(int color) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
BackgroundStyleApplicator.setBackgroundColor(this, color);
} else {
mReactBackgroundManager.setBackgroundColor(color);
}
BackgroundStyleApplicator.setBackgroundColor(this, color);
}
public void setBorderWidth(int position, float width) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
BackgroundStyleApplicator.setBorderWidth(
this, LogicalEdge.values()[position], PixelUtil.toDIPFromPixel(width));
} else {
mReactBackgroundManager.setBorderWidth(position, width);
}
BackgroundStyleApplicator.setBorderWidth(
this, LogicalEdge.values()[position], PixelUtil.toDIPFromPixel(width));
}
public void setBorderColor(int position, @Nullable Integer color) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
BackgroundStyleApplicator.setBorderColor(this, LogicalEdge.values()[position], color);
} else {
mReactBackgroundManager.setBorderColor(position, color);
}
BackgroundStyleApplicator.setBorderColor(this, LogicalEdge.values()[position], color);
}
public int getBorderColor(int position) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
@Nullable
Integer borderColor =
BackgroundStyleApplicator.getBorderColor(this, LogicalEdge.values()[position]);
return borderColor == null ? Color.TRANSPARENT : borderColor;
} else {
return mReactBackgroundManager.getBorderColor(position);
}
@Nullable
Integer borderColor =
BackgroundStyleApplicator.getBorderColor(this, LogicalEdge.values()[position]);
return borderColor == null ? Color.TRANSPARENT : borderColor;
}
public void setBorderRadius(float borderRadius) {
@@ -1127,26 +1108,18 @@ public class ReactEditText extends AppCompatEditText {
}
public void setBorderRadius(float borderRadius, int position) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
@Nullable
LengthPercentage radius =
Float.isNaN(borderRadius)
? null
: new LengthPercentage(
PixelUtil.toDIPFromPixel(borderRadius), LengthPercentageType.POINT);
BackgroundStyleApplicator.setBorderRadius(this, BorderRadiusProp.values()[position], radius);
} else {
mReactBackgroundManager.setBorderRadius(borderRadius, position);
}
@Nullable
LengthPercentage radius =
Float.isNaN(borderRadius)
? null
: new LengthPercentage(
PixelUtil.toDIPFromPixel(borderRadius), LengthPercentageType.POINT);
BackgroundStyleApplicator.setBorderRadius(this, BorderRadiusProp.values()[position], radius);
}
public void setBorderStyle(@Nullable String style) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
BackgroundStyleApplicator.setBorderStyle(
this, style == null ? null : BorderStyle.fromString(style));
} else {
mReactBackgroundManager.setBorderStyle(style);
}
BackgroundStyleApplicator.setBorderStyle(
this, style == null ? null : BorderStyle.fromString(style));
}
public void setLetterSpacingPt(float letterSpacingPt) {
@@ -1300,18 +1273,13 @@ public class ReactEditText extends AppCompatEditText {
mOverflow = parsedOverflow == null ? Overflow.VISIBLE : parsedOverflow;
}
mReactBackgroundManager.setOverflow(overflow);
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
if (mOverflow != Overflow.VISIBLE) {
BackgroundStyleApplicator.clipToPaddingBox(this, canvas);
}
} else {
mReactBackgroundManager.maybeClipToPaddingBox(canvas);
if (mOverflow != Overflow.VISIBLE) {
BackgroundStyleApplicator.clipToPaddingBox(this, canvas);
}
super.onDraw(canvas);
@@ -45,7 +45,6 @@ import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.mapbuffer.MapBuffer;
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.BackgroundStyleApplicator;
import com.facebook.react.uimanager.BaseViewManager;
@@ -957,36 +956,20 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
},
defaultFloat = Float.NaN)
public void setBorderRadius(ReactEditText view, int index, float borderRadius) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
@Nullable
LengthPercentage radius =
Float.isNaN(borderRadius)
? null
: new LengthPercentage(borderRadius, LengthPercentageType.POINT);
BackgroundStyleApplicator.setBorderRadius(view, BorderRadiusProp.values()[index], radius);
} else {
if (!Float.isNaN(borderRadius)) {
borderRadius = PixelUtil.toPixelFromDIP(borderRadius);
}
if (index == 0) {
view.setBorderRadius(borderRadius);
} else {
view.setBorderRadius(borderRadius, index - 1);
}
}
@Nullable
LengthPercentage radius =
Float.isNaN(borderRadius)
? null
: new LengthPercentage(borderRadius, LengthPercentageType.POINT);
BackgroundStyleApplicator.setBorderRadius(view, BorderRadiusProp.values()[index], radius);
}
@ReactProp(name = "borderStyle")
public void setBorderStyle(ReactEditText view, @Nullable String borderStyle) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
@Nullable
BorderStyle parsedBorderStyle =
borderStyle == null ? null : BorderStyle.fromString(borderStyle);
BackgroundStyleApplicator.setBorderStyle(view, parsedBorderStyle);
} else {
view.setBorderStyle(borderStyle);
}
@Nullable
BorderStyle parsedBorderStyle =
borderStyle == null ? null : BorderStyle.fromString(borderStyle);
BackgroundStyleApplicator.setBorderStyle(view, parsedBorderStyle);
}
@ReactProp(name = "showSoftInputOnFocus", defaultBoolean = true)
@@ -1023,14 +1006,7 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
},
defaultFloat = Float.NaN)
public void setBorderWidth(ReactEditText view, int index, float width) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
BackgroundStyleApplicator.setBorderWidth(view, LogicalEdge.values()[index], width);
} else {
if (!Float.isNaN(width)) {
width = PixelUtil.toPixelFromDIP(width);
}
view.setBorderWidth(SPACING_TYPES[index], width);
}
BackgroundStyleApplicator.setBorderWidth(view, LogicalEdge.values()[index], width);
}
@ReactPropGroup(
@@ -1043,12 +1019,7 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
},
customType = "Color")
public void setBorderColor(ReactEditText view, int index, @Nullable Integer color) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
BackgroundStyleApplicator.setBorderColor(view, LogicalEdge.ALL, color);
} else {
view.setBorderColor(SPACING_TYPES[index], color);
}
BackgroundStyleApplicator.setBorderColor(view, LogicalEdge.ALL, color);
}
@ReactProp(name = "overflow")
@@ -1058,18 +1029,12 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
@ReactProp(name = ViewProps.BOX_SHADOW, customType = "BoxShadow")
public void setBoxShadow(ReactEditText view, @Nullable ReadableArray shadows) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
BackgroundStyleApplicator.setBoxShadow(view, shadows);
}
BackgroundStyleApplicator.setBoxShadow(view, shadows);
}
@Override
public void setBackgroundColor(ReactEditText view, @ColorInt int backgroundColor) {
if (ReactNativeFeatureFlags.enableBackgroundStyleApplicator()) {
BackgroundStyleApplicator.setBackgroundColor(view, backgroundColor);
} else {
super.setBackgroundColor(view, backgroundColor);
}
BackgroundStyleApplicator.setBackgroundColor(view, backgroundColor);
}
@Override