Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix 250304 #1609

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.osfans.trime.data.theme

import android.content.res.Configuration
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
Expand All @@ -17,6 +18,7 @@ import com.osfans.trime.util.ColorUtils
import com.osfans.trime.util.WeakHashSet
import com.osfans.trime.util.bitmapDrawable
import com.osfans.trime.util.isNightMode
import timber.log.Timber

object ColorManager {
private lateinit var theme: Theme
Expand Down Expand Up @@ -164,7 +166,12 @@ object ColorManager {
): Int {
val color =
resolveValue(key) { value ->
ColorUtils.parseColor(value)
runCatching {
ColorUtils.parseColor(value)
}.getOrElse {
Timber.e(it, "Cannot parse color $key=$value, fallback to 0x00")
Color.TRANSPARENT
}
}
if (putCache) {
synchronized(colorCache) { colorCache.put(key, color) }
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/osfans/trime/data/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import timber.log.Timber
/** 主题和样式配置 */
class Theme(
val configId: String,
parseOnly: Boolean = false,
) {
private val config: Config

init {
if (!Rime.deployRimeConfigFile(configId, CONFIG_VERSION_KEY)) {
throw IllegalArgumentException("Failed to deploy theme config file '$configId'")
if (!parseOnly) {
if (!Rime.deployRimeConfigFile(configId, CONFIG_VERSION_KEY)) {
throw IllegalArgumentException("Failed to deploy theme config file '$configId'")
}
}
config = Config.create(configId)
}
Expand Down
22 changes: 2 additions & 20 deletions app/src/main/java/com/osfans/trime/data/theme/ThemeFilesManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,18 @@

package com.osfans.trime.data.theme

import com.charleskorn.kaml.AnchorsAndAliases
import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import timber.log.Timber
import java.io.File

object ThemeFilesManager {
private val yaml =
Yaml(
configuration =
YamlConfiguration(
strictMode = false,
anchorsAndAliases = AnchorsAndAliases.Permitted(200u),
),
)

fun listThemes(dir: File): MutableList<ThemeStub> {
fun listThemes(dir: File): MutableList<Theme> {
val files = dir.listFiles { _, name -> name.endsWith("trime.yaml") } ?: return mutableListOf()
return files
.sortedByDescending { it.lastModified() }
.mapNotNull decode@{
val theme =
runCatching {
yaml
.decodeFromString(
ThemeStub.serializer(),
it.inputStream().bufferedReader().readText(),
).apply {
configId = it.nameWithoutExtension
}
Theme(it.nameWithoutExtension, parseOnly = true)
}.getOrElse { e ->
Timber.w("Failed to decode theme file ${it.absolutePath}: ${e.message}")
return@decode null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object ThemeManager {
fun onThemeChange(theme: Theme)
}

fun getAllThemes(): List<ThemeStub> {
fun getAllThemes(): List<Theme> {
val sharedThemes = ThemeFilesManager.listThemes(DataManager.sharedDataDir)
val userThemes = ThemeFilesManager.listThemes(DataManager.userDataDir)
return sharedThemes + userThemes
Expand Down
16 changes: 0 additions & 16 deletions app/src/main/java/com/osfans/trime/data/theme/ThemeStub.kt

This file was deleted.

4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/util/ColorUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ object ColorUtils {
if (colorString.startsWith("#") || colorString.startsWith("0x", ignoreCase = true)) {
val sub = colorString.replace("^#|^0x".toRegex(), "")
when (sub.length) {
1, 2 -> "#%02x000000".format(colorString.toLong(16)) // 0xA(A) -> #AA000000
in 3..5 -> "#%06x".format(colorString.toLong(16)) // 0xGBB... -> #RRGGBB
1, 2 -> "#%02x000000".format(java.lang.Long.decode(colorString)) // 0xA(A) -> #AA000000
in 3..5 -> "#%06x".format(java.lang.Long.decode(colorString)) // 0xGBB... -> #RRGGBB
7 -> "#0$sub"
else -> "#$sub" // 0x(AA)RRGGBB -> #(AA)RRGGBB
}
Expand Down
Loading