Skip to content

Commit

Permalink
fix: keyboard layout became cluttered after changing themes
Browse files Browse the repository at this point in the history
fix: forgot reset some parameters when call `KeyboardSwitcher.newOrReset()`
refactor: rename ThemeAndColor to Theme in AppPrefs

Co-authored-by: nopdan <xci@live.com>
  • Loading branch information
WhiredPlanck and nopdan committed Feb 2, 2024
1 parent 8c13f2d commit 6e4fdee
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/data/AppPrefs.kt
Expand Up @@ -22,7 +22,7 @@ class AppPrefs(

val internal = Internal(this)
val keyboard = Keyboard(this)
val themeAndColor = ThemeAndColor(this)
val theme = Theme(this)
val profile = Profile(this)
val clipboard = Clipboard(this)
val other = Other(this)
Expand Down Expand Up @@ -306,7 +306,7 @@ class AppPrefs(
/**
* Wrapper class of theme and color settings.
*/
class ThemeAndColor(private val prefs: AppPrefs) {
class Theme(private val prefs: AppPrefs) {
companion object {
const val SELECTED_THEME = "theme_selected_theme"
const val SELECTED_COLOR = "theme_selected_color"
Expand Down
32 changes: 17 additions & 15 deletions app/src/main/java/com/osfans/trime/data/theme/Theme.kt
Expand Up @@ -78,7 +78,7 @@ class Theme(private var isDarkMode: Boolean) {
}

fun init() {
val active = appPrefs.themeAndColor.selectedTheme
val active = appPrefs.theme.selectedTheme
Timber.i("Initializing theme, currentThemeName=%s ...", active)
runCatching {
val themeFileName = "$active.yaml"
Expand Down Expand Up @@ -107,8 +107,8 @@ class Theme(private var isDarkMode: Boolean) {
Timber.i("The theme is initialized")
}.getOrElse {
Timber.e("Failed to parse the theme: ${it.message}")
if (appPrefs.themeAndColor.selectedTheme != DEFAULT_THEME_NAME) {
appPrefs.themeAndColor.selectedTheme = DEFAULT_THEME_NAME
if (appPrefs.theme.selectedTheme != DEFAULT_THEME_NAME) {
appPrefs.theme.selectedTheme = DEFAULT_THEME_NAME
init()
}
}
Expand Down Expand Up @@ -283,31 +283,33 @@ class Theme(private var isDarkMode: Boolean) {
}
}

var hasDarkLight = false
private set

/**
* 获取暗黑模式/明亮模式下配色方案的名称
*
* @param isDarkMode 是否暗黑模式
* @return 配色方案名称
*/
private fun getColorSchemeName(): String? {
var scheme = appPrefs.themeAndColor.selectedColor
if (!presetColorSchemes!!.containsKey(scheme)) scheme = style.getString("color_scheme") // 主題中指定的配色
if (!presetColorSchemes!!.containsKey(scheme)) scheme = "default" // 主題中的default配色
val colorMap = presetColorSchemes!![scheme] as Map<String, Any>
if (colorMap.containsKey("dark_scheme") || colorMap.containsKey("light_scheme")) hasDarkLight = true
val final =
appPrefs.theme.selectedColor
.takeIf { presetColorSchemes!!.containsKey(it) }
?: style.getString("color_scheme") // 主題中指定的配色
.takeIf { presetColorSchemes!!.containsKey(it) }
?: "default" // 主題中的default配色
val colorMap = presetColorSchemes!![final] as Map<String, Any>
if (isDarkMode) {
if (colorMap.containsKey("dark_scheme")) {
return colorMap["dark_scheme"] as String?
return (colorMap["dark_scheme"] as String?).also {
if (!it.isNullOrEmpty()) appPrefs.theme.selectedColor = it
}
}
} else {
if (colorMap.containsKey("light_scheme")) {
return colorMap["light_scheme"] as String?
return (colorMap["light_scheme"] as String?).also {
if (!it.isNullOrEmpty()) appPrefs.theme.selectedColor = it
}
}
}
return scheme
return final
}

private fun joinToFullImagePath(value: String): String {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/data/theme/ThemeManager.kt
Expand Up @@ -81,10 +81,10 @@ object ThemeManager {
onChangeListeners.forEach { it.onThemeChange(_activeTheme) }
}

val prefs = AppPrefs.defaultInstance().themeAndColor
val prefs = AppPrefs.defaultInstance().theme

fun setNormalTheme(name: String) {
AppPrefs.defaultInstance().themeAndColor.selectedTheme = name
AppPrefs.defaultInstance().theme.selectedTheme = name
activeTheme = evalActiveTheme()
}

Expand Down
Expand Up @@ -8,16 +8,16 @@ import timber.log.Timber

/** Manages [Keyboard]s and their status. **/
object KeyboardSwitcher {
var currentId: Int = 0
var lastId: Int = 0
var lastLockId: Int = 0
private var currentId: Int = 0
private var lastId: Int = 0
private var lastLockId: Int = 0

private var currentDisplayWidth: Int = 0
private val keyboardPrefs = KeyboardPrefs()

private val theme = ThemeManager.activeTheme
lateinit var availableKeyboardIds: List<String>
lateinit var availableKeyboards: List<Keyboard>
private val theme get() = ThemeManager.activeTheme
private lateinit var availableKeyboardIds: List<String>
private lateinit var availableKeyboards: List<Keyboard>

/** To get current keyboard instance. **/
@JvmStatic
Expand Down Expand Up @@ -46,6 +46,11 @@ object KeyboardSwitcher {
Keyboard("default")
}
}

currentId = 0
lastId = 0
lastLockId = 0
currentDisplayWidth = 0
}

fun switchKeyboard(name: String?) {
Expand Down Expand Up @@ -91,7 +96,7 @@ object KeyboardSwitcher {
if ("mini" in availableKeyboardIds) {
val mini = availableKeyboardIds.indexOf("mini")
currentId =
if (AppPrefs.defaultInstance().themeAndColor.useMiniKeyboard && deviceKeyboard != Configuration.KEYBOARD_NOKEYS) {
if (AppPrefs.defaultInstance().theme.useMiniKeyboard && deviceKeyboard != Configuration.KEYBOARD_NOKEYS) {
if (currentId == 0) mini else currentId
} else {
if (currentId == mini) 0 else currentId
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/osfans/trime/ui/main/Pickers.kt
Expand Up @@ -32,7 +32,7 @@ suspend fun Context.themePicker(
.map { it.substringBeforeLast('.') }
.toTypedArray()
val current =
AppPrefs.defaultInstance().themeAndColor.selectedTheme
AppPrefs.defaultInstance().theme.selectedTheme
.substringBeforeLast('.')
checkedItem = items.indexOf(current)
}
Expand All @@ -59,14 +59,14 @@ suspend fun Context.colorPicker(
val all by lazy { ThemeManager.activeTheme.getPresetColorSchemes() }
onInit {
items = all.map { it.second }.toTypedArray()
val current = prefs.themeAndColor.selectedColor
val current = prefs.theme.selectedColor
val schemeIds = all.map { it.first }
checkedItem = schemeIds.indexOf(current).takeIf { it > -1 } ?: 1
}
postiveDispatcher = Dispatchers.Default
onOKButton {
val schemeIds = all.map { it.first }
prefs.themeAndColor.selectedColor = schemeIds[checkedItem]
prefs.theme.selectedColor = schemeIds[checkedItem]
launch {
Trime.getServiceOrNull()?.initKeyboard() // 立刻重初始化键盘生效
}
Expand Down

0 comments on commit 6e4fdee

Please sign in to comment.