Making progress

This commit is contained in:
Nehemiah of Zebulun 2023-11-09 10:26:17 -05:00
parent 9d5c6364f0
commit 590975d708
7 changed files with 84 additions and 33 deletions

View File

@ -284,7 +284,7 @@ class WKT9: WKT9Interface, InputMethodService() {
}
override fun onSwitchInputHandler(inputMode: InputMode) {
inputManager.switchToHandler(inputMode)
inputManager.switchToHandler(inputMode, cursorPosition)
}
override fun onTranscribe() {
@ -327,10 +327,11 @@ class WKT9: WKT9Interface, InputMethodService() {
}
private fun commitText(text: CharSequence) {
markComposingRegion()
composeText(text)
finishComposingText()
clearCandidates()
val insertTextStart = cursorPosition
val insertTextEnd = cursorPosition + text.length
currentInputConnection?.commitText(text, 1)
inputManager.handler?.onInsertText(text, insertTextStart, insertTextEnd)
}
private fun composeText(text: CharSequence, cursorPosition: Int = 1) {

View File

@ -15,15 +15,11 @@ class Capitalize(
if (index == 0) sentenceStart = true
if (char.isLetter()) {
capitalized = if (
capMode == InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS ||
(sentenceStart && (capMode == InputType.TYPE_TEXT_FLAG_CAP_WORDS || capMode == InputType.TYPE_TEXT_FLAG_CAP_SENTENCES)) ||
(wordStart && capMode == InputType.TYPE_TEXT_FLAG_CAP_WORDS)
) {
capitalized.replaceRange(index, index +1, char.uppercase())
} else {
capitalized.replaceRange(index, index +1, char.lowercase())
}
capitalized = capitalized.replaceRange(
index,
index +1,
character(char, sentenceStart, wordStart)
)
sentenceStart = false
wordStart = false
@ -47,4 +43,13 @@ class Capitalize(
else -> word
}
}
fun character(char: Char, sentenceStart: Boolean, wordStart: Boolean): String {
return if (
capMode == InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS ||
(sentenceStart && (capMode == InputType.TYPE_TEXT_FLAG_CAP_SENTENCES || capMode == InputType.TYPE_TEXT_FLAG_CAP_WORDS)) ||
(wordStart && capMode == InputType.TYPE_TEXT_FLAG_CAP_WORDS)
) char.uppercase()
else char.lowercase()
}
}

View File

@ -1,5 +1,6 @@
package net.mezimmah.wkt9.inputmode
import android.text.InputType
import android.util.Log
import android.view.KeyEvent
import net.mezimmah.wkt9.WKT9
@ -37,6 +38,8 @@ open class InputHandler(
override fun onFinish() {}
override fun onInsertText(text: CharSequence, insertTextStart: Int, insertTextEnd: Int) {}
override fun onLongClickCandidate(text: String) {}
override fun onRunCommand(command: Command, key: Key, event: KeyEvent, stats: KeyEventStat) {}
@ -48,10 +51,24 @@ open class InputHandler(
}
protected open fun capMode(key: Key) {
// capMode = when (key) {
// Key.B2 -> CapMode.previous(capMode)
// else -> CapMode.next(capMode)
// }
val modes = listOf(
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES,
InputType.TYPE_TEXT_FLAG_CAP_WORDS,
InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS,
null
)
var index = modes.indexOf(capMode)
when (key) {
Key.B2 -> {
if (index == 0) index = modes.count()
index--
}
else -> index++
}
capMode = modes[index % modes.count()]
}
protected open fun finalizeWordOrSentence(stats: KeyEventStat) {
@ -76,6 +93,20 @@ open class InputHandler(
)
}
protected fun getDefaultCapMode(typeFlags: Int): Int? {
val modes = listOf(
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES,
InputType.TYPE_TEXT_FLAG_CAP_WORDS,
InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
)
modes.forEach {
if (typeFlags.and(it) == it) return it
}
return null
}
protected fun triggerKeyEvent(keyCode: Int, finishComposing: Boolean) {
val down = KeyEvent(KeyEvent.ACTION_DOWN, keyCode)
val up = KeyEvent(KeyEvent.ACTION_UP, keyCode)

View File

@ -22,6 +22,8 @@ interface InputHandlerInterface {
fun onFinish()
fun onInsertText(text: CharSequence, insertTextStart: Int, insertTextEnd: Int)
fun onLongClickCandidate(text: String)
fun onRunCommand(command: Command, key: Key, event: KeyEvent, stats: KeyEventStat)

View File

@ -49,7 +49,7 @@ class InputManager(val context: WKT9) {
typeFlags = inputType.and(InputType.TYPE_MASK_FLAGS)
allowSuggestions = typeFlags != InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
if (override != null) return switchToHandler(override)
if (override != null) return switchToHandler(override, editor.initialSelEnd)
val handler = if (numericClasses.contains(typeClass)) {
InputMode.Number
@ -63,10 +63,10 @@ class InputManager(val context: WKT9) {
InputMode.Idle
}
switchToHandler(handler)
switchToHandler(handler, editor.initialSelEnd)
}
fun switchToHandler(inputMode: InputMode) {
fun switchToHandler(inputMode: InputMode, cursorPosition: Int) {
this.handler?.onFinish()
this.mode = inputMode
this.handler = when (inputMode) {
@ -76,6 +76,7 @@ class InputManager(val context: WKT9) {
else -> idleInputHandler
}.apply {
onStart(typeClass, typeVariation, typeFlags)
onUpdateCursorPosition(cursorPosition)
}
}

View File

@ -72,6 +72,7 @@ class LetterInputHandler(wkt9: WKT9Interface, context: WKT9): SpellCheckerSessio
override fun onCommitText() {
composing.clear()
wkt9.onClearCandidates()
val info = getCursorPositionInfo(content)
@ -128,6 +129,11 @@ class LetterInputHandler(wkt9: WKT9Interface, context: WKT9): SpellCheckerSessio
)
}
// This is for the text that should be committed without 'handling'.
override fun onInsertText(text: CharSequence, insertTextStart: Int, insertTextEnd: Int) {
content.replace(insertTextStart, insertTextEnd, text.toString())
}
override fun onRunCommand(command: Command, key: Key, event: KeyEvent, stats: KeyEventStat) {
when (command) {
Command.CAP_MODE -> capMode(key)
@ -144,6 +150,8 @@ class LetterInputHandler(wkt9: WKT9Interface, context: WKT9): SpellCheckerSessio
}
override fun onStart(typeClass: Int, typeVariations: Int, typeFlags: Int) {
capMode = getDefaultCapMode(typeFlags)
// Get current editor content on start
wkt9.onGetText()?.let {
content.replace(0, content.length, it.toString())
@ -153,14 +161,10 @@ class LetterInputHandler(wkt9: WKT9Interface, context: WKT9): SpellCheckerSessio
override fun onUpdateCursorPosition(cursorPosition: Int) {
super.onUpdateCursorPosition(cursorPosition)
if (cursorPosition > content.length) return
var info: CursorPositionInfo
if (cursorPosition > content.length) {
Log.d(tag, "This should not happen and is just a fail over.")
content.replace(0, content.length, wkt9.onGetText().toString())
}
if (cursorPosition == 0) {
info = CursorPositionInfo(
startSentence = true,
@ -240,7 +244,7 @@ class LetterInputHandler(wkt9: WKT9Interface, context: WKT9): SpellCheckerSessio
if (stats.repeats == 0 && composing.isNotEmpty()) wkt9.onFinishComposing()
layout.forEach {
candidates.add(capitalize.word(it.toString(),sentenceStart))
candidates.add(capitalize.character(it, sentenceStart, wordStart))
}
wkt9.onCandidates(

View File

@ -74,6 +74,11 @@ class WordInputHandler(wkt9: WKT9Interface, context: WKT9) : InputHandler(wkt9,
composing.replace(0, composing.length, text.toString())
}
// This is for the text that should be committed without 'handling'.
override fun onInsertText(text: CharSequence, insertTextStart: Int, insertTextEnd: Int) {
content.replace(insertTextStart, insertTextEnd, text.toString())
}
override fun onFinish() {
if (composing.isNotEmpty()) wkt9.onFinishComposing()
}
@ -102,6 +107,12 @@ class WordInputHandler(wkt9: WKT9Interface, context: WKT9) : InputHandler(wkt9,
}
override fun onStart(typeClass: Int, typeVariations: Int, typeFlags: Int) {
codeword.clear()
content.clear()
composing.clear()
capMode = getDefaultCapMode(typeFlags)
// Get current editor content on start
wkt9.onGetText()?.let {
content.replace(0, content.length, it.toString())
@ -111,11 +122,7 @@ class WordInputHandler(wkt9: WKT9Interface, context: WKT9) : InputHandler(wkt9,
override fun onUpdateCursorPosition(cursorPosition: Int) {
super.onUpdateCursorPosition(cursorPosition)
if (cursorPosition > content.length) {
Log.d(tag, "This should not happen and is just a fail over.")
content.replace(0, content.length, wkt9.onGetText().toString())
}
if (cursorPosition > content.length) return
var info: CursorPositionInfo