Skip to content

Commit fa6e56c

Browse files
committed
refactor(ime): reduce redundant text committing functions
1 parent 4af1789 commit fa6e56c

File tree

4 files changed

+12
-24
lines changed

4 files changed

+12
-24
lines changed

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
295295
private fun handleRimeResponse(response: RimeResponse) {
296296
val (commit, ctx, _) = response
297297
if (commit != null && !commit.text.isNullOrEmpty()) {
298-
commitCharSequence(commit.text)
298+
commitText(commit.text)
299299
}
300300
if (ctx != null) {
301301
updateComposingText(ctx)
@@ -561,35 +561,23 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
561561
}
562562

563563
// 直接commit不做任何处理
564-
fun commitCharSequence(
564+
fun commitText(
565565
text: CharSequence,
566566
clearMeatKeyState: Boolean = false,
567-
): Boolean {
568-
val ic = currentInputConnection ?: return false
569-
ic.commitText(text, 1)
570-
if (text.isNotEmpty()) {
567+
) {
568+
val ic = currentInputConnection ?: return
569+
if (ic.commitText(text, 1)) {
571570
lastCommittedText = text
572571
}
573572
if (clearMeatKeyState) {
574573
ic.clearMetaKeyStates(KeyEvent.getModifierMetaStateMask())
575574
DraftHelper.onInputEventChanged()
576575
}
577-
return true
578-
}
579-
580-
/**
581-
* Commit the current composing text together with the new text
582-
*
583-
* @param text the new text to be committed
584-
*/
585-
fun commitText(text: String?) {
586-
currentInputConnection.finishComposingText()
587-
commitCharSequence(text!!, true)
588576
}
589577

590578
private fun commitTextByChar(text: String) {
591579
for (char in text) {
592-
if (!commitCharSequence(char.toString(), false)) break
580+
commitText(char.toString())
593581
}
594582
}
595583

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class CommonKeyboardActionListener(
198198
"set_color_scheme" -> ColorManager.setColorScheme(arg)
199199
else -> {
200200
ShortcutUtils.call(service, event.command, arg)?.let {
201-
service.commitCharSequence(it)
201+
service.commitText(it)
202202
service.updateComposing()
203203
}
204204
}
@@ -260,7 +260,7 @@ class CommonKeyboardActionListener(
260260
if ((metaState == KeyEvent.META_SHIFT_ON || metaState == 0) && keyEventCode >= Keycode.A.ordinal) {
261261
val text = Keycode.getSymbolLabel(Keycode.valueOf(keyEventCode))
262262
if (text.length == 1) {
263-
service.commitCharSequence(text)
263+
service.commitText(text)
264264
return
265265
}
266266
}
@@ -291,10 +291,10 @@ class CommonKeyboardActionListener(
291291
// for this.
292292
if (Rime.simulateKeySequence(slice)) {
293293
if (Rime.isAsciiMode) {
294-
service.commitCharSequence(slice)
294+
service.commitText(slice)
295295
}
296296
} else {
297-
service.commitCharSequence(slice)
297+
service.commitText(slice)
298298
}
299299
}
300300
BRACED_KEY_EVENT.matches(sequence) -> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class KeyboardWindow(
266266
override fun onEvent(event: Event) {
267267
if (event.commit.isNotEmpty()) {
268268
// Directly commit the text and don't dispatch to Rime
269-
service.commitCharSequence(event.commit, true)
269+
service.commitText(event.commit, true)
270270
return
271271
}
272272

app/src/main/java/com/osfans/trime/ime/symbol/DbAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DbAdapter(
2525
var type = SymbolBoardType.CLIPBOARD
2626

2727
override fun onPaste(bean: DatabaseBean) {
28-
service.commitText(bean.text)
28+
service.commitText(bean.text ?: "")
2929
}
3030

3131
override fun onPin(bean: DatabaseBean) {

0 commit comments

Comments
 (0)