Skip to content

Commit

Permalink
refactor: replace with GeneralStyle in ColorManager
Browse files Browse the repository at this point in the history
  • Loading branch information
goofyz committed Apr 17, 2024
1 parent ae982ab commit c395770
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
31 changes: 16 additions & 15 deletions app/src/main/java/com/osfans/trime/data/theme/ColorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import androidx.annotation.ColorInt
import androidx.core.math.MathUtils
import com.osfans.trime.data.AppPrefs
import com.osfans.trime.data.DataManager
import com.osfans.trime.data.sound.SoundEffectManager
Expand All @@ -20,7 +21,7 @@ import java.io.File
object ColorManager {
private val theme get() = ThemeManager.activeTheme
private val prefs = AppPrefs.defaultInstance().theme
private val backgroundFolder get() = theme.style.getString("background_folder")
private val backgroundFolder get() = theme.generalStyle.backgroundFolder

var selectedColor = "default" // 当前配色 id
private var lastLightColorSchemeId: String? = null // 上一个 light 配色
Expand Down Expand Up @@ -111,7 +112,7 @@ object ColorManager {
lastLightColorSchemeId = null

val selected = prefs.selectedColor
val fromStyle = theme.style.getString("color_scheme") // 主題中指定的配色
val fromStyle = theme.generalStyle.colorScheme
val default = "default" // 主題中的 default 配色

selectedColor = arrayOf(selected, fromStyle, default)
Expand Down Expand Up @@ -339,33 +340,33 @@ object ColorManager {
fun getDrawable(
context: Context = appContext,
key: String,
borderKey: String = "",
border: Int = 0,
borderColorKey: String = "",
roundCornerKey: String = "",
alphaKey: String = "",
roundCorner: Int = 0,
alpha: Int = 255,
): Drawable? {
val value = getColorValue(key)
if (value is Drawable) {
if (alphaKey.isNotEmpty() && theme.style.getString(alphaKey).isNotEmpty()) {
value.alpha = theme.style.getInt(alphaKey).coerceIn(0, 255)
if (alpha < 255) {
value.alpha = MathUtils.clamp(alpha, 0, 255)
}
return value
}

if (value is Int) {
val gradient = GradientDrawable().apply { setColor(value) }
if (roundCornerKey.isNotEmpty()) {
gradient.cornerRadius = theme.style.getFloat(roundCornerKey)
if (roundCorner > 0) {
gradient.cornerRadius = roundCorner.toFloat()
}
if (borderColorKey.isNotEmpty() && borderKey.isNotEmpty()) {
val border = context.dp(theme.style.getFloat(borderKey))
if (borderColorKey.isNotEmpty() && border > 0) {
val borderPx = context.dp(border)
val stroke = getColor(borderColorKey)
if (stroke != null && border > 0) {
gradient.setStroke(border.toInt(), stroke)
if (stroke != null && borderPx > 0) {
gradient.setStroke(borderPx, stroke)
}
}
if (alphaKey.isNotEmpty() && theme.style.getString(alphaKey).isNotEmpty()) {
gradient.alpha = theme.style.getInt(alphaKey).coerceIn(0, 255)
if (alpha < 255) {
gradient.alpha = MathUtils.clamp(alpha, 0, 255)
}
return gradient
}
Expand Down
7 changes: 4 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 @@ -8,6 +8,7 @@ import android.widget.ViewAnimator
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeNotification.OptionNotification
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.databinding.CandidateBarBinding
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
import com.osfans.trime.ime.core.TrimeInputMethodService
Expand All @@ -21,7 +22,7 @@ import splitties.views.dsl.core.matchParent

@InputScope
@Inject
class QuickBar(context: Context, service: TrimeInputMethodService) : InputBroadcastReceiver {
class QuickBar(context: Context, service: TrimeInputMethodService, theme: Theme) : InputBroadcastReceiver {
val oldCandidateBar by lazy {
CandidateBarBinding.inflate(LayoutInflater.from(context)).apply {
with(root) {
Expand Down Expand Up @@ -71,9 +72,9 @@ class QuickBar(context: Context, service: TrimeInputMethodService) : InputBroadc
ColorManager.getDrawable(
context,
"candidate_background",
"candidate_border",
theme.generalStyle.candidateBorder,
"candidate_border_color",
"candidate_border_round",
theme.generalStyle.candidateBorderRound,
)
add(oldCandidateBar.root, lParams(matchParent, matchParent))
add(tabUi.root, lParams(matchParent, matchParent))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ class CandidateAdapter(theme: Theme) : RecyclerView.Adapter<CandidateAdapter.Vie
return mCandidates.size
}

private val mCandidateTextSize = theme.style.getFloat("candidate_text_size").coerceAtLeast(1f)
private val mCandidateTextSize = theme.generalStyle.candidateTextSize.toFloat().coerceAtLeast(1f)
private val mCandidateFont = FontManager.getTypeface("candidate_font")
private val mCandidateTextColor = ColorManager.getColor("candidate_text_color")
private val mHilitedCandidateTextColor = ColorManager.getColor("hilited_candidate_text_color")
private val mCommentPosition = theme.style.getInt("comment_position")
private val mCommentTextSize = theme.style.getFloat("comment_text_size").coerceAtLeast(1f)
private val mCommentPosition = theme.generalStyle.commentPosition
private val mCommentTextSize = theme.generalStyle.commentTextSize.toFloat().coerceAtLeast(1f)
private val mCommentFont = FontManager.getTypeface("comment_font")
private val mCommentTextColor = ColorManager.getColor("comment_text_color")
private val mBackground =
ColorManager.getDrawable(
key = "key_back_color",
borderKey = "key_border",
border = theme.generalStyle.candidateBorder,
borderColorKey = "key_border_color",
roundCornerKey = "round_corner",
roundCorner = theme.generalStyle.roundCorner,
)

override fun onCreateViewHolder(
Expand All @@ -81,7 +81,7 @@ class CandidateAdapter(theme: Theme) : RecyclerView.Adapter<CandidateAdapter.Vie
}
val candidate = binding.candidate
val comment = binding.comment
when (CommentPosition.entries[mCommentPosition]) {
when (mCommentPosition) {
CommentPosition.BOTTOM -> {
candidate.updateLayoutParams<ConstraintLayout.LayoutParams> {
centerHorizontally()
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/osfans/trime/ime/symbol/FlexibleAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.osfans.trime.databinding.SimpleKeyItemBinding
import splitties.resources.drawable
import splitties.resources.styledColor

abstract class FlexibleAdapter(theme: Theme) : RecyclerView.Adapter<FlexibleAdapter.ViewHolder>() {
abstract class FlexibleAdapter(private val theme: Theme) : RecyclerView.Adapter<FlexibleAdapter.ViewHolder>() {
private val mBeans = mutableListOf<DatabaseBean>()

// 映射条目的 id 和其在视图中位置的关系
Expand Down Expand Up @@ -54,8 +54,8 @@ abstract class FlexibleAdapter(theme: Theme) : RecyclerView.Adapter<FlexibleAdap
private val mTypeface = FontManager.getTypeface("long_text_font")
private val mLongTextColor = ColorManager.getColor("long_text_color")
private val mKeyTextColor = ColorManager.getColor("key_text_color")
private val mKeyLongTextSize = theme.style.getFloat("key_long_text_size")
private val mLabelTextSize = theme.style.getFloat("label_text_size")
private val mKeyLongTextSize = theme.generalStyle.keyLongTextSize
private val mLabelTextSize = theme.generalStyle.labelTextSize

override fun onCreateViewHolder(
parent: ViewGroup,
Expand All @@ -66,15 +66,15 @@ abstract class FlexibleAdapter(theme: Theme) : RecyclerView.Adapter<FlexibleAdap
ColorManager.getDrawable(
parent.context,
"long_text_back_color",
"key_border",
border = theme.generalStyle.keyBorder,
"key_long_text_border",
"round_corner",
roundCorner = theme.generalStyle.roundCorner,
)
binding.simpleKey.apply {
typeface = mTypeface
(mLongTextColor ?: mKeyTextColor)?.let { setTextColor(it) }
(mKeyLongTextSize.takeIf { it > 0f } ?: mLabelTextSize.takeIf { it > 0f })
?.let { textSize = it }
?.let { textSize = it.toFloat() }
}
return ViewHolder(binding)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ class SimpleAdapter(theme: Theme, private val columnSize: Int) : RecyclerView.Ad
}

private val mSingleWidth = theme.liquid.getInt("single_width")
private val mTextSize = theme.style.getFloat("label_text_size")
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",
borderKey = "key_border",
border = theme.generalStyle.keyBorder,
borderColorKey = "key_border_color",
roundCornerKey = "round_corner",
roundCorner = theme.generalStyle.roundCorner,
)

override fun onCreateViewHolder(
Expand All @@ -66,7 +66,7 @@ class SimpleAdapter(theme: Theme, private val columnSize: Int) : RecyclerView.Ad
holder.simpleKeyTexts.forEachIndexed { index, textView ->
holder.wrappers[index].tag = index
textView.apply {
mTextSize.takeIf { it > 0f }?.let { this.textSize = it }
mTextSize.takeIf { it > 0f }?.let { this.textSize = it.toFloat() }
mTextColor?.let { setTextColor(it) }
typeface = mTypeface
gravity = Gravity.CENTER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ class CompositionPopupWindow(
// 顯示懸浮窗口
val isPopupWindowEnabled =
AppPrefs.defaultInstance().keyboard.popupWindowEnabled &&
theme.style.getItem("window") != null
theme.generalStyle.window != null

val composition =
CompositionRootBinding.inflate(LayoutInflater.from(ctx)).apply {
root.visibility = if (isPopupWindowEnabled) View.VISIBLE else View.GONE
}

// 悬浮窗口是否可移動
private val isPopupWindowMovable = theme.style.getString("layout/movable")
private val isPopupWindowMovable = theme.generalStyle.layout.movable

private var popupWindowX = 0
private var popupWindowY = 0 // 悬浮床移动座標

// 候選窗與邊緣空隙
private val popupMargin = theme.style.getInt("layout/spacing")
private val popupMargin = theme.generalStyle.layout.spacing

// 悬浮窗与屏幕两侧的间距
private val popupMarginH = theme.style.getInt("layout/real_margin")
private val popupMarginH = theme.generalStyle.layout.realMargin

// 悬浮窗口彈出位置
private var popupWindowPos = PopupPosition.fromString(theme.style.getString("layout/position"))
private var popupWindowPos = PopupPosition.fromString(theme.generalStyle.layout.position)

private val mPopupWindow =
PopupWindow(composition.root).apply {
Expand All @@ -65,13 +65,13 @@ class CompositionPopupWindow(
ColorManager.getDrawable(
ctx,
"text_back_color",
"layout/border",
theme.generalStyle.layout.border,
"border_color",
"layout/round_corner",
"layout/alpha",
theme.generalStyle.layout.roundCorner,
theme.generalStyle.layout.alpha,
),
)
elevation = ctx.dp(theme.style.getFloat("layout/elevation"))
elevation = ctx.dp(theme.generalStyle.layout.elevation.toFloat())
}

var isCursorUpdated = false // 光標是否移動
Expand Down Expand Up @@ -175,7 +175,7 @@ class CompositionPopupWindow(

fun updateCompositionView() {
if (isPopupWindowMovable == "once") {
popupWindowPos = PopupPosition.fromString(ThemeManager.activeTheme.style.getString("layout/position"))
popupWindowPos = PopupPosition.fromString(ThemeManager.activeTheme.generalStyle.layout.position)
}
composition.root.measure(
ViewGroup.LayoutParams.WRAP_CONTENT,
Expand Down

0 comments on commit c395770

Please sign in to comment.