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.ktindex 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.Buildimport android.text.Editableimport android.text.InputTypeimport android.text.InputFilter+import android.text.Layoutimport android.text.Spannedimport android.text.TextUtilsimport android.text.TextWatcher@@ -32,6 +33,7 @@ import expo.modules.kotlin.AppContextimport expo.modules.kotlin.viewevent.EventDispatcherimport expo.modules.kotlin.views.ExpoViewimport org.json.JSONObject+import kotlin.math.ceilimport kotlin.math.maxclass 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.layoutval 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) returnlastContentHeight = contentHeightlastContentWidth = 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.ktnew file mode 100644index 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)+ }+}