@@ -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}
0 commit comments