Skip to content

Commit 19203e3

Browse files
author
if-can
committed
feat: clear text selection after copy
1 parent 9a3e71a commit 19203e3

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

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

+24-4
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,14 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
250250
return
251251
}
252252

253-
it.modifiers.shift.apply {
254-
if (this) sendDownKeyEvent(eventTime, KeyEvent.KEYCODE_SHIFT_LEFT)
253+
it.modifiers.apply {
254+
if (this.shift) sendDownKeyEvent(eventTime, KeyEvent.KEYCODE_SHIFT_LEFT)
255255

256256
sendDownKeyEvent(eventTime, keyCode, it.modifiers.metaState)
257257

258-
if (this) sendUpKeyEvent(eventTime, KeyEvent.KEYCODE_SHIFT_LEFT)
258+
if (this.shift) sendUpKeyEvent(eventTime, KeyEvent.KEYCODE_SHIFT_LEFT)
259+
260+
if (ctrl && keyCode == KeyEvent.KEYCODE_C) cancelTextSelection()
259261
}
260262
}
261263
} else {
@@ -863,7 +865,14 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
863865
etr.token = 0
864866
val et = ic.getExtractedText(etr, 0)
865867
if (et != null) {
866-
if (et.selectionStart != et.selectionEnd) return ic.performContextMenuAction(android.R.id.copy)
868+
if (et.selectionStart != et.selectionEnd) {
869+
ic.performContextMenuAction(android.R.id.copy).also { result ->
870+
if (result) {
871+
cancelTextSelection()
872+
}
873+
return result
874+
}
875+
}
867876
}
868877
}
869878
Timber.w("hookKeyboard copy fail")
@@ -918,6 +927,17 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
918927
return false
919928
}
920929

930+
fun cancelTextSelection() {
931+
val ic = currentInputConnection ?: return
932+
val etr = ExtractedTextRequest().apply { token = 0 }
933+
val et = currentInputConnection.getExtractedText(etr, 0)
934+
et?.let {
935+
if (it.selectionStart != it.selectionEnd) {
936+
ic.setSelection(it.selectionEnd, it.selectionEnd)
937+
}
938+
}
939+
}
940+
921941
private val composingTextMode by prefs.general.composingTextMode
922942

923943
private fun updateComposingText(ctx: RimeProto.Context) {

0 commit comments

Comments
 (0)