Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ internal class SuggestionStripLayoutHelper(
suggestedWords, maxSuggestions
)
}
if (wordCountToShow == 1 || getTextScaleX(centerWordView.text, centerWidth, centerWordView.paint) < MIN_TEXT_XSCALE) {
val centerInfo = (centerWordView.tag as? Int)?.let { suggestedWords.getInfo(it) }
if (wordCountToShow == 1 || (!isExpanderPreview(centerInfo)
&& getTextScaleX(centerWordView.text, centerWidth, centerWordView.paint) < MIN_TEXT_XSCALE)) {
val countInStrip = 1
mMoreSuggestionsAvailable = wordCountToShow > countInStrip
layoutWord(context, mCenterPositionInStrip, stripWidth - mPadding, suggestedWords)
Expand Down Expand Up @@ -648,6 +650,11 @@ internal class SuggestionStripLayoutHelper(
return bitmapDrawable
}

// Text expander previews may be ellipsized, so a long one must not hide the other suggestions.
// Dictionary shortcuts have no display label: they are committed as shown and need to be readable.
fun isExpanderPreview(info: SuggestedWords.SuggestedWordInfo?): Boolean =
info != null && info.isKindOf(SuggestedWords.SuggestedWordInfo.KIND_SHORTCUT) && info.mDisplayLabel != null

fun shouldOmitTypedWord(
inputStyle: Int,
gestureFloatingPreviewTextEnabled: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,9 @@ object TextExpanderUtils {
}
val label = if (isImage) {
if (entry.template.isNotBlank()) formatPreview(entry.template) else ""
} else if (isExactMatch) {
formatPreview(entry.template)
} else {
"$shortcut: ${formatPreview(entry.template)}"
// a blank expansion (e.g. only a line break) would give an empty chip
formatPreview(entry.template).ifEmpty { shortcut }
}
matches.add(
RankedSuggestion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,32 +975,6 @@ private fun ShortcutItem(
)
}
}
if (isImage) {
Box(
modifier = Modifier
.clip(RoundedCornerShape(6.dp))
.background(MaterialTheme.colorScheme.secondaryContainer)
.padding(horizontal = 6.dp, vertical = 2.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(3.dp)
) {
Icon(
painter = painterResource(R.drawable.ic_image),
contentDescription = null,
modifier = Modifier.size(12.dp),
tint = MaterialTheme.colorScheme.onSecondaryContainer
)
Text(
text = stringResource(R.string.text_expander_image_shortcut_badge),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSecondaryContainer,
fontWeight = FontWeight.Bold
)
}
}
}
}
Spacer(modifier = Modifier.height(6.dp))
val displayText = if (isImageMissing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
package helium314.keyboard.latin.suggestions

import helium314.keyboard.latin.SuggestedWords
import helium314.keyboard.latin.dictionary.Dictionary
import helium314.keyboard.latin.suggestions.SuggestionStripLayoutHelper.Companion.getPositionInSuggestionStrip
import helium314.keyboard.latin.suggestions.SuggestionStripLayoutHelper.Companion.isExpanderPreview
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@RunWith(RobolectricTestRunner::class)
class SuggestionStripLayoutHelperTest {
Expand Down Expand Up @@ -46,4 +50,16 @@ class SuggestionStripLayoutHelperTest {
val positions = (1..5).map { position(it, willAutoCorrect = true, centerTypedWord = true, center = 2, left = 1) }
assertEquals(listOf(1, 2, 3, 0, 4), positions)
}

private fun info(kind: Int, label: String?) = SuggestedWords.SuggestedWordInfo(
"Upashahar, sylhet", "", SuggestedWords.SuggestedWordInfo.MAX_SCORE, kind, Dictionary.DICTIONARY_USER_TYPED,
SuggestedWords.SuggestedWordInfo.NOT_AN_INDEX, SuggestedWords.SuggestedWordInfo.NOT_A_CONFIDENCE, label
)

@Test fun onlyExpanderChipsMayBeEllipsizedInsteadOfCollapsingTheStrip() {
assertTrue(isExpanderPreview(info(SuggestedWords.SuggestedWordInfo.KIND_SHORTCUT, "Add3: Upashahar, sylhet")))
assertFalse(isExpanderPreview(info(SuggestedWords.SuggestedWordInfo.KIND_SHORTCUT, null)))
assertFalse(isExpanderPreview(info(SuggestedWords.SuggestedWordInfo.KIND_CORRECTION, null)))
assertFalse(isExpanderPreview(null))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TextExpanderTabTest {

// Prefixed text shortcuts: keep prefix autocomplete ("shortcut: preview") -> exact shows only preview
val prefixedMatches = TextExpanderUtils.getMatchingSuggestions("ad", ";ad", context)
assertEquals("addr: 456 Oak Ave", prefixedMatches.first().previewLabel)
assertEquals("456 Oak Ave", prefixedMatches.first().previewLabel)
val prefixedFullMatches = TextExpanderUtils.getMatchingSuggestions("addr", ";addr", context)
assertEquals("456 Oak Ave", prefixedFullMatches.first().previewLabel)

Expand All @@ -71,8 +71,8 @@ class TextExpanderTabTest {
assertEquals(emptyList<TextExpanderUtils.SuggestionMatch>(), TextExpanderUtils.getMatchingSuggestions("a", "a", context))
assertEquals(emptyList<TextExpanderUtils.SuggestionMatch>(), TextExpanderUtils.getMatchingSuggestions("c", "c", context))
assertEquals(emptyList<TextExpanderUtils.SuggestionMatch>(), TextExpanderUtils.getMatchingSuggestions("d", "d", context))
assertEquals("addr: 123 Main St", TextExpanderUtils.getMatchingSuggestions("ad", "ad", context).first().previewLabel)
assertEquals("Olp: H", TextExpanderUtils.getMatchingSuggestions("Ol", "Ol", context).first().previewLabel)
assertEquals("123 Main St", TextExpanderUtils.getMatchingSuggestions("ad", "ad", context).first().previewLabel)
assertEquals("H", TextExpanderUtils.getMatchingSuggestions("Ol", "Ol", context).first().previewLabel)
assertEquals("123 Main St", TextExpanderUtils.getMatchingSuggestions("addr", "addr", context).first().previewLabel)
assertEquals("H", TextExpanderUtils.getMatchingSuggestions("olp", "olp", context).first().previewLabel)
assertEquals("Cute Kitty", TextExpanderUtils.getMatchingSuggestions("ca", "ca", context).first().previewLabel)
Expand Down Expand Up @@ -136,7 +136,7 @@ class TextExpanderTabTest {
val prefixedTextPrefix = TextExpanderUtils.getMatchingSuggestions("c", ";c", context)
assertEquals(1, prefixedTextPrefix.size)
assertEquals(1, prefixedTextPrefix.single().prefixLength)
assertEquals("cic: Hello world", prefixedTextPrefix.single().previewLabel)
assertEquals("Hello world", prefixedTextPrefix.single().previewLabel)

val prefixedTextExact = TextExpanderUtils.getMatchingSuggestions("cic", ";cic", context)
assertEquals(1, prefixedTextExact.size)
Expand Down Expand Up @@ -187,7 +187,13 @@ class TextExpanderTabTest {
val matchesHashPartial = TextExpanderUtils.getMatchingSuggestions("to", "note #to", context)
assertEquals(1, matchesHashPartial.size)
assertEquals(1, matchesHashPartial.single().prefixLength) // '#' needs to be deleted
assertEquals("#todo: Task list:", matchesHashPartial.single().previewLabel)
assertEquals("Task list:", matchesHashPartial.single().previewLabel)

// a blank expansion falls back to the shortcut instead of an empty chip
TextExpanderUtils.saveShortcuts(context, mutableMapOf(
"nl" to TextExpanderUtils.ShortcutEntry(template = "\n", prefix = "")
))
assertEquals("nl", TextExpanderUtils.getMatchingSuggestions("nl", "nl", context).single().previewLabel)
}

@Test
Expand Down
Loading