Skip to content

Commit

Permalink
fix: selected text not provide to presetkey option
Browse files Browse the repository at this point in the history
  • Loading branch information
tumuyan authored and Bambooin committed Jul 3, 2022
1 parent 05428eb commit 0572618
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
23 changes: 23 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/core/EditorInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.osfans.trime.ime.core

import android.inputmethodservice.InputMethodService
import android.os.SystemClock
import android.text.TextUtils
import android.view.InputDevice
import android.view.KeyCharacterMap
import android.view.KeyEvent
Expand Down Expand Up @@ -126,6 +127,28 @@ class EditorInstance(private val ims: InputMethodService) {
return ic.getTextBeforeCursor(n.coerceAtMost(1024), 0)?.toString() ?: ""
}

/** 獲得當前漢字:候選字、選中字、剛上屏字/光標前字/光標前所有字、光標後所有字
* %s或者%1$s爲當前字符
* %2$s爲當前輸入的編碼
* %3$s爲光標前字符
* %4$s爲光標前所有字符
* */
fun getActiveText(type: Int): String {
if (type == 2) return Rime.RimeGetInput() // 當前編碼
var s = Rime.getComposingText() // 當前候選
if (TextUtils.isEmpty(s)) {
val ic = inputConnection
var cs = if (ic != null) ic.getSelectedText(0) else null // 選中字
if (type == 1 && TextUtils.isEmpty(cs)) cs = lastCommittedText // 剛上屏字
if (TextUtils.isEmpty(cs) && ic != null) {
cs = ic.getTextBeforeCursor(if (type == 4) 1024 else 1, 0) // 光標前字
}
if (TextUtils.isEmpty(cs) && ic != null) cs = ic.getTextAfterCursor(1024, 0) // 光標後面所有字
if (cs != null) s = cs.toString()
}
return s
}

/**
* Constructs a meta state integer flag which can be used for setting the `metaState` field when sending a KeyEvent
* to the input connection. If this method is called without a meta modifier set to true, the default value `0` is
Expand Down
24 changes: 17 additions & 7 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,23 @@ class TextInputManager private constructor() :
// %2$s爲當前輸入的編碼
// %3$s爲光標前字符
// %4$s爲光標前所有字符
val arg = String.format(
event.option,
activeEditorInstance.lastCommittedText,
Rime.RimeGetInput(),
activeEditorInstance.getTextBeforeCursor(1),
activeEditorInstance.getTextBeforeCursor(1024)
)
var arg = event.option
val activeTextRegex = Regex(".*%(\\d*)\\$" + "s.*")
if (arg.matches(activeTextRegex)) {
var activeTextMode =
arg.replaceFirst(activeTextRegex, "$1").toDouble().toInt()
if (activeTextMode <1)
activeTextMode = 1
val activeText = activeEditorInstance.getActiveText(activeTextMode)
arg = String.format(
arg,
activeEditorInstance.lastCommittedText,
Rime.RimeGetInput(),
activeText,
activeText
)
}

if (event.command == "liquid_keyboard") {
trime.selectLiquidKeyboard(arg)
} else if (event.command == "paste_by_char") {
Expand Down

0 comments on commit 0572618

Please sign in to comment.