Skip to content
TypeScript

fix(mobile): stop Android composer flicker at the wrap boundary (#27682)

shivam16f1d0a239f8
diff --git a/apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt b/apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt
index 6b0534fdacc..e9a15a07d2f 100644
--- a/apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt
+++ b/apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt
@@ -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
@@ -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(
@@ -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
@@ -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 {
diff --git a/apps/mobile/modules/t3-composer-editor/android/src/test/java/expo/modules/t3composereditor/ComposerContentWidthTest.kt b/apps/mobile/modules/t3-composer-editor/android/src/test/java/expo/modules/t3composereditor/ComposerContentWidthTest.kt
new file mode 100644
index 00000000000..458ad4e007d
--- /dev/null
+++ b/apps/mobile/modules/t3-composer-editor/android/src/test/java/expo/modules/t3composereditor/ComposerContentWidthTest.kt
@@ -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)
+ }
+}