diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a0fc7583..d3b2c4517 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Emojis for the following are chosen based on [gitmoji](https://gitmoji.dev/). ### 🎨 Design - Compose theme colors now come from the shared `scribe_*` resources. Dark mode button text stays orange on orange. The dark caption uses the named token `#CAC4D0`, the same grey this slot already used ([#782](https://github.com/scribe-org/Scribe-Android/issues/782)). +- The keyboard reads the shared `scribe_*` colors instead of hex literals. Small intentional changes: the light invalid and already-plural bar matches the keyboard background, dark letter keys use `scribe_key`, suggestions match the key labels, Enter, delete, shift and the mode key use the Figma special-key colors, and the clipboard clear-all icon uses the new `scribe_delete` red, and the Wikidata info-panel page dots use the key text color when active and `scribe_caption` when not ([#783](https://github.com/scribe-org/Scribe-Android/issues/783)). ### 🔒 Security diff --git a/app/src/keyboards/java/be/scri/helpers/FloatingKeyboardHandler.kt b/app/src/keyboards/java/be/scri/helpers/FloatingKeyboardHandler.kt index 884b839fb..406bcd594 100644 --- a/app/src/keyboards/java/be/scri/helpers/FloatingKeyboardHandler.kt +++ b/app/src/keyboards/java/be/scri/helpers/FloatingKeyboardHandler.kt @@ -17,6 +17,7 @@ import android.view.WindowManager import androidx.core.content.ContextCompat import be.scri.R import be.scri.helpers.PreferencesHelper.getIsDarkModeOrNot +import be.scri.ui.theme.scribeColorInt /** * Manages floating keyboard state, resize handles, touch listeners, and layout transitions @@ -160,7 +161,7 @@ class FloatingKeyboardHandler( shape = GradientDrawable.RECTANGLE cornerRadius = 16f * density setColor(kbBgColor) - setStroke((1f * density).toInt(), 0x40888888.toInt()) + setStroke((1f * density).toInt(), ime.imeContext.scribeColorInt(R.color.scribe_keyboard_border, dark = isDarkMode)) } card.background = floatingBg card.elevation = 8f * density diff --git a/app/src/keyboards/java/be/scri/helpers/ui/KeyboardThemeManager.kt b/app/src/keyboards/java/be/scri/helpers/ui/KeyboardThemeManager.kt index 788ef0f94..404645d57 100644 --- a/app/src/keyboards/java/be/scri/helpers/ui/KeyboardThemeManager.kt +++ b/app/src/keyboards/java/be/scri/helpers/ui/KeyboardThemeManager.kt @@ -16,13 +16,13 @@ import android.widget.Button import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.graphics.ColorUtils -import androidx.core.graphics.toColorInt import androidx.core.view.ViewCompat import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsControllerCompat import be.scri.R import be.scri.helpers.PreferencesHelper.getIsDarkModeOrNot +import be.scri.ui.theme.scribeColorInt /** * Manages UI theme color resolutions, gradient/ripple drawables creation, @@ -232,6 +232,7 @@ class KeyboardThemeManager { */ fun getSuggestionTextColor(context: Context): Int { val isDarkMode = getIsDarkModeOrNot(context) - return if (isDarkMode) Color.WHITE else "#1E1E1E".toColorInt() + // Same color as the characters on the keys. + return context.scribeColorInt(R.color.scribe_text, dark = isDarkMode) } } diff --git a/app/src/keyboards/java/be/scri/helpers/ui/KeyboardUIManager.kt b/app/src/keyboards/java/be/scri/helpers/ui/KeyboardUIManager.kt index 8d6155576..c5a176119 100644 --- a/app/src/keyboards/java/be/scri/helpers/ui/KeyboardUIManager.kt +++ b/app/src/keyboards/java/be/scri/helpers/ui/KeyboardUIManager.kt @@ -24,10 +24,10 @@ import android.view.View import android.widget.Button import android.widget.LinearLayout import android.widget.TextView +import androidx.annotation.ColorInt import androidx.appcompat.content.res.AppCompatResources import androidx.core.content.ContextCompat import androidx.core.content.edit -import androidx.core.graphics.toColorInt import androidx.core.view.updateLayoutParams import androidx.recyclerview.widget.GridLayoutManager import be.scri.R @@ -52,11 +52,10 @@ import be.scri.helpers.parseRawEmojiSpecsFile import be.scri.models.ScribeLanguage import be.scri.models.ScribeState import be.scri.services.GeneralKeyboardIME +import be.scri.ui.theme.scribeColorInt import be.scri.views.KeyboardView private const val SUB_SELECTION_LABEL_TEXT_SIZE_RATIO = 0.8f -private const val CONJUGATION_LABEL_COLOR_LIGHT = "#E558564B" -private const val CONJUGATION_LABEL_COLOR_DARK = "#CAC4D0" private const val MAX_FORM_COUNT_2X1 = 2 private const val FORM_COUNT_3X1 = 3 private const val MAX_FORM_COUNT_2X2 = 4 @@ -269,7 +268,8 @@ class KeyboardUIManager( ), ) - val textColor = if (isUserDarkMode) Color.WHITE else "#1E1E1E".toColorInt() + // Same color as the characters on the keys. + val textColor = context.scribeColorInt(R.color.scribe_text, dark = isUserDarkMode) listOf(binding.translateBtn, binding.conjugateBtn, binding.pluralBtn).forEachIndexed { index, button -> button.visibility = View.VISIBLE @@ -370,10 +370,6 @@ class KeyboardUIManager( binding.pluralBtn.foreground = null } - val separatorColor = (if (isUserDarkMode) GeneralKeyboardIME.DARK_THEME else GeneralKeyboardIME.LIGHT_THEME).toColorInt() - binding.separator2.setBackgroundColor(separatorColor) - binding.separator3.setBackgroundColor(separatorColor) - val spaceInDp = 4 val spaceInPx = (spaceInDp * context.resources.displayMetrics.density).toInt() listOf(binding.separator2, binding.separator3).forEach { separator -> @@ -516,10 +512,16 @@ class KeyboardUIManager( R.id.conjugate_btn_6, ) + /** + * Color of the small label above a conjugation form, resolved once per grid. + */ + @ColorInt + private fun conjugationLabelColor(isDarkMode: Boolean): Int = context.scribeColorInt(R.color.scribe_caption, dark = isDarkMode) + private fun formatButtonText( label: String, form: String, - isDarkMode: Boolean, + @ColorInt labelColor: Int, ): CharSequence { if (label.isEmpty() || label == "all") return form val spannable = SpannableStringBuilder() @@ -530,8 +532,6 @@ class KeyboardUIManager( label.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE, ) - val labelColor = - (if (isDarkMode) CONJUGATION_LABEL_COLOR_DARK else CONJUGATION_LABEL_COLOR_LIGHT).toColorInt() spannable.setSpan( ForegroundColorSpan(labelColor), 0, @@ -586,6 +586,7 @@ class KeyboardUIManager( subPairs: List>, isDarkMode: Boolean, ) { + val labelColor = conjugationLabelColor(isDarkMode) buttonIds.forEachIndexed { i, btnId -> val btn = gridContent.findViewById(btnId) ?: return@forEachIndexed if (i < subPairs.size) { @@ -599,7 +600,7 @@ class KeyboardUIManager( btn.setTextColor(if (isDarkMode) Color.WHITE else Color.BLACK) val hasLabel = label.isNotEmpty() && label != "all" applyButtonLayout(btn, hasLabel) - btn.text = formatButtonText(label, form, isDarkMode) + btn.text = formatButtonText(label, form, labelColor) btn.setOnClickListener { listener.commitText("$form ") listener.processLinguisticSuggestions(form) @@ -616,6 +617,7 @@ class KeyboardUIManager( categoryEntries: List>>>, isDarkMode: Boolean, ) { + val labelColor = conjugationLabelColor(isDarkMode) buttonIds.forEachIndexed { i, btnId -> val btn = gridContent.findViewById(btnId) ?: return@forEachIndexed if (i < categoryEntries.size) { @@ -629,7 +631,7 @@ class KeyboardUIManager( btn.setTextColor(if (isDarkMode) Color.WHITE else Color.BLACK) val formsPreview = pairs.map { it.second.trim() }.filter { it.isNotBlank() }.joinToString(" / ") applyButtonLayout(btn, categoryTitle.isNotEmpty()) - btn.text = formatButtonText(categoryTitle, formsPreview, isDarkMode) + btn.text = formatButtonText(categoryTitle, formsPreview, labelColor) if (pairs.size == 1) { val (_, form) = pairs.first() btn.setOnClickListener { @@ -653,6 +655,7 @@ class KeyboardUIManager( directPairs: List>, isDarkMode: Boolean, ) { + val labelColor = conjugationLabelColor(isDarkMode) buttonIds.forEachIndexed { i, btnId -> val btn = gridContent.findViewById(btnId) ?: return@forEachIndexed if (i < directPairs.size) { @@ -666,7 +669,7 @@ class KeyboardUIManager( btn.setTextColor(if (isDarkMode) Color.WHITE else Color.BLACK) val hasLabel = label.isNotEmpty() && label != "all" applyButtonLayout(btn, hasLabel) - btn.text = formatButtonText(label, form, isDarkMode) + btn.text = formatButtonText(label, form, labelColor) btn.setOnClickListener { listener.commitText("$form ") listener.processLinguisticSuggestions(form) @@ -748,9 +751,7 @@ class KeyboardUIManager( val isDarkMode = getIsDarkModeOrNot(context) // Restore original logic: Set background on toolbarBar, not invalidInfoBar. - binding.toolbarBar.setBackgroundColor( - if (isDarkMode) "#1E1E1E".toColorInt() else "#d2d4da".toColorInt(), - ) + binding.toolbarBar.setBackgroundColor(commandInfoBarColor(isDarkMode)) val isWikidata = invalidCommandSource != ScribeState.TRANSLATE val invalidMsg = @@ -772,6 +773,11 @@ class KeyboardUIManager( binding.scribeKeyToolbar.foreground = AppCompatResources.getDrawable(context, R.drawable.ic_scribe_icon_vector) } + /** + * Background of the command bar while it shows the invalid or already-plural message. + */ + private fun commandInfoBarColor(isDarkMode: Boolean): Int = context.scribeColorInt(R.color.scribe_keyboard_background, dark = isDarkMode) + /** * Configures the UI for the `ALREADY_PLURAL` state, which is shown when the user * attempts to pluralize a word that is already plural. @@ -781,7 +787,7 @@ class KeyboardUIManager( binding.commandOptionsBar.visibility = View.GONE binding.toolbarBar.visibility = View.VISIBLE val isDarkMode = getIsDarkModeOrNot(context) - binding.toolbarBar.setBackgroundColor(if (isDarkMode) "#1E1E1E".toColorInt() else "#d2d4da".toColorInt()) + binding.toolbarBar.setBackgroundColor(commandInfoBarColor(isDarkMode)) binding.ivInfo.visibility = View.VISIBLE binding.promptText.text = "$ALREADY_PLURAL_MSG: " binding.commandBar.hint = "" @@ -1429,16 +1435,11 @@ class KeyboardUIManager( private fun updateDotIndicators() { val pageIndicators = binding.pageIndicators val isDarkMode = getIsDarkModeOrNot(context) + val activeColor = context.scribeColorInt(R.color.scribe_text, dark = isDarkMode) + val inactiveColor = context.scribeColorInt(R.color.scribe_caption, dark = isDarkMode) for (i in 0 until pageIndicators.childCount) { - val dot = pageIndicators.getChildAt(i) - val drawableRes = if (i == currentPage) R.drawable.dot_active else R.drawable.dot_inactive - val drawable = ContextCompat.getDrawable(context, drawableRes)?.mutate() - if (isDarkMode) { - drawable?.setTint(if (i == currentPage) Color.WHITE else 0x66FFFFFF.toInt()) - } else { - drawable?.setTint(if (i == currentPage) Color.BLACK else 0xFFCCCCCC.toInt()) - } - dot.background = drawable + val color = if (i == currentPage) activeColor else inactiveColor + pageIndicators.getChildAt(i).backgroundTintList = ColorStateList.valueOf(color) } } @@ -1503,6 +1504,8 @@ class KeyboardUIManager( binding.clipboardPanelClose.setColorFilter(textColor) binding.clipboardPanelLabel.setTextColor(textColor) binding.clipboardEmptyText.setTextColor(textColor) + binding.clipboardClearAll.imageTintList = + ColorStateList.valueOf(context.scribeColorInt(R.color.scribe_delete, dark = isDarkMode)) } fun hideClipboardPanel() { diff --git a/app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt index f849cc7c6..ced220d7a 100644 --- a/app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt @@ -266,8 +266,6 @@ abstract class GeneralKeyboardIME( const val SMALLEST_SCREEN_WIDTH_TABLET = 600 const val TEXT_LENGTH = 20 const val SUGGESTION_SIZE = 15f - const val DARK_THEME = "#aeb3be" - const val LIGHT_THEME = "#4b4b4b" internal const val MAX_TEXT_LENGTH = 1000 const val COMMIT_TEXT_CURSOR_POSITION = 1 internal const val CUSTOM_CURSOR = "│" // special tall cursor character diff --git a/app/src/main/java/be/scri/ui/theme/Colors.kt b/app/src/main/java/be/scri/ui/theme/Colors.kt index 5624a6f03..c7bc4855b 100644 --- a/app/src/main/java/be/scri/ui/theme/Colors.kt +++ b/app/src/main/java/be/scri/ui/theme/Colors.kt @@ -3,7 +3,6 @@ package be.scri.ui.theme import android.content.Context -import android.content.res.Configuration import androidx.compose.material3.ColorScheme import androidx.compose.material3.darkColorScheme import androidx.compose.material3.lightColorScheme @@ -16,7 +15,7 @@ import be.scri.R * [dark] follows the app theme argument, not the device night mode. */ fun Context.scribeColorScheme(dark: Boolean): ColorScheme { - val themed = configurationContext(dark) + val themed = scribeNightContext(dark) val button = themed.scribeColor(R.color.scribe_button) val onButton = themed.scribeColor(R.color.scribe_on_button) val background = themed.scribeColor(R.color.scribe_background) @@ -67,11 +66,4 @@ fun Context.scribeColorScheme(dark: Boolean): ColorScheme { } } -private fun Context.configurationContext(dark: Boolean): Context { - val nightMode = if (dark) Configuration.UI_MODE_NIGHT_YES else Configuration.UI_MODE_NIGHT_NO - val config = Configuration(resources.configuration) - config.uiMode = (config.uiMode and Configuration.UI_MODE_NIGHT_MASK.inv()) or nightMode - return createConfigurationContext(config) -} - private fun Context.scribeColor(id: Int): Color = Color(color = ContextCompat.getColor(this, id)) diff --git a/app/src/main/java/be/scri/ui/theme/ScribeColorResolver.kt b/app/src/main/java/be/scri/ui/theme/ScribeColorResolver.kt new file mode 100644 index 000000000..d77d0557e --- /dev/null +++ b/app/src/main/java/be/scri/ui/theme/ScribeColorResolver.kt @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package be.scri.ui.theme + +import android.content.Context +import android.content.res.Configuration +import android.util.SparseIntArray +import androidx.annotation.ColorInt +import androidx.annotation.ColorRes +import androidx.annotation.MainThread +import androidx.annotation.VisibleForTesting +import androidx.core.content.ContextCompat +import androidx.core.util.size + +// Colors are only split by night mode (values and values-night), so a color id and the +// dark flag fully decide the value. One SparseIntArray per mode keeps lookups free of boxing, +// as suggestions are colored on every keystroke. Every caller sets a view or paint color, +// so lookups happen on the main thread and the caches are not synchronized. +private val lightColorCache = SparseIntArray() +private val darkColorCache = SparseIntArray() + +/** + * Reads a shared `scribe_*` color for light or dark. + * [dark] follows the Scribe dark mode setting, not the device night mode, + * so the keyboard and the Compose theme pick the same value. + */ +@MainThread +@ColorInt +fun Context.scribeColorInt( + @ColorRes id: Int, + dark: Boolean, +): Int { + val cache = if (dark) darkColorCache else lightColorCache + // indexOfKey rather than get(id, default): any int, including 0 (transparent), is a valid color. + val index = cache.indexOfKey(id) + if (index >= 0) return cache.valueAt(index) + val color = ContextCompat.getColor(scribeNightContext(dark), id) + cache.put(id, color) + return color +} + +/** + * Empties the color caches so tests can resolve again under a different device configuration. + */ +@VisibleForTesting +@MainThread +internal fun clearScribeColorCache() { + lightColorCache.clear() + darkColorCache.clear() +} + +/** + * Number of cached colors in both modes, for tests. + */ +@VisibleForTesting +internal fun scribeColorCacheSize(): Int = lightColorCache.size + darkColorCache.size + +/** + * A context whose resources resolve as day or night according to [dark]. + */ +internal fun Context.scribeNightContext(dark: Boolean): Context { + val nightMode = if (dark) Configuration.UI_MODE_NIGHT_YES else Configuration.UI_MODE_NIGHT_NO + val config = Configuration(resources.configuration) + config.uiMode = (config.uiMode and Configuration.UI_MODE_NIGHT_MASK.inv()) or nightMode + return createConfigurationContext(config) +} diff --git a/app/src/main/java/be/scri/views/KeyboardView.kt b/app/src/main/java/be/scri/views/KeyboardView.kt index 6ceb257bc..a50bb98b5 100644 --- a/app/src/main/java/be/scri/views/KeyboardView.kt +++ b/app/src/main/java/be/scri/views/KeyboardView.kt @@ -31,6 +31,8 @@ import android.view.accessibility.AccessibilityManager import android.view.inputmethod.EditorInfo import android.widget.PopupWindow import android.widget.TextView +import androidx.annotation.ColorInt +import androidx.annotation.VisibleForTesting import androidx.core.content.edit import androidx.core.graphics.createBitmap import androidx.core.graphics.withSave @@ -66,6 +68,7 @@ import be.scri.helpers.SHIFT_OFF import be.scri.helpers.SHIFT_ON_ONE_CHAR import be.scri.helpers.SHIFT_ON_PERMANENT import be.scri.models.ScribeState +import be.scri.ui.theme.scribeColorInt import java.util.Arrays import java.util.Locale import kotlin.ranges.coerceIn @@ -244,6 +247,10 @@ class KeyboardView private var mEnterKeyColor: Int = 0 + /** Current Enter key background, for tests. */ + @get:VisibleForTesting + internal val enterKeyColor: Int get() = mEnterKeyColor + private var mKeyBackground: Drawable? = null private var mToolbarHolder: View? = null @@ -275,16 +282,28 @@ class KeyboardView private var mKeyboardBackgroundColor = 0 - private val alpha = FULL_ALPHA - private val redDark = (DARK_COLOR_FACTOR * FULL_ALPHA).toInt() - private val greenDark = (DARK_COLOR_FACTOR * FULL_ALPHA).toInt() - private val blueDark = (DARK_COLOR_FACTOR * FULL_ALPHA).toInt() - private val darkSpecialKey = Color.argb(FULL_ALPHA, redDark, greenDark, blueDark) + /** + * Shared `scribe_*` colors for the keys in one mode. [special] paints Enter, delete, shift and the mode key. + */ + internal data class KeyColors( + @ColorInt val key: Int, + @ColorInt val label: Int, + @ColorInt val special: Int, + ) - private val red = (LIGHT_COLOR_RED_FACTOR * FULL_ALPHA).toInt() - private val green = (LIGHT_COLOR_GREEN_FACTOR * FULL_ALPHA).toInt() - private val blue = (LIGHT_COLOR_BLUE_FACTOR * FULL_ALPHA).toInt() - private val lightSpecialKey = Color.argb(alpha, red, green, blue) + private var cachedKeyColors: Pair? = null + + /** + * Key colors for the Scribe dark mode setting, read once per mode instead of on every draw. + */ + @VisibleForTesting + internal fun keyColorsFor(dark: Boolean): KeyColors = + cachedKeyColors?.takeIf { it.first == dark }?.second + ?: KeyColors( + key = context.scribeColorInt(R.color.scribe_key, dark = dark), + label = context.scribeColorInt(R.color.scribe_text, dark = dark), + special = context.scribeColorInt(R.color.scribe_key_special, dark = dark), + ).also { cachedKeyColors = dark to it } /** * Contains constants and configuration values used across KeyboardView. @@ -303,10 +322,6 @@ class KeyboardView private const val DOUBLE_TAP_DELAY = 300L private const val NUMBER_OF_KEYS = 12 private const val FULL_ALPHA = 255 - private const val DARK_COLOR_FACTOR = 0.180 - private const val LIGHT_COLOR_RED_FACTOR = 0.682 - private const val LIGHT_COLOR_GREEN_FACTOR = 0.702 - private const val LIGHT_COLOR_BLUE_FACTOR = 0.745 private const val DEFAULT_KEY_TEXT_SIZE = 18 private const val MARGIN_ADJUSTMENT = 10 private const val PROXIMITY_SCALING_FACTOR = 1.4f @@ -350,12 +365,12 @@ class KeyboardView } else { when (isDarkMode) { true -> { - mEnterKeyColor = darkSpecialKey + mEnterKeyColor = keyColorsFor(dark = true).special invalidateAllKeys() } else -> { - mEnterKeyColor = lightSpecialKey + mEnterKeyColor = keyColorsFor(dark = false).special invalidateAllKeys() } } @@ -858,24 +873,10 @@ class KeyboardView val isUserDarkMode = be.scri.helpers.PreferencesHelper .getIsDarkModeOrNot(context) - val keyBackgroundColor = - if (isUserDarkMode) { - Color.DKGRAY - } else { - Color.WHITE - } - mBackgroundColor = - if (isUserDarkMode) { - Color.DKGRAY - } else { - Color.WHITE - } - mTextColor = - if (keyBackgroundColor == Color.WHITE) { - Color.BLACK - } else { - Color.WHITE - } + val keyColors = keyColorsFor(isUserDarkMode) + val keyBackgroundColor = keyColors.key + mBackgroundColor = keyColors.key + mTextColor = keyColors.label val pressedColorResId = if (isUserDarkMode) { R.color.dark_key_press_color @@ -883,13 +884,7 @@ class KeyboardView R.color.light_key_press_color } val pressedColor = resources.getColor(pressedColorResId, context.theme) - val specialKeyColorResource = - if (isUserDarkMode) { - R.color.special_key_dark - } else { - R.color.special_key_light - } - val specialKeyColorValue = resources.getColor(specialKeyColorResource, context.theme) + val specialKeyColorValue = keyColors.special val focusedColorResId = if (isUserDarkMode) { R.color.theme_scribe_blue diff --git a/app/src/main/res/drawable/dot_active.xml b/app/src/main/res/drawable/dot_active.xml index 87bf83efc..3cd90b3ba 100644 --- a/app/src/main/res/drawable/dot_active.xml +++ b/app/src/main/res/drawable/dot_active.xml @@ -1,5 +1,5 @@ - + diff --git a/app/src/main/res/drawable/dot_inactive.xml b/app/src/main/res/drawable/dot_inactive.xml deleted file mode 100644 index 169edfd7b..000000000 --- a/app/src/main/res/drawable/dot_inactive.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - diff --git a/app/src/main/res/drawable/floating_keyboard_background.xml b/app/src/main/res/drawable/floating_keyboard_background.xml deleted file mode 100644 index 4b34336d7..000000000 --- a/app/src/main/res/drawable/floating_keyboard_background.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - diff --git a/app/src/main/res/layout/input_method_view.xml b/app/src/main/res/layout/input_method_view.xml index d9317366b..c9f3254c0 100644 --- a/app/src/main/res/layout/input_method_view.xml +++ b/app/src/main/res/layout/input_method_view.xml @@ -671,13 +671,13 @@ GeneralKeyboardIME.kt android:layout_width="6dp" android:layout_height="6dp" android:layout_margin="4dp" - android:background="@drawable/dot_inactive" /> + android:background="@drawable/dot_active" /> + android:background="@drawable/dot_active" /> @@ -837,8 +837,7 @@ GeneralKeyboardIME.kt android:background="?android:attr/selectableItemBackgroundBorderless" android:contentDescription="@string/i18n.app._global.clear_all" android:padding="@dimen/small_margin" - android:src="@drawable/ic_delete_vector" - android:tint="#E53935" /> + android:src="@drawable/ic_delete_vector" /> diff --git a/app/src/main/res/values-night/colors.xml b/app/src/main/res/values-night/colors.xml index 0587b35ce..db2b32998 100644 --- a/app/src/main/res/values-night/colors.xml +++ b/app/src/main/res/values-night/colors.xml @@ -67,7 +67,7 @@ #FF434343 #95FFFFFF #FF363636 - #FF202020 + #FF2D2D2D #FF4CADE6 #FFFB5F6C #FF339EEB @@ -76,4 +76,6 @@ #FFFD9F5D #FF808080 #FFFFFFFF + #40888888 + #FFD11149 diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index fa8f0c45f..e79c6c4b8 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -25,7 +25,6 @@ @android:color/transparent #aeb3be - #202020 #2E2E2E #3F3F46 @@ -839,7 +838,7 @@ #95000000 #FFE9E9E9 #FFAEB3BE + name="scribe_key_special">#FFADB3BD #FF75CEFA #FF9F1722 @@ -852,5 +851,7 @@ #FF808080 #FF000000 + #40888888 + #FFE4572E diff --git a/app/src/test/kotlin/be/scri/ui/theme/ScribeColorResolverTest.kt b/app/src/test/kotlin/be/scri/ui/theme/ScribeColorResolverTest.kt new file mode 100644 index 000000000..2061cf959 --- /dev/null +++ b/app/src/test/kotlin/be/scri/ui/theme/ScribeColorResolverTest.kt @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package be.scri.ui.theme + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import be.scri.R +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +/** + * The keyboard colors moved off hex literals in #783 must keep the exact values they had. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class ScribeColorResolverTest { + @Before + fun resetColorCache() = clearScribeColorCache() + + @Test + fun keyboardColorsMatchTheOldHexOnADayDevice() { + assertKeyboardColors(ApplicationProvider.getApplicationContext()) + } + + @Test + @Config(qualifiers = "night") + fun keyboardColorsMatchTheOldHexOnANightDevice() { + assertKeyboardColors(ApplicationProvider.getApplicationContext()) + } + + @Test + fun everyScribeColorIgnoresTheDeviceNightMode() { + val ids = + R.color::class.java.fields + .filter { it.name.startsWith("scribe_") } + .associate { it.name to it.getInt(null) } + // Guards against the list silently shrinking if the R class shape changes. + assertEquals(true, ids.size >= 26) + + clearScribeColorCache() + val onDayDevice = ids.mapValues { (_, id) -> context().bothModes(id) } + clearScribeColorCache() + RuntimeEnvironment.setQualifiers("+night") + val onNightDevice = ids.mapValues { (_, id) -> context().bothModes(id) } + + assertEquals(onDayDevice, onNightDevice) + // The light and dark halves really come from different files. + assertEquals(0xFF5EB5E8.toInt() to 0xFF235270.toInt(), onDayDevice.getValue("scribe_background")) + } + + @Test + fun cachedLookupsReturnTheSameValuesInBothModes() { + val context = context() + val first = context.bothModes(R.color.scribe_key) + assertEquals(2, scribeColorCacheSize()) + // Second read comes from the cache and must keep light and dark apart. + assertEquals(first, context.bothModes(R.color.scribe_key)) + assertEquals(0xFFFFFFFF.toInt() to 0xFF434343.toInt(), first) + assertEquals(2, scribeColorCacheSize()) + } + + @Test + fun aCachedTransparentColorIsNotMistakenForAMiss() { + val context = context() + assertEquals(0, context.scribeColorInt(R.color.transparent, dark = false)) + assertEquals(1, scribeColorCacheSize()) + assertEquals(0, context.scribeColorInt(R.color.transparent, dark = false)) + assertEquals(1, scribeColorCacheSize()) + } + + @Test + fun clearingTheCacheEmptiesBothModes() { + context().bothModes(R.color.scribe_text) + assertEquals(2, scribeColorCacheSize()) + clearScribeColorCache() + assertEquals(0, scribeColorCacheSize()) + assertEquals(0xFF000000.toInt() to 0xFFFFFFFF.toInt(), context().bothModes(R.color.scribe_text)) + } + + private fun context(): Context = ApplicationProvider.getApplicationContext() + + private fun Context.bothModes(id: Int): Pair = scribeColorInt(id, dark = false) to scribeColorInt(id, dark = true) + + private fun assertKeyboardColors(context: Context) { + // Conjugation sub-labels, was "#E558564B" / "#CAC4D0". + assertEquals(0xE558564B.toInt(), context.scribeColorInt(R.color.scribe_caption, dark = false)) + assertEquals(0xFFCAC4D0.toInt(), context.scribeColorInt(R.color.scribe_caption, dark = true)) + // Dark invalid and already-plural command bar, was "#1E1E1E". + assertEquals(0xFF1E1E1E.toInt(), context.scribeColorInt(R.color.scribe_keyboard_background, dark = true)) + // Floating keyboard border, was 0x40888888 in both modes. + assertEquals(0x40888888, context.scribeColorInt(R.color.scribe_keyboard_border, dark = false)) + assertEquals(0x40888888, context.scribeColorInt(R.color.scribe_keyboard_border, dark = true)) + // Clipboard clear-all, the delete red from #772 (was #E53935 in both modes). + assertEquals(0xFFE4572E.toInt(), context.scribeColorInt(R.color.scribe_delete, dark = false)) + assertEquals(0xFFD11149.toInt(), context.scribeColorInt(R.color.scribe_delete, dark = true)) + } +} diff --git a/app/src/testKeyboards/kotlin/be/scri/helpers/FloatingKeyboardBorderTest.kt b/app/src/testKeyboards/kotlin/be/scri/helpers/FloatingKeyboardBorderTest.kt new file mode 100644 index 000000000..5b93a3c11 --- /dev/null +++ b/app/src/testKeyboards/kotlin/be/scri/helpers/FloatingKeyboardBorderTest.kt @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package be.scri.helpers + +import android.content.Context +import android.graphics.drawable.GradientDrawable +import android.view.LayoutInflater +import androidx.test.core.app.ApplicationProvider +import be.scri.databinding.InputMethodViewBinding +import be.scri.ui.theme.clearScribeColorCache +import io.mockk.every +import io.mockk.mockk +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config + +/** + * The floating keyboard border reads `scribe_keyboard_border` (#40888888) in both modes. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class FloatingKeyboardBorderTest { + @Before + fun resetColorCache() = clearScribeColorCache() + + @Test + fun floatingBorderUsesScribeKeyboardBorderInLightMode() { + assertEquals(0x40888888, floatingBorderColor(scribeDark = false)) + } + + @Test + @Config(qualifiers = "night") + fun floatingBorderUsesScribeKeyboardBorderInDarkMode() { + assertEquals(0x40888888, floatingBorderColor(scribeDark = true)) + } + + private fun floatingBorderColor(scribeDark: Boolean): Int { + val context = ApplicationProvider.getApplicationContext() + val deviceNight = context.resources.configuration.isNightModeActive + context + .getSharedPreferences(PreferencesHelper.SCRIBE_PREFS, Context.MODE_PRIVATE) + .edit() + .putBoolean("dark_mode", scribeDark) + .putBoolean("last_system_dark_mode", deviceNight) + .commit() + PreferencesHelper.setIsFloatingModeEnabled(context, LANGUAGE, true) + + val binding = InputMethodViewBinding.inflate(LayoutInflater.from(context)) + val ime = mockk(relaxed = true) + every { ime.imeContext } returns context + every { ime.language } returns LANGUAGE + every { ime.binding } returns binding + every { ime.isUiManagerInitialized } returns true + every { ime.getImeResources() } returns context.resources + every { ime.getImeWindow() } returns null + + FloatingKeyboardHandler(ime).initFloatingMode() + return shadowOf(binding.keyboardCard.background as GradientDrawable).strokeColor + } + + private companion object { + const val LANGUAGE = "English" + } +} diff --git a/app/src/testKeyboards/kotlin/be/scri/helpers/ui/KeyboardSharedColorsTest.kt b/app/src/testKeyboards/kotlin/be/scri/helpers/ui/KeyboardSharedColorsTest.kt new file mode 100644 index 000000000..0fe6f53e6 --- /dev/null +++ b/app/src/testKeyboards/kotlin/be/scri/helpers/ui/KeyboardSharedColorsTest.kt @@ -0,0 +1,322 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package be.scri.helpers.ui + +import android.content.Context +import android.graphics.drawable.ColorDrawable +import android.text.Spanned +import android.text.style.ForegroundColorSpan +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Button +import androidx.test.core.app.ApplicationProvider +import be.scri.R +import be.scri.databinding.InputMethodViewBinding +import be.scri.helpers.PreferencesHelper +import be.scri.models.ScribeState +import be.scri.ui.theme.clearScribeColorCache +import io.mockk.every +import io.mockk.mockk +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * The keyboard colors moved to shared `scribe_*` names in #783 keep their old hex, and follow the + * Scribe dark mode setting even when the device night mode says the opposite. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class KeyboardSharedColorsTest { + @Before + fun resetColorCache() = clearScribeColorCache() + + @Test + fun invalidBarIsDarkWhenScribeIsDarkOnADayDevice() { + val binding = showState(ScribeState.INVALID, scribeDark = true, deviceNight = false) + assertEquals(0xFF1E1E1E.toInt(), binding.toolbarBar.backgroundColor()) + } + + @Test + @Config(qualifiers = "night") + fun invalidBarIsLightWhenScribeIsLightOnANightDevice() { + val binding = showState(ScribeState.INVALID, scribeDark = false, deviceNight = true) + assertEquals(0xFFD3D6DD.toInt(), binding.toolbarBar.backgroundColor()) + } + + @Test + fun alreadyPluralBarIsDarkWhenScribeIsDarkOnADayDevice() { + val binding = showState(ScribeState.ALREADY_PLURAL, scribeDark = true, deviceNight = false) + assertEquals(0xFF1E1E1E.toInt(), binding.toolbarBar.backgroundColor()) + } + + @Test + @Config(qualifiers = "night") + fun alreadyPluralBarIsLightWhenScribeIsLightOnANightDevice() { + val binding = showState(ScribeState.ALREADY_PLURAL, scribeDark = false, deviceNight = true) + assertEquals(0xFFD3D6DD.toInt(), binding.toolbarBar.backgroundColor()) + } + + @Test + fun conjugationLabelUsesTheDarkCaptionWhenScribeIsDarkOnADayDevice() { + val binding = showConjugationGrid(scribeDark = true, deviceNight = false) + assertEquals(0xFFCAC4D0.toInt(), binding.root.labelColor("1ps")) + } + + @Test + @Config(qualifiers = "night") + fun conjugationLabelUsesTheLightCaptionWhenScribeIsLightOnANightDevice() { + val binding = showConjugationGrid(scribeDark = false, deviceNight = true) + assertEquals(0xE558564B.toInt(), binding.root.labelColor("1ps")) + } + + @Test + fun commandSeparatorsStayTransparentInBothModes() { + listOf(true, false).forEach { dark -> + val binding = showState(ScribeState.SELECT_COMMAND, scribeDark = dark, deviceNight = false) + assertEquals(0, binding.separator2.backgroundColor()) + assertEquals(0, binding.separator3.backgroundColor()) + } + } + + @Test + @Config(qualifiers = "night") + fun letterKeysUseScribeKeyAndTextOnANightDevice() { + val binding = showState(ScribeState.IDLE, scribeDark = false, deviceNight = true) + val light = binding.keyboardView.keyColorsFor(dark = false) + val dark = binding.keyboardView.keyColorsFor(dark = true) + assertEquals(0xFFFFFFFF.toInt(), light.key) + assertEquals(0xFF000000.toInt(), light.label) + assertEquals(0xFF434343.toInt(), dark.key) + assertEquals(0xFFFFFFFF.toInt(), dark.label) + // Enter, delete, shift and the mode key use the Figma special-key values. + assertEquals(0xFFADB3BD.toInt(), light.special) + assertEquals(0xFF2D2D2D.toInt(), dark.special) + } + + @Test + fun letterKeysUseScribeKeyAndTextOnADayDevice() { + val binding = showState(ScribeState.IDLE, scribeDark = true, deviceNight = false) + assertEquals(0xFF434343.toInt(), binding.keyboardView.keyColorsFor(dark = true).key) + assertEquals(0xFFFFFFFF.toInt(), binding.keyboardView.keyColorsFor(dark = false).key) + assertEquals(0xFF2D2D2D.toInt(), binding.keyboardView.keyColorsFor(dark = true).special) + assertEquals(0xFFADB3BD.toInt(), binding.keyboardView.keyColorsFor(dark = false).special) + } + + @Test + @Config(qualifiers = "night") + fun suggestionsMatchTheKeyLabelsWhenScribeIsLightOnANightDevice() { + val binding = showState(ScribeState.IDLE, scribeDark = false, deviceNight = true) + val context = ApplicationProvider.getApplicationContext() + assertEquals(0xFF000000.toInt(), KeyboardThemeManager().getSuggestionTextColor(context)) + assertEquals(0xFF000000.toInt(), binding.translateBtn.currentTextColor) + } + + @Test + fun suggestionsMatchTheKeyLabelsWhenScribeIsDarkOnADayDevice() { + val binding = showState(ScribeState.IDLE, scribeDark = true, deviceNight = false) + val context = ApplicationProvider.getApplicationContext() + assertEquals(0xFFFFFFFF.toInt(), KeyboardThemeManager().getSuggestionTextColor(context)) + assertEquals(0xFFFFFFFF.toInt(), binding.translateBtn.currentTextColor) + } + + @Test + @Config(qualifiers = "night") + fun clipboardClearAllUsesTheLightDeleteRedWhenScribeIsLightOnANightDevice() { + val binding = showState(ScribeState.IDLE, scribeDark = false, deviceNight = true) + KeyboardUIManager(binding, ApplicationProvider.getApplicationContext(), relaxedListener()).applyClipboardPanelTheme() + assertEquals(0xFFE4572E.toInt(), binding.clipboardClearAll.imageTintList!!.defaultColor) + } + + @Test + fun clipboardClearAllUsesTheDarkDeleteRedWhenScribeIsDarkOnADayDevice() { + val binding = showState(ScribeState.IDLE, scribeDark = true, deviceNight = false) + KeyboardUIManager(binding, ApplicationProvider.getApplicationContext(), relaxedListener()).applyClipboardPanelTheme() + assertEquals(0xFFD11149.toInt(), binding.clipboardClearAll.imageTintList!!.defaultColor) + } + + @Test + @Config(qualifiers = "night") + fun directConjugationLabelsFollowTheScribeSetting() { + val light = showConjugationGrid(scribeDark = false, deviceNight = true, output = DIRECT_OUTPUT) + assertEquals(0xE558564B.toInt(), light.root.labelColor("1ps")) + val dark = showConjugationGrid(scribeDark = true, deviceNight = true, output = DIRECT_OUTPUT) + assertEquals(0xFFCAC4D0.toInt(), dark.root.labelColor("1ps")) + } + + @Test + fun subSelectionConjugationLabelsFollowTheScribeSetting() { + val dark = showConjugationGrid(scribeDark = true, deviceNight = false, output = SUB_OUTPUT, subCategory = "1ps") + assertEquals(0xFFCAC4D0.toInt(), dark.root.labelColor("sg")) + val light = showConjugationGrid(scribeDark = false, deviceNight = false, output = SUB_OUTPUT, subCategory = "1ps") + assertEquals(0xE558564B.toInt(), light.root.labelColor("sg")) + } + + @Test + fun enterKeyUsesTheSpecialKeyColorForTheScribeSetting() { + val binding = showState(ScribeState.IDLE, scribeDark = false, deviceNight = false) + binding.keyboardView.setEnterKeyColor(null, isDarkMode = true) + assertEquals(0xFF2D2D2D.toInt(), binding.keyboardView.enterKeyColor) + binding.keyboardView.setEnterKeyColor(null, isDarkMode = false) + assertEquals(0xFFADB3BD.toInt(), binding.keyboardView.enterKeyColor) + } + + @Test + fun clearAllHasNoXmlTintAndShowingTheClipboardTintsIt() { + val binding = showState(ScribeState.IDLE, scribeDark = true, deviceNight = false) + // The XML tint followed the device night mode, so the color now only comes from code. + val inflated = InputMethodViewBinding.inflate(LayoutInflater.from(ApplicationProvider.getApplicationContext())) + assertNull(inflated.clipboardClearAll.imageTintList) + + KeyboardUIManager(binding, ApplicationProvider.getApplicationContext(), relaxedListener()).showClipboardPanel() + assertEquals(0xFFD11149.toInt(), binding.clipboardClearAll.imageTintList!!.defaultColor) + } + + @Test + @Config(qualifiers = "night") + fun pageDotsUseTextAndCaptionWhenScribeIsLightOnANightDevice() { + val binding = showInvalidInfoPanel(scribeDark = false, deviceNight = true) + binding.assertDotColors(active = 0xFF000000.toInt(), inactive = 0xE558564B.toInt(), currentPage = 0) + binding.buttonRight.performClick() + binding.assertDotColors(active = 0xFF000000.toInt(), inactive = 0xE558564B.toInt(), currentPage = 1) + } + + @Test + fun pageDotsUseTextAndCaptionWhenScribeIsDarkOnADayDevice() { + val binding = showInvalidInfoPanel(scribeDark = true, deviceNight = false) + binding.assertDotColors(active = 0xFFFFFFFF.toInt(), inactive = 0xFFCAC4D0.toInt(), currentPage = 0) + binding.buttonRight.performClick() + binding.assertDotColors(active = 0xFFFFFFFF.toInt(), inactive = 0xFFCAC4D0.toInt(), currentPage = 1) + } + + private fun showInvalidInfoPanel( + scribeDark: Boolean, + deviceNight: Boolean, + ): InputMethodViewBinding { + val binding = showState(ScribeState.INVALID, scribeDark = scribeDark, deviceNight = deviceNight) + binding.ivInfo.performClick() + return binding + } + + private fun InputMethodViewBinding.assertDotColors( + active: Int, + inactive: Int, + currentPage: Int, + ) { + assertEquals(3, pageIndicators.childCount) + for (i in 0 until pageIndicators.childCount) { + val expected = if (i == currentPage) active else inactive + val actual = pageIndicators.getChildAt(i).backgroundTintList!!.defaultColor + assertEquals("dot $i on page $currentPage", expected, actual) + } + } + + private fun showState( + state: ScribeState, + scribeDark: Boolean, + deviceNight: Boolean, + conjugateOutput: Map>>>? = null, + conjugateLabels: Set? = null, + verb: String? = null, + subCategory: String? = null, + ): InputMethodViewBinding { + val context = ApplicationProvider.getApplicationContext() + context + .getSharedPreferences(PreferencesHelper.SCRIBE_PREFS, Context.MODE_PRIVATE) + .edit() + .putBoolean("dark_mode", scribeDark) + .putBoolean("last_system_dark_mode", deviceNight) + .commit() + assertEquals(scribeDark, PreferencesHelper.getIsDarkModeOrNot(context)) + + val binding = InputMethodViewBinding.inflate(LayoutInflater.from(context)) + KeyboardUIManager(binding, context, relaxedListener()).updateUI( + currentState = state, + language = "English", + emojiAutoSuggestionEnabled = false, + autoSuggestEmojis = null, + conjugateOutput = conjugateOutput, + conjugateLabels = conjugateLabels, + selectedConjugationSubCategory = subCategory, + currentVerbForConjugation = verb, + ) + return binding + } + + private fun relaxedListener(): KeyboardUIManager.KeyboardUIListener { + val listener = mockk(relaxed = true) + every { listener.getKeyboardLayoutXML() } returns R.xml.keys_letters_english + every { listener.getCurrentKeyboardLayoutXML() } returns R.xml.keys_letters_english + every { listener.onKeyboardActionListener() } returns mockk() + every { listener.isNumericKeyboardActive() } returns false + return listener + } + + private fun showConjugationGrid( + scribeDark: Boolean, + deviceNight: Boolean, + output: Map>>> = CATEGORY_OUTPUT, + subCategory: String? = null, + ): InputMethodViewBinding { + val context = ApplicationProvider.getApplicationContext() + context + .getSharedPreferences("keyboard_preferences", Context.MODE_PRIVATE) + .edit() + .putInt("conjugate_index", 0) + .commit() + return showState( + ScribeState.SELECT_VERB_CONJUNCTION, + scribeDark = scribeDark, + deviceNight = deviceNight, + conjugateOutput = output, + conjugateLabels = setOf("1ps", "2ps"), + verb = "be", + subCategory = subCategory, + ) + } + + private companion object { + val CATEGORY_OUTPUT = + mapOf( + "Present" to + mapOf( + "1ps" to listOf("1ps" to "I am"), + "2ps" to listOf("2ps" to "You are"), + ), + ) + val DIRECT_OUTPUT = + mapOf( + "Present" to mapOf("Present" to listOf("1ps" to "I am", "2ps" to "You are")), + ) + val SUB_OUTPUT = + mapOf( + "Present" to mapOf("1ps" to listOf("sg" to "I am", "pl" to "we are")), + ) + } + + private fun View.backgroundColor(): Int = (background as ColorDrawable).color + + private fun View.labelColor(label: String): Int { + val button = findButtonStartingWith(label) + assertNotNull("No conjugation button starts with $label", button) + val text = button!!.text as Spanned + val span = text.getSpans(0, label.length, ForegroundColorSpan::class.java).single() + return span.foregroundColor + } + + private fun View.findButtonStartingWith(label: String): Button? { + if (this is Button && text is Spanned && text.startsWith(label)) return this + if (this is ViewGroup) { + for (i in 0 until childCount) { + getChildAt(i).findButtonStartingWith(label)?.let { return it } + } + } + return null + } +}