Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -12,6 +12,7 @@ import android.os.Build
import android.text.Editable
import android.text.InputType
import android.text.InputFilter
import android.text.Layout
import android.text.Spanned
import android.text.TextUtils
import android.text.TextWatcher
Expand All @@ -32,6 +33,7 @@ import expo.modules.kotlin.AppContext
import expo.modules.kotlin.viewevent.EventDispatcher
import expo.modules.kotlin.views.ExpoView
import org.json.JSONObject
import kotlin.math.ceil
import kotlin.math.max

class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
Expand Down Expand Up @@ -435,7 +437,7 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
// wrapping it, so getLineMax leaves it out.
val layout = editor.layout
val contentHeight = layout?.height ?: editor.measuredHeight
val contentWidth = if (layout?.lineCount == 1) layout.getLineMax(0).toInt() else 0
val contentWidth = singleLineContentWidth(layout)
if (contentHeight == lastContentHeight && contentWidth == lastContentWidth) return
lastContentHeight = contentHeight
lastContentWidth = contentWidth
Expand Down Expand Up @@ -497,6 +499,11 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
}
}

// Round up: a truncated width can match a slot the line overflows, and the composer
// then flips between its compact and expanded rows.
internal fun singleLineContentWidth(layout: Layout?): Int =
if (layout?.lineCount == 1) ceil(layout.getLineMax(0)).toInt() else 0

private fun Int.withAlpha(alpha: Int): Int = (this and 0x00FFFFFF) or (alpha shl 24)

private fun Context.resolveThemeColor(attribute: Int, fallback: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package expo.modules.t3composereditor

import android.text.Layout
import android.view.View
import android.view.ViewGroup
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import kotlin.math.floor

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [36], manifest = Config.NONE)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class ComposerContentWidthTest {
private val editor = SelectionAwareEditText(RuntimeEnvironment.getApplication()).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
)
textSize = 16f
setPadding(0, 0, 0, 0)
}

private fun layoutAt(width: Int): Layout {
editor.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
)
editor.layout(0, 0, width, editor.measuredHeight)
return editor.layout
}

// JS keeps the compact row when the reported width is at most the compact slot's.
@Test
fun reportedWidthStillFitsOnOneLine() {
editor.setText("I know t3 code exposes a websocket")
val wide = layoutAt(10_000)
val natural = wide.getLineMax(0)
assertNotEquals("the draft needs a fractional width", floor(natural), natural)

val reported = singleLineContentWidth(wide)
assertEquals(1, layoutAt(reported).lineCount)
}
}
Loading