Skip to content

Commit 0bbbec5

Browse files
committed
refactor: reduce redundant nesting during key processing
1 parent cd5154b commit 0bbbec5

File tree

4 files changed

+63
-85
lines changed

4 files changed

+63
-85
lines changed

app/src/main/java/com/osfans/trime/ime/core/InputView.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class InputView(
103103
private val themedContext = context.withTheme(android.R.style.Theme_DeviceDefault_Settings)
104104
private val inputComponent = InputComponent::class.create(this, themedContext, theme, service, rime)
105105
private val broadcaster = inputComponent.broadcaster
106-
val commonKeyboardActionListener = inputComponent.commonKeyboardActionListener
107106
private val enterKeyLabel = inputComponent.enterKeyLabel
108107
private val windowManager = inputComponent.windowManager
109108
private val quickBar: QuickBar = inputComponent.quickBar

app/src/main/java/com/osfans/trime/ime/core/TrimeInputMethodService.kt

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,14 @@ import com.osfans.trime.daemon.RimeSession
4848
import com.osfans.trime.data.db.DraftHelper
4949
import com.osfans.trime.data.prefs.AppPrefs
5050
import com.osfans.trime.data.theme.ColorManager
51-
import com.osfans.trime.data.theme.KeyActionManager
5251
import com.osfans.trime.data.theme.Theme
5352
import com.osfans.trime.data.theme.ThemeManager
5453
import com.osfans.trime.ime.broadcast.IntentReceiver
5554
import com.osfans.trime.ime.enums.FullscreenMode
5655
import com.osfans.trime.ime.enums.InlinePreeditMode
57-
import com.osfans.trime.ime.enums.Keycode
58-
import com.osfans.trime.ime.keyboard.CommonKeyboardActionListener
5956
import com.osfans.trime.ime.keyboard.InitializationUi
6057
import com.osfans.trime.ime.keyboard.InputFeedbackManager
6158
import com.osfans.trime.util.ShortcutUtils
62-
import com.osfans.trime.util.ShortcutUtils.openCategory
6359
import com.osfans.trime.util.findSectionFrom
6460
import com.osfans.trime.util.isLandscape
6561
import com.osfans.trime.util.isNightMode
@@ -87,16 +83,12 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
8783
private val prefs: AppPrefs
8884
get() = AppPrefs.defaultInstance()
8985
var inputView: InputView? = null
90-
private val commonKeyboardActionListener: CommonKeyboardActionListener?
91-
get() = inputView?.commonKeyboardActionListener
9286
private var initializationUi: InitializationUi? = null
9387
private var mIntentReceiver: IntentReceiver? = null
9488
private var isWindowShown = false // 键盘窗口是否已显示
9589
private var isComposable: Boolean = false
9690
private val locales = Array(2) { Locale.getDefault() }
9791

98-
var shouldUpdateRimeOption = false
99-
10092
var lastCommittedText: CharSequence = ""
10193
private set
10294

@@ -161,18 +153,13 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
161153
isWindowShown = false
162154
}
163155

164-
private fun updateRimeOption(): Boolean {
156+
private suspend fun updateRimeOption(api: RimeApi) {
165157
try {
166-
if (shouldUpdateRimeOption) {
167-
Rime.setOption("soft_cursor", prefs.keyboard.softCursorEnabled) // 軟光標
168-
Rime.setOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal) // 水平模式
169-
shouldUpdateRimeOption = false
170-
}
158+
api.setRuntimeOption("soft_cursor", prefs.keyboard.softCursorEnabled) // 軟光標
159+
api.setRuntimeOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal) // 水平模式
171160
} catch (e: Exception) {
172161
Timber.e(e)
173-
return false
174162
}
175-
return true
176163
}
177164

178165
/** 防止重启系统 强行停止应用时alarm任务丢失 */
@@ -204,6 +191,9 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
204191

205192
override fun onCreate() {
206193
rime = RimeDaemon.createSession(javaClass.name)
194+
postRimeJob {
195+
updateRimeOption(this)
196+
}
207197
lifecycleScope.launch {
208198
jobs.consumeEach { it.join() }
209199
}
@@ -231,7 +221,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
231221
ThemeManager.init()
232222
InputFeedbackManager.init()
233223
restartSystemStartTimingSync()
234-
shouldUpdateRimeOption = true
235224
val theme = ThemeManager.activeTheme
236225
val defaultLocale = theme.generalStyle.locale.split(DELIMITER_SPLITTER)
237226
locales[0] =
@@ -259,7 +248,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
259248
when (it) {
260249
is RimeNotification.OptionNotification -> {
261250
val value = it.value.value
262-
when (val option = it.value.option) {
251+
when (it.value.option) {
263252
"ascii_mode" -> {
264253
InputFeedbackManager.ttsLanguage =
265254
locales[if (value) 1 else 0]
@@ -269,16 +258,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
269258
-> {
270259
setCandidatesViewShown(isComposable && !value)
271260
}
272-
else ->
273-
if (option.startsWith("_key_") && option.length > 5 && value) {
274-
shouldUpdateRimeOption = false // 防止在 handleRimeNotification 中 setOption
275-
val key = option.substring(5)
276-
inputView
277-
?.commonKeyboardActionListener
278-
?.listener
279-
?.onAction(KeyActionManager.getAction(key))
280-
shouldUpdateRimeOption = true
281-
}
282261
}
283262
}
284263
is RimeEvent.IpcResponseEvent ->
@@ -322,7 +301,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
322301
*
323302
* 重置鍵盤、候選條、狀態欄等 !!注意,如果其中調用Rime.setOption,切換方案會卡住 */
324303
fun recreateInputView(theme: Theme) {
325-
shouldUpdateRimeOption = true // 不能在Rime.onMessage中調用set_option,會卡死
326304
updateComposing() // 切換主題時刷新候選
327305
setInputView(InputView(this, rime, theme).also { inputView = it })
328306
initializationUi = null
@@ -822,40 +800,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
822800
}
823801
}
824802

825-
// 处理键盘事件(Android keycode)
826-
fun handleKey(
827-
keyEventCode: Int,
828-
metaState: Int,
829-
): Boolean { // 軟鍵盤
830-
commonKeyboardActionListener?.shouldReleaseKey = false
831-
val value =
832-
RimeKeyMapping
833-
.keyCodeToVal(keyEventCode)
834-
.takeIf { it != RimeKeyMapping.RimeKey_VoidSymbol }
835-
?: Rime.getRimeKeycodeByName(Keycode.keyNameOf(keyEventCode))
836-
val modifiers = KeyModifiers.fromMetaState(metaState).modifiers
837-
val code = ScancodeMapping.keyCodeToScancode(keyEventCode)
838-
var result = false
839-
postRimeJob {
840-
if (!processKey(value, modifiers, code)) {
841-
if (!hookKeyboard(keyEventCode, metaState)) {
842-
if (!openCategory(keyEventCode)) {
843-
result = false
844-
} else {
845-
Timber.d("handleKey: openCategory")
846-
}
847-
} else {
848-
Timber.d("handleKey: hook")
849-
}
850-
} else {
851-
commonKeyboardActionListener?.shouldReleaseKey = true
852-
Timber.d("handleKey: processKey")
853-
}
854-
}
855-
if (!result) commonKeyboardActionListener?.shouldReleaseKey = true
856-
return result
857-
}
858-
859803
fun shareText(): Boolean {
860804
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
861805
val ic = currentInputConnection ?: return false
@@ -867,7 +811,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
867811
}
868812

869813
/** 編輯操作 */
870-
private fun hookKeyboard(
814+
fun hookKeyboard(
871815
code: Int,
872816
mask: Int,
873817
): Boolean {

app/src/main/java/com/osfans/trime/ime/keyboard/CommonKeyboardActionListener.kt

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@ import android.view.KeyEvent
1111
import androidx.lifecycle.lifecycleScope
1212
import com.osfans.trime.R
1313
import com.osfans.trime.core.KeyModifier
14+
import com.osfans.trime.core.KeyModifiers
1415
import com.osfans.trime.core.Rime
1516
import com.osfans.trime.core.RimeApi
1617
import com.osfans.trime.core.RimeKeyMapping
18+
import com.osfans.trime.core.ScancodeMapping
1719
import com.osfans.trime.daemon.RimeSession
1820
import com.osfans.trime.daemon.launchOnReady
1921
import com.osfans.trime.data.prefs.AppPrefs
2022
import com.osfans.trime.data.theme.ColorManager
2123
import com.osfans.trime.data.theme.KeyActionManager
22-
import com.osfans.trime.data.theme.ThemeManager
2324
import com.osfans.trime.ime.core.InputView
2425
import com.osfans.trime.ime.core.Speech
2526
import com.osfans.trime.ime.core.TrimeInputMethodService
2627
import com.osfans.trime.ime.dependency.InputScope
2728
import com.osfans.trime.ime.dialog.AvailableSchemaPickerDialog
2829
import com.osfans.trime.ime.dialog.EnabledSchemaPickerDialog
30+
import com.osfans.trime.ime.enums.Keycode
2931
import com.osfans.trime.ime.symbol.LiquidKeyboard
3032
import com.osfans.trime.ime.symbol.SymbolBoardType
3133
import com.osfans.trime.ime.symbol.TabManager
@@ -34,10 +36,12 @@ import com.osfans.trime.ui.main.settings.ColorPickerDialog
3436
import com.osfans.trime.ui.main.settings.KeySoundEffectPickerDialog
3537
import com.osfans.trime.ui.main.settings.ThemePickerDialog
3638
import com.osfans.trime.util.ShortcutUtils
39+
import com.osfans.trime.util.ShortcutUtils.openCategory
3740
import com.osfans.trime.util.isAsciiPrintable
3841
import kotlinx.coroutines.launch
3942
import me.tatarka.inject.annotations.Inject
4043
import splitties.systemservices.inputMethodManager
44+
import timber.log.Timber
4145

4246
@InputScope
4347
@Inject
@@ -59,7 +63,7 @@ class CommonKeyboardActionListener(
5963

6064
private val prefs = AppPrefs.defaultInstance()
6165

62-
var shouldReleaseKey: Boolean = false
66+
private var shouldReleaseKey: Boolean = false
6367

6468
private fun showDialog(dialog: suspend (RimeApi) -> Dialog) {
6569
rime.launchOnReady { api ->
@@ -118,11 +122,6 @@ class CommonKeyboardActionListener(
118122

119123
override fun onRelease(keyEventCode: Int) {
120124
if (shouldReleaseKey) {
121-
if (service.shouldUpdateRimeOption) {
122-
Rime.setOption("soft_cursors", prefs.keyboard.softCursorEnabled)
123-
Rime.setOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal)
124-
service.shouldUpdateRimeOption = false
125-
}
126125
// FIXME: 释放按键可能不对
127126
val value = RimeKeyMapping.keyCodeToVal(keyEventCode)
128127
if (value != RimeKeyMapping.RimeKey_VoidSymbol) {
@@ -255,18 +254,41 @@ class CommonKeyboardActionListener(
255254
keyEventCode: Int,
256255
metaState: Int,
257256
) {
258-
// 优先由librime处理按键事件
259-
if (service.handleKey(keyEventCode, metaState)) return
260-
261257
shouldReleaseKey = false
258+
val value =
259+
RimeKeyMapping
260+
.keyCodeToVal(keyEventCode)
261+
.takeIf { it != RimeKeyMapping.RimeKey_VoidSymbol }
262+
?: Rime.getRimeKeycodeByName(Keycode.keyNameOf(keyEventCode))
263+
val modifiers = KeyModifiers.fromMetaState(metaState).modifiers
264+
val code = ScancodeMapping.keyCodeToScancode(keyEventCode)
265+
service.postRimeJob {
266+
if (processKey(value, modifiers, code)) {
267+
shouldReleaseKey = true
268+
Timber.d("handleKey: processKey")
269+
return@postRimeJob
270+
}
271+
if (service.hookKeyboard(keyEventCode, metaState)) {
272+
Timber.d("handleKey: hook")
273+
return@postRimeJob
274+
}
275+
if (context.openCategory(keyEventCode)) {
276+
Timber.d("handleKey: openCategory")
277+
return@postRimeJob
278+
}
279+
shouldReleaseKey = false
262280

263-
when (keyEventCode) {
264-
KeyEvent.KEYCODE_ENTER -> service.handleReturnKey()
265-
KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_ESCAPE -> service.requestHideSelf(0)
266-
else -> {
267-
// 小键盘自动增加锁定
268-
if (keyEventCode in KeyEvent.KEYCODE_NUMPAD_0..KeyEvent.KEYCODE_NUMPAD_EQUALS) {
269-
service.sendDownUpKeyEvent(keyEventCode, metaState or KeyEvent.META_NUM_LOCK_ON)
281+
when (keyEventCode) {
282+
KeyEvent.KEYCODE_ENTER -> service.handleReturnKey()
283+
KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_ESCAPE -> service.requestHideSelf(0)
284+
else -> {
285+
// 小键盘自动增加锁定
286+
if (keyEventCode in KeyEvent.KEYCODE_NUMPAD_0..KeyEvent.KEYCODE_NUMPAD_EQUALS) {
287+
service.sendDownUpKeyEvent(
288+
keyEventCode,
289+
metaState or KeyEvent.META_NUM_LOCK_ON,
290+
)
291+
}
270292
}
271293
}
272294
}

app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardWindow.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.osfans.trime.core.SchemaItem
1919
import com.osfans.trime.daemon.RimeSession
2020
import com.osfans.trime.data.prefs.AppPrefs
2121
import com.osfans.trime.data.schema.SchemaManager
22+
import com.osfans.trime.data.theme.KeyActionManager
2223
import com.osfans.trime.data.theme.Theme
2324
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
2425
import com.osfans.trime.ime.core.TrimeInputMethodService
@@ -245,9 +246,21 @@ class KeyboardWindow(
245246
"_hide_key_hint" -> mainKeyboardView.showKeyHint = !value.value
246247
"_hide_key_symbol" -> mainKeyboardView.showKeySymbol = !value.value
247248
else -> {
248-
if (opt.matches("^_keyboard_.+".toRegex())) {
249-
switchKeyboard(opt.removePrefix("_keyboard_"))
250-
return
249+
when {
250+
opt.startsWith("_keyboard_") -> {
251+
val target = opt.removePrefix("_keyboard_")
252+
if (target.isNotEmpty()) {
253+
switchKeyboard(target)
254+
}
255+
}
256+
opt.startsWith("_key_") -> {
257+
val what = opt.removePrefix("_key_")
258+
if (what.isNotEmpty() && value.value) {
259+
commonKeyboardActionListener
260+
.listener
261+
.onAction(KeyActionManager.getAction(what))
262+
}
263+
}
251264
}
252265
}
253266
}

0 commit comments

Comments
 (0)