Skip to content

Commit

Permalink
fix: resolve color and image path correctly in themes
Browse files Browse the repository at this point in the history
Refs #1061
  • Loading branch information
goofyz committed Dec 11, 2023
1 parent 9db179f commit 265a91a
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions app/src/main/java/com/osfans/trime/data/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class Theme private constructor(isDarkMode: Boolean) {
}

private val defaultThemeName = "trime"

fun isImageString(str: String?): Boolean {
return str?.contains(Regex(".(png|jpg)")) == true
}
}

init {
Expand Down Expand Up @@ -205,7 +209,23 @@ class Theme private constructor(isDarkMode: Boolean) {
if (override != null) {
return GradientDrawable().apply { setColor(override) }
}
return if (theme.currentColors.containsKey(value)) getDrawable(value!!) else getDrawable(key)

// value maybe a color label or a image path
return if (isImageString(value)) {
val path = theme.getImagePath(value!!)
if (path.isNotEmpty()) {
bitmapDrawable(path)
} else {
// fallback if image not found
getDrawable(key)
}
} else if (theme.currentColors.containsKey(value)) {
// use custom color label
getDrawable(value!!)
} else {
// fallback color
getDrawable(key)
}
}

// 返回图片或背景的drawable,支持null参数。 Config 2.0
Expand Down Expand Up @@ -445,8 +465,7 @@ class Theme private constructor(isDarkMode: Boolean) {
value ?: return null
if (value is String) {
if (value.matches(".*[.\\\\/].*".toRegex())) {
val fullPath = joinToFullImagePath(value)
return if (File(fullPath).exists()) fullPath else ""
return getImagePath(value)
} else {
runCatching {
return ColorUtils.parseColor(value)
Expand All @@ -457,4 +476,13 @@ class Theme private constructor(isDarkMode: Boolean) {
}
return null
}

private fun getImagePath(value: String): String {
return if (value.matches(".*[.\\\\/].*".toRegex())) {
val fullPath = joinToFullImagePath(value)
if (File(fullPath).exists()) fullPath else ""
} else {
""
}
}
}

0 comments on commit 265a91a

Please sign in to comment.