Skip to content

Commit 259036b

Browse files
committed
fix: custom key colors were transparent sometimes
1 parent 8a6c9b2 commit 259036b

File tree

4 files changed

+48
-40
lines changed

4 files changed

+48
-40
lines changed

app/src/main/java/com/osfans/trime/data/theme/ColorManager.kt

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ object ColorManager {
3535

3636
private var isNightMode = false
3737

38-
val presetColorSchemes: List<ColorScheme>
39-
get() = theme.colorSchemes
40-
41-
private val customFallbackRules: Map<String, String>
42-
get() = theme.fallbackColors
43-
44-
private val fullFallbackRules get() = customFallbackRules + defaultFallbackColors
45-
4638
private lateinit var _activeColorScheme: ColorScheme
4739

4840
var activeColorScheme: ColorScheme
@@ -65,7 +57,7 @@ object ColorManager {
6557

6658
private var darkModeColorScheme: ColorScheme? = null
6759

68-
private val defaultFallbackColors =
60+
private val BuiltinFallbackColors =
6961
mapOf(
7062
"candidate_text_color" to "text_color",
7163
"comment_text_color" to "candidate_text_color",
@@ -128,7 +120,7 @@ object ColorManager {
128120
onChangeListeners.forEach { it.onColorChange(theme) }
129121
}
130122

131-
private fun colorScheme(id: String) = presetColorSchemes.find { it.id == id }
123+
private fun colorScheme(id: String) = theme.colorSchemes.find { it.id == id }
132124

133125
fun init(configuration: Configuration) {
134126
isNightMode = configuration.isNightMode()
@@ -169,7 +161,7 @@ object ColorManager {
169161
}
170162

171163
@ColorInt
172-
fun resolveColor(
164+
private fun resolveColor(
173165
key: String,
174166
putCache: Boolean = true,
175167
): Int {
@@ -187,7 +179,7 @@ object ColorManager {
187179
return color
188180
}
189181

190-
fun resolveDrawable(
182+
private fun resolveDrawable(
191183
key: String,
192184
putCache: Boolean = true,
193185
): Drawable? {
@@ -210,20 +202,23 @@ object ColorManager {
210202
parser: (String) -> T,
211203
): T {
212204
var currentKey = key
213-
val visitedKeys = mutableSetOf<String>()
214205

215206
while (true) {
216-
when {
217-
activeColorScheme.colors.containsKey(currentKey) -> {
218-
return parser(activeColorScheme.colors[currentKey]!!)
219-
}
220-
fullFallbackRules.containsKey(currentKey) -> {
221-
currentKey =
222-
fullFallbackRules[currentKey]!!.also {
223-
check(visitedKeys.add(it)) { "Circular fallback: $key" }
224-
}
225-
}
226-
else -> throw IllegalArgumentException("Color not found: $key")
207+
val target = activeColorScheme.colors[currentKey]
208+
if (!target.isNullOrEmpty()) {
209+
Timber.d("current: $currentKey, origin: $key, target: $target")
210+
return parser(target)
211+
}
212+
val fallback = theme.fallbackColors[currentKey]
213+
if (!fallback.isNullOrEmpty()) {
214+
currentKey = fallback
215+
continue
216+
}
217+
val altFallback = BuiltinFallbackColors[currentKey]
218+
if (!altFallback.isNullOrEmpty()) {
219+
currentKey = altFallback
220+
} else {
221+
throw IllegalArgumentException("$key not found")
227222
}
228223
}
229224
}
@@ -276,16 +271,15 @@ object ColorManager {
276271
@ColorInt
277272
fun getColor(key: String): Int = colorCache[key] ?: resolveColor(key)
278273

279-
fun getDrawable(key: String): Drawable? = drawableCache[key] ?: resolveDrawable(key)
280-
281274
fun getDrawable(
282275
colorKey: String,
283276
borderColorKey: String? = null,
284277
borderPx: Int = 0,
285278
cornerRadius: Float = 0f,
286279
alpha: Int = 255,
287-
): Drawable? =
288-
when (val drawable = getDrawable(colorKey)) {
280+
): Drawable? {
281+
val drawable = drawableCache[colorKey] ?: resolveDrawable(colorKey)
282+
return when (drawable) {
289283
is BitmapDrawable -> drawable.also { it.alpha = MathUtils.clamp(alpha, 0, 255) }
290284
is GradientDrawable ->
291285
drawable.also {
@@ -301,6 +295,7 @@ object ColorManager {
301295
}
302296
else -> null
303297
}
298+
}
304299

305300
private val SUPPORTED_IMG_FORMATS = arrayOf(".png", ".webp", ".jpg", ".gif")
306301
}

app/src/main/java/com/osfans/trime/ime/keyboard/CommonKeyboardActionListener.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.osfans.trime.daemon.launchOnReady
2222
import com.osfans.trime.data.prefs.AppPrefs
2323
import com.osfans.trime.data.theme.ColorManager
2424
import com.osfans.trime.data.theme.KeyActionManager
25+
import com.osfans.trime.data.theme.ThemeManager
2526
import com.osfans.trime.ime.core.TrimeInputMethodService
2627
import com.osfans.trime.ime.dependency.InputScope
2728
import com.osfans.trime.ime.dialog.AvailableSchemaPickerDialog
@@ -210,7 +211,7 @@ class CommonKeyboardActionListener(
210211
}
211212
}
212213
"set_color_scheme" -> {
213-
val newScheme = ColorManager.presetColorSchemes.find { it.id == arg }
214+
val newScheme = ThemeManager.activeTheme.colorSchemes.find { it.id == arg }
214215
if (newScheme != null) ColorManager.setColorScheme(newScheme)
215216
}
216217
"broadcast" -> service.sendBroadcast(Intent(arg))

app/src/main/java/com/osfans/trime/ime/keyboard/Key.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package com.osfans.trime.ime.keyboard
66

7+
import android.graphics.Color
78
import android.graphics.drawable.Drawable
89
import android.view.KeyEvent
910
import com.osfans.trime.core.Rime.Companion.hasLeft
@@ -72,16 +73,26 @@ class Key(
7273
var keyPressOffsetX = 0
7374
var keyPressOffsetY = 0
7475

75-
private fun getColor(src: String): Int = ColorManager.resolveColor(src)
76-
77-
private fun getDrawable(src: String) = ColorManager.resolveDrawable(src)
78-
79-
private val keyBackColor by lazy { getDrawable(textKey.keyBackColor) }
80-
private val keyTextColor by lazy { getColor(textKey.keyTextColor) }
81-
private val keySymbolColor by lazy { getColor(textKey.keySymbolColor) }
82-
private val hilitedKeyBackColor by lazy { getDrawable(textKey.hlKeyBackColor) }
83-
private val hilitedKeyTextColor by lazy { getColor(textKey.hlKeyTextColor) }
84-
private val hilitedKeySymbolColor by lazy { getColor(textKey.hlKeySymbolColor) }
76+
private fun getColor(
77+
src: String,
78+
fallback: String,
79+
): Int =
80+
ColorManager
81+
.getColor(src)
82+
.takeIf { it != Color.TRANSPARENT }
83+
?: ColorManager.getColor(fallback)
84+
85+
private fun getDrawable(
86+
src: String,
87+
fallback: String,
88+
) = ColorManager.getDrawable(src) ?: ColorManager.getDrawable(fallback)
89+
90+
private val keyBackColor by lazy { getDrawable(textKey.keyBackColor, "key_back_color") }
91+
private val keyTextColor by lazy { getColor(textKey.keyTextColor, "key_text_color") }
92+
private val keySymbolColor by lazy { getColor(textKey.keySymbolColor, "key_symbol_color") }
93+
private val hilitedKeyBackColor by lazy { getDrawable(textKey.hlKeyBackColor, "hilited_key_back_color") }
94+
private val hilitedKeyTextColor by lazy { getColor(textKey.hlKeyTextColor, "hilited_key_text_color") }
95+
private val hilitedKeySymbolColor by lazy { getColor(textKey.hlKeySymbolColor, "hilited_key_symbol_color") }
8596

8697
/**
8798
* Create an empty key with no attributes.

app/src/main/java/com/osfans/trime/ui/main/settings/ColorPickerDialog.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.content.Context
99
import androidx.lifecycle.LifecycleCoroutineScope
1010
import com.osfans.trime.R
1111
import com.osfans.trime.data.theme.ColorManager
12+
import com.osfans.trime.data.theme.ThemeManager
1213
import kotlinx.coroutines.launch
1314

1415
object ColorPickerDialog {
@@ -17,7 +18,7 @@ object ColorPickerDialog {
1718
context: Context,
1819
afterConfirm: (suspend () -> Unit)? = null,
1920
): AlertDialog {
20-
val presetSchemes = ColorManager.presetColorSchemes
21+
val presetSchemes = ThemeManager.activeTheme.colorSchemes
2122
val currentScheme = ColorManager.activeColorScheme
2223
val currentIndex = presetSchemes.indexOfFirst { it.id == currentScheme.id }
2324
return AlertDialog

0 commit comments

Comments
 (0)