Skip to content

Commit e7e2556

Browse files
NickGerlemankelset
authored andcommittedApr 19, 2023
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 7374892 commit e7e2556

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed
 

‎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;
@@ -645,6 +647,16 @@ public boolean test(ReactAbsoluteSizeSpan span) {
645647
return span.getSize() == mTextAttributes.getEffectiveFontSize();
646648
}
647649
});
650+
651+
stripSpansOfKind(
652+
sb,
653+
ReactBackgroundColorSpan.class,
654+
new SpanPredicate<ReactBackgroundColorSpan>() {
655+
@Override
656+
public boolean test(ReactBackgroundColorSpan span) {
657+
return span.getBackgroundColor() == mReactBackgroundManager.getBackgroundColor();
658+
}
659+
});
648660
}
649661

650662
private <T> void stripSpansOfKind(
@@ -669,11 +681,17 @@ private void restoreStyleEquivalentSpans(SpannableStringBuilder workingText) {
669681
// (least precedence). This ensures the span is behind any overlapping spans.
670682
spanFlags |= Spannable.SPAN_PRIORITY;
671683

672-
workingText.setSpan(
673-
new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize()),
674-
0,
675-
workingText.length(),
676-
spanFlags);
684+
List<Object> spans = new ArrayList<>();
685+
spans.add(new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize()));
686+
687+
int backgroundColor = mReactBackgroundManager.getBackgroundColor();
688+
if (backgroundColor != Color.TRANSPARENT) {
689+
spans.add(new ReactBackgroundColorSpan(backgroundColor));
690+
}
691+
692+
for (Object span : spans) {
693+
workingText.setSpan(span, 0, workingText.length(), spanFlags);
694+
}
677695
}
678696

679697
private static boolean sameTextForSpan(

‎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;
@@ -50,6 +51,10 @@ public void setBackgroundColor(int color) {
5051
}
5152
}
5253

54+
public int getBackgroundColor() {
55+
return mColor;
56+
}
57+
5358
public void setBorderWidth(int position, float width) {
5459
getOrCreateReactViewBackground().setBorderWidth(position, width);
5560
}

0 commit comments

Comments
 (0)
Please sign in to comment.