Skip to content

Commit

Permalink
Reverting delete key hacks. (#1183)
Browse files Browse the repository at this point in the history
- Context #1178
  • Loading branch information
dessalines authored Dec 8, 2024
1 parent 4fddba8 commit 50c3aae
Showing 1 changed file with 4 additions and 44 deletions.
48 changes: 4 additions & 44 deletions app/src/main/java/com/dessalines/thumbkey/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import com.dessalines.thumbkey.db.LayoutsUpdate
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.text.BreakIterator
import java.text.NumberFormat
import kotlin.math.PI
import kotlin.math.abs
Expand Down Expand Up @@ -362,20 +361,11 @@ fun performKeyAction(
ime.currentInputConnection.sendKeyEvent(ev)
}

// Some apps are having problems with delete key events, and issues need to be opened up
// on their repos.
is KeyAction.DeleteKeyAction -> {
if (ime.currentInputConnection.getSelectedText(0)?.isEmpty() != false) {
val textBeforeCursor = ime.currentInputConnection.getTextBeforeCursor(20, 0)?.toString()
if (textBeforeCursor.isNullOrEmpty()) {
return
}

val lastClusterBoundary = findLastGraphemeClusterBoundary(textBeforeCursor)
if (lastClusterBoundary != -1) {
ime.currentInputConnection.deleteSurroundingText(textBeforeCursor.length - lastClusterBoundary, 0)
}
} else {
ime.currentInputConnection.commitText("", 0)
}
val ev = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)
ime.currentInputConnection.sendKeyEvent(ev)
}

is KeyAction.DeleteWordBeforeCursor -> {
Expand Down Expand Up @@ -1375,33 +1365,3 @@ fun updateLayouts(
),
)
}

fun findLastGraphemeClusterBoundary(text: String): Int {
val boundary = BreakIterator.getCharacterInstance()
boundary.setText(text)

val end = text.length
var lastBoundary = boundary.preceding(end) // Find the previous grapheme boundary

// Check if this boundary is part of a ZWJ sequence
while (lastBoundary > 0 && isPartOfZWJSequence(text, lastBoundary)) {
lastBoundary = boundary.preceding(lastBoundary)
}

return lastBoundary
}

fun isPartOfZWJSequence(
text: String,
index: Int,
): Boolean {
if (index <= 0 || index >= text.length) return false

// Check if the character before this boundary is a ZWJ
val previousChar = text[index - 1]
if (previousChar.code == 0x200D) { // Zero-Width Joiner
return true
}

return false
}

0 comments on commit 50c3aae

Please sign in to comment.