Compare commits

...
Author SHA1 Message Date
The412Banner f34db60462 fix(typing): lay out only a window of long text in the typing panel
A single-line Compose text field measures its whole text at full width.
A pasted 40000-character string is wider than Constraints can represent,
so the measure pass threw IllegalArgumentException on the main thread and
the session closed. The field now keeps and sends the full value but lays
out at most 500 characters around the cursor.
2026-11-29 17:46:22 +00:00
2 changed files with 124 additions and 1 deletions

No files matched your search

@@ -28,6 +28,7 @@ import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.CommitTextCommand
import androidx.compose.ui.text.input.DeleteAllCommand
@@ -35,12 +36,14 @@ import androidx.compose.ui.text.input.EditCommand
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.ImeOptions
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.OffsetMapping
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.PlatformTextInputService
import androidx.compose.ui.text.input.SetSelectionCommand
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.TextInputService
import androidx.compose.ui.text.input.TextInputSession
import androidx.compose.ui.text.input.TransformedText
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
@@ -73,6 +76,33 @@ class TypingRequest(
}
}
internal data class FieldWindow(
private val cursor: Int,
private val inner: VisualTransformation = VisualTransformation.None,
private val size: Int = SIZE,
) : VisualTransformation {
override fun filter(text: AnnotatedString): TransformedText {
if (text.length <= size) return inner.filter(text)
var start = (cursor - size / 2).coerceIn(0, text.length - size)
var end = start + size
if (Character.isLowSurrogate(text[start])) start++
if (end < text.length && Character.isLowSurrogate(text[end])) end--
val shown = inner.filter(text.subSequence(start, end))
val length = end - start
return TransformedText(shown.text, object : OffsetMapping {
override fun originalToTransformed(offset: Int): Int =
shown.offsetMapping.originalToTransformed((offset - start).coerceIn(0, length))
override fun transformedToOriginal(offset: Int): Int =
start + shown.offsetMapping.transformedToOriginal(offset).coerceIn(0, length)
})
}
companion object {
const val SIZE = 500
}
}
internal val LocalBottomTyping = staticCompositionLocalOf { false }
internal class BottomScreenTextInput(private val fallback: TextInputService) : PlatformTextInputService {
@@ -156,7 +186,7 @@ internal fun TypingPanel(
singleLine = options.singleLine,
maxLines = if (options.singleLine) 1 else 4,
textStyle = MaterialTheme.typography.titleLarge,
visualTransformation = if (secret) PasswordVisualTransformation() else VisualTransformation.None,
visualTransformation = FieldWindow(field.selection.end, if (secret) PasswordVisualTransformation() else VisualTransformation.None),
keyboardOptions = KeyboardOptions(
capitalization = options.capitalization, autoCorrect = options.autoCorrect,
keyboardType = options.keyboardType, imeAction = options.imeAction,
@@ -0,0 +1,93 @@
package com.droiddeck.launcher.ui
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.ImeOptions
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.TextFieldValue
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33], qualifiers = "w960dp-h540dp-land-xxhdpi", application = android.app.Application::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class BottomScreenTypingLayoutTest {
@get:Rule val compose = createRobolectricComposeRule()
private val digits = AnnotatedString((0 until 100).joinToString("") { (it % 10).toString() })
@Test fun shortTextIsShownWhole() {
val shown = FieldWindow(2).filter(AnnotatedString("abc"))
assertEquals("abc", shown.text.text)
assertEquals(2, shown.offsetMapping.originalToTransformed(2))
assertEquals(3, shown.offsetMapping.transformedToOriginal(3))
}
@Test fun longTextShowsTheEndAfterAPaste() {
val shown = FieldWindow(100, size = 10).filter(digits)
assertEquals(digits.text.takeLast(10), shown.text.text)
assertEquals(10, shown.offsetMapping.originalToTransformed(100))
assertEquals(0, shown.offsetMapping.originalToTransformed(0))
assertEquals(90, shown.offsetMapping.transformedToOriginal(0))
assertEquals(100, shown.offsetMapping.transformedToOriginal(10))
}
@Test fun longTextFollowsTheCursor() {
val shown = FieldWindow(50, size = 10).filter(digits)
assertEquals(digits.text.substring(45, 55), shown.text.text)
assertEquals(5, shown.offsetMapping.originalToTransformed(50))
assertEquals(52, shown.offsetMapping.transformedToOriginal(7))
assertEquals(10, shown.offsetMapping.originalToTransformed(99))
}
@Test fun windowNeverSplitsASurrogatePair() {
val text = AnnotatedString("😀".repeat(20))
listOf(0, 7, 13, 20, 40).forEach { cursor ->
val shown = FieldWindow(cursor, size = 9).filter(text).text.text
assertEquals(false, Character.isLowSurrogate(shown.first()))
assertEquals(false, Character.isHighSurrogate(shown.last()))
}
}
@Test fun secretTextStaysMaskedInTheWindow() {
val shown = FieldWindow(100, PasswordVisualTransformation(), 10).filter(digits)
assertEquals("•".repeat(10), shown.text.text)
assertEquals(95, shown.offsetMapping.transformedToOriginal(5))
}
@Test fun longPasteIntoTheFieldLaysOutAndSendsWhole() {
longPasteLaysOutAndSendsWhole(ImeOptions(singleLine = true, imeAction = ImeAction.Send))
}
@Test fun longPasteIntoAMultiLineFieldLaysOut() {
longPasteLaysOutAndSendsWhole(ImeOptions(singleLine = false))
}
@Test fun longPasteIntoASecretFieldLaysOut() {
longPasteLaysOutAndSendsWhole(ImeOptions(singleLine = true, keyboardType = KeyboardType.Password))
}
private fun longPasteLaysOutAndSendsWhole(options: ImeOptions) {
val pasted = "W".repeat(40_000)
val request = TypingRequest(TextFieldValue(pasted, TextRange(pasted.length)), options, {}, {})
var sent: String? = null
compose.setContent {
DroidDeckTheme {
TypingPanel(request, showKeyboard = false, onDone = { sent = request.value.text })
}
}
compose.waitForIdle()
compose.onNodeWithTag("dual-typing-done").performClick()
compose.waitForIdle()
assertEquals(pasted, sent)
}
}