From cd90c483487302ab85100e2d56e3e3d003ab5e88 Mon Sep 17 00:00:00 2001 From: nvgurova Date: Wed, 28 Aug 2024 12:17:16 +0200 Subject: [PATCH] DROID-2756 Fix text ellipsizing logic in custom TextInputWidget - Implement pending text and buffer type storage in setText to handle ellipsize logic properly. - Override onMeasure to apply ellipsize after measuring widget dimensions. - Ensure that text is correctly truncated based on available width. --- .../core_ui/widgets/text/TextInputWidget.kt | 31 +++++++++++++++++++ .../src/main/res/layout/item_block_file.xml | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/widgets/text/TextInputWidget.kt b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/widgets/text/TextInputWidget.kt index 95f2845ef0..e3e63e8250 100644 --- a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/widgets/text/TextInputWidget.kt +++ b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/widgets/text/TextInputWidget.kt @@ -4,9 +4,12 @@ import android.R.id.copy import android.R.id.paste import android.content.Context import android.graphics.Canvas +import android.graphics.Paint import android.os.Parcelable import android.text.InputType import android.text.Spanned +import android.text.TextPaint +import android.text.TextUtils import android.text.TextWatcher import android.text.util.Linkify import android.util.AttributeSet @@ -257,6 +260,34 @@ class TextInputWidget : AppCompatEditText { super.onDraw(canvas) } + private var pendingText : CharSequence? = null + private var pendingType: BufferType? = null + + override fun setText(text: CharSequence?, type: BufferType?) { + // try to cover most popular cases of setting up the text: by resId and by text + if (ellipsize != null) { + pendingText = text + pendingType = type + } + super.setText(text, pendingType) + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec) + if (pendingText != null) { + var text = pendingText + if (ellipsize != null) { + val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG) + textPaint.textSize = textSize + // We don't have layout initialized until onMeasure is called + text = TextUtils.ellipsize(text, textPaint, layout.width.toFloat(), ellipsize) + } + super.setText(text, pendingType) + pendingText = null + pendingType = null + } + } + fun setLinksClickable() { makeLinksActive() } diff --git a/core-ui/src/main/res/layout/item_block_file.xml b/core-ui/src/main/res/layout/item_block_file.xml index ba0488bd1e..2f2f89130e 100644 --- a/core-ui/src/main/res/layout/item_block_file.xml +++ b/core-ui/src/main/res/layout/item_block_file.xml @@ -54,7 +54,7 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/graphic" app:layout_constraintTop_toTopOf="parent" - tools:text="W. J. T. Mitchell — There Are No Visual Media.pdf" /> + tools:text="@string/about" /> \ No newline at end of file