Skip to content

Commit

Permalink
feat(android): add breakStrategy prop for Text and TextInput (Tencent…
Browse files Browse the repository at this point in the history
  • Loading branch information
iPel committed Jul 29, 2022
1 parent eee04c3 commit 0d13279
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public class NodeProps {
public static final String TEXT_ALIGN = "textAlign";
public static final String TEXT_ALIGN_VERTICAL = "textAlignVertical";
public static final String TEXT_DECORATION_LINE = "textDecorationLine";
public static final String BREAK_STRATEGY = "breakStrategy";
public static final String ON_CLICK = "onClick";
public static final String ON_LONG_CLICK = "onLongClick";
public static final String ON_PRESS_IN = "onPressIn";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.text.*;
import android.text.style.*;

import androidx.annotation.RequiresApi;

import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.adapter.font.HippyFontScaleAdapter;
import com.tencent.mtt.hippy.adapter.image.HippyDrawable;
Expand All @@ -51,9 +53,13 @@ public class TextNode extends StyleNode {
public final static String MODE_MIDDLE = "middle";
public final static String MODE_TAIL = "tail";
public final static String MODE_CLIP = "clip";
public final static String STRATEGY_SIMPLE = "simple";
public final static String STRATEGY_HIGH_QUALITY = "high_quality";
public final static String STRATEGY_BALANCED = "balanced";
CharSequence mText;
protected int mNumberOfLines = UNSET;
private String mEllipsizeMode = MODE_TAIL;
private String mBreakStrategy = STRATEGY_SIMPLE;

protected int mFontSize = (int) Math.ceil(PixelUtil.dp2px(NodeProps.FONT_SIZE_SP));
private float mLineHeight = UNSET;
Expand Down Expand Up @@ -421,9 +427,9 @@ public void setNumberOfLines(int numberOfLines) {
markUpdated();
}

@HippyControllerProps(name = NodeProps.ELLIPSIZE_MODE, defaultType = HippyControllerProps.STRING, defaultString = "tail")
@HippyControllerProps(name = NodeProps.ELLIPSIZE_MODE, defaultType = HippyControllerProps.STRING, defaultString = MODE_TAIL)
public void setEllipsizeMode(String mode) {
if (mode == null) {
if (TextUtils.isEmpty(mode)) {
mode = MODE_TAIL;
}
if (!mEllipsizeMode.equals(mode)) {
Expand All @@ -436,6 +442,24 @@ public void setEllipsizeMode(String mode) {
}
}

@HippyControllerProps(name = NodeProps.BREAK_STRATEGY, defaultType = HippyControllerProps.STRING, defaultString = STRATEGY_SIMPLE)
public void setBreakStrategy(String strategy) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return;
}
if (TextUtils.isEmpty(strategy)) {
strategy = STRATEGY_SIMPLE;
}
if (!mBreakStrategy.equals(strategy)) {
if (STRATEGY_SIMPLE.equals(strategy) || STRATEGY_HIGH_QUALITY.equals(strategy) || STRATEGY_BALANCED.equals(strategy)) {
mBreakStrategy = strategy;
markUpdated();
} else {
throw new RuntimeException("Invalid breakStrategy: " + strategy);
}
}
}

protected HippyFontScaleAdapter mFontScaleAdapter;
protected HippyEngineContext engineContext;
protected HippyImageLoader mImageAdapter;
Expand Down Expand Up @@ -676,6 +700,21 @@ protected float getLineSpacingMultiplier() {
return mLineSpacingMultiplier <= 0 ? 1.0f : mLineSpacingMultiplier;
}

@RequiresApi(api = Build.VERSION_CODES.M)
private int getBreakStrategy() {
final String strategy = mBreakStrategy;
switch (strategy) {
case STRATEGY_SIMPLE:
return Layout.BREAK_STRATEGY_SIMPLE;
case STRATEGY_HIGH_QUALITY:
return Layout.BREAK_STRATEGY_HIGH_QUALITY;
case STRATEGY_BALANCED:
return Layout.BREAK_STRATEGY_BALANCED;
default:
throw new RuntimeException("Invalid breakStrategy: " + strategy);
}
}

private StaticLayout buildStaticLayout(CharSequence source, TextPaint paint, int width) {
Layout.Alignment textAlign = mTextAlign;
if (I18nUtil.isRTL()) {
Expand All @@ -685,8 +724,17 @@ private StaticLayout buildStaticLayout(CharSequence source, TextPaint paint, int
}
}

return new StaticLayout(source, paint, width, textAlign, getLineSpacingMultiplier(), mLineSpacingExtra,
true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return StaticLayout.Builder.obtain(source, 0, source.length(), paint, width)
.setAlignment(textAlign)
.setLineSpacing(mLineSpacingExtra, getLineSpacingMultiplier())
.setIncludePad(true)
.setBreakStrategy(getBreakStrategy())
.build();
} else {
return new StaticLayout(source, paint, width, textAlign, getLineSpacingMultiplier(),
mLineSpacingExtra, true);
}
}

protected Layout createLayout(float width, FlexMeasureMode widthMode) {
Expand All @@ -704,8 +752,7 @@ protected Layout createLayout(float width, FlexMeasureMode widthMode) {
boolean unconstrainedWidth = widthMode == FlexMeasureMode.UNDEFINED || width < 0;
if (boring == null && (unconstrainedWidth || (!FlexConstants.isUndefined(desiredWidth)
&& desiredWidth <= width))) {
layout = new StaticLayout(text, textPaint, (int)Math.ceil(desiredWidth), mTextAlign, getLineSpacingMultiplier(),
mLineSpacingExtra, true);
layout = buildStaticLayout(text, textPaint, (int)Math.ceil(desiredWidth));
} else if (boring != null && (unconstrainedWidth || boring.width <= width)) {
layout = BoringLayout.make(text, textPaint, boring.width, mTextAlign, getLineSpacingMultiplier(), mLineSpacingExtra, boring, true);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Layout;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.util.TypedValue;
Expand All @@ -41,6 +42,7 @@
import com.tencent.mtt.hippy.dom.node.NodeProps;
import com.tencent.mtt.hippy.dom.node.StyleNode;
import com.tencent.mtt.hippy.dom.node.TextExtra;
import com.tencent.mtt.hippy.dom.node.TextNode;
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.mtt.hippy.uimanager.HippyViewController;
import com.tencent.mtt.hippy.utils.LogUtils;
Expand Down Expand Up @@ -439,6 +441,23 @@ public void setTextAlignVertical(HippyTextInput view, String textAlignVertical)

}

@HippyControllerProps(name = NodeProps.BREAK_STRATEGY, defaultType = HippyControllerProps.STRING, defaultString = TextNode.STRATEGY_SIMPLE)
public void setBreakStrategy(HippyTextInput view, String strategy) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int strategyInt;
if (TextUtils.isEmpty(strategy) || TextNode.STRATEGY_SIMPLE.equals(strategy)) {
strategyInt = Layout.BREAK_STRATEGY_SIMPLE;
} else if (TextNode.STRATEGY_HIGH_QUALITY.equals(strategy)) {
strategyInt = Layout.BREAK_STRATEGY_HIGH_QUALITY;
} else if (TextNode.STRATEGY_BALANCED.equals(strategy)) {
strategyInt = Layout.BREAK_STRATEGY_BALANCED;
} else {
throw new RuntimeException("Invalid breakStrategy: " + strategy);
}
view.setBreakStrategy(strategyInt);
}
}

@Override
public void dispatchFunction(final HippyTextInput view, String functionName, HippyArray params,
Promise promise) {
Expand Down
Loading

0 comments on commit 0d13279

Please sign in to comment.