Get sentence position of cursor
This commit is contained in:
parent
48a9a2e822
commit
23cccf2455
@ -29,18 +29,7 @@ open class DefaultInputHandler(
|
||||
protected var sentenceStart: Boolean = false
|
||||
|
||||
init {
|
||||
val textBeforeCursor = getTextBeforeCursor(15)
|
||||
|
||||
if (
|
||||
textBeforeCursor.isNullOrEmpty() ||
|
||||
textBeforeCursor.trim().isEmpty() ||
|
||||
sentenceDelimiters.contains(textBeforeCursor.trim().last())
|
||||
) {
|
||||
sentenceStart = true
|
||||
wordStart = true
|
||||
} else if (wordDelimiters.contains(textBeforeCursor.last())) {
|
||||
wordStart = true
|
||||
}
|
||||
setCursorPositionStatus()
|
||||
|
||||
wkt9.currentInputEditorInfo?.let {
|
||||
val inputType = it.inputType
|
||||
@ -99,7 +88,26 @@ open class DefaultInputHandler(
|
||||
triggerKeyEvent(key.keyCode)
|
||||
}
|
||||
|
||||
protected fun getTextBeforeCursor(count: Int): CharSequence? {
|
||||
return wkt9.currentInputConnection?.getTextBeforeCursor(count, 0)
|
||||
protected fun setCursorPositionStatus() {
|
||||
val textBeforeCursor = getTextBeforeCursor()
|
||||
|
||||
if (
|
||||
textBeforeCursor.isNullOrEmpty() ||
|
||||
textBeforeCursor.trim().isEmpty() ||
|
||||
sentenceDelimiters.contains(textBeforeCursor.trim().last())
|
||||
) {
|
||||
sentenceStart = true
|
||||
wordStart = true
|
||||
} else if (wordDelimiters.contains(textBeforeCursor.last())) {
|
||||
sentenceStart = false
|
||||
wordStart = true
|
||||
} else {
|
||||
sentenceStart = false
|
||||
wordStart = false
|
||||
}
|
||||
}
|
||||
|
||||
private fun getTextBeforeCursor(): CharSequence? {
|
||||
return wkt9.currentInputConnection?.getTextBeforeCursor(15, 0)
|
||||
}
|
||||
}
|
@ -59,64 +59,22 @@ class LetterInputHandler(
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun finalizeWordOrSentence(stats: KeyEventStat) {
|
||||
if (word.isNotEmpty()) storeWord()
|
||||
override fun onDeleteWord(word: Word) {}
|
||||
|
||||
timeoutJob?.cancel()
|
||||
override fun onGetSuggestions(results: Array<out SuggestionsInfo>?) {}
|
||||
|
||||
val index = stats.repeats % punctuationMarks.count()
|
||||
val lastIndex = if (stats.repeats > 0) (stats.repeats - 1) % punctuationMarks.count() else null
|
||||
var beforeCursor = 0
|
||||
|
||||
if (lastIndex != null) beforeCursor += punctuationMarks[lastIndex].length
|
||||
|
||||
wordStart = true
|
||||
sentenceStart = index in 1..3
|
||||
|
||||
wkt9.onCommit(punctuationMarks[index], beforeCursor)
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun storeWord() {
|
||||
val str = word.toString()
|
||||
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
word.clear()
|
||||
|
||||
scope.launch {
|
||||
val word = Word(
|
||||
word = str,
|
||||
code = keypad.getCodeForWord(str),
|
||||
weight = 0,
|
||||
length = str.length,
|
||||
locale = locale.language
|
||||
)
|
||||
val result = wordDao.selectWord(str, locale.language)
|
||||
|
||||
if (result == null) wordDao.insert(word)
|
||||
}
|
||||
}
|
||||
override fun onFinishComposing() {}
|
||||
|
||||
override fun onGetSentenceSuggestions(results: Array<out SentenceSuggestionsInfo>?) {}
|
||||
override fun onSwitchLocale(locale: Locale) {
|
||||
this.locale = locale
|
||||
}
|
||||
|
||||
override fun onDeleteWord(word: Word) {}
|
||||
override fun onGetSuggestions(results: Array<out SuggestionsInfo>?) {}
|
||||
override fun onGetSentenceSuggestions(results: Array<out SentenceSuggestionsInfo>?) {}
|
||||
override fun onFinishComposing() {}
|
||||
|
||||
override fun toggleCapMode(key: Key) {
|
||||
super.toggleCapMode(key)
|
||||
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
override fun onRunCommand(command: Command, key: Key, event: KeyEvent, stats: KeyEventStat) {
|
||||
when (command) {
|
||||
Command.CAP_MODE -> toggleCapMode(key)
|
||||
Command.CHARACTER -> composeCharacter(key)
|
||||
Command.DELETE -> delete()
|
||||
Command.FINISH_DELETE -> finishDelete()
|
||||
Command.INPUT_MODE -> inputMode(key)
|
||||
Command.NUMBER -> triggerOriginalKeyEvent(key)
|
||||
// Command.RECORD -> wkt9.onRecord(true)
|
||||
@ -125,9 +83,14 @@ class LetterInputHandler(
|
||||
else -> Log.d(tag, "Command not implemented: $command")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onWordSelected(word: Word) {}
|
||||
|
||||
override fun toggleCapMode(key: Key) {
|
||||
super.toggleCapMode(key)
|
||||
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun composeCharacter(key: Key) {
|
||||
val composing = timeoutJob?.isActive ?: false
|
||||
|
||||
@ -157,11 +120,54 @@ class LetterInputHandler(
|
||||
setComposeTimeout()
|
||||
}
|
||||
|
||||
private fun finalizeWordOrSentence(stats: KeyEventStat) {
|
||||
if (word.isNotEmpty()) storeWord()
|
||||
|
||||
timeoutJob?.cancel()
|
||||
|
||||
val index = stats.repeats % punctuationMarks.count()
|
||||
val lastIndex = if (stats.repeats > 0) (stats.repeats - 1) % punctuationMarks.count() else null
|
||||
var beforeCursor = 0
|
||||
|
||||
if (lastIndex != null) beforeCursor += punctuationMarks[lastIndex].length
|
||||
|
||||
wordStart = true
|
||||
sentenceStart = index in 1..3
|
||||
|
||||
wkt9.onCommit(punctuationMarks[index], beforeCursor)
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun finishDelete() {
|
||||
setCursorPositionStatus()
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun resetKey(key: Key? = null) {
|
||||
lastKey = key
|
||||
repeats = 0
|
||||
}
|
||||
|
||||
private fun storeWord() {
|
||||
val str = word.toString()
|
||||
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
word.clear()
|
||||
|
||||
scope.launch {
|
||||
val word = Word(
|
||||
word = str,
|
||||
code = keypad.getCodeForWord(str),
|
||||
weight = 0,
|
||||
length = str.length,
|
||||
locale = locale.language
|
||||
)
|
||||
val result = wordDao.selectWord(str, locale.language)
|
||||
|
||||
if (result == null) wordDao.insert(word)
|
||||
}
|
||||
}
|
||||
|
||||
private fun finishComposingChar() {
|
||||
wkt9.onCommit()
|
||||
|
||||
|
@ -90,6 +90,7 @@ class WordInputHandler(
|
||||
Command.CHARACTER -> buildCodeword(key)
|
||||
Command.DELETE -> delete(event.repeatCount)
|
||||
Command.ENTER -> enter(key)
|
||||
Command.FINISH_DELETE -> finishDelete()
|
||||
Command.INPUT_MODE -> inputMode(key)
|
||||
Command.MOVE_CURSOR -> moveCursor(key)
|
||||
Command.NUMBER -> triggerOriginalKeyEvent(key)
|
||||
@ -131,6 +132,11 @@ class WordInputHandler(
|
||||
}
|
||||
}
|
||||
|
||||
private fun finishDelete() {
|
||||
setCursorPositionStatus()
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun enter(key: Key) {
|
||||
if (codeword.isNotEmpty()) wkt9.onCommit("")
|
||||
else triggerOriginalKeyEvent(key)
|
||||
|
@ -7,6 +7,7 @@ enum class Command {
|
||||
DELETE,
|
||||
DIAL,
|
||||
ENTER,
|
||||
FINISH_DELETE,
|
||||
INPUT_MODE,
|
||||
MOVE_CURSOR,
|
||||
NUMBER,
|
||||
|
@ -388,6 +388,12 @@ enum class Key(
|
||||
command = Command.DELETE
|
||||
),
|
||||
|
||||
CommandMapping(
|
||||
events = listOf(Event.afterShortDown, Event.afterLongDown),
|
||||
inputModes = listOf(InputMode.Word, InputMode.Letter),
|
||||
command = Command.FINISH_DELETE
|
||||
),
|
||||
|
||||
CommandMapping(
|
||||
inputModes = listOf(InputMode.Number),
|
||||
overrideConsume = true,
|
||||
|
Loading…
x
Reference in New Issue
Block a user