Suppress InputLogic selection updates during transaction

This commit is contained in:
Aleksandras Kostarevas 2024-03-21 22:38:33 -05:00
parent 21da28ff22
commit ce92ea6845
2 changed files with 20 additions and 15 deletions

View File

@ -111,6 +111,14 @@ public final class InputLogic {
// Note: This does not have a composing span, so it must be handled separately. // Note: This does not have a composing span, so it must be handled separately.
private String mWordBeingCorrectedByCursor = null; private String mWordBeingCorrectedByCursor = null;
private boolean isLogicSuppressedByExternalLogic = false;
public void startSuppressingLogic() {
isLogicSuppressedByExternalLogic = true;
}
public void endSuppressingLogic() {
isLogicSuppressedByExternalLogic = false;
}
/** /**
* Create a new instance of the input logic. * Create a new instance of the input logic.
* @param latinIMELegacy the instance of the parent LatinIME. We should remove this when we can. * @param latinIMELegacy the instance of the parent LatinIME. We should remove this when we can.
@ -354,6 +362,8 @@ public final class InputLogic {
*/ */
public boolean onUpdateSelection(final int oldSelStart, final int oldSelEnd, public boolean onUpdateSelection(final int oldSelStart, final int oldSelEnd,
final int newSelStart, final int newSelEnd, final SettingsValues settingsValues) { final int newSelStart, final int newSelEnd, final SettingsValues settingsValues) {
if(isLogicSuppressedByExternalLogic) return false;
if (mConnection.isBelatedExpectedUpdate(oldSelStart, newSelStart, oldSelEnd, newSelEnd)) { if (mConnection.isBelatedExpectedUpdate(oldSelStart, newSelStart, oldSelEnd, newSelEnd)) {
return false; return false;
} }

View File

@ -6,7 +6,6 @@ import android.content.Intent
import android.os.Build import android.os.Build
import android.os.VibrationEffect import android.os.VibrationEffect
import android.os.Vibrator import android.os.Vibrator
import android.util.Log
import android.view.View import android.view.View
import android.view.WindowManager import android.view.WindowManager
import android.view.inputmethod.InlineSuggestionsResponse import android.view.inputmethod.InlineSuggestionsResponse
@ -65,30 +64,27 @@ import org.futo.inputmethod.updates.retrieveSavedLastUpdateCheckResult
private class LatinIMEActionInputTransaction( private class LatinIMEActionInputTransaction(
private val inputLogic: InputLogic, private val inputLogic: InputLogic,
shouldApplySpace: Boolean shouldApplySpace: Boolean,
private val context: Context
): ActionInputTransaction { ): ActionInputTransaction {
private val isSpaceNecessary: Boolean private val isSpaceNecessary: Boolean
private var isFinished = false private var isFinished = false
init { init {
val priorText = inputLogic.mConnection.getTextBeforeCursor(1, 0) val priorText = inputLogic.mConnection.getTextBeforeCursor(1, 0)
isSpaceNecessary = shouldApplySpace && !priorText.isNullOrEmpty() && !priorText.last().isWhitespace() isSpaceNecessary = shouldApplySpace && !priorText.isNullOrEmpty() && !priorText.last().isWhitespace()
Log.i("LatinIMEActionInputTransaction", "Beginning batch edit due to input transaction") inputLogic.startSuppressingLogic()
inputLogic.mConnection.beginBatchEdit()
}
private fun finish() {
isFinished = true
inputLogic.mConnection.endBatchEdit()
Log.i("LatinIMEActionInputTransaction", "Ending batch edit due to input transaction having been finished or cancelled")
} }
private fun transformText(text: String): String { private fun transformText(text: String): String {
return if(isSpaceNecessary) { " $text" } else { text } return if(isSpaceNecessary) { " $text" } else { text }
} }
private var previousText = ""
override fun updatePartial(text: String) { override fun updatePartial(text: String) {
if(isFinished) return if(isFinished) return
previousText = text
inputLogic.mConnection.setComposingText( inputLogic.mConnection.setComposingText(
transformText(text), transformText(text),
1 1
@ -97,17 +93,16 @@ private class LatinIMEActionInputTransaction(
override fun commit(text: String) { override fun commit(text: String) {
if(isFinished) return if(isFinished) return
isFinished = true
inputLogic.mConnection.commitText( inputLogic.mConnection.commitText(
transformText(text), transformText(text),
1 1
) )
finish() inputLogic.endSuppressingLogic()
} }
override fun cancel() { override fun cancel() {
if(isFinished) return commit(previousText)
inputLogic.mConnection.finishComposingText()
finish()
} }
} }
@ -125,7 +120,7 @@ class UixActionKeyboardManager(val uixManager: UixManager, val latinIME: LatinIM
} }
override fun createInputTransaction(applySpaceIfNeeded: Boolean): ActionInputTransaction { override fun createInputTransaction(applySpaceIfNeeded: Boolean): ActionInputTransaction {
return LatinIMEActionInputTransaction(latinIME.inputLogic, applySpaceIfNeeded) return LatinIMEActionInputTransaction(latinIME.inputLogic, applySpaceIfNeeded, latinIME)
} }
override fun typeText(v: String) { override fun typeText(v: String) {