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

Commit

Permalink
Android: add onTextLayout listener on the TextView component (#912)
Browse files Browse the repository at this point in the history
* Android: add `onTextLayout` listener on the TextView component.

This should allow us to use this result for text-based layouts.

* Rename `onTextLayoutChanged` to `onTextLayout` to match Compose `Text` parameter
  • Loading branch information
jmartinesp authored Dec 26, 2023
1 parent fcfef8a commit e9e9048
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
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 = {},
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.onTextLayout = 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 onTextLayout: ((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 { onTextLayout?.invoke(it) }
}

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) {
Timber.d("Cleaning up disposable: $disposable")
}
disposable.destroy()
}
}

0 comments on commit e9e9048

Please sign in to comment.