2016-04-22 15:49:00 +02:00
|
|
|
/*
|
2019-01-23 18:03:33 +01:00
|
|
|
* This is the source code of Telegram for Android v. 5.x.x.
|
2016-04-22 15:49:00 +02:00
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
2019-01-23 18:03:33 +01:00
|
|
|
* Copyright Nikolai Kudashov, 2013-2018.
|
2016-04-22 15:49:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.ui.ActionBar;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.text.Layout;
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.StaticLayout;
|
|
|
|
import android.text.TextPaint;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
|
|
|
|
|
|
|
public class SimpleTextView extends View implements Drawable.Callback {
|
|
|
|
|
|
|
|
private Layout layout;
|
|
|
|
private TextPaint textPaint;
|
|
|
|
private int gravity = Gravity.LEFT | Gravity.TOP;
|
|
|
|
private CharSequence text;
|
|
|
|
private SpannableStringBuilder spannableStringBuilder;
|
|
|
|
private Drawable leftDrawable;
|
|
|
|
private Drawable rightDrawable;
|
|
|
|
private int drawablePadding = AndroidUtilities.dp(4);
|
|
|
|
private int leftDrawableTopPadding;
|
|
|
|
private int rightDrawableTopPadding;
|
|
|
|
|
|
|
|
private int offsetX;
|
2019-01-23 18:03:33 +01:00
|
|
|
private int offsetY;
|
2016-04-22 15:49:00 +02:00
|
|
|
private int textWidth;
|
|
|
|
private int textHeight;
|
|
|
|
private boolean wasLayout;
|
|
|
|
|
|
|
|
public SimpleTextView(Context context) {
|
|
|
|
super(context);
|
|
|
|
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTextColor(int color) {
|
|
|
|
textPaint.setColor(color);
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
public void setLinkTextColor(int color) {
|
|
|
|
textPaint.linkColor = color;
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
@Override
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
wasLayout = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTextSize(int size) {
|
|
|
|
int newSize = AndroidUtilities.dp(size);
|
|
|
|
if (newSize == textPaint.getTextSize()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
textPaint.setTextSize(newSize);
|
2016-06-24 12:27:15 +02:00
|
|
|
if (!recreateLayoutMaybe()) {
|
|
|
|
invalidate();
|
|
|
|
}
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setGravity(int value) {
|
|
|
|
gravity = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTypeface(Typeface typeface) {
|
|
|
|
textPaint.setTypeface(typeface);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getSideDrawablesSize() {
|
|
|
|
int size = 0;
|
|
|
|
if (leftDrawable != null) {
|
|
|
|
size += leftDrawable.getIntrinsicWidth() + drawablePadding;
|
|
|
|
}
|
|
|
|
if (rightDrawable != null) {
|
|
|
|
size += rightDrawable.getIntrinsicWidth() + drawablePadding;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Paint getPaint() {
|
|
|
|
return textPaint;
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:57:01 +02:00
|
|
|
private void calcOffset(int width) {
|
|
|
|
if (layout.getLineCount() > 0) {
|
|
|
|
textWidth = (int) Math.ceil(layout.getLineWidth(0));
|
|
|
|
textHeight = layout.getLineBottom(0);
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.CENTER_VERTICAL) {
|
|
|
|
offsetY = (getMeasuredHeight() - textHeight) / 2;
|
|
|
|
} else {
|
|
|
|
offsetY = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:57:01 +02:00
|
|
|
if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT) {
|
|
|
|
offsetX = -(int) layout.getLineLeft(0);
|
|
|
|
} else if (layout.getLineLeft(0) == 0) {
|
|
|
|
offsetX = width - textWidth;
|
|
|
|
} else {
|
|
|
|
offsetX = -AndroidUtilities.dp(8);
|
|
|
|
}
|
2017-12-08 18:35:59 +01:00
|
|
|
offsetX += getPaddingLeft();
|
2016-10-11 13:57:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 12:27:15 +02:00
|
|
|
private boolean createLayout(int width) {
|
2016-04-22 15:49:00 +02:00
|
|
|
if (text != null) {
|
|
|
|
try {
|
|
|
|
if (leftDrawable != null) {
|
|
|
|
width -= leftDrawable.getIntrinsicWidth();
|
|
|
|
width -= drawablePadding;
|
|
|
|
}
|
|
|
|
if (rightDrawable != null) {
|
|
|
|
width -= rightDrawable.getIntrinsicWidth();
|
|
|
|
width -= drawablePadding;
|
|
|
|
}
|
|
|
|
CharSequence string = TextUtils.ellipsize(text, textPaint, width, TextUtils.TruncateAt.END);
|
2017-03-31 01:58:05 +02:00
|
|
|
/*if (layout != null && TextUtils.equals(layout.getText(), string)) {
|
2016-10-11 13:57:01 +02:00
|
|
|
calcOffset(width);
|
2016-06-24 12:27:15 +02:00
|
|
|
return false;
|
2017-03-31 01:58:05 +02:00
|
|
|
}*/
|
2016-04-22 15:49:00 +02:00
|
|
|
layout = new StaticLayout(string, 0, string.length(), textPaint, width + AndroidUtilities.dp(8), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
|
2016-10-11 13:57:01 +02:00
|
|
|
calcOffset(width);
|
2017-12-08 18:35:59 +01:00
|
|
|
} catch (Exception ignore) {
|
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
layout = null;
|
|
|
|
textWidth = 0;
|
|
|
|
textHeight = 0;
|
|
|
|
}
|
2016-05-25 23:49:47 +02:00
|
|
|
invalidate();
|
2016-06-24 12:27:15 +02:00
|
|
|
return true;
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
int width = MeasureSpec.getSize(widthMeasureSpec);
|
|
|
|
int height = MeasureSpec.getSize(heightMeasureSpec);
|
|
|
|
createLayout(width - getPaddingLeft() - getPaddingRight());
|
|
|
|
|
|
|
|
int finalHeight;
|
2016-06-24 12:27:15 +02:00
|
|
|
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
|
2016-04-22 15:49:00 +02:00
|
|
|
finalHeight = height;
|
|
|
|
} else {
|
|
|
|
finalHeight = textHeight;
|
|
|
|
}
|
|
|
|
setMeasuredDimension(width, finalHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
2016-05-25 23:49:47 +02:00
|
|
|
wasLayout = true;
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getTextWidth() {
|
|
|
|
return textWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTextHeight() {
|
|
|
|
return textHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLeftDrawableTopPadding(int value) {
|
|
|
|
leftDrawableTopPadding = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRightDrawableTopPadding(int value) {
|
|
|
|
rightDrawableTopPadding = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLeftDrawable(int resId) {
|
|
|
|
setLeftDrawable(resId == 0 ? null : getContext().getResources().getDrawable(resId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRightDrawable(int resId) {
|
|
|
|
setRightDrawable(resId == 0 ? null : getContext().getResources().getDrawable(resId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLeftDrawable(Drawable drawable) {
|
|
|
|
if (leftDrawable == drawable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (leftDrawable != null) {
|
|
|
|
leftDrawable.setCallback(null);
|
|
|
|
}
|
|
|
|
leftDrawable = drawable;
|
|
|
|
if (drawable != null) {
|
|
|
|
drawable.setCallback(this);
|
|
|
|
}
|
2016-06-24 12:27:15 +02:00
|
|
|
if (!recreateLayoutMaybe()) {
|
|
|
|
invalidate();
|
|
|
|
}
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setRightDrawable(Drawable drawable) {
|
|
|
|
if (rightDrawable == drawable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rightDrawable != null) {
|
|
|
|
rightDrawable.setCallback(null);
|
|
|
|
}
|
|
|
|
rightDrawable = drawable;
|
|
|
|
if (drawable != null) {
|
|
|
|
drawable.setCallback(this);
|
|
|
|
}
|
2016-06-24 12:27:15 +02:00
|
|
|
if (!recreateLayoutMaybe()) {
|
|
|
|
invalidate();
|
|
|
|
}
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
public boolean setText(CharSequence value) {
|
|
|
|
return setText(value, false);
|
2017-03-31 01:58:05 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
public boolean setText(CharSequence value, boolean force) {
|
2017-03-31 01:58:05 +02:00
|
|
|
if (text == null && value == null || !force && text != null && value != null && text.equals(value)) {
|
2019-01-23 18:03:33 +01:00
|
|
|
return false;
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
text = value;
|
|
|
|
recreateLayoutMaybe();
|
2019-01-23 18:03:33 +01:00
|
|
|
return true;
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setDrawablePadding(int value) {
|
|
|
|
if (drawablePadding == value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
drawablePadding = value;
|
2016-06-24 12:27:15 +02:00
|
|
|
if (!recreateLayoutMaybe()) {
|
|
|
|
invalidate();
|
|
|
|
}
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 12:27:15 +02:00
|
|
|
private boolean recreateLayoutMaybe() {
|
2019-01-23 18:03:33 +01:00
|
|
|
if (wasLayout && getMeasuredHeight() != 0) {
|
2016-06-24 12:27:15 +02:00
|
|
|
return createLayout(getMeasuredWidth());
|
2016-04-22 15:49:00 +02:00
|
|
|
} else {
|
|
|
|
requestLayout();
|
|
|
|
}
|
2016-06-24 12:27:15 +02:00
|
|
|
return true;
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public CharSequence getText() {
|
|
|
|
if (text == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
public int getTextStartX() {
|
|
|
|
if (layout == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int textOffsetX = 0;
|
|
|
|
if (leftDrawable != null) {
|
|
|
|
if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT) {
|
|
|
|
textOffsetX += drawablePadding + leftDrawable.getIntrinsicWidth();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (int) getX() + offsetX + textOffsetX;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TextPaint getTextPaint() {
|
|
|
|
return textPaint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTextStartY() {
|
|
|
|
if (layout == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (int) getY();
|
|
|
|
}
|
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
@Override
|
|
|
|
protected void onDraw(Canvas canvas) {
|
|
|
|
int textOffsetX = 0;
|
|
|
|
if (leftDrawable != null) {
|
|
|
|
int y = (textHeight - leftDrawable.getIntrinsicHeight()) / 2 + leftDrawableTopPadding;
|
|
|
|
leftDrawable.setBounds(0, y, leftDrawable.getIntrinsicWidth(), y + leftDrawable.getIntrinsicHeight());
|
|
|
|
leftDrawable.draw(canvas);
|
|
|
|
if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT) {
|
|
|
|
textOffsetX += drawablePadding + leftDrawable.getIntrinsicWidth();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rightDrawable != null) {
|
|
|
|
int x = textOffsetX + textWidth + drawablePadding;
|
|
|
|
int y = (textHeight - rightDrawable.getIntrinsicHeight()) / 2 + rightDrawableTopPadding;
|
|
|
|
rightDrawable.setBounds(x, y, x + rightDrawable.getIntrinsicWidth(), y + rightDrawable.getIntrinsicHeight());
|
|
|
|
rightDrawable.draw(canvas);
|
|
|
|
}
|
|
|
|
if (layout != null) {
|
2019-01-23 18:03:33 +01:00
|
|
|
if (offsetX + textOffsetX != 0 || offsetY != 0) {
|
2016-04-22 15:49:00 +02:00
|
|
|
canvas.save();
|
2019-01-23 18:03:33 +01:00
|
|
|
canvas.translate(offsetX + textOffsetX, offsetY);
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
layout.draw(canvas);
|
2019-01-23 18:03:33 +01:00
|
|
|
if (offsetX + textOffsetX != 0 || offsetY != 0) {
|
2016-04-22 15:49:00 +02:00
|
|
|
canvas.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void invalidateDrawable(Drawable who) {
|
2016-05-25 23:49:47 +02:00
|
|
|
if (who == leftDrawable) {
|
|
|
|
invalidate(leftDrawable.getBounds());
|
|
|
|
} else if (who == rightDrawable) {
|
|
|
|
invalidate(rightDrawable.getBounds());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasOverlappingRendering() {
|
|
|
|
return false;
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
}
|