Skip to content

Commit 3334b68

Browse files
committed
update Android text to support color longs
1 parent 9c00d6d commit 3334b68

File tree

10 files changed

+88
-40
lines changed

10 files changed

+88
-40
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactStylesDiffMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public int getInt(String name, int restoreNullToDefaultValue) {
7272
return mBackingMap.isNull(name) ? restoreNullToDefaultValue : mBackingMap.getInt(name);
7373
}
7474

75+
public long getLong(String name, long restoreNullToDefaultValue) {
76+
return mBackingMap.isNull(name) ? restoreNullToDefaultValue : mBackingMap.getLong(name);
77+
}
78+
7579
@Nullable
7680
public String getString(String name) {
7781
return mBackingMap.getString(name);

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/BasicTextAttributeProvider.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ internal interface BasicTextAttributeProvider {
2020

2121
val isBackgroundColorSet: Boolean
2222

23-
val backgroundColor: Int
23+
val backgroundColor: Long
2424

2525
val isColorSet: Boolean
2626

27-
val color: Int
27+
val color: Long
2828

2929
val fontStyle: Int
3030

@@ -44,5 +44,5 @@ internal interface BasicTextAttributeProvider {
4444

4545
val textShadowRadius: Float
4646

47-
val textShadowColor: Int
47+
val textShadowColor: Long
4848
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,9 @@ protected Spannable spannedFromShadowNode(
427427
protected TextAttributes mTextAttributes;
428428

429429
protected boolean mIsColorSet = false;
430-
protected int mColor;
430+
protected long mColor;
431431
protected boolean mIsBackgroundColorSet = false;
432-
protected int mBackgroundColor;
432+
protected long mBackgroundColor;
433433

434434
protected @Nullable AccessibilityRole mAccessibilityRole = null;
435435
protected @Nullable Role mRole = null;
@@ -444,7 +444,7 @@ protected Spannable spannedFromShadowNode(
444444
protected float mTextShadowOffsetDx = 0;
445445
protected float mTextShadowOffsetDy = 0;
446446
protected float mTextShadowRadius = 0;
447-
protected int mTextShadowColor = DEFAULT_TEXT_SHADOW_COLOR;
447+
protected long mTextShadowColor = Color.pack(DEFAULT_TEXT_SHADOW_COLOR);
448448

449449
protected boolean mIsUnderlineTextDecorationSet = false;
450450
protected boolean mIsLineThroughTextDecorationSet = false;
@@ -583,12 +583,12 @@ public void setFontSize(float fontSize) {
583583
}
584584

585585
@Override
586-
public int getColor() {
586+
public long getColor() {
587587
return mColor;
588588
}
589589

590590
@ReactProp(name = ViewProps.COLOR, customType = "Color")
591-
public void setColor(@Nullable Integer color) {
591+
public void setColor(@Nullable Long color) {
592592
mIsColorSet = (color != null);
593593
if (mIsColorSet) {
594594
mColor = color;
@@ -602,12 +602,12 @@ public boolean isColorSet() {
602602
}
603603

604604
@Override
605-
public int getBackgroundColor() {
605+
public long getBackgroundColor() {
606606
return mBackgroundColor;
607607
}
608608

609609
@ReactProp(name = ViewProps.BACKGROUND_COLOR, customType = "Color")
610-
public void setBackgroundColor(@Nullable Integer color) {
610+
public void setBackgroundColor(@Nullable Long color) {
611611
// Background color needs to be handled here for virtual nodes so it can be incorporated into
612612
// the span. However, it doesn't need to be applied to non-virtual nodes because non-virtual
613613
// nodes get mapped to native views and native views get their background colors get set via
@@ -798,12 +798,12 @@ public void setTextShadowRadius(float textShadowRadius) {
798798
}
799799

800800
@Override
801-
public int getTextShadowColor() {
801+
public long getTextShadowColor() {
802802
return mTextShadowColor;
803803
}
804804

805805
@ReactProp(name = PROP_SHADOW_COLOR, defaultInt = DEFAULT_TEXT_SHADOW_COLOR, customType = "Color")
806-
public void setTextShadowColor(int textShadowColor) {
806+
public void setTextShadowColor(long textShadowColor) {
807807
if (textShadowColor != mTextShadowColor) {
808808
mTextShadowColor = textShadowColor;
809809
markUpdated();

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextAnchorViewManager.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package com.facebook.react.views.text;
99

10+
import android.graphics.Color;
1011
import android.text.Layout;
1112
import android.text.Spannable;
1213
import android.text.TextUtils;
@@ -106,12 +107,12 @@ public void setSelectable(ReactTextView view, boolean isSelectable) {
106107
}
107108

108109
@ReactProp(name = "selectionColor", customType = "Color")
109-
public void setSelectionColor(ReactTextView view, @Nullable Integer color) {
110+
public void setSelectionColor(ReactTextView view, @Nullable Long color) {
110111
if (color == null) {
111112
view.setHighlightColor(
112113
DefaultStyleValuesUtil.getDefaultTextColorHighlight(view.getContext()));
113114
} else {
114-
view.setHighlightColor(color);
115+
view.setHighlightColor(Color.toArgb(color));
115116
}
116117
}
117118

@@ -180,11 +181,9 @@ public void setBorderWidth(ReactTextView view, int index, float width) {
180181
"borderBottomColor"
181182
},
182183
customType = "Color")
183-
public void setBorderColor(ReactTextView view, int index, Integer color) {
184-
float rgbComponent =
185-
color == null ? YogaConstants.UNDEFINED : (float) ((int) color & 0x00FFFFFF);
186-
float alphaComponent = color == null ? YogaConstants.UNDEFINED : (float) ((int) color >>> 24);
187-
view.setBorderColor(SPACING_TYPES[index], rgbComponent, alphaComponent);
184+
public void setBorderColor(ReactTextView view, int index, Long color) {
185+
long borderColor = color == null ? 0 : color;
186+
view.setBorderColor(SPACING_TYPES[index], borderColor);
188187
}
189188

190189
@ReactProp(name = ViewProps.INCLUDE_FONT_PADDING, defaultBoolean = true)

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,16 @@ public void setBackgroundColor(int color) {
626626
mReactBackgroundManager.setBackgroundColor(color);
627627
}
628628

629+
public void setBackgroundColor(long color) {
630+
mReactBackgroundManager.setBackgroundColor(color);
631+
}
632+
629633
public void setBorderWidth(int position, float width) {
630634
mReactBackgroundManager.setBorderWidth(position, width);
631635
}
632636

633-
public void setBorderColor(int position, float color, float alpha) {
634-
mReactBackgroundManager.setBorderColor(position, color, alpha);
637+
public void setBorderColor(int position, long color) {
638+
mReactBackgroundManager.setBorderColor(position, color);
635639
}
636640

637641
public void setBorderRadius(float borderRadius) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package com.facebook.react.views.text;
99

10+
import android.graphics.Color;
1011
import android.os.Build;
1112
import android.text.Layout;
1213
import android.text.TextUtils;
@@ -72,7 +73,7 @@ public class TextAttributeProps implements EffectiveTextAttributeProvider {
7273

7374
private static final String PROP_TEXT_TRANSFORM = "textTransform";
7475

75-
private static final int DEFAULT_TEXT_SHADOW_COLOR = 0x55000000;
76+
private static final long DEFAULT_TEXT_SHADOW_COLOR = Color.pack(0x55000000);
7677
private static final int DEFAULT_JUSTIFICATION_MODE =
7778
(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) ? 0 : Layout.JUSTIFICATION_MODE_NONE;
7879
private static final int DEFAULT_BREAK_STRATEGY = Layout.BREAK_STRATEGY_HIGH_QUALITY;
@@ -81,9 +82,9 @@ public class TextAttributeProps implements EffectiveTextAttributeProvider {
8182
protected float mLineHeight = Float.NaN;
8283
protected boolean mIsColorSet = false;
8384
protected boolean mAllowFontScaling = true;
84-
protected int mColor;
85+
protected long mColor;
8586
protected boolean mIsBackgroundColorSet = false;
86-
protected int mBackgroundColor;
87+
protected long mBackgroundColor;
8788

8889
protected int mNumberOfLines = ReactConstants.UNSET;
8990
protected int mFontSize = ReactConstants.UNSET;
@@ -100,7 +101,7 @@ public class TextAttributeProps implements EffectiveTextAttributeProvider {
100101
protected float mTextShadowOffsetDx = 0;
101102
protected float mTextShadowOffsetDy = 0;
102103
protected float mTextShadowRadius = 0;
103-
protected int mTextShadowColor = DEFAULT_TEXT_SHADOW_COLOR;
104+
protected long mTextShadowColor = DEFAULT_TEXT_SHADOW_COLOR;
104105

105106
protected boolean mIsUnderlineTextDecorationSet = false;
106107
protected boolean mIsLineThroughTextDecorationSet = false;
@@ -155,10 +156,10 @@ public static TextAttributeProps fromMapBuffer(MapBuffer props) {
155156
MapBuffer.Entry entry = iterator.next();
156157
switch (entry.getKey()) {
157158
case TA_KEY_FOREGROUND_COLOR:
158-
result.setColor(entry.getIntValue());
159+
result.setColor(entry.getLongValue());
159160
break;
160161
case TA_KEY_BACKGROUND_COLOR:
161-
result.setBackgroundColor(entry.getIntValue());
162+
result.setBackgroundColor(entry.getLongValue());
162163
break;
163164
case TA_KEY_OPACITY:
164165
break;
@@ -203,7 +204,7 @@ public static TextAttributeProps fromMapBuffer(MapBuffer props) {
203204
result.setTextShadowRadius((float) entry.getDoubleValue());
204205
break;
205206
case TA_KEY_TEXT_SHADOW_COLOR:
206-
result.setTextShadowColor(entry.getIntValue());
207+
result.setTextShadowColor(entry.getLongValue());
207208
break;
208209
case TA_KEY_TEXT_SHADOW_OFFSET_DX:
209210
result.setTextShadowOffsetDx((float) entry.getDoubleValue());
@@ -242,14 +243,14 @@ public static TextAttributeProps fromReadableMap(ReactStylesDiffMap props) {
242243
result.setLetterSpacing(getFloatProp(props, ViewProps.LETTER_SPACING, Float.NaN));
243244
result.setAllowFontScaling(getBooleanProp(props, ViewProps.ALLOW_FONT_SCALING, true));
244245
result.setFontSize(getFloatProp(props, ViewProps.FONT_SIZE, ReactConstants.UNSET));
245-
result.setColor(props.hasKey(ViewProps.COLOR) ? props.getInt(ViewProps.COLOR, 0) : null);
246+
result.setColor(props.hasKey(ViewProps.COLOR) ? props.getLong(ViewProps.COLOR, 0) : null);
246247
result.setColor(
247248
props.hasKey(ViewProps.FOREGROUND_COLOR)
248-
? props.getInt(ViewProps.FOREGROUND_COLOR, 0)
249+
? props.getLong(ViewProps.FOREGROUND_COLOR, 0)
249250
: null);
250251
result.setBackgroundColor(
251252
props.hasKey(ViewProps.BACKGROUND_COLOR)
252-
? props.getInt(ViewProps.BACKGROUND_COLOR, 0)
253+
? props.getLong(ViewProps.BACKGROUND_COLOR, 0)
253254
: null);
254255
result.setFontFamily(getStringProp(props, ViewProps.FONT_FAMILY));
255256
result.setFontWeight(getStringProp(props, ViewProps.FONT_WEIGHT));
@@ -260,7 +261,7 @@ public static TextAttributeProps fromReadableMap(ReactStylesDiffMap props) {
260261
result.setTextShadowOffset(
261262
props.hasKey(PROP_SHADOW_OFFSET) ? props.getMap(PROP_SHADOW_OFFSET) : null);
262263
result.setTextShadowRadius(getFloatProp(props, PROP_SHADOW_RADIUS, 1));
263-
result.setTextShadowColor(getIntProp(props, PROP_SHADOW_COLOR, DEFAULT_TEXT_SHADOW_COLOR));
264+
result.setTextShadowColor(getLongProp(props, PROP_SHADOW_COLOR, DEFAULT_TEXT_SHADOW_COLOR));
264265
result.setTextTransform(getStringProp(props, PROP_TEXT_TRANSFORM));
265266
result.setLayoutDirection(getStringProp(props, ViewProps.LAYOUT_DIRECTION));
266267
result.setAccessibilityRole(getStringProp(props, ViewProps.ACCESSIBILITY_ROLE));
@@ -329,6 +330,14 @@ private static int getIntProp(ReactStylesDiffMap mProps, String name, int defaul
329330
}
330331
}
331332

333+
private static long getLongProp(ReactStylesDiffMap mProps, String name, long defaultvalue) {
334+
if (mProps.hasKey(name)) {
335+
return mProps.getLong(name, defaultvalue);
336+
} else {
337+
return defaultvalue;
338+
}
339+
}
340+
332341
private static float getFloatProp(ReactStylesDiffMap mProps, String name, float defaultvalue) {
333342
if (mProps.hasKey(name)) {
334343
return mProps.getFloat(name, defaultvalue);
@@ -428,11 +437,11 @@ private void setFontSize(float fontSize) {
428437
}
429438

430439
@Override
431-
public int getColor() {
440+
public long getColor() {
432441
return mColor;
433442
}
434443

435-
private void setColor(@Nullable Integer color) {
444+
private void setColor(@Nullable Long color) {
436445
mIsColorSet = (color != null);
437446
if (mIsColorSet) {
438447
mColor = color;
@@ -445,11 +454,11 @@ public boolean isColorSet() {
445454
}
446455

447456
@Override
448-
public int getBackgroundColor() {
457+
public long getBackgroundColor() {
449458
return mBackgroundColor;
450459
}
451460

452-
private void setBackgroundColor(Integer color) {
461+
private void setBackgroundColor(Long color) {
453462
// TODO: Don't apply background color to anchor TextView since it will be applied on the View
454463
// directly
455464
// if (!isVirtualAnchor()) {
@@ -690,11 +699,11 @@ private void setTextShadowRadius(float textShadowRadius) {
690699
}
691700

692701
@Override
693-
public int getTextShadowColor() {
702+
public long getTextShadowColor() {
694703
return mTextShadowColor;
695704
}
696705

697-
private void setTextShadowColor(int textShadowColor) {
706+
private void setTextShadowColor(long textShadowColor) {
698707
if (textShadowColor != mTextShadowColor) {
699708
mTextShadowColor = textShadowColor;
700709
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ internal object TextLayoutUtils {
339339
textAttributeProvider.textShadowOffsetDx != 0f ||
340340
textAttributeProvider.textShadowOffsetDy != 0f
341341
val hasTextShadowRadius = textAttributeProvider.textShadowRadius != 0f
342-
val hasTextShadowColorAlpha = Color.alpha(textAttributeProvider.textShadowColor) != 0
342+
val hasTextShadowColorAlpha = Color.alpha(textAttributeProvider.textShadowColor) != 0f
343343

344344
if ((hasTextShadowOffset || hasTextShadowRadius) && hasTextShadowColorAlpha) {
345345
ops.add(

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/ReactBackgroundColorSpan.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package com.facebook.react.views.text.internal.span;
99

10+
import android.graphics.Color;
1011
import android.text.style.BackgroundColorSpan;
1112
import com.facebook.infer.annotation.Nullsafe;
1213

@@ -18,4 +19,8 @@ public class ReactBackgroundColorSpan extends BackgroundColorSpan implements Rea
1819
public ReactBackgroundColorSpan(int color) {
1920
super(color);
2021
}
22+
23+
public ReactBackgroundColorSpan(long color) {
24+
super(Color.toArgb(color));
25+
}
2126
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/ReactForegroundColorSpan.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,33 @@
77

88
package com.facebook.react.views.text.internal.span;
99

10+
import android.graphics.Color;
1011
import android.text.style.ForegroundColorSpan;
1112
import com.facebook.infer.annotation.Nullsafe;
13+
import android.text.TextPaint;
14+
import androidx.annotation.NonNull;
1215

1316
/*
1417
* Wraps {@link ForegroundColorSpan} as a {@link ReactSpan}.
1518
*/
1619
@Nullsafe(Nullsafe.Mode.LOCAL)
1720
public class ReactForegroundColorSpan extends ForegroundColorSpan implements ReactSpan {
21+
private long mColor = 0;
22+
1823
public ReactForegroundColorSpan(int color) {
1924
super(color);
2025
}
26+
27+
public ReactForegroundColorSpan(long color) {
28+
super(Color.toArgb(color));
29+
this.mColor = color;
30+
}
31+
32+
@Override
33+
public void updateDrawState(@NonNull TextPaint tp) {
34+
super.updateDrawState(tp);
35+
if (mColor != 0) {
36+
tp.setColor(mColor);
37+
}
38+
}
2139
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/ShadowStyleSpan.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@
77

88
package com.facebook.react.views.text.internal.span;
99

10+
import android.graphics.Color;
11+
import android.graphics.ColorSpace;
1012
import android.text.TextPaint;
1113
import android.text.style.CharacterStyle;
1214
import com.facebook.infer.annotation.Nullsafe;
1315

1416
@Nullsafe(Nullsafe.Mode.LOCAL)
1517
public class ShadowStyleSpan extends CharacterStyle implements ReactSpan {
1618
private final float mDx, mDy, mRadius;
17-
private final int mColor;
19+
private final long mColor;
1820

1921
public ShadowStyleSpan(float dx, float dy, float radius, int color) {
22+
mDx = dx;
23+
mDy = dy;
24+
mRadius = radius;
25+
mColor = Color.pack(color);
26+
}
27+
28+
public ShadowStyleSpan(float dx, float dy, float radius, long color) {
2029
mDx = dx;
2130
mDy = dy;
2231
mRadius = radius;

0 commit comments

Comments
 (0)