diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelper.kt b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelper.kt index 4a0bfe022e..8e16df703c 100644 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelper.kt +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelper.kt @@ -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) @@ -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, diff --git a/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt index bc00a8fb2b..58973c530e 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt @@ -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( diff --git a/app/src/main/java/helium314/keyboard/settings/screens/TextExpanderScreen.kt b/app/src/main/java/helium314/keyboard/settings/screens/TextExpanderScreen.kt index 1d17fc5812..d6a9a7f61d 100644 --- a/app/src/main/java/helium314/keyboard/settings/screens/TextExpanderScreen.kt +++ b/app/src/main/java/helium314/keyboard/settings/screens/TextExpanderScreen.kt @@ -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) { diff --git a/app/src/test/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelperTest.kt b/app/src/test/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelperTest.kt index 293b57f8c0..c005984225 100644 --- a/app/src/test/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelperTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelperTest.kt @@ -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 { @@ -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)) + } } diff --git a/app/src/test/java/helium314/keyboard/latin/utils/TextExpanderTabTest.kt b/app/src/test/java/helium314/keyboard/latin/utils/TextExpanderTabTest.kt index bcf1002eb6..f2113687cc 100644 --- a/app/src/test/java/helium314/keyboard/latin/utils/TextExpanderTabTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/utils/TextExpanderTabTest.kt @@ -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) @@ -71,8 +71,8 @@ class TextExpanderTabTest { assertEquals(emptyList(), TextExpanderUtils.getMatchingSuggestions("a", "a", context)) assertEquals(emptyList(), TextExpanderUtils.getMatchingSuggestions("c", "c", context)) assertEquals(emptyList(), 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) @@ -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) @@ -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