-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInputConnection.kt
More file actions
68 lines (55 loc) · 1.81 KB
/
InputConnection.kt
File metadata and controls
68 lines (55 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.rickyhu.hushkeyboard.utils
import android.content.Context
import android.util.Log
import android.view.inputmethod.InputConnection
import com.rickyhu.hushkeyboard.model.Notation
import com.rickyhu.hushkeyboard.service.HushIMEService
private const val TAG = "InputConnection"
private const val END_CURSOR_POSITION = 1
val notationCharList = Notation.getCharList() + listOf('\n')
fun Context.toInputConnection(): InputConnection = (this as HushIMEService).currentInputConnection
fun InputConnection.inputText(text: String) {
commitText(text, END_CURSOR_POSITION)
Log.d(TAG, "inputText $text")
}
fun InputConnection.deleteText() {
val selectedText = getSelectedText(0)
if (selectedText.isNullOrEmpty()) {
deleteSurroundingText(1, 0)
Log.d(TAG, "deleteText")
} else {
commitText("", END_CURSOR_POSITION)
Log.d(TAG, "delete selected text: $selectedText")
}
}
fun InputConnection.smartDelete() {
Log.d(TAG, "smartDelete")
val selectedText = getSelectedText(0)
if (!selectedText.isNullOrEmpty()) {
commitText("", END_CURSOR_POSITION)
Log.d(TAG, "delete selected text: $selectedText")
return
}
beginBatchEdit()
try {
val scanWindow = 50
val textBeforeCursor = getTextBeforeCursor(scanWindow, 0)
if (textBeforeCursor.isNullOrEmpty()) {
deleteSurroundingText(1, 0)
return
}
var charsToDelete = 0
for (i in textBeforeCursor.indices.reversed()) {
val char = textBeforeCursor[i]
charsToDelete++
if (char.uppercaseChar() in notationCharList) {
break
}
}
if (charsToDelete > 0) {
deleteSurroundingText(charsToDelete, 0)
}
} finally {
endBatchEdit()
}
}