From 3ee710d1faa8daf041695dbb03c9b8e30da43045 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Wed, 18 Nov 2026 20:46:17 +0530 Subject: [PATCH] feat(khipro): add Blinder mode with backslash trigger Backslash switches Khipro to plain Latin input until ';', as in the official spec. ';;' types a literal ';', and a new input field leaves the mode. '?' is not a trigger since it ends Bengali questions. Backslash is a long-press popup on the ';' key. Refs #789 --- .../functional/functional_keys_khipro.json | 2 +- .../keyboard/event/BnKhiproCombiner.kt | 35 +++++++++++++ .../java/helium314/keyboard/event/Combiner.kt | 3 ++ .../helium314/keyboard/event/CombinerChain.kt | 6 +++ .../helium314/keyboard/latin/WordComposer.kt | 1 + .../keyboard/event/BnKhiproCombinerTest.kt | 52 +++++++++++++++++++ 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 app/src/test/java/helium314/keyboard/event/BnKhiproCombinerTest.kt diff --git a/app/src/main/assets/layouts/functional/functional_keys_khipro.json b/app/src/main/assets/layouts/functional/functional_keys_khipro.json index 771e45dc2..09211fbe8 100644 --- a/app/src/main/assets/layouts/functional/functional_keys_khipro.json +++ b/app/src/main/assets/layouts/functional/functional_keys_khipro.json @@ -19,7 +19,7 @@ { "$": "keyboard_state_selector", "emojiKeyEnabled": { "$": "keyboard_state_selector", "alphabet": { "label": "emoji" }}}, { "$": "keyboard_state_selector", "symbols": { "label": "numpad" }}, { "label": "space" }, - { "$": "keyboard_state_selector", "alphabet": { "label": ";" }}, + { "$": "keyboard_state_selector", "alphabet": { "label": ";", "popup": { "main": { "label": "\\" } } }}, { "label": "period" }, { "label": "action", "width": 0.15 } ] diff --git a/app/src/main/java/helium314/keyboard/event/BnKhiproCombiner.kt b/app/src/main/java/helium314/keyboard/event/BnKhiproCombiner.kt index b3895f0a9..7951c1e11 100644 --- a/app/src/main/java/helium314/keyboard/event/BnKhiproCombiner.kt +++ b/app/src/main/java/helium314/keyboard/event/BnKhiproCombiner.kt @@ -21,6 +21,11 @@ class BnKhiproCombiner : Combiner { private val composingText = StringBuilder() + // Blinder mode (Khipro spec): after '\' Latin passes through untransliterated until ';'. + // '?' is not a trigger here, since it ends Bengali questions. ';;' types a literal ';'. + private var blinder = false + private var blinderExitPending = false + enum class State { INIT, SHOR_STATE, @@ -181,6 +186,27 @@ class BnKhiproCombiner : Combiner { if (event.keyCode == KeyCode.SHIFT) return event + if (blinder) { + if (blinderExitPending) { + blinderExitPending = false + if (codePoint == ';'.code) return event + blinder = false + } else if (codePoint == ';'.code) { + blinderExitPending = true + return Event.createConsumedEvent(event) + } else { + return event + } + } + + if (codePoint == '\\'.code) { // the trigger itself is not typed, as in the spec + blinder = true + if (composingText.isEmpty()) return Event.createConsumedEvent(event) + val text = combiningStateFeedback + reset() + return createEventChainFromSequence(text, Event.createConsumedEvent(event)) + } + if (isValidCodePoint && Character.isWhitespace(codePoint)) { return commitAndReset(event) } else if (event.isFunctionalKeyEvent) { @@ -204,6 +230,15 @@ class BnKhiproCombiner : Combiner { override fun reset() { composingText.setLength(0) + if (blinderExitPending) { + blinder = false + blinderExitPending = false + } + } + + override fun onStartInput() { + blinder = false + blinderExitPending = false } private fun commitAndReset(event: Event): Event { diff --git a/app/src/main/java/helium314/keyboard/event/Combiner.kt b/app/src/main/java/helium314/keyboard/event/Combiner.kt index 4ea4c9ab4..11fc49716 100644 --- a/app/src/main/java/helium314/keyboard/event/Combiner.kt +++ b/app/src/main/java/helium314/keyboard/event/Combiner.kt @@ -35,4 +35,7 @@ interface Combiner { * Reset the state of this combiner, for example when the cursor was moved. */ fun reset() + + /** Called when input starts in a new field, to clear state that outlives [reset]. */ + fun onStartInput() {} } \ No newline at end of file diff --git a/app/src/main/java/helium314/keyboard/event/CombinerChain.kt b/app/src/main/java/helium314/keyboard/event/CombinerChain.kt index 2cceaab32..6b3c7330a 100644 --- a/app/src/main/java/helium314/keyboard/event/CombinerChain.kt +++ b/app/src/main/java/helium314/keyboard/event/CombinerChain.kt @@ -53,6 +53,12 @@ class CombinerChain(initialText: String, combiningSpec: String) { } } + fun onStartInput() { + for (c in mCombiners) { + c.onStartInput() + } + } + private fun updateStateFeedback() { mStateFeedback.clear() for (i in mCombiners.indices.reversed()) { diff --git a/app/src/main/java/helium314/keyboard/latin/WordComposer.kt b/app/src/main/java/helium314/keyboard/latin/WordComposer.kt index 5c7461efe..ff782f32d 100644 --- a/app/src/main/java/helium314/keyboard/latin/WordComposer.kt +++ b/app/src/main/java/helium314/keyboard/latin/WordComposer.kt @@ -89,6 +89,7 @@ class WordComposer { ) mCombiningSpec = nonNullCombiningSpec } + mCombinerChain.onStartInput() } /** diff --git a/app/src/test/java/helium314/keyboard/event/BnKhiproCombinerTest.kt b/app/src/test/java/helium314/keyboard/event/BnKhiproCombinerTest.kt new file mode 100644 index 000000000..1f4070bca --- /dev/null +++ b/app/src/test/java/helium314/keyboard/event/BnKhiproCombinerTest.kt @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-3.0-only +package helium314.keyboard.event + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class BnKhiproCombinerTest { + private val combiner = BnKhiproCombiner() + + private fun type(c: Char): Event = + combiner.processEvent(null, Event.createSoftwareKeypressEvent(c.code, 0, 0, 0, 0, false)) + + @Test fun backslashEntersBlinderAndLatinPassesThrough() { + assertTrue(type('\\').isConsumed) // trigger is not typed + val e = type('k') + assertFalse(e.isConsumed) + assertEquals('k'.code, e.codePoint) + } + + @Test fun semicolonLeavesBlinder() { + type('\\') + assertTrue(type(';').isConsumed) + assertTrue(type('k').isConsumed) // composed by Khipro again + } + + @Test fun doubleSemicolonTypesLiteralAndStaysInBlinder() { + type('\\') + type(';') + val e = type(';') + assertFalse(e.isConsumed) + assertEquals(';'.code, e.codePoint) + assertFalse(type('k').isConsumed) + } + + @Test fun questionMarkDoesNotEnterBlinder() { + combiner.reset() + type('?') + combiner.reset() + assertTrue(type('k').isConsumed) + } + + @Test fun newInputFieldLeavesBlinder() { + type('\\') + combiner.onStartInput() + assertTrue(type('k').isConsumed) + } +}