Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Button Deletion Issue #229

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading