Minimize EditText Spans 3/9: ReactBackgroundColorSpan (#36547)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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]( https://github.com/facebook/react-native/issues/35936#issuecomment-1411437789) 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
This commit is contained in:
Nick Gerleman
2023-03-24 05:24:09 -07:00
committed by Lorenzo Sciandra
parent 485b8ef156
commit 3cdcbe7e72
2 changed files with 28 additions and 5 deletions
@@ -11,6 +11,7 @@ import static com.facebook.react.uimanager.UIManagerHelper.getReactContext;
import static com.facebook.react.views.text.TextAttributeProps.UNSET;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
@@ -50,6 +51,7 @@ import com.facebook.react.views.text.CustomLetterSpacingSpan;
import com.facebook.react.views.text.CustomLineHeightSpan;
import com.facebook.react.views.text.CustomStyleSpan;
import com.facebook.react.views.text.ReactAbsoluteSizeSpan;
import com.facebook.react.views.text.ReactBackgroundColorSpan;
import com.facebook.react.views.text.ReactSpan;
import com.facebook.react.views.text.ReactTextUpdate;
import com.facebook.react.views.text.ReactTypefaceUtils;
@@ -644,6 +646,16 @@ public class ReactEditText extends AppCompatEditText
return span.getSize() == mTextAttributes.getEffectiveFontSize();
}
});
stripSpansOfKind(
sb,
ReactBackgroundColorSpan.class,
new SpanPredicate<ReactBackgroundColorSpan>() {
@Override
public boolean test(ReactBackgroundColorSpan span) {
return span.getBackgroundColor() == mReactBackgroundManager.getBackgroundColor();
}
});
}
private <T> void stripSpansOfKind(
@@ -668,11 +680,17 @@ public class ReactEditText extends AppCompatEditText
// (least precedence). This ensures the span is behind any overlapping spans.
spanFlags |= Spannable.SPAN_PRIORITY;
workingText.setSpan(
new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize()),
0,
workingText.length(),
spanFlags);
List<Object> spans = new ArrayList<>();
spans.add(new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize()));
int backgroundColor = mReactBackgroundManager.getBackgroundColor();
if (backgroundColor != Color.TRANSPARENT) {
spans.add(new ReactBackgroundColorSpan(backgroundColor));
}
for (Object span : spans) {
workingText.setSpan(span, 0, workingText.length(), spanFlags);
}
}
private static boolean sameTextForSpan(
@@ -19,6 +19,7 @@ public class ReactViewBackgroundManager {
private @Nullable ReactViewBackgroundDrawable mReactBackgroundDrawable;
private View mView;
private int mColor = Color.TRANSPARENT;
public ReactViewBackgroundManager(View view) {
this.mView = view;
@@ -50,6 +51,10 @@ public class ReactViewBackgroundManager {
}
}
public int getBackgroundColor() {
return mColor;
}
public void setBorderWidth(int position, float width) {
getOrCreateReactViewBackground().setBorderWidth(position, width);
}