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

[Android] Fix Gboard enter detection in EnterPressedWatcher #30368

Closed
wants to merge 6 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter)
private var selEnd: Int = 0
var done = false

private var gboardReplacement: CharSequence? = null


override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {
val aztecText = aztecTextRef.get()
if (aztecText?.getAztecKeyListener() != null && !aztecText.isTextChangedListenerDisabled()) {
Expand All @@ -32,6 +35,13 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter)
this.start = start
this.selStart = aztecText.selectionStart
this.selEnd = aztecText.selectionEnd

if (selStart == selEnd && 0 < count && count < after) {
// possible gboard replacement detected
gboardReplacement = text.subSequence(start, start + count)
} else {
gboardReplacement = null
}
}
}

Expand All @@ -40,14 +50,25 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter)
val aztecKeyListener = aztecText?.getAztecKeyListener()
if (aztecText != null && !aztecText.isTextChangedListenerDisabled() && aztecKeyListener != null) {
val newTextCopy = SpannableStringBuilder(text)

var gboardOffset = this.start
// If gboard replacement is happening, we offset the start position by the length
// of the gboard replacement
if (gboardReplacement != null) {
val gboardRestored = newTextCopy.subSequence(start, start + before)
if (gboardRestored.toString() == gboardReplacement.toString()) {
gboardOffset += gboardRestored.length
}
}

// if new text length is longer than original text by 1
if (textBefore?.length == newTextCopy.length - 1) {
// now check that the inserted character is actually a NEWLINE
if (newTextCopy[this.start] == Constants.NEWLINE) {
if (newTextCopy[gboardOffset] == Constants.NEWLINE) {
done = false
aztecText.editableText.setSpan(EnterPressedUnderway(), 0, 0, Spanned.SPAN_USER)
aztecKeyListener.onEnterKey(
newTextCopy.replace(this.start, this.start + 1, ""),
newTextCopy.replace(gboardOffset, gboardOffset + 1, ""),
true,
selStart,
selEnd
Expand All @@ -61,9 +82,12 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter)
aztecTextRef.get()?.editableText?.getSpans(0, 0, EnterPressedUnderway::class.java)?.forEach {
if (!done) {
done = true
if (enterDeleter.shouldDeleteEnter())
text.replace(start, start + 1, "")
if (enterDeleter.shouldDeleteEnter()) {
var gboardOffset = start
gboardReplacement?.let { replacement -> gboardOffset += replacement.length }
text.replace(gboardOffset, gboardOffset + 1, "")
}
}
aztecTextRef.get()?.editableText?.removeSpan(it)
}
}
Expand Down