Get sentence position of cursor

This commit is contained in:
Nehemiah of Zebulun 2023-12-04 05:48:46 -05:00
parent 48a9a2e822
commit 23cccf2455
5 changed files with 89 additions and 62 deletions

View File

@ -29,18 +29,7 @@ open class DefaultInputHandler(
protected var sentenceStart: Boolean = false protected var sentenceStart: Boolean = false
init { init {
val textBeforeCursor = getTextBeforeCursor(15) setCursorPositionStatus()
if (
textBeforeCursor.isNullOrEmpty() ||
textBeforeCursor.trim().isEmpty() ||
sentenceDelimiters.contains(textBeforeCursor.trim().last())
) {
sentenceStart = true
wordStart = true
} else if (wordDelimiters.contains(textBeforeCursor.last())) {
wordStart = true
}
wkt9.currentInputEditorInfo?.let { wkt9.currentInputEditorInfo?.let {
val inputType = it.inputType val inputType = it.inputType
@ -99,7 +88,26 @@ open class DefaultInputHandler(
triggerKeyEvent(key.keyCode) triggerKeyEvent(key.keyCode)
} }
protected fun getTextBeforeCursor(count: Int): CharSequence? { protected fun setCursorPositionStatus() {
return wkt9.currentInputConnection?.getTextBeforeCursor(count, 0) 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)
} }
} }

View File

@ -59,64 +59,22 @@ class LetterInputHandler(
updateIcon() updateIcon()
} }
private fun finalizeWordOrSentence(stats: KeyEventStat) { override fun onDeleteWord(word: Word) {}
if (word.isNotEmpty()) storeWord()
timeoutJob?.cancel() override fun onGetSuggestions(results: Array<out SuggestionsInfo>?) {}
val index = stats.repeats % punctuationMarks.count() override fun onFinishComposing() {}
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 onGetSentenceSuggestions(results: Array<out SentenceSuggestionsInfo>?) {}
override fun onSwitchLocale(locale: Locale) { override fun onSwitchLocale(locale: Locale) {
this.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) { override fun onRunCommand(command: Command, key: Key, event: KeyEvent, stats: KeyEventStat) {
when (command) { when (command) {
Command.CAP_MODE -> toggleCapMode(key) Command.CAP_MODE -> toggleCapMode(key)
Command.CHARACTER -> composeCharacter(key) Command.CHARACTER -> composeCharacter(key)
Command.DELETE -> delete() Command.DELETE -> delete()
Command.FINISH_DELETE -> finishDelete()
Command.INPUT_MODE -> inputMode(key) Command.INPUT_MODE -> inputMode(key)
Command.NUMBER -> triggerOriginalKeyEvent(key) Command.NUMBER -> triggerOriginalKeyEvent(key)
// Command.RECORD -> wkt9.onRecord(true) // Command.RECORD -> wkt9.onRecord(true)
@ -125,9 +83,14 @@ class LetterInputHandler(
else -> Log.d(tag, "Command not implemented: $command") else -> Log.d(tag, "Command not implemented: $command")
} }
} }
override fun onWordSelected(word: Word) {} override fun onWordSelected(word: Word) {}
override fun toggleCapMode(key: Key) {
super.toggleCapMode(key)
updateIcon()
}
private fun composeCharacter(key: Key) { private fun composeCharacter(key: Key) {
val composing = timeoutJob?.isActive ?: false val composing = timeoutJob?.isActive ?: false
@ -157,11 +120,54 @@ class LetterInputHandler(
setComposeTimeout() 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) { private fun resetKey(key: Key? = null) {
lastKey = key lastKey = key
repeats = 0 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() { private fun finishComposingChar() {
wkt9.onCommit() wkt9.onCommit()

View File

@ -90,6 +90,7 @@ class WordInputHandler(
Command.CHARACTER -> buildCodeword(key) Command.CHARACTER -> buildCodeword(key)
Command.DELETE -> delete(event.repeatCount) Command.DELETE -> delete(event.repeatCount)
Command.ENTER -> enter(key) Command.ENTER -> enter(key)
Command.FINISH_DELETE -> finishDelete()
Command.INPUT_MODE -> inputMode(key) Command.INPUT_MODE -> inputMode(key)
Command.MOVE_CURSOR -> moveCursor(key) Command.MOVE_CURSOR -> moveCursor(key)
Command.NUMBER -> triggerOriginalKeyEvent(key) Command.NUMBER -> triggerOriginalKeyEvent(key)
@ -131,6 +132,11 @@ class WordInputHandler(
} }
} }
private fun finishDelete() {
setCursorPositionStatus()
updateIcon()
}
private fun enter(key: Key) { private fun enter(key: Key) {
if (codeword.isNotEmpty()) wkt9.onCommit("") if (codeword.isNotEmpty()) wkt9.onCommit("")
else triggerOriginalKeyEvent(key) else triggerOriginalKeyEvent(key)

View File

@ -7,6 +7,7 @@ enum class Command {
DELETE, DELETE,
DIAL, DIAL,
ENTER, ENTER,
FINISH_DELETE,
INPUT_MODE, INPUT_MODE,
MOVE_CURSOR, MOVE_CURSOR,
NUMBER, NUMBER,

View File

@ -388,6 +388,12 @@ enum class Key(
command = Command.DELETE command = Command.DELETE
), ),
CommandMapping(
events = listOf(Event.afterShortDown, Event.afterLongDown),
inputModes = listOf(InputMode.Word, InputMode.Letter),
command = Command.FINISH_DELETE
),
CommandMapping( CommandMapping(
inputModes = listOf(InputMode.Number), inputModes = listOf(InputMode.Number),
overrideConsume = true, overrideConsume = true,