From a356774001b42af971783e5e0f92aef9912f8fb5 Mon Sep 17 00:00:00 2001 From: Nehemiah of Zebulun Date: Wed, 30 Aug 2023 11:26:55 +0200 Subject: [PATCH] Press on --- app/src/main/java/net/mezimmah/wkt9/WKT9.kt | 2 +- .../mezimmah/wkt9/inputmode/AlphaInputMode.kt | 2 +- .../wkt9/inputmode/NumericInputMode.kt | 154 +++++++++++++++--- .../net/mezimmah/wkt9/keypad/KeyLayout.kt | 20 +-- .../main/res/drawable/numeric_en_us_num.xml | 10 ++ 5 files changed, 152 insertions(+), 36 deletions(-) create mode 100644 app/src/main/res/drawable/numeric_en_us_num.xml diff --git a/app/src/main/java/net/mezimmah/wkt9/WKT9.kt b/app/src/main/java/net/mezimmah/wkt9/WKT9.kt index 0d398ae..e4d493d 100644 --- a/app/src/main/java/net/mezimmah/wkt9/WKT9.kt +++ b/app/src/main/java/net/mezimmah/wkt9/WKT9.kt @@ -386,7 +386,7 @@ class WKT9: InputMethodService() { } loadCandidates(candidateIndex) - composeText(candidates[candidateIndex]) + composeText(this.candidates[candidateIndex]) handleComposeTimeout(timeout) } diff --git a/app/src/main/java/net/mezimmah/wkt9/inputmode/AlphaInputMode.kt b/app/src/main/java/net/mezimmah/wkt9/inputmode/AlphaInputMode.kt index f385d84..41fa4fd 100644 --- a/app/src/main/java/net/mezimmah/wkt9/inputmode/AlphaInputMode.kt +++ b/app/src/main/java/net/mezimmah/wkt9/inputmode/AlphaInputMode.kt @@ -174,7 +174,7 @@ class AlphaInputMode: InputMode { return KeyEventResult( consumed = true, finishComposing = composing, - switchInputMode = WKT9InputMode.WORD + switchInputMode = WKT9InputMode.NUMERIC ) } diff --git a/app/src/main/java/net/mezimmah/wkt9/inputmode/NumericInputMode.kt b/app/src/main/java/net/mezimmah/wkt9/inputmode/NumericInputMode.kt index 4171a14..eefc88d 100644 --- a/app/src/main/java/net/mezimmah/wkt9/inputmode/NumericInputMode.kt +++ b/app/src/main/java/net/mezimmah/wkt9/inputmode/NumericInputMode.kt @@ -2,51 +2,157 @@ package net.mezimmah.wkt9.inputmode import android.util.Log import net.mezimmah.wkt9.keypad.Command -import net.mezimmah.wkt9.keypad.KeyCommandResolver import net.mezimmah.wkt9.keypad.Key +import net.mezimmah.wkt9.keypad.KeyCommandResolver import net.mezimmah.wkt9.keypad.KeyEventResult -import java.lang.StringBuilder +import net.mezimmah.wkt9.keypad.KeyLayout class NumericInputMode: InputMode { - override val mode: String = "numeric" - private val tag = "WKT9" - private val keyCommandResolver: KeyCommandResolver = KeyCommandResolver.getBasic() - private val codeWord = StringBuilder() + private val parentKeyCommandResolver: KeyCommandResolver = KeyCommandResolver.getBasic() + private val keyCommandResolver: KeyCommandResolver = KeyCommandResolver( + parent = parentKeyCommandResolver, + afterShort = HashMap(mapOf( + Key.N0 to Command.NUMBER, + Key.N1 to Command.NUMBER, + Key.N2 to Command.NUMBER, + Key.N3 to Command.NUMBER, + Key.N4 to Command.NUMBER, + Key.N5 to Command.NUMBER, + Key.N6 to Command.NUMBER, + Key.N7 to Command.NUMBER, + Key.N8 to Command.NUMBER, + Key.N9 to Command.NUMBER + )) + ) + private var newKey = true + private var keyIndex = 0 + private var lastKey: Key? = null + + override val mode: String = "numeric" override var status: Status = Status.NUM private set - override fun onKeyDown(key: Key, sentenceStart: Boolean): KeyEventResult { + init { + Log.d(tag, "Started numeric input mode.") + } + + override fun onKeyDown(key: Key, composing: Boolean): KeyEventResult { + keyStats(key) + return when(keyCommandResolver.getCommand(key)) { - Command.CHARACTER -> buildCodeWord(key) -// Command.SELECT -> true -// Command.DELETE -> delete() -// Command.SPACE -> finalizeWordOrSentence() -// Command.CYCLE_CANDIDATES -> cycleCandidates() - else -> KeyEventResult(consumed = true) + Command.BACK -> KeyEventResult(consumed = false) + Command.DELETE -> deleteCharacter(composing) + Command.LEFT -> navigateLeft() + Command.RIGHT -> navigateRight() + else -> KeyEventResult() } } - override fun onKeyLongDown(key: Key, sentenceStart: Boolean): KeyEventResult { - return KeyEventResult(consumed = false) + override fun onKeyLongDown(key: Key, composing: Boolean): KeyEventResult { + return when(keyCommandResolver.getCommand(key, true)) { + Command.SWITCH_MODE -> switchMode(composing) + else -> KeyEventResult(true) + } } - override fun onKeyDownRepeatedly(key: Key, repeat: Int, sentenceStart: Boolean): KeyEventResult { - return KeyEventResult(consumed = false) + override fun onKeyDownRepeatedly(key: Key, repeat: Int, composing: Boolean): KeyEventResult { + return when(keyCommandResolver.getCommand(key, repeat = repeat)) { + Command.HOME -> goHome(repeat, composing) + Command.DELETE -> deleteCharacter(composing) + else -> KeyEventResult() + } } - override fun afterKeyDown(key: Key, sentenceStart: Boolean): KeyEventResult { - return KeyEventResult(consumed = false) + override fun afterKeyDown(key: Key, composing: Boolean): KeyEventResult { + return when(keyCommandResolver.getCommand(key, after = true)) { + Command.BACK -> goBack(composing) + Command.NUMBER -> composeNumber(key, composing) + else -> KeyEventResult() + } } - override fun afterKeyLongDown(key: Key, keyDownMS: Long, sentenceStart: Boolean): KeyEventResult { - return KeyEventResult(consumed = false) + override fun afterKeyLongDown(key: Key, keyDownMS: Long, composing: Boolean): KeyEventResult { + return when(keyCommandResolver.getCommand(key, after = true, longPress = true)) { + else -> KeyEventResult() + } } - private fun buildCodeWord(key: Key): KeyEventResult { - Log.d(tag, "GR8!") + private fun deleteCharacter(composing: Boolean): KeyEventResult { + return KeyEventResult( + finishComposing = composing, + deleteBeforeCursor = 1 + ) + } - return KeyEventResult(consumed = true) + private fun composeNumber(key: Key, composing: Boolean): KeyEventResult { + if (composing && !newKey) return navigateRight() + + val layout = KeyLayout.numeric[key] ?: return KeyEventResult(true) + val candidates = layout.map { it.toString() } + + return KeyEventResult( + consumed = true, + finishComposing = composing, + startComposing = true, + candidates = candidates, + timeout = 400 + ) + } + + private fun focus(): KeyEventResult { + return KeyEventResult( + consumed = true, + focus = true + ) + } + + private fun goBack(composing: Boolean): KeyEventResult { + return KeyEventResult( + consumed = false, + finishComposing = composing + ) + } + + private fun goHome(repeat: Int, composing: Boolean): KeyEventResult { + if (repeat > 1) return KeyEventResult(true) + + return KeyEventResult( + consumed = true, + finishComposing = composing, + goHome = true + ) + } + + private fun keyStats(key: Key) { + when (key != lastKey) { + true -> { + newKey = true + keyIndex = 0 + } + false -> { + newKey = false + keyIndex++ + } + } + + lastKey = key + } + + private fun navigateLeft(): KeyEventResult { + return KeyEventResult(left = true) + } + + private fun navigateRight(): KeyEventResult { + return KeyEventResult(right = true) + } + + private fun switchMode(composing: Boolean): KeyEventResult { + return KeyEventResult( + consumed = true, + finishComposing = composing, + switchInputMode = WKT9InputMode.WORD + ) } } \ No newline at end of file diff --git a/app/src/main/java/net/mezimmah/wkt9/keypad/KeyLayout.kt b/app/src/main/java/net/mezimmah/wkt9/keypad/KeyLayout.kt index 0eb4689..bc90a95 100644 --- a/app/src/main/java/net/mezimmah/wkt9/keypad/KeyLayout.kt +++ b/app/src/main/java/net/mezimmah/wkt9/keypad/KeyLayout.kt @@ -3,16 +3,16 @@ package net.mezimmah.wkt9.keypad object KeyLayout { // Map for number input mode val numeric = mapOf( - Key.N0 to '0', - Key.N1 to '1', - Key.N2 to '2', - Key.N3 to '3', - Key.N4 to '4', - Key.N5 to '5', - Key.N6 to '6', - Key.N7 to '7', - Key.N8 to '8', - Key.N9 to '9', + Key.N0 to listOf('0'), + Key.N1 to listOf('1'), + Key.N2 to listOf('2'), + Key.N3 to listOf('3'), + Key.N4 to listOf('4'), + Key.N5 to listOf('5'), + Key.N6 to listOf('6'), + Key.N7 to listOf('7'), + Key.N8 to listOf('8'), + Key.N9 to listOf('9'), ) val en_US = mapOf( diff --git a/app/src/main/res/drawable/numeric_en_us_num.xml b/app/src/main/res/drawable/numeric_en_us_num.xml new file mode 100644 index 0000000..8fe0071 --- /dev/null +++ b/app/src/main/res/drawable/numeric_en_us_num.xml @@ -0,0 +1,10 @@ + + +