Skip to content

Commit

Permalink
Minimize Spans 3/N: ReactBackgroundColorSpan
Browse files Browse the repository at this point in the history
Summary:
This adds `ReactBackgroundColorSpan` to the list of spans eligible to be stripped.

Changelog:
[Android][Fixed] - Minimize Spans 3/N: ReactBackgroundColorSpan

Differential Revision: https://www.internalfb.com/diff/D44240782?entry_point=27

fbshipit-source-id: 23b1a3f08071fb5ca3c023f5b854f1e71f9d10d9
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Mar 24, 2023
1 parent 1d7e498 commit 2e2bba3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -50,6 +51,7 @@
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;
Expand Down Expand Up @@ -680,6 +682,16 @@ public boolean test(ReactAbsoluteSizeSpan span) {
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(
Expand All @@ -704,11 +716,17 @@ private void restoreStyleEquivalentSpans(SpannableStringBuilder workingText) {
// (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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +57,10 @@ public void setBackgroundColor(int color) {
}
}

public int getBackgroundColor() {
return mColor;
}

public void setBorderWidth(int position, float width) {
getOrCreateReactViewBackground().setBorderWidth(position, width);
}
Expand Down

0 comments on commit 2e2bba3

Please sign in to comment.