Skip to content

Commit

Permalink
refactor: improve getting drawable from color schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 27, 2025
1 parent 6deb98f commit 07ed340
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 61 deletions.
49 changes: 20 additions & 29 deletions app/src/main/java/com/osfans/trime/data/theme/ColorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package com.osfans.trime.data.theme

import android.content.Context
import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
Expand All @@ -15,10 +14,8 @@ import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.sound.SoundEffectManager
import com.osfans.trime.util.ColorUtils
import com.osfans.trime.util.WeakHashSet
import com.osfans.trime.util.appContext
import com.osfans.trime.util.bitmapDrawable
import com.osfans.trime.util.isNightMode
import splitties.dimensions.dp
import timber.log.Timber
import java.io.File

Expand Down Expand Up @@ -345,36 +342,30 @@ object ColorManager {
}
}

// 返回图片或背景的drawable,支持null参数。 Config 2.0
fun getDrawable(
context: Context = appContext,
key: String,
border: Int = 0,
borderColorKey: String = "",
roundCorner: Float = 0f,
colorKey: String,
borderColorKey: String? = null,
borderPx: Int = 0,
cornerRadius: Float = 0f,
alpha: Int = 255,
): Drawable? {
val value = getColorValue(key)
if (value is Drawable) {
value.alpha = MathUtils.clamp(alpha, 0, 255)
return value
}

if (value is Int) {
val gradient = GradientDrawable().apply { setColor(value) }
if (roundCorner > 0) {
gradient.cornerRadius = roundCorner
): Drawable? =
when (val value = getColorValue(colorKey)) {
is Drawable -> {
value.also { it.alpha = MathUtils.clamp(alpha, 0, 255) }
}
if (borderColorKey.isNotEmpty() && border > 0) {
val borderPx = context.dp(border)
val stroke = getColor(borderColorKey)
if (stroke != null && borderPx > 0) {
gradient.setStroke(borderPx, stroke)
is Int -> {
GradientDrawable().apply {
setColor(value)
this.cornerRadius = cornerRadius
this.alpha = MathUtils.clamp(alpha, 0, 255)
if (!borderColorKey.isNullOrEmpty()) {
val borderColor = getColor(borderColorKey)
if (borderColor != null) {
setStroke(borderPx, borderColor)
}
}
}
}
gradient.alpha = MathUtils.clamp(alpha, 0, 255)
return gradient
else -> null
}
return null
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.osfans.trime.ime.window.BoardWindow
import com.osfans.trime.ime.window.BoardWindowManager
import kotlinx.coroutines.launch
import me.tatarka.inject.annotations.Inject
import splitties.dimensions.dp
import splitties.views.dsl.core.add
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.matchParent
Expand Down Expand Up @@ -191,11 +192,10 @@ class QuickBar(
}
background =
ColorManager.getDrawable(
context,
"candidate_background",
theme.generalStyle.candidateBorder,
"candidate_border_color",
theme.generalStyle.candidateBorderRound,
dp(theme.generalStyle.candidateBorder),
dp(theme.generalStyle.candidateBorderRound),
)
add(alwaysUi.root, lParams(matchParent, matchParent))
add(candidateUi.root, lParams(matchParent, matchParent))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import com.osfans.trime.R
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.Theme
import splitties.dimensions.dp
import splitties.views.dsl.constraintlayout.centerInParent
import splitties.views.dsl.constraintlayout.lParams
import splitties.views.dsl.core.add
Expand All @@ -29,11 +30,10 @@ class UnrolledCandidateLayout(
id = R.id.unrolled_candidate_view
background =
ColorManager.getDrawable(
context,
"candidate_background",
theme.generalStyle.candidateBorder,
"candidate_border_color",
theme.generalStyle.candidateBorderRound,
dp(theme.generalStyle.candidateBorder),
dp(theme.generalStyle.candidateBorderRound),
)

add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ class CandidatesView(
horizontalPadding = dp(theme.generalStyle.layout.marginY)
background =
ColorManager.getDrawable(
ctx,
"candidate_background",
theme.generalStyle.candidateBorder,
"candidate_border_color",
theme.generalStyle.candidateBorderRound,
dp(theme.generalStyle.candidateBorder),
dp(theme.generalStyle.candidateBorderRound),
)
add(
preeditUi.root,
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/com/osfans/trime/ime/symbol/LiquidLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ class LiquidLayout(
frameLayout {
background =
ColorManager.getDrawable(
context,
key = "key_back_color",
border = theme.generalStyle.keyBorder,
borderColorKey = "key_border_color",
roundCorner = theme.generalStyle.roundCorner,
"key_back_color",
"key_border_color",
dp(theme.generalStyle.keyBorder),
dp(theme.generalStyle.roundCorner),
)
add(
text,
Expand Down
18 changes: 8 additions & 10 deletions app/src/main/java/com/osfans/trime/ime/symbol/SimpleAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ import com.osfans.trime.databinding.SimpleItemRowBinding
import splitties.dimensions.dp

class SimpleAdapter(
theme: Theme,
private val theme: Theme,
private val columnSize: Int,
) : RecyclerView.Adapter<SimpleAdapter.ViewHolder>() {
private val mBeans = mutableListOf<SimpleKeyBean>()
private val mBeansByRows = mutableListOf<List<SimpleKeyBean>>()
val beans get() = mBeans

fun updateBeans(beans: List<SimpleKeyBean>) {
val prevSize = mBeansByRows.size
Expand All @@ -47,13 +46,6 @@ class SimpleAdapter(
private val mTextSize = theme.generalStyle.labelTextSize
private val mTextColor = ColorManager.getColor("key_text_color")
private val mTypeface = FontManager.getTypeface("key_font")
private val mBackground =
ColorManager.getDrawable(
key = "key_back_color",
border = theme.generalStyle.keyBorder,
borderColorKey = "key_border_color",
roundCorner = theme.generalStyle.roundCorner,
)

override fun onCreateViewHolder(
parent: ViewGroup,
Expand Down Expand Up @@ -82,7 +74,13 @@ class SimpleAdapter(
typeface = mTypeface
gravity = Gravity.CENTER
ellipsize = TextUtils.TruncateAt.MARQUEE
background = mBackground
background =
ColorManager.getDrawable(
"key_back_color",
"key_border_color",
dp(theme.generalStyle.keyBorder),
dp(theme.generalStyle.roundCorner),
)
}
}
return holder
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/com/osfans/trime/ime/symbol/SimpleItemUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ class SimpleItemUi(
minimumHeight = dp(30)
background =
ColorManager.getDrawable(
ctx,
"long_text_back_color",
border = theme.generalStyle.keyBorder,
"key_long_text_border",
roundCorner = theme.generalStyle.roundCorner,
dp(theme.generalStyle.keyBorder),
dp(theme.generalStyle.roundCorner),
)
add(layout, lParams(matchParent, matchParent))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ class VarLengthAdapter(
addState(
intArrayOf(),
ColorManager.getDrawable(
context = context,
key = "key_back_color",
border = theme.generalStyle.candidateBorder,
borderColorKey = "key_border_color",
roundCorner = theme.generalStyle.roundCorner,
"key_back_color",
"key_border_color",
dp(theme.generalStyle.candidateBorder),
dp(theme.generalStyle.roundCorner),
),
)
mHilitedCandidateBackColor?.let {
Expand Down

0 comments on commit 07ed340

Please sign in to comment.