Skip to content

Commit 2ec0e10

Browse files
committed
feat: highlight autocomplete suggestion when it matches typed word
Signed-off-by: Mounil Kanakhara <mounilkankhara@gmail.com>
1 parent 4348ffa commit 2ec0e10

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

app/src/keyboards/java/be/scri/helpers/AutocompletionHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class AutocompletionHandler(
4949
val completions = ime.getAutocompletions(currentWord, limit = 5)
5050

5151
if (completions.isNotEmpty()) {
52-
ime.updateAutocompleteSuggestions(completions)
52+
ime.updateAutocompleteSuggestions(completions, currentWord)
5353
} else {
5454
ime.clearAutocomplete()
5555
}

app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.content.Context
88
import android.content.Intent
99
import android.database.sqlite.SQLiteException
1010
import android.graphics.Color
11+
import android.graphics.Typeface
1112
import android.graphics.drawable.GradientDrawable
1213
import android.graphics.drawable.LayerDrawable
1314
import android.graphics.drawable.RippleDrawable
@@ -1766,16 +1767,25 @@ abstract class GeneralKeyboardIME(
17661767
private fun setSuggestionButton(
17671768
button: Button,
17681769
text: String,
1770+
isExactMatch: Boolean = false,
17691771
) {
17701772
val isUserDarkMode = getIsDarkModeOrNot(applicationContext)
1771-
val textColor = if (isUserDarkMode) Color.WHITE else "#1E1E1E".toColorInt()
1773+
val textColor =
1774+
if (isExactMatch) {
1775+
ContextCompat.getColor(applicationContext, R.color.theme_scribe_blue)
1776+
} else if (isUserDarkMode) {
1777+
Color.WHITE
1778+
} else {
1779+
"#1E1E1E".toColorInt()
1780+
}
17721781
button.text = text
17731782
button.isAllCaps = false
17741783
button.visibility = View.VISIBLE
17751784
button.textSize = SUGGESTION_SIZE
17761785
button.setOnClickListener(null)
17771786
button.background = null
17781787
button.setTextColor(textColor)
1788+
button.setTypeface(button.typeface, if (isExactMatch) Typeface.BOLD else Typeface.NORMAL)
17791789
button.setOnClickListener {
17801790
currentInputConnection?.commitText("$text ", 1)
17811791
moveToIdleState()
@@ -1788,7 +1798,10 @@ abstract class GeneralKeyboardIME(
17881798
* Updates autocomplete UI with a new list of suggestions.
17891799
* Clears it if not idle or no completions.
17901800
*/
1791-
fun updateAutocompleteSuggestions(completions: List<String>?) {
1801+
fun updateAutocompleteSuggestions(
1802+
completions: List<String>?,
1803+
currentWord: String? = null,
1804+
) {
17921805
if (currentState != ScribeState.IDLE) {
17931806
uiManager.disableAutoSuggest(language)
17941807
return
@@ -1802,9 +1815,9 @@ abstract class GeneralKeyboardIME(
18021815
val completion2 = completions.getOrNull(1) ?: ""
18031816
val completion3 = completions.getOrNull(2) ?: ""
18041817

1805-
setAutocompleteButton(uiManager.binding.conjugateBtn, completion1)
1806-
setAutocompleteButton(uiManager.binding.translateBtn, completion2)
1807-
setAutocompleteButton(uiManager.pluralBtn!!, completion3)
1818+
setAutocompleteButton(uiManager.binding.conjugateBtn, completion1, currentWord)
1819+
setAutocompleteButton(uiManager.binding.translateBtn, completion2, currentWord)
1820+
setAutocompleteButton(uiManager.pluralBtn!!, completion3, currentWord)
18081821

18091822
uiManager.binding.separator1.visibility = View.VISIBLE
18101823
uiManager.binding.separator2.visibility = View.VISIBLE
@@ -1813,12 +1826,16 @@ abstract class GeneralKeyboardIME(
18131826
/**
18141827
* Sets up an autocomplete button with the given suggestion text.
18151828
* When clicked, it replaces the current word with the suggestion.
1829+
* If the suggestion exactly matches what the user has already typed,
1830+
* the button is highlighted to signal that pressing it is a no-op.
18161831
*/
18171832
private fun setAutocompleteButton(
18181833
button: Button,
18191834
text: String,
1835+
currentWord: String? = null,
18201836
) {
1821-
setSuggestionButton(button, text)
1837+
val isExactMatch = text.isNotBlank() && text.equals(currentWord, ignoreCase = true)
1838+
setSuggestionButton(button, text, isExactMatch)
18221839
if (text.isBlank()) {
18231840
button.setOnClickListener(null)
18241841
return

0 commit comments

Comments
 (0)