From 4e67752850d8c7008aa785d969ef18b4a32a36a3 Mon Sep 17 00:00:00 2001 From: Rashad Sookram Date: Thu, 6 Jan 2022 12:41:50 -0500 Subject: [PATCH] Fix horizontal resize bug with quotes in messages. --- .../components/emoji/EmojiTextView.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/emoji/EmojiTextView.java b/app/src/main/java/org/thoughtcrime/securesms/components/emoji/EmojiTextView.java index 281de7f24..6478f7d96 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/emoji/EmojiTextView.java +++ b/app/src/main/java/org/thoughtcrime/securesms/components/emoji/EmojiTextView.java @@ -159,9 +159,16 @@ public class EmojiTextView extends AppCompatTextView { } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int originalWidthMode = MeasureSpec.getMode(widthMeasureSpec); widthMeasureSpec = applyWidthMeasureRoundingFix(widthMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + widthMeasureSpec = getPreciseWidthForMaxLines(originalWidthMode); + if (widthMeasureSpec != 0) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + CharSequence text = getText(); if (getLayout() == null || !measureLastLine || text == null || text.length() == 0) { lastLineWidth = -1; @@ -209,6 +216,29 @@ public class EmojiTextView extends AppCompatTextView { return widthMeasureSpec; } + /** + * Determines the width to use for this view based on the lines of text that will be shown, as + * opposed to the default behavior which will consider lines after the max lines constraint. + * + * @param originalWidthMode the original mode passed for the width measure spec in {@link #onMeasure(int, int)} + * @return the new measure spec to use for the width. 0 if the existing measurement should be used. + */ + private int getPreciseWidthForMaxLines(int originalWidthMode) { + if (originalWidthMode != MeasureSpec.EXACTLY && getLineCount() > getMaxLines() && getLayout() != null) { + float maxWidth = 0; + for (int i = 0; i < getMaxLines(); i++) { + maxWidth = Math.max(maxWidth, getLayout().getLineWidth(i)); + } + + double desiredWidth = Math.ceil(maxWidth); + if (desiredWidth < getMeasuredWidth()) { + return MeasureSpec.makeMeasureSpec((int) desiredWidth, MeasureSpec.AT_MOST); + } + } + + return 0; + } + private boolean hasMetricAffectingSpan(@NonNull CharSequence text) { if (!(text instanceof Spanned)) { return false;