Skip to content

Commit 31a8e92

Browse files
javachefacebook-github-bot
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
1 parent bc749a1 commit 31a8e92

File tree

5 files changed

+55
-37
lines changed

5 files changed

+55
-37
lines changed

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: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,36 +250,36 @@ public static TextAttributeProps fromReadableMap(ReactStylesDiffMap props) {
250250
return result;
251251
}
252252

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

258+
String textAlignPropValue = props.getString(ViewProps.TEXT_ALIGN);
259259
if ("justify".equals(textAlignPropValue)) {
260-
textAlignment = Gravity.LEFT;
260+
return Gravity.LEFT;
261261
} else {
262262
if (textAlignPropValue == null || "auto".equals(textAlignPropValue)) {
263-
textAlignment = Gravity.NO_GRAVITY;
263+
return Gravity.NO_GRAVITY;
264264
} else if ("left".equals(textAlignPropValue)) {
265-
textAlignment = isRTL ? Gravity.RIGHT : Gravity.LEFT;
265+
return isRTL ? Gravity.RIGHT : Gravity.LEFT;
266266
} else if ("right".equals(textAlignPropValue)) {
267-
textAlignment = isRTL ? Gravity.LEFT : Gravity.RIGHT;
267+
return isRTL ? Gravity.LEFT : Gravity.RIGHT;
268268
} else if ("center".equals(textAlignPropValue)) {
269-
textAlignment = Gravity.CENTER_HORIZONTAL;
269+
return Gravity.CENTER_HORIZONTAL;
270270
} else {
271271
FLog.w(ReactConstants.TAG, "Invalid textAlign: " + textAlignPropValue);
272-
textAlignment = Gravity.NO_GRAVITY;
272+
return Gravity.NO_GRAVITY;
273273
}
274274
}
275-
return textAlignment;
276275
}
277276

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

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

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
@@ -845,6 +845,11 @@ private void setIntrinsicContentSize() {
845845
}
846846
}
847847

848+
/* package */ int getGravityHorizontal() {
849+
return getGravity()
850+
& (Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK);
851+
}
852+
848853
/* package */ void setGravityHorizontal(int gravityHorizontal) {
849854
if (gravityHorizontal == 0) {
850855
gravityHorizontal = mDefaultGravityHorizontal;

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,10 +1315,7 @@ public Object updateState(
13151315
}
13161316

13171317
ReadableNativeMap state = stateWrapper.getStateData();
1318-
if (state == null) {
1319-
return null;
1320-
}
1321-
if (!state.hasKey("attributedString")) {
1318+
if (state == null || !state.hasKey("attributedString")) {
13221319
return null;
13231320
}
13241321

@@ -1336,14 +1333,18 @@ public Object updateState(
13361333
attributedString.getArray("fragments").toArrayList().size() > 1;
13371334

13381335
int textBreakStrategy =
1339-
TextAttributeProps.getTextBreakStrategy(paragraphAttributes.getString("textBreakStrategy"));
1336+
TextAttributeProps.getTextBreakStrategy(
1337+
paragraphAttributes.getString(ViewProps.TEXT_BREAK_STRATEGY));
1338+
int currentJustificationMode =
1339+
Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : view.getJustificationMode();
13401340

13411341
return ReactTextUpdate.buildReactTextUpdateFromState(
13421342
spanned,
13431343
state.getInt("mostRecentEventCount"),
1344-
TextAttributeProps.getTextAlignment(props, TextLayoutManager.isRTL(attributedString)),
1344+
TextAttributeProps.getTextAlignment(
1345+
props, TextLayoutManager.isRTL(attributedString), view.getGravityHorizontal()),
13451346
textBreakStrategy,
1346-
TextAttributeProps.getJustificationMode(props),
1347+
TextAttributeProps.getJustificationMode(props, currentJustificationMode),
13471348
containsMultipleFragments);
13481349
}
13491350

@@ -1371,14 +1372,16 @@ public Object getReactTextUpdate(ReactEditText view, ReactStylesDiffMap props, M
13711372
int textBreakStrategy =
13721373
TextAttributeProps.getTextBreakStrategy(
13731374
paragraphAttributes.getString(TextLayoutManagerMapBuffer.PA_KEY_TEXT_BREAK_STRATEGY));
1375+
int currentJustificationMode =
1376+
Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : view.getJustificationMode();
13741377

13751378
return ReactTextUpdate.buildReactTextUpdateFromState(
13761379
spanned,
13771380
state.getInt(TX_STATE_KEY_MOST_RECENT_EVENT_COUNT),
13781381
TextAttributeProps.getTextAlignment(
1379-
props, TextLayoutManagerMapBuffer.isRTL(attributedString)),
1382+
props, TextLayoutManagerMapBuffer.isRTL(attributedString), view.getGravityHorizontal()),
13801383
textBreakStrategy,
1381-
TextAttributeProps.getJustificationMode(props),
1384+
TextAttributeProps.getJustificationMode(props, currentJustificationMode),
13821385
containsMultipleFragments);
13831386
}
13841387
}

0 commit comments

Comments
 (0)