From 01a2f7efbb29e75ff3edba38d8b99b1bd8128e57 Mon Sep 17 00:00:00 2001 From: Linfye <3158203624@qq.com> Date: Sun, 27 Oct 2024 20:38:17 +0800 Subject: [PATCH 1/2] Fix the button deletion issue and set the button color --- .../be/scri/services/SimpleKeyboardIME.kt | 28 +++++++++++++++---- .../emoji_phone_background_rounded.xml | 2 +- .../emoji_tablet_background_rounded.xml | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/be/scri/services/SimpleKeyboardIME.kt b/app/src/main/java/be/scri/services/SimpleKeyboardIME.kt index ffcbc23d..d4caa5d0 100644 --- a/app/src/main/java/be/scri/services/SimpleKeyboardIME.kt +++ b/app/src/main/java/be/scri/services/SimpleKeyboardIME.kt @@ -321,15 +321,15 @@ abstract class SimpleKeyboardIME( } } - fun getLastWordBeforeCursor(): String? { + fun getText(): String? { val inputConnection = currentInputConnection ?: return null + return inputConnection.getTextBeforeCursor(20, 0)?.toString() + } - val textBeforeCursor = inputConnection.getTextBeforeCursor(50, 0) ?: return null - + fun getLastWordBeforeCursor(): String? { + val textBeforeCursor = getText() ?: return null val trimmedText = textBeforeCursor.trim().toString() - val lastWord = trimmedText.split("\\s+".toRegex()).lastOrNull() - return lastWord } @@ -574,6 +574,7 @@ abstract class SimpleKeyboardIME( currentState: Boolean? = false, binding: KeyboardViewKeyboardBinding? = null, ) { + val wordBeforeCursor = getText() val inputConnection = currentInputConnection if (keyboard!!.mShiftState == SHIFT_ON_ONE_CHAR) { keyboard!!.mShiftState = SHIFT_OFF @@ -587,13 +588,28 @@ abstract class SimpleKeyboardIME( } else { val selectedText = inputConnection.getSelectedText(0) if (TextUtils.isEmpty(selectedText)) { - inputConnection.deleteSurroundingText(1, 0) + if (isEmoji(wordBeforeCursor)) { + inputConnection.deleteSurroundingText(2, 0) + } + else { + inputConnection.deleteSurroundingText(1, 0) + } } else { inputConnection.commitText("", 1) } } } + private fun isEmoji(word: String?): Boolean { + if (word.isNullOrEmpty() || word.length < 2) { + return false + } + + val lastTwoChars = word.substring(word.length - 2) + val emojiRegex = Regex("[\\uD83C\\uDF00-\\uD83E\\uDDFF]|[\\u2600-\\u26FF]|[\\u2700-\\u27BF]") + return emojiRegex.containsMatchIn(lastTwoChars) + } + fun handleElseCondition( code: Int, keyboardMode: Int, diff --git a/app/src/main/res/drawable/emoji_phone_background_rounded.xml b/app/src/main/res/drawable/emoji_phone_background_rounded.xml index 64976d43..49bc55e3 100644 --- a/app/src/main/res/drawable/emoji_phone_background_rounded.xml +++ b/app/src/main/res/drawable/emoji_phone_background_rounded.xml @@ -5,7 +5,7 @@ - + diff --git a/app/src/main/res/drawable/emoji_tablet_background_rounded.xml b/app/src/main/res/drawable/emoji_tablet_background_rounded.xml index 68bee71f..2d4b3126 100644 --- a/app/src/main/res/drawable/emoji_tablet_background_rounded.xml +++ b/app/src/main/res/drawable/emoji_tablet_background_rounded.xml @@ -5,7 +5,7 @@ - + From 94343f1aafdc37be80de26fe94a19ac770fa0f71 Mon Sep 17 00:00:00 2001 From: Linfye <3158203624@qq.com> Date: Sun, 27 Oct 2024 20:43:56 +0800 Subject: [PATCH 2/2] Fix lint issues --- app/src/main/java/be/scri/services/SimpleKeyboardIME.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/be/scri/services/SimpleKeyboardIME.kt b/app/src/main/java/be/scri/services/SimpleKeyboardIME.kt index d4caa5d0..d9fba932 100644 --- a/app/src/main/java/be/scri/services/SimpleKeyboardIME.kt +++ b/app/src/main/java/be/scri/services/SimpleKeyboardIME.kt @@ -590,8 +590,7 @@ abstract class SimpleKeyboardIME( if (TextUtils.isEmpty(selectedText)) { if (isEmoji(wordBeforeCursor)) { inputConnection.deleteSurroundingText(2, 0) - } - else { + } else { inputConnection.deleteSurroundingText(1, 0) } } else {