Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Android: add onTextLayout listener on the TextView component #912

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.element.android.wysiwyg.compose

import android.text.Layout
import android.text.Spanned
import android.widget.TextView
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -34,6 +35,7 @@ fun EditorStyledText(
resolveMentionDisplay: (text: String, url: String) -> TextDisplay = RichTextEditorDefaults.MentionDisplay,
resolveRoomMentionDisplay: () -> TextDisplay = RichTextEditorDefaults.RoomMentionDisplay,
onLinkClickedListener: ((String) -> Unit) = {},
onTextLayout: (Layout) -> Unit = {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why the parameter is not called onTextLayoutChanged?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, I think I just missed the mismatch here.

style: RichTextEditorStyle = RichTextEditorDefaults.style(),
) {
val typeface by style.text.rememberTypeface()
Expand Down Expand Up @@ -67,6 +69,7 @@ fun EditorStyledText(
view.setHtml(text.toString())
}
view.onLinkClickedListener = onLinkClickedListener
view.onTextLayoutChanged = onTextLayout
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package io.element.android.wysiwyg

import android.content.Context
import android.graphics.Canvas
import android.text.Layout
import android.text.Spanned
import android.text.style.ClickableSpan
import android.text.style.URLSpan
import android.util.AttributeSet
import android.view.GestureDetector
Expand All @@ -21,7 +21,6 @@ import io.element.android.wysiwyg.view.StyleConfig
import io.element.android.wysiwyg.view.inlinebg.SpanBackgroundHelper
import io.element.android.wysiwyg.view.inlinebg.SpanBackgroundHelperFactory
import io.element.android.wysiwyg.view.spans.CustomMentionSpan
import io.element.android.wysiwyg.view.spans.LinkSpan
import io.element.android.wysiwyg.view.spans.PillSpan
import io.element.android.wysiwyg.view.spans.ReuseSourceSpannableFactory
import uniffi.wysiwyg_composer.MentionDetector
Expand Down Expand Up @@ -63,6 +62,8 @@ open class EditorStyledTextView : AppCompatTextView {

var onLinkClickedListener: ((String) -> Unit)? = null

var onTextLayoutChanged: ((Layout) -> Unit)? = null

/**
* In some contexts, such as screenshot tests, [isInEditMode] is may be forced to be false, when we
* need it to be true to disable native library loading. With this we can override this behaviour.
Expand Down Expand Up @@ -158,6 +159,12 @@ open class EditorStyledTextView : AppCompatTextView {
htmlConverter?.fromHtmlToSpans(htmlText)?.let { setText(it, BufferType.SPANNABLE) }
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)

layout?.let { onTextLayoutChanged?.invoke(it) }
Copy link
Contributor Author

@jmartinesp jmartinesp Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the text has already been measured so we can return this.layout through the callback.

}

override fun onDraw(canvas: Canvas) {
// need to draw bg first so that text can be on top during super.onDraw()
if (text is Spanned && layout != null && isInit) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package io.element.android.wysiwyg.utils

import io.element.android.wysiwyg.BuildConfig
import timber.log.Timber
import uniffi.wysiwyg_composer.Disposable

internal class RustCleanerTask(
private val disposable: Disposable,
) : Runnable {
override fun run() {
Timber.d("Cleaning up disposable: $disposable")
if (BuildConfig.DEBUG) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because the debug logs added here were being printed by the EXA app in debug mode. I could also remove the log completely.

Timber.d("Cleaning up disposable: $disposable")
}
disposable.destroy()
}
}
Loading