commit b80d5c5f9fc018dbb3b6bd4d652aaa7933dcd5c4
parent 7f4a35f0f09fbc1947411478227805987f8ee458
Author: Mugurell <Mugurell@users.noreply.github.com>
Date: Thu, 20 Nov 2025 07:59:24 +0000
Bug 1999634 - Prevent commiting lingering autocomplete suggestions in the browser toolbar r=android-reviewers,Roger
Speculative fix to avoid a potential race condition between updated autocomplete
suggestions and the query edited by user to ensure that the right text is commited
when pressing Go on the keyboard.
Differential Revision: https://phabricator.services.mozilla.com/D273262
Diffstat:
1 file changed, 15 insertions(+), 13 deletions(-)
diff --git a/mobile/android/android-components/components/compose/browser-toolbar/src/main/java/mozilla/components/compose/browser/toolbar/ui/InlineAutocompleteTextField.kt b/mobile/android/android-components/components/compose/browser-toolbar/src/main/java/mozilla/components/compose/browser/toolbar/ui/InlineAutocompleteTextField.kt
@@ -125,15 +125,15 @@ internal fun InlineAutocompleteTextField(
focusRequester.requestFocus()
}
- var currentSuggestion: AutocompleteResult? by remember(suggestion) { mutableStateOf(suggestion) }
+ var useSuggestion by remember { mutableStateOf(true) }
val suggestionTextColor = MaterialTheme.colorScheme.onSurface
val highlightBackgroundColor = Color(TEXT_HIGHLIGHT_COLOR.toColorInt())
- val suggestionVisualTransformation = remember(currentSuggestion, textFieldValue) {
- when (textFieldValue.text.isEmpty()) {
+ val suggestionVisualTransformation = remember(useSuggestion, suggestion, textFieldValue) {
+ when (textFieldValue.text.isEmpty() || !useSuggestion) {
true -> VisualTransformation.None
false -> AutocompleteVisualTransformation(
userInput = textFieldValue,
- suggestion = currentSuggestion,
+ suggestion = suggestion,
textColor = suggestionTextColor,
textBackground = highlightBackgroundColor,
)
@@ -141,8 +141,8 @@ internal fun InlineAutocompleteTextField(
}
val localView = LocalView.current
- LaunchedEffect(currentSuggestion) {
- currentSuggestion?.text?.let {
+ LaunchedEffect(suggestion) {
+ suggestion?.text?.let {
@Suppress("DEPRECATION")
localView.announceForAccessibility(it)
}
@@ -185,7 +185,7 @@ internal fun InlineAutocompleteTextField(
textFieldValue.composition == newValue.composition &&
textFieldValue.annotatedString == newValue.annotatedString
if (onlySelectionChanged) {
- currentSuggestion = null
+ useSuggestion = false
textFieldValue = newValue
return@BasicTextField
}
@@ -196,10 +196,11 @@ internal fun InlineAutocompleteTextField(
val newText = newValue.text
val isBackspaceHidingSuggestion = originalText.length == newText.length + 1 &&
originalText.startsWith(newText) &&
- currentSuggestion?.text?.startsWith(originalText) == true
+ (useSuggestion && suggestion?.text?.startsWith(originalText) == true)
if (isBackspaceHidingSuggestion) {
- currentSuggestion = null
+ useSuggestion = false
} else {
+ useSuggestion = true
onUrlEdit(
BrowserToolbarQuery(
previous = originalText,
@@ -235,9 +236,10 @@ internal fun InlineAutocompleteTextField(
keyboardActions = KeyboardActions(
onGo = {
keyboardController?.hide()
+ val currentSuggestion = suggestion?.text
onUrlCommitted(
- when (currentSuggestion?.text?.isNotEmpty()) {
- true -> currentSuggestion?.text.orEmpty()
+ when (useSuggestion && currentSuggestion?.startsWith(textFieldValue.text) == true) {
+ true -> currentSuggestion
else -> textFieldValue.text
},
)
@@ -262,11 +264,11 @@ internal fun InlineAutocompleteTextField(
modifier = Modifier
.fillMaxWidth()
// Commit the suggestion when users tap on the outside of the typed in text.
- .pointerInput(currentSuggestion, suggestionBounds) {
+ .pointerInput(suggestion, suggestionBounds) {
awaitEachGesture {
val downEvent = awaitFirstDown(requireUnconsumed = false)
val bounds = suggestionBounds
- val suggestion = currentSuggestion?.text
+ val suggestion = suggestion?.text
if (bounds != null && suggestion != null &&
bounds.right < downEvent.position.x
) {