Skip to content

Commit c95136e

Browse files
committed
update Android text to support color longs
1 parent b3a8eba commit c95136e

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;
@@ -580,12 +580,12 @@ public void setFontSize(float fontSize) {
580580
}
581581

582582
@Override
583-
public int getColor() {
583+
public long getColor() {
584584
return mColor;
585585
}
586586

587587
@ReactProp(name = ViewProps.COLOR, customType = "Color")
588-
public void setColor(@Nullable Integer color) {
588+
public void setColor(@Nullable Long color) {
589589
mIsColorSet = (color != null);
590590
if (mIsColorSet) {
591591
mColor = color;
@@ -599,12 +599,12 @@ public boolean isColorSet() {
599599
}
600600

601601
@Override
602-
public int getBackgroundColor() {
602+
public long getBackgroundColor() {
603603
return mBackgroundColor;
604604
}
605605

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

797797
@Override
798-
public int getTextShadowColor() {
798+
public long getTextShadowColor() {
799799
return mTextShadowColor;
800800
}
801801

802802
@ReactProp(name = PROP_SHADOW_COLOR, defaultInt = DEFAULT_TEXT_SHADOW_COLOR, customType = "Color")
803-
public void setTextShadowColor(int textShadowColor) {
803+
public void setTextShadowColor(long textShadowColor) {
804804
if (textShadowColor != mTextShadowColor) {
805805
mTextShadowColor = textShadowColor;
806806
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;
@@ -152,10 +153,10 @@ public static TextAttributeProps fromMapBuffer(MapBuffer props) {
152153
MapBuffer.Entry entry = iterator.next();
153154
switch (entry.getKey()) {
154155
case TA_KEY_FOREGROUND_COLOR:
155-
result.setColor(entry.getIntValue());
156+
result.setColor(entry.getLongValue());
156157
break;
157158
case TA_KEY_BACKGROUND_COLOR:
158-
result.setBackgroundColor(entry.getIntValue());
159+
result.setBackgroundColor(entry.getLongValue());
159160
break;
160161
case TA_KEY_OPACITY:
161162
break;
@@ -200,7 +201,7 @@ public static TextAttributeProps fromMapBuffer(MapBuffer props) {
200201
result.setTextShadowRadius((float) entry.getDoubleValue());
201202
break;
202203
case TA_KEY_TEXT_SHADOW_COLOR:
203-
result.setTextShadowColor(entry.getIntValue());
204+
result.setTextShadowColor(entry.getLongValue());
204205
break;
205206
case TA_KEY_TEXT_SHADOW_OFFSET_DX:
206207
result.setTextShadowOffsetDx((float) entry.getDoubleValue());
@@ -239,14 +240,14 @@ public static TextAttributeProps fromReadableMap(ReactStylesDiffMap props) {
239240
result.setLetterSpacing(getFloatProp(props, ViewProps.LETTER_SPACING, Float.NaN));
240241
result.setAllowFontScaling(getBooleanProp(props, ViewProps.ALLOW_FONT_SCALING, true));
241242
result.setFontSize(getFloatProp(props, ViewProps.FONT_SIZE, ReactConstants.UNSET));
242-
result.setColor(props.hasKey(ViewProps.COLOR) ? props.getInt(ViewProps.COLOR, 0) : null);
243+
result.setColor(props.hasKey(ViewProps.COLOR) ? props.getLong(ViewProps.COLOR, 0) : null);
243244
result.setColor(
244245
props.hasKey(ViewProps.FOREGROUND_COLOR)
245-
? props.getInt(ViewProps.FOREGROUND_COLOR, 0)
246+
? props.getLong(ViewProps.FOREGROUND_COLOR, 0)
246247
: null);
247248
result.setBackgroundColor(
248249
props.hasKey(ViewProps.BACKGROUND_COLOR)
249-
? props.getInt(ViewProps.BACKGROUND_COLOR, 0)
250+
? props.getLong(ViewProps.BACKGROUND_COLOR, 0)
250251
: null);
251252
result.setFontFamily(getStringProp(props, ViewProps.FONT_FAMILY));
252253
result.setFontWeight(getStringProp(props, ViewProps.FONT_WEIGHT));
@@ -257,7 +258,7 @@ public static TextAttributeProps fromReadableMap(ReactStylesDiffMap props) {
257258
result.setTextShadowOffset(
258259
props.hasKey(PROP_SHADOW_OFFSET) ? props.getMap(PROP_SHADOW_OFFSET) : null);
259260
result.setTextShadowRadius(getFloatProp(props, PROP_SHADOW_RADIUS, 1));
260-
result.setTextShadowColor(getIntProp(props, PROP_SHADOW_COLOR, DEFAULT_TEXT_SHADOW_COLOR));
261+
result.setTextShadowColor(getLongProp(props, PROP_SHADOW_COLOR, DEFAULT_TEXT_SHADOW_COLOR));
261262
result.setTextTransform(getStringProp(props, PROP_TEXT_TRANSFORM));
262263
result.setLayoutDirection(getStringProp(props, ViewProps.LAYOUT_DIRECTION));
263264
result.setAccessibilityRole(getStringProp(props, ViewProps.ACCESSIBILITY_ROLE));
@@ -326,6 +327,14 @@ private static int getIntProp(ReactStylesDiffMap mProps, String name, int defaul
326327
}
327328
}
328329

330+
private static long getLongProp(ReactStylesDiffMap mProps, String name, long defaultvalue) {
331+
if (mProps.hasKey(name)) {
332+
return mProps.getLong(name, defaultvalue);
333+
} else {
334+
return defaultvalue;
335+
}
336+
}
337+
329338
private static float getFloatProp(ReactStylesDiffMap mProps, String name, float defaultvalue) {
330339
if (mProps.hasKey(name)) {
331340
return mProps.getFloat(name, defaultvalue);
@@ -425,11 +434,11 @@ private void setFontSize(float fontSize) {
425434
}
426435

427436
@Override
428-
public int getColor() {
437+
public long getColor() {
429438
return mColor;
430439
}
431440

432-
private void setColor(@Nullable Integer color) {
441+
private void setColor(@Nullable Long color) {
433442
mIsColorSet = (color != null);
434443
if (mIsColorSet) {
435444
mColor = color;
@@ -442,11 +451,11 @@ public boolean isColorSet() {
442451
}
443452

444453
@Override
445-
public int getBackgroundColor() {
454+
public long getBackgroundColor() {
446455
return mBackgroundColor;
447456
}
448457

449-
private void setBackgroundColor(Integer color) {
458+
private void setBackgroundColor(Long color) {
450459
// TODO: Don't apply background color to anchor TextView since it will be applied on the View
451460
// directly
452461
// if (!isVirtualAnchor()) {
@@ -687,11 +696,11 @@ private void setTextShadowRadius(float textShadowRadius) {
687696
}
688697

689698
@Override
690-
public int getTextShadowColor() {
699+
public long getTextShadowColor() {
691700
return mTextShadowColor;
692701
}
693702

694-
private void setTextShadowColor(int textShadowColor) {
703+
private void setTextShadowColor(long textShadowColor) {
695704
if (textShadowColor != mTextShadowColor) {
696705
mTextShadowColor = textShadowColor;
697706
}

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

1213
/*
@@ -16,4 +17,8 @@ public class ReactBackgroundColorSpan extends BackgroundColorSpan implements Rea
1617
public ReactBackgroundColorSpan(int color) {
1718
super(color);
1819
}
20+
21+
public ReactBackgroundColorSpan(long color) {
22+
super(Color.toArgb(color));
23+
}
1924
}

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,13 +7,31 @@
77

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

10+
import android.graphics.Color;
1011
import android.text.style.ForegroundColorSpan;
12+
import android.text.TextPaint;
13+
import androidx.annotation.NonNull;
1114

1215
/*
1316
* Wraps {@link ForegroundColorSpan} as a {@link ReactSpan}.
1417
*/
1518
public class ReactForegroundColorSpan extends ForegroundColorSpan implements ReactSpan {
19+
private long mColor = 0;
20+
1621
public ReactForegroundColorSpan(int color) {
1722
super(color);
1823
}
24+
25+
public ReactForegroundColorSpan(long color) {
26+
super(Color.toArgb(color));
27+
this.mColor = color;
28+
}
29+
30+
@Override
31+
public void updateDrawState(@NonNull TextPaint tp) {
32+
super.updateDrawState(tp);
33+
if (mColor != 0) {
34+
tp.setColor(mColor);
35+
}
36+
}
1937
}

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,14 +7,23 @@
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

1315
public class ShadowStyleSpan extends CharacterStyle implements ReactSpan {
1416
private final float mDx, mDy, mRadius;
15-
private final int mColor;
17+
private final long mColor;
1618

1719
public ShadowStyleSpan(float dx, float dy, float radius, int color) {
20+
mDx = dx;
21+
mDy = dy;
22+
mRadius = radius;
23+
mColor = Color.pack(color);
24+
}
25+
26+
public ShadowStyleSpan(float dx, float dy, float radius, long color) {
1827
mDx = dx;
1928
mDy = dy;
2029
mRadius = radius;

0 commit comments

Comments
 (0)