From 1d441a8ca68db2d2444a78a744bd1a20eff3d869 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Sun, 27 Apr 2025 18:59:29 +0200 Subject: [PATCH] more workarounds for Korean not just space might delete text, but also other separators now this problem is avoided in a more generic way fixes GH-1447 --- .../keyboard/latin/WordComposer.java | 5 +++ .../keyboard/latin/inputlogic/InputLogic.java | 35 ++++++++++++------- .../keyboard/latin/InputLogicTest.kt | 8 +++++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/WordComposer.java b/app/src/main/java/helium314/keyboard/latin/WordComposer.java index 203eb205c..a5b50f353 100644 --- a/app/src/main/java/helium314/keyboard/latin/WordComposer.java +++ b/app/src/main/java/helium314/keyboard/latin/WordComposer.java @@ -218,6 +218,11 @@ public final class WordComposer { // TODO: compute where that puts us inside the events } + public void resetInvalidCursorPosition() { + if (mCursorPositionWithinWord > mCodePointSize) + mCursorPositionWithinWord = 0; + } + public boolean isCursorFrontOrMiddleOfComposingWord() { if (DebugFlags.DEBUG_ENABLED && mCursorPositionWithinWord > mCodePointSize) { throw new RuntimeException("Wrong cursor position : " + mCursorPositionWithinWord diff --git a/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.java b/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.java index 79464dee3..4b1bd4df2 100644 --- a/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.java +++ b/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.java @@ -440,19 +440,28 @@ public final class InputLogic { mWordBeingCorrectedByCursor = null; mJustRevertedACommit = false; final Event processedEvent; - if (currentKeyboardScript.equals(ScriptUtils.SCRIPT_HANGUL) - // only use the Hangul chain if codepoint may actually be Hangul - // todo: this whole hangul-related logic should probably be somewhere else - // need to use hangul combiner for whitespace, because otherwise the current word - // seems to get deleted / replaced by space during mConnection.endBatchEdit() - // similar for functional keys (codePoint -1) - && (event.getMCodePoint() >= 0x1100 || Character.isWhitespace(event.getMCodePoint()) || event.getMCodePoint() == -1)) { - mWordComposer.setHangul(true); - final Event hangulDecodedEvent = HangulEventDecoder.decodeSoftwareKeyEvent(event); - // todo: here hangul combiner does already consume the event, and appends typed codepoint - // to the current word instead of considering the cursor position - // position is actually not visible to the combiner, how to fix? - processedEvent = mWordComposer.processEvent(hangulDecodedEvent); + if (currentKeyboardScript.equals(ScriptUtils.SCRIPT_HANGUL)) { + // only use the Hangul chain if codepoint may actually be Hangul + // todo: this whole hangul-related logic should probably be somewhere else + // need to use hangul combiner for functional keys (codePoint -1), because otherwise the current word + // seems to get deleted / replaced by space during mConnection.endBatchEdit() + if (event.getMCodePoint() >= 0x1100 || event.getMCodePoint() == -1) { + mWordComposer.setHangul(true); + final Event hangulDecodedEvent = HangulEventDecoder.decodeSoftwareKeyEvent(event); + // todo: here hangul combiner does already consume the event, and appends typed codepoint + // to the current word instead of considering the cursor position + // position is actually not visible to the combiner, how to fix? + processedEvent = mWordComposer.processEvent(hangulDecodedEvent); + } else { + mWordComposer.setHangul(false); + final boolean wasComposingWord = mWordComposer.isComposingWord(); + processedEvent = mWordComposer.processEvent(event); + // workaround for space and some other separators deleting / replacing the word + if (wasComposingWord && !mWordComposer.isComposingWord()) { + mWordComposer.resetInvalidCursorPosition(); + mConnection.finishComposingText(); + } + } } else { mWordComposer.setHangul(false); processedEvent = mWordComposer.processEvent(event); diff --git a/app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt b/app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt index 4f75173e5..1fd8dea0b 100644 --- a/app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt @@ -152,6 +152,14 @@ class InputLogicTest { assertEquals(4, cursor) } + // see issue 1447 + @Test fun separatorAfterHangul() { + reset() + currentScript = ScriptUtils.SCRIPT_HANGUL + chainInput("ㅛ.") + assertEquals("ㅛ.", text) + } + @Test fun separatorUnselectsWord() { reset() setText("hello")