Skip to content

Commit

Permalink
perf: reduce unnecessary cache refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan committed Feb 7, 2024
1 parent 9a474b5 commit 16d6b45
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
26 changes: 9 additions & 17 deletions app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -219,25 +219,17 @@ class InputView(
}

private fun handleRimeNotification(it: RimeNotification) {
when (it) {
is RimeNotification.OptionNotification -> {
when (it.option) {
"_hide_comment" -> {
quickBar.oldCandidateBar.candidates.setShowComment(!it.value)
}
"_hide_bar",
"_hide_candidate",
-> {
quickBar.view.visibility =
if (it.value) View.GONE else View.VISIBLE
}
"_hide_key_hint" -> keyboardWindow.oldMainInputView.mainKeyboardView.setShowHint(!it.value)
"_hide_key_symbol" -> keyboardWindow.oldMainInputView.mainKeyboardView.setShowSymbol(!it.value)
}
keyboardWindow.oldMainInputView.mainKeyboardView.invalidateAllKeys()
if (it !is RimeNotification.OptionNotification) return

when (it.option) {
"_hide_comment" -> quickBar.oldCandidateBar.candidates.setShowComment(!it.value)
"_hide_bar", "_hide_candidate" -> {
quickBar.view.visibility = if (it.value) View.GONE else View.VISIBLE
}
else -> {}
"_hide_key_hint" -> keyboardWindow.oldMainInputView.mainKeyboardView.setShowHint(!it.value)
"_hide_key_symbol" -> keyboardWindow.oldMainInputView.mainKeyboardView.setShowSymbol(!it.value)
}
keyboardWindow.oldMainInputView.mainKeyboardView.invalidateAllKeys()
}

fun switchUiByState(state: KeyboardWindow.State) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ object KeyboardSwitcher {
private var lastKeyboardId = ".default"
private var lastLockKeyboardId = ".default"
private val keyboardPrefs = KeyboardPrefs()
private var currentTheme = ""
private var currentColor = ""
private val prefs get() = AppPrefs.defaultInstance()

lateinit var currentKeyboard: Keyboard

Expand All @@ -39,7 +42,13 @@ object KeyboardSwitcher {
currentKeyboardId = ".default"
lastKeyboardId = ".default"
lastLockKeyboardId = ".default"
keyboardCache.clear()
if (currentTheme != prefs.theme.selectedTheme ||
currentColor != prefs.theme.selectedColor
) {
currentTheme = prefs.theme.selectedTheme
currentColor = prefs.theme.selectedColor
keyboardCache.clear()
}
switchKeyboard(currentKeyboardId)
}

Expand All @@ -48,7 +57,7 @@ object KeyboardSwitcher {
val currentIdx = theme.allKeyboardIds.indexOf(currentKeyboardId)
var mappedName =
when (name) {
".default" -> autoMatch(name)
".default" -> autoMatch()
".prior" ->
try {
theme.allKeyboardIds[currentIdx - 1]
Expand All @@ -72,10 +81,8 @@ object KeyboardSwitcher {
}
}
else -> {
if (name.isNullOrEmpty()) {
name.ifEmpty {
if (currentKeyboard.isLock) currentKeyboardId else lastLockKeyboardId
} else {
name
}
}
}
Expand All @@ -92,6 +99,10 @@ object KeyboardSwitcher {
mappedName = landscapeKeyboard
}
}
if (mappedName == currentKeyboardId) {
Timber.d("Keyboard is already $mappedName, no need to switch.")
return
}
// 应用键盘布局
Timber.i(
"Switched keyboard from $currentKeyboardId " +
Expand All @@ -109,7 +120,7 @@ object KeyboardSwitcher {
/**
* .default 自动匹配键盘布局
* */
private fun autoMatch(name: String): String {
private fun autoMatch(): String {
// 主题的布局中包含方案id,直接采用
val currentSchemaId = Rime.getCurrentRimeSchema()
if (currentSchemaId in allKeyboardIds) {
Expand Down

0 comments on commit 16d6b45

Please sign in to comment.