Skip to content

Commit cc0ba57

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Minimize EditText Spans 3/9: ReactBackgroundColorSpan (#36547)
Summary: Pull Request resolved: #36547 This is part of a series of changes to minimize the number of spans committed to EditText, as a mitigation for platform issues on Samsung devices. See this [GitHub thread]( #35936 (comment)) for greater context on the platform behavior. This adds `ReactBackgroundColorSpan` to the list of spans eligible to be stripped. Changelog: [Android][Fixed] - Minimize Spans 3/N: ReactBackgroundColorSpan Reviewed By: javache Differential Revision: D44240782 fbshipit-source-id: 2ded1a1687a41cf6d5f83e89ffadd2d932089969
1 parent b9e2627 commit cc0ba57

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static com.facebook.react.views.text.TextAttributeProps.UNSET;
1212

1313
import android.content.Context;
14+
import android.graphics.Color;
1415
import android.graphics.Rect;
1516
import android.graphics.Typeface;
1617
import android.graphics.drawable.Drawable;
@@ -50,6 +51,7 @@
5051
import com.facebook.react.views.text.CustomLineHeightSpan;
5152
import com.facebook.react.views.text.CustomStyleSpan;
5253
import com.facebook.react.views.text.ReactAbsoluteSizeSpan;
54+
import com.facebook.react.views.text.ReactBackgroundColorSpan;
5355
import com.facebook.react.views.text.ReactSpan;
5456
import com.facebook.react.views.text.ReactTextUpdate;
5557
import com.facebook.react.views.text.ReactTypefaceUtils;
@@ -680,6 +682,16 @@ public boolean test(ReactAbsoluteSizeSpan span) {
680682
return span.getSize() == mTextAttributes.getEffectiveFontSize();
681683
}
682684
});
685+
686+
stripSpansOfKind(
687+
sb,
688+
ReactBackgroundColorSpan.class,
689+
new SpanPredicate<ReactBackgroundColorSpan>() {
690+
@Override
691+
public boolean test(ReactBackgroundColorSpan span) {
692+
return span.getBackgroundColor() == mReactBackgroundManager.getBackgroundColor();
693+
}
694+
});
683695
}
684696

685697
private <T> void stripSpansOfKind(
@@ -704,11 +716,17 @@ private void restoreStyleEquivalentSpans(SpannableStringBuilder workingText) {
704716
// (least precedence). This ensures the span is behind any overlapping spans.
705717
spanFlags |= Spannable.SPAN_PRIORITY;
706718

707-
workingText.setSpan(
708-
new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize()),
709-
0,
710-
workingText.length(),
711-
spanFlags);
719+
List<Object> spans = new ArrayList<>();
720+
spans.add(new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize()));
721+
722+
int backgroundColor = mReactBackgroundManager.getBackgroundColor();
723+
if (backgroundColor != Color.TRANSPARENT) {
724+
spans.add(new ReactBackgroundColorSpan(backgroundColor));
725+
}
726+
727+
for (Object span : spans) {
728+
workingText.setSpan(span, 0, workingText.length(), spanFlags);
729+
}
712730
}
713731

714732
private static boolean sameTextForSpan(

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundManager.java

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class ReactViewBackgroundManager {
1919

2020
private @Nullable ReactViewBackgroundDrawable mReactBackgroundDrawable;
2121
private View mView;
22+
private int mColor = Color.TRANSPARENT;
2223

2324
public ReactViewBackgroundManager(View view) {
2425
this.mView = view;
@@ -56,6 +57,10 @@ public void setBackgroundColor(int color) {
5657
}
5758
}
5859

60+
public int getBackgroundColor() {
61+
return mColor;
62+
}
63+
5964
public void setBorderWidth(int position, float width) {
6065
getOrCreateReactViewBackground().setBorderWidth(position, width);
6166
}

0 commit comments

Comments
 (0)