From a8b6a0b95acd63e8d39f65411621fe1da261fe39 Mon Sep 17 00:00:00 2001 From: sspanak Date: Thu, 1 May 2025 14:43:24 +0300 Subject: [PATCH] prevented an unnecessary call to getTextBeforeCursor() when Backspace is pressed --- .../io/github/sspanak/tt9/hacks/AppHacks.java | 13 ++++++++----- .../io/github/sspanak/tt9/ime/TypingHandler.java | 2 +- .../io/github/sspanak/tt9/ime/helpers/Key.java | 15 +++++++++++---- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/github/sspanak/tt9/hacks/AppHacks.java b/app/src/main/java/io/github/sspanak/tt9/hacks/AppHacks.java index 84294cc4..e3c337d2 100644 --- a/app/src/main/java/io/github/sspanak/tt9/hacks/AppHacks.java +++ b/app/src/main/java/io/github/sspanak/tt9/hacks/AppHacks.java @@ -5,10 +5,12 @@ import android.view.KeyEvent; import androidx.annotation.NonNull; import io.github.sspanak.tt9.ime.helpers.CursorOps; +import io.github.sspanak.tt9.ime.helpers.Key; import io.github.sspanak.tt9.ime.helpers.SuggestionOps; import io.github.sspanak.tt9.ime.helpers.TextField; import io.github.sspanak.tt9.ime.helpers.TextSelection; import io.github.sspanak.tt9.ime.modes.InputMode; +import io.github.sspanak.tt9.preferences.settings.SettingsStore; public class AppHacks { private final InputType inputType; @@ -41,17 +43,19 @@ public class AppHacks { * Performs extra Backspace operations and returns "false", or completely replaces Backspace and returns "true". When "true" is * returned, you must not attempt to delete text. This function has already done everything necessary. */ - public boolean onBackspace(InputMode inputMode) { + public boolean onBackspace(@NonNull SettingsStore settings, @NonNull InputMode inputMode) { if (inputType.isKindleInvertedTextField()) { inputMode.clearWordStem(); } else if (inputType.isTermux()) { return false; } - // When no text is selected and the cursor is at the beginning, - // allow double function keys to function normally (e.g. "Back" navigates back) + // When Backspace function is assigned to a different key (not hardware Backspace), we must + // allow the key to function normally if there is nothing to delete (e.g. "Back" navigates back). return - inputMode.getSuggestions().isEmpty() + Key.exists(settings.getKeyBackspace()) + && !Key.isHardwareBackspace(settings.getKeyBackspace()) + && inputMode.getSuggestions().isEmpty() && textSelection.isEmpty() && textField.getStringBeforeCursor(1).isEmpty(); } @@ -87,7 +91,6 @@ public class AppHacks { /** * Performs extra operations when the cursor moves and returns "true" if the selection was handled, * "false" otherwise. - * * CURSOR RESET * When sending messages using the Viber or the SMS app SEND button, it does so and clears the text * field, but without notifying the keyboard. This means, after sending the message, the InputMode diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/TypingHandler.java b/app/src/main/java/io/github/sspanak/tt9/ime/TypingHandler.java index ef740458..9efdc1fe 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/TypingHandler.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/TypingHandler.java @@ -115,7 +115,7 @@ public abstract class TypingHandler extends KeyPadHandler { return false; } - if (appHacks.onBackspace(mInputMode)) { + if (appHacks.onBackspace(settings, mInputMode)) { mInputMode.reset(); return false; } diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/helpers/Key.java b/app/src/main/java/io/github/sspanak/tt9/ime/helpers/Key.java index 74b2add1..7c0bb3eb 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/helpers/Key.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/helpers/Key.java @@ -40,11 +40,18 @@ public class Key { } + public static boolean exists(int keyCode) { + return keyCode != KeyEvent.KEYCODE_UNKNOWN; + } + + public static boolean isBackspace(SettingsStore settings, int keyCode) { - return - keyCode == KeyEvent.KEYCODE_DEL - || keyCode == KeyEvent.KEYCODE_CLEAR - || keyCode == settings.getKeyBackspace(); + return isHardwareBackspace(keyCode) || keyCode == settings.getKeyBackspace(); + } + + + public static boolean isHardwareBackspace(int keyCode) { + return keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_CLEAR; }