Skip to content

Commit 843d57e

Browse files
javachekelset
authored andcommitted
Fix TextView alignment being reset on state updates
Summary: Changelog: [Android][Fixed] Resolved bug with Text components in new arch losing text alignment state. Reviewed By: mdvacca Differential Revision: D34108943 fbshipit-source-id: 3992e9406345be919b5e3595fc1f9e61cf67a699 # Conflicts: # ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java # ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java
1 parent 9be2959 commit 843d57e

5 files changed

Lines changed: 52 additions & 34 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class ReactTextView extends AppCompatTextView implements ReactCompoundVie
5454
private boolean mContainsImages;
5555
private final int mDefaultGravityHorizontal;
5656
private final int mDefaultGravityVertical;
57-
private int mTextAlign;
5857
private int mNumberOfLines;
5958
private TextUtils.TruncateAt mEllipsizeLocation;
6059
private boolean mAdjustsFontSizeToFit;
@@ -69,8 +68,7 @@ public ReactTextView(Context context) {
6968
super(context);
7069

7170
// Get these defaults only during the constructor - these should never be set otherwise
72-
mDefaultGravityHorizontal =
73-
getGravity() & (Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK);
71+
mDefaultGravityHorizontal = getGravityHorizontal();
7472
mDefaultGravityVertical = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
7573

7674
initView();
@@ -89,10 +87,10 @@ private void initView() {
8987

9088
mReactBackgroundManager = new ReactViewBackgroundManager(this);
9189

92-
mTextAlign = Gravity.NO_GRAVITY;
9390
mNumberOfLines = ViewDefaults.NUMBER_OF_LINES;
9491
mAdjustsFontSizeToFit = false;
9592
mLinkifyMaskType = 0;
93+
mNotifyOnInlineViewLayout = false;
9694
mTextIsSelectable = false;
9795
mEllipsizeLocation = TextUtils.TruncateAt.END;
9896

@@ -392,10 +390,9 @@ public void setText(ReactTextUpdate update) {
392390
}
393391

394392
int nextTextAlign = update.getTextAlign();
395-
if (mTextAlign != nextTextAlign) {
396-
mTextAlign = nextTextAlign;
393+
if (nextTextAlign != getGravityHorizontal()) {
394+
setGravityHorizontal(nextTextAlign);
397395
}
398-
setGravityHorizontal(mTextAlign);
399396
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
400397
if (getBreakStrategy() != update.getTextBreakStrategy()) {
401398
setBreakStrategy(update.getTextBreakStrategy());
@@ -552,6 +549,11 @@ public boolean hasOverlappingRendering() {
552549
return false;
553550
}
554551

552+
/* package */ int getGravityHorizontal() {
553+
return getGravity()
554+
& (Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK);
555+
}
556+
555557
/* package */ void setGravityHorizontal(int gravityHorizontal) {
556558
if (gravityHorizontal == 0) {
557559
gravityHorizontal = mDefaultGravityHorizontal;

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManager.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.facebook.react.views.text;
99

1010
import android.content.Context;
11+
import android.os.Build;
1112
import android.text.Spannable;
1213
import androidx.annotation.NonNull;
1314
import androidx.annotation.Nullable;
@@ -23,6 +24,7 @@
2324
import com.facebook.react.uimanager.ReactStylesDiffMap;
2425
import com.facebook.react.uimanager.StateWrapper;
2526
import com.facebook.react.uimanager.ThemedReactContext;
27+
import com.facebook.react.uimanager.ViewProps;
2628
import com.facebook.yoga.YogaMeasureMode;
2729
import java.util.HashMap;
2830
import java.util.Map;
@@ -148,15 +150,19 @@ public Object updateState(
148150
view.setSpanned(spanned);
149151

150152
int textBreakStrategy =
151-
TextAttributeProps.getTextBreakStrategy(paragraphAttributes.getString("textBreakStrategy"));
153+
TextAttributeProps.getTextBreakStrategy(
154+
paragraphAttributes.getString(ViewProps.TEXT_BREAK_STRATEGY));
155+
int currentJustificationMode =
156+
Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : view.getJustificationMode();
152157

153158
return new ReactTextUpdate(
154159
spanned,
155160
state.hasKey("mostRecentEventCount") ? state.getInt("mostRecentEventCount") : -1,
156161
false, // TODO add this into local Data
157-
TextAttributeProps.getTextAlignment(props, TextLayoutManager.isRTL(attributedString)),
162+
TextAttributeProps.getTextAlignment(
163+
props, TextLayoutManager.isRTL(attributedString), view.getGravityHorizontal()),
158164
textBreakStrategy,
159-
TextAttributeProps.getJustificationMode(props));
165+
TextAttributeProps.getJustificationMode(props, currentJustificationMode));
160166
}
161167

162168
private Object getReactTextUpdate(ReactTextView view, ReactStylesDiffMap props, MapBuffer state) {
@@ -171,15 +177,17 @@ private Object getReactTextUpdate(ReactTextView view, ReactStylesDiffMap props,
171177
int textBreakStrategy =
172178
TextAttributeProps.getTextBreakStrategy(
173179
paragraphAttributes.getString(TextLayoutManagerMapBuffer.PA_KEY_TEXT_BREAK_STRATEGY));
180+
int currentJustificationMode =
181+
Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : view.getJustificationMode();
174182

175183
return new ReactTextUpdate(
176184
spanned,
177185
-1, // UNUSED FOR TEXT
178186
false, // TODO add this into local Data
179187
TextAttributeProps.getTextAlignment(
180-
props, TextLayoutManagerMapBuffer.isRTL(attributedString)),
188+
props, TextLayoutManagerMapBuffer.isRTL(attributedString), view.getGravityHorizontal()),
181189
textBreakStrategy,
182-
TextAttributeProps.getJustificationMode(props));
190+
TextAttributeProps.getJustificationMode(props, currentJustificationMode));
183191
}
184192

185193
@Override

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,35 +249,35 @@ public static TextAttributeProps fromReadableMap(ReactStylesDiffMap props) {
249249
return result;
250250
}
251251

252-
public static int getTextAlignment(ReactStylesDiffMap props, boolean isRTL) {
253-
@Nullable
254-
String textAlignPropValue =
255-
props.hasKey(ViewProps.TEXT_ALIGN) ? props.getString(ViewProps.TEXT_ALIGN) : null;
256-
int textAlignment;
252+
public static int getTextAlignment(ReactStylesDiffMap props, boolean isRTL, int defaultValue) {
253+
if (!props.hasKey(ViewProps.TEXT_ALIGN)) {
254+
return defaultValue;
255+
}
257256

257+
String textAlignPropValue = props.getString(ViewProps.TEXT_ALIGN);
258258
if ("justify".equals(textAlignPropValue)) {
259-
textAlignment = Gravity.LEFT;
259+
return Gravity.LEFT;
260260
} else {
261261
if (textAlignPropValue == null || "auto".equals(textAlignPropValue)) {
262-
textAlignment = Gravity.NO_GRAVITY;
262+
return Gravity.NO_GRAVITY;
263263
} else if ("left".equals(textAlignPropValue)) {
264-
textAlignment = isRTL ? Gravity.RIGHT : Gravity.LEFT;
264+
return isRTL ? Gravity.RIGHT : Gravity.LEFT;
265265
} else if ("right".equals(textAlignPropValue)) {
266-
textAlignment = isRTL ? Gravity.LEFT : Gravity.RIGHT;
266+
return isRTL ? Gravity.LEFT : Gravity.RIGHT;
267267
} else if ("center".equals(textAlignPropValue)) {
268-
textAlignment = Gravity.CENTER_HORIZONTAL;
268+
return Gravity.CENTER_HORIZONTAL;
269269
} else {
270270
throw new JSApplicationIllegalArgumentException("Invalid textAlign: " + textAlignPropValue);
271271
}
272272
}
273-
return textAlignment;
274273
}
275274

276-
public static int getJustificationMode(ReactStylesDiffMap props) {
277-
@Nullable
278-
String textAlignPropValue =
279-
props.hasKey(ViewProps.TEXT_ALIGN) ? props.getString(ViewProps.TEXT_ALIGN) : null;
275+
public static int getJustificationMode(ReactStylesDiffMap props, int defaultValue) {
276+
if (!props.hasKey(ViewProps.TEXT_ALIGN)) {
277+
return defaultValue;
278+
}
280279

280+
String textAlignPropValue = props.getString(ViewProps.TEXT_ALIGN);
281281
if ("justify".equals(textAlignPropValue) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
282282
return Layout.JUSTIFICATION_MODE_INTER_WORD;
283283
}

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,11 @@ private void setIntrinsicContentSize() {
911911
}
912912
}
913913

914+
/* package */ int getGravityHorizontal() {
915+
return getGravity()
916+
& (Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK);
917+
}
918+
914919
/* package */ void setGravityHorizontal(int gravityHorizontal) {
915920
if (gravityHorizontal == 0) {
916921
gravityHorizontal = mDefaultGravityHorizontal;

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,10 +1326,7 @@ public Object updateState(
13261326
}
13271327

13281328
ReadableNativeMap state = stateWrapper.getStateData();
1329-
if (state == null) {
1330-
return null;
1331-
}
1332-
if (!state.hasKey("attributedString")) {
1329+
if (state == null || !state.hasKey("attributedString")) {
13331330
return null;
13341331
}
13351332

@@ -1344,12 +1341,16 @@ public Object updateState(
13441341
view.getContext(), attributedString, mReactTextViewManagerCallback);
13451342

13461343
int textBreakStrategy =
1347-
TextAttributeProps.getTextBreakStrategy(paragraphAttributes.getString("textBreakStrategy"));
1344+
TextAttributeProps.getTextBreakStrategy(
1345+
paragraphAttributes.getString(ViewProps.TEXT_BREAK_STRATEGY));
1346+
int currentJustificationMode =
1347+
Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : view.getJustificationMode();
13481348

13491349
return ReactTextUpdate.buildReactTextUpdateFromState(
13501350
spanned,
13511351
state.getInt("mostRecentEventCount"),
1352-
TextAttributeProps.getTextAlignment(props, TextLayoutManager.isRTL(attributedString)),
1352+
TextAttributeProps.getTextAlignment(
1353+
props, TextLayoutManager.isRTL(attributedString), view.getGravityHorizontal()),
13531354
textBreakStrategy,
13541355
TextAttributeProps.getJustificationMode(props, currentJustificationMode));
13551356
}
@@ -1375,12 +1376,14 @@ public Object getReactTextUpdate(ReactEditText view, ReactStylesDiffMap props, M
13751376
int textBreakStrategy =
13761377
TextAttributeProps.getTextBreakStrategy(
13771378
paragraphAttributes.getString(TextLayoutManagerMapBuffer.PA_KEY_TEXT_BREAK_STRATEGY));
1379+
int currentJustificationMode =
1380+
Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : view.getJustificationMode();
13781381

13791382
return ReactTextUpdate.buildReactTextUpdateFromState(
13801383
spanned,
13811384
state.getInt(TX_STATE_KEY_MOST_RECENT_EVENT_COUNT),
13821385
TextAttributeProps.getTextAlignment(
1383-
props, TextLayoutManagerMapBuffer.isRTL(attributedString)),
1386+
props, TextLayoutManagerMapBuffer.isRTL(attributedString), view.getGravityHorizontal()),
13841387
textBreakStrategy,
13851388
TextAttributeProps.getJustificationMode(props, currentJustificationMode));
13861389
}

0 commit comments

Comments
 (0)