Skip to content

Commit

Permalink
Merge pull request #229 from Linfye/main
Browse files Browse the repository at this point in the history
Fix the Button Deletion Issue
  • Loading branch information
andrewtavis authored Oct 27, 2024
2 parents 43f71a8 + 94343f1 commit 7ead6c1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
27 changes: 21 additions & 6 deletions app/src/main/java/be/scri/services/SimpleKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -587,13 +588,27 @@ 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<layer-list>
<item android:id="@+id/button_background_shape">
<shape android:shape="rectangle">
<solid android:color="@android:color/holo_orange_light" />
<solid android:color="#80ffbb33" />
<corners android:radius="@dimen/command_button_corner_radius" />
</shape>
</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<layer-list>
<item android:id="@+id/button_background_shape">
<shape android:shape="rectangle">
<solid android:color="@android:color/holo_red_light" />
<solid android:color="#80ff4444" />
<corners android:radius="@dimen/command_button_corner_radius" />
</shape>
</item>
Expand Down

0 comments on commit 7ead6c1

Please sign in to comment.