From b71ed8c7054984c6b34b94da99fc94b624078191 Mon Sep 17 00:00:00 2001 From: sspanak Date: Tue, 3 Sep 2024 18:39:00 +0300 Subject: [PATCH] fixed suggestion selection getting reset when toggling the text case of generated suggestions --- .../sspanak/tt9/ime/CommandHandler.java | 28 +++++++++---------- .../tt9/ime/helpers/SuggestionOps.java | 5 ++++ .../sspanak/tt9/ui/tray/SuggestionsBar.java | 6 ++++ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/CommandHandler.java b/app/src/main/java/io/github/sspanak/tt9/ime/CommandHandler.java index 416661f5..38b1fc2d 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/CommandHandler.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/CommandHandler.java @@ -153,29 +153,29 @@ abstract public class CommandHandler extends TextEditingHandler { protected void nextTextCase() { - String currentSuggestionBefore = suggestionOps.getCurrent(); - int currentSuggestionIndex = suggestionOps.getCurrentIndex(); - // When we are in AUTO mode and the dictionary word is in uppercase, // the mode would switch to UPPERCASE, but visually, the word would not change. // This is why we retry, until there is a visual change. for (int retries = 0; retries < 2 && mInputMode.nextTextCase(); retries++) { - String currentSuggestionAfter = mInputMode.getSuggestions().size() >= suggestionOps.getCurrentIndex() ? mInputMode.getSuggestions().get(suggestionOps.getCurrentIndex()) : ""; - // If the suggestions are special characters, changing the text case means selecting the - // next character group. Hence, "before" and "after" are different. Also, if the new suggestion - // list is shorter, the "before" index may be invalid, so "after" would be empty. - // In these cases, we scroll to the first one, for consistency. - if (currentSuggestionAfter.isEmpty() || !currentSuggestionBefore.equalsIgnoreCase(currentSuggestionAfter)) { - currentSuggestionIndex = 0; - break; - } + String firstSuggestionAfter = mInputMode.getSuggestions().get(0); - // the suggestion list is the same and the text case is different, so let's use it - if (!currentSuggestionBefore.equals(currentSuggestionAfter)) { + // this is a word and the text case has finally changed + if (!firstSuggestionAfter.equalsIgnoreCase(suggestionOps.get(0))) { break; } } + int currentSuggestionIndex = suggestionOps.getCurrentIndex(); + currentSuggestionIndex = suggestionOps.containsStem() ? currentSuggestionIndex - 1 : currentSuggestionIndex; + + // If the suggestions are special characters, changing the text case means selecting the + // next character group. Hence, "before" and "after" are different. Also, if the new suggestion + // list is shorter, the "before" index may be invalid, so "after" would be empty. + // In these cases, we scroll to the first one, for consistency. + if (!Character.isAlphabetic(mInputMode.getSuggestions().get(0).charAt(0))) { + currentSuggestionIndex = 0; + } + suggestionOps.set(mInputMode.getSuggestions(), currentSuggestionIndex, mInputMode.containsGeneratedSuggestions()); textField.setComposingText(suggestionOps.getCurrent()); } diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/helpers/SuggestionOps.java b/app/src/main/java/io/github/sspanak/tt9/ime/helpers/SuggestionOps.java index 28d95057..6cca6a96 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/helpers/SuggestionOps.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/helpers/SuggestionOps.java @@ -38,6 +38,11 @@ public class SuggestionOps { } + public boolean containsStem() { + return suggestionBar.containsStem(); + } + + public String get(int index) { return suggestionBar.getSuggestion(index); } diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/tray/SuggestionsBar.java b/app/src/main/java/io/github/sspanak/tt9/ui/tray/SuggestionsBar.java index d222e06f..0996e2ea 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/tray/SuggestionsBar.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/tray/SuggestionsBar.java @@ -110,6 +110,11 @@ public class SuggestionsBar { } + public boolean containsStem() { + return !stem.isEmpty(); + } + + public int getCurrentIndex() { return selectedIndex; } @@ -141,6 +146,7 @@ public class SuggestionsBar { setStem(newSuggestions, containsGenerated); addAllSuggestions(newSuggestions); + selectedIndex = Math.min(selectedIndex, suggestions.size() - 1); setSuggestionsOnScreen(); }