Skip to content

Commit 0572618

Browse files
tumuyanBambooin
authored andcommitted
fix: selected text not provide to presetkey option
1 parent 05428eb commit 0572618

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.osfans.trime.ime.core
22

33
import android.inputmethodservice.InputMethodService
44
import android.os.SystemClock
5+
import android.text.TextUtils
56
import android.view.InputDevice
67
import android.view.KeyCharacterMap
78
import android.view.KeyEvent
@@ -126,6 +127,28 @@ class EditorInstance(private val ims: InputMethodService) {
126127
return ic.getTextBeforeCursor(n.coerceAtMost(1024), 0)?.toString() ?: ""
127128
}
128129

130+
/** 獲得當前漢字:候選字、選中字、剛上屏字/光標前字/光標前所有字、光標後所有字
131+
* %s或者%1$s爲當前字符
132+
* %2$s爲當前輸入的編碼
133+
* %3$s爲光標前字符
134+
* %4$s爲光標前所有字符
135+
* */
136+
fun getActiveText(type: Int): String {
137+
if (type == 2) return Rime.RimeGetInput() // 當前編碼
138+
var s = Rime.getComposingText() // 當前候選
139+
if (TextUtils.isEmpty(s)) {
140+
val ic = inputConnection
141+
var cs = if (ic != null) ic.getSelectedText(0) else null // 選中字
142+
if (type == 1 && TextUtils.isEmpty(cs)) cs = lastCommittedText // 剛上屏字
143+
if (TextUtils.isEmpty(cs) && ic != null) {
144+
cs = ic.getTextBeforeCursor(if (type == 4) 1024 else 1, 0) // 光標前字
145+
}
146+
if (TextUtils.isEmpty(cs) && ic != null) cs = ic.getTextAfterCursor(1024, 0) // 光標後面所有字
147+
if (cs != null) s = cs.toString()
148+
}
149+
return s
150+
}
151+
129152
/**
130153
* Constructs a meta state integer flag which can be used for setting the `metaState` field when sending a KeyEvent
131154
* to the input connection. If this method is called without a meta modifier set to true, the default value `0` is

app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt

+17-7
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,23 @@ class TextInputManager private constructor() :
354354
// %2$s爲當前輸入的編碼
355355
// %3$s爲光標前字符
356356
// %4$s爲光標前所有字符
357-
val arg = String.format(
358-
event.option,
359-
activeEditorInstance.lastCommittedText,
360-
Rime.RimeGetInput(),
361-
activeEditorInstance.getTextBeforeCursor(1),
362-
activeEditorInstance.getTextBeforeCursor(1024)
363-
)
357+
var arg = event.option
358+
val activeTextRegex = Regex(".*%(\\d*)\\$" + "s.*")
359+
if (arg.matches(activeTextRegex)) {
360+
var activeTextMode =
361+
arg.replaceFirst(activeTextRegex, "$1").toDouble().toInt()
362+
if (activeTextMode <1)
363+
activeTextMode = 1
364+
val activeText = activeEditorInstance.getActiveText(activeTextMode)
365+
arg = String.format(
366+
arg,
367+
activeEditorInstance.lastCommittedText,
368+
Rime.RimeGetInput(),
369+
activeText,
370+
activeText
371+
)
372+
}
373+
364374
if (event.command == "liquid_keyboard") {
365375
trime.selectLiquidKeyboard(arg)
366376
} else if (event.command == "paste_by_char") {

0 commit comments

Comments
 (0)