Skip to content

Commit

Permalink
refactor(ime): reduce redundant text committing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Oct 4, 2024
1 parent 4af1789 commit fa6e56c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
private fun handleRimeResponse(response: RimeResponse) {
val (commit, ctx, _) = response
if (commit != null && !commit.text.isNullOrEmpty()) {
commitCharSequence(commit.text)
commitText(commit.text)
}
if (ctx != null) {
updateComposingText(ctx)
Expand Down Expand Up @@ -561,35 +561,23 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}

// 直接commit不做任何处理
fun commitCharSequence(
fun commitText(
text: CharSequence,
clearMeatKeyState: Boolean = false,
): Boolean {
val ic = currentInputConnection ?: return false
ic.commitText(text, 1)
if (text.isNotEmpty()) {
) {
val ic = currentInputConnection ?: return
if (ic.commitText(text, 1)) {
lastCommittedText = text
}
if (clearMeatKeyState) {
ic.clearMetaKeyStates(KeyEvent.getModifierMetaStateMask())
DraftHelper.onInputEventChanged()
}
return true
}

/**
* Commit the current composing text together with the new text
*
* @param text the new text to be committed
*/
fun commitText(text: String?) {
currentInputConnection.finishComposingText()
commitCharSequence(text!!, true)
}

private fun commitTextByChar(text: String) {
for (char in text) {
if (!commitCharSequence(char.toString(), false)) break
commitText(char.toString())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class CommonKeyboardActionListener(
"set_color_scheme" -> ColorManager.setColorScheme(arg)
else -> {
ShortcutUtils.call(service, event.command, arg)?.let {
service.commitCharSequence(it)
service.commitText(it)
service.updateComposing()
}
}
Expand Down Expand Up @@ -260,7 +260,7 @@ class CommonKeyboardActionListener(
if ((metaState == KeyEvent.META_SHIFT_ON || metaState == 0) && keyEventCode >= Keycode.A.ordinal) {
val text = Keycode.getSymbolLabel(Keycode.valueOf(keyEventCode))
if (text.length == 1) {
service.commitCharSequence(text)
service.commitText(text)
return
}
}
Expand Down Expand Up @@ -291,10 +291,10 @@ class CommonKeyboardActionListener(
// for this.
if (Rime.simulateKeySequence(slice)) {
if (Rime.isAsciiMode) {
service.commitCharSequence(slice)
service.commitText(slice)
}
} else {
service.commitCharSequence(slice)
service.commitText(slice)
}
}
BRACED_KEY_EVENT.matches(sequence) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class KeyboardWindow(
override fun onEvent(event: Event) {
if (event.commit.isNotEmpty()) {
// Directly commit the text and don't dispatch to Rime
service.commitCharSequence(event.commit, true)
service.commitText(event.commit, true)
return
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/ime/symbol/DbAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DbAdapter(
var type = SymbolBoardType.CLIPBOARD

override fun onPaste(bean: DatabaseBean) {
service.commitText(bean.text)
service.commitText(bean.text ?: "")
}

override fun onPin(bean: DatabaseBean) {
Expand Down

0 comments on commit fa6e56c

Please sign in to comment.