From 1e88f81e77896acd22ee7fab31625263a7e0e614 Mon Sep 17 00:00:00 2001 From: Dimo Karaivanov Date: Mon, 7 Nov 2022 16:03:55 +0200 Subject: [PATCH] selecting a word stem filter now also highlights the stem in the current word, to make it clear it is on --- .../github/sspanak/tt9/ime/TraditionalT9.java | 32 +++++++++++++----- src/io/github/sspanak/tt9/ime/Util.java | 33 +++++++++++++++++++ .../sspanak/tt9/ime/modes/InputMode.java | 1 + .../sspanak/tt9/ime/modes/ModePredictive.java | 8 +++++ 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 src/io/github/sspanak/tt9/ime/Util.java diff --git a/src/io/github/sspanak/tt9/ime/TraditionalT9.java b/src/io/github/sspanak/tt9/ime/TraditionalT9.java index af9756a0..e3849d5d 100644 --- a/src/io/github/sspanak/tt9/ime/TraditionalT9.java +++ b/src/io/github/sspanak/tt9/ime/TraditionalT9.java @@ -146,20 +146,25 @@ public class TraditionalT9 extends KeyPadHandler { protected boolean onUp() { if (previousSuggestion()) { mInputMode.setWordStem(mLanguage, mSuggestionView.getCurrentSuggestion(), true); + setComposingTextWithWordStemIndication(mSuggestionView.getCurrentSuggestion()); return true; } return false; } + + protected boolean onDown() { if (nextSuggestion()) { mInputMode.setWordStem(mLanguage, mSuggestionView.getCurrentSuggestion(), true); + setComposingTextWithWordStemIndication(mSuggestionView.getCurrentSuggestion()); return true; } return false; } + protected boolean onLeft() { if (mInputMode.clearWordStem()) { mInputMode.loadSuggestions(handleSuggestionsAsync, mLanguage, getComposingText()); @@ -170,6 +175,7 @@ public class TraditionalT9 extends KeyPadHandler { return true; } + protected boolean onRight(boolean repeat) { String filter = repeat ? mSuggestionView.getSuggestion(1) : getComposingText(); @@ -182,6 +188,7 @@ public class TraditionalT9 extends KeyPadHandler { return true; } + /** * onNumber * @@ -287,7 +294,7 @@ public class TraditionalT9 extends KeyPadHandler { } mSuggestionView.scrollToSuggestion(-1); - setComposingTextFromCurrentSuggestion(); + setComposingTextWithWordStemIndication(mSuggestionView.getCurrentSuggestion()); return true; } @@ -299,7 +306,7 @@ public class TraditionalT9 extends KeyPadHandler { } mSuggestionView.scrollToSuggestion(1); - setComposingTextFromCurrentSuggestion(); + setComposingTextWithWordStemIndication(mSuggestionView.getCurrentSuggestion()); return true; } @@ -312,7 +319,7 @@ public class TraditionalT9 extends KeyPadHandler { private void commitCurrentSuggestion(boolean entireSuggestion) { if (!isSuggestionViewHidden() && currentInputConnection != null) { if (entireSuggestion) { - setComposingTextFromCurrentSuggestion(); + setComposingText(mSuggestionView.getCurrentSuggestion()); } currentInputConnection.finishComposingText(); } @@ -346,7 +353,7 @@ public class TraditionalT9 extends KeyPadHandler { // for a more intuitive experience. String word = mSuggestionView.getCurrentSuggestion(); word = word.substring(0, Math.min(mInputMode.getSequenceLength(), word.length())); - setComposingText(word); + setComposingTextWithWordStemIndication(word); } @@ -391,20 +398,27 @@ public class TraditionalT9 extends KeyPadHandler { } - private void setComposingText(String text) { + private void setComposingText(CharSequence text) { if (text != null && currentInputConnection != null) { currentInputConnection.setComposingText(text, 1); } } - private void setComposingTextFromCurrentSuggestion() { - if (!isSuggestionViewHidden()) { - setComposingText(mSuggestionView.getCurrentSuggestion()); + private void setComposingTextWithWordStemIndication(CharSequence word) { + if (mInputMode.getWordStem().length() > 0) { + setComposingText(Util.highlightComposingText(word, 0, mInputMode.getWordStem().length())); + } else { + setComposingText(word); } } + private void refreshComposingText() { + setComposingText(getComposingText()); + } + + private void nextInputMode() { if (mEditing == EDITING_STRICT_NUMERIC || mEditing == EDITING_DIALER) { clearSuggestions(); @@ -416,7 +430,7 @@ public class TraditionalT9 extends KeyPadHandler { ArrayList switchedSuggestions = mInputMode.getSuggestions(mLanguage); setSuggestions(switchedSuggestions, mSuggestionView.getCurrentIndex()); - setComposingText(getComposingText()); // no mistake, this forces the new text case + refreshComposingText(); } // make "abc" and "ABC" separate modes from user perspective else if (mInputMode.isABC() && mInputMode.getTextCase() == InputMode.CASE_LOWER) { diff --git a/src/io/github/sspanak/tt9/ime/Util.java b/src/io/github/sspanak/tt9/ime/Util.java new file mode 100644 index 00000000..d9b6a297 --- /dev/null +++ b/src/io/github/sspanak/tt9/ime/Util.java @@ -0,0 +1,33 @@ +package io.github.sspanak.tt9.ime; + +import android.graphics.Typeface; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.style.StyleSpan; +import android.text.style.UnderlineSpan; + +import io.github.sspanak.tt9.Logger; + +public class Util { + public static CharSequence highlightComposingText(CharSequence word, int start, int end) { + if (end < start || start < 0) { + Logger.w("tt9.util.highlightComposingText", "Cannot highlight invalid composing text range: [" + start + ", " + end + "]"); + return word; + } + + SpannableString styledWord = new SpannableString(word); + + // default underline style + styledWord.setSpan(new UnderlineSpan(), 0, word.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); + + // highlight the requested range + styledWord.setSpan( + new StyleSpan(Typeface.BOLD), + start, + Math.min(word.length(), end), + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE + ); + + return styledWord; + } +} diff --git a/src/io/github/sspanak/tt9/ime/modes/InputMode.java b/src/io/github/sspanak/tt9/ime/modes/InputMode.java index 686ce02d..626c1e8c 100644 --- a/src/io/github/sspanak/tt9/ime/modes/InputMode.java +++ b/src/io/github/sspanak/tt9/ime/modes/InputMode.java @@ -102,6 +102,7 @@ abstract public class InputMode { // Stem filtering. // Where applicable, return "true" if the mode supports it and the operation was possible. public boolean clearWordStem() { return false; } + public String getWordStem() { return ""; } public boolean setWordStem(Language language, String stem, boolean exact) { return false; } public boolean shouldTrackNumPress() { return true; } diff --git a/src/io/github/sspanak/tt9/ime/modes/ModePredictive.java b/src/io/github/sspanak/tt9/ime/modes/ModePredictive.java index 7dff60a4..b07f51c5 100644 --- a/src/io/github/sspanak/tt9/ime/modes/ModePredictive.java +++ b/src/io/github/sspanak/tt9/ime/modes/ModePredictive.java @@ -156,6 +156,14 @@ public class ModePredictive extends InputMode { } } + /** + * getWordStem + * If "setWordStem()" has accepted a new stem by returning "true", it can be obtained using this. + */ + public String getWordStem() { + return stem; + } + /** * loadSuggestions