Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
57 changes: 30 additions & 27 deletions app/src/keyboards/java/be/scri/helpers/ui/KeyboardUIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -586,6 +586,7 @@ class KeyboardUIManager(
subPairs: List<Pair<String, String>>,
isDarkMode: Boolean,
) {
val labelColor = conjugationLabelColor(isDarkMode)
buttonIds.forEachIndexed { i, btnId ->
val btn = gridContent.findViewById<Button?>(btnId) ?: return@forEachIndexed
if (i < subPairs.size) {
Expand All @@ -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)
Expand All @@ -616,6 +617,7 @@ class KeyboardUIManager(
categoryEntries: List<Map.Entry<String, List<Pair<String, String>>>>,
isDarkMode: Boolean,
) {
val labelColor = conjugationLabelColor(isDarkMode)
buttonIds.forEachIndexed { i, btnId ->
val btn = gridContent.findViewById<Button?>(btnId) ?: return@forEachIndexed
if (i < categoryEntries.size) {
Expand All @@ -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 {
Expand All @@ -653,6 +655,7 @@ class KeyboardUIManager(
directPairs: List<Pair<String, String>>,
isDarkMode: Boolean,
) {
val labelColor = conjugationLabelColor(isDarkMode)
buttonIds.forEachIndexed { i, btnId ->
val btn = gridContent.findViewById<Button?>(btnId) ?: return@forEachIndexed
if (i < directPairs.size) {
Expand All @@ -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)
Expand Down Expand Up @@ -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 =
Expand All @@ -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.
Expand All @@ -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 = ""
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 0 additions & 2 deletions app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions app/src/main/java/be/scri/ui/theme/Colors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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))
66 changes: 66 additions & 0 deletions app/src/main/java/be/scri/ui/theme/ScribeColorResolver.kt
Original file line number Diff line number Diff line change
@@ -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)
}
Loading
Loading