1
0
Fork 0

selecting a word stem filter now also highlights the stem in the current word, to make it clear it is on

This commit is contained in:
Dimo Karaivanov 2022-11-07 16:03:55 +02:00
parent acc9d1c8c4
commit 1e88f81e77
4 changed files with 65 additions and 9 deletions

View file

@ -146,20 +146,25 @@ public class TraditionalT9 extends KeyPadHandler {
protected boolean onUp() { protected boolean onUp() {
if (previousSuggestion()) { if (previousSuggestion()) {
mInputMode.setWordStem(mLanguage, mSuggestionView.getCurrentSuggestion(), true); mInputMode.setWordStem(mLanguage, mSuggestionView.getCurrentSuggestion(), true);
setComposingTextWithWordStemIndication(mSuggestionView.getCurrentSuggestion());
return true; return true;
} }
return false; return false;
} }
protected boolean onDown() { protected boolean onDown() {
if (nextSuggestion()) { if (nextSuggestion()) {
mInputMode.setWordStem(mLanguage, mSuggestionView.getCurrentSuggestion(), true); mInputMode.setWordStem(mLanguage, mSuggestionView.getCurrentSuggestion(), true);
setComposingTextWithWordStemIndication(mSuggestionView.getCurrentSuggestion());
return true; return true;
} }
return false; return false;
} }
protected boolean onLeft() { protected boolean onLeft() {
if (mInputMode.clearWordStem()) { if (mInputMode.clearWordStem()) {
mInputMode.loadSuggestions(handleSuggestionsAsync, mLanguage, getComposingText()); mInputMode.loadSuggestions(handleSuggestionsAsync, mLanguage, getComposingText());
@ -170,6 +175,7 @@ public class TraditionalT9 extends KeyPadHandler {
return true; return true;
} }
protected boolean onRight(boolean repeat) { protected boolean onRight(boolean repeat) {
String filter = repeat ? mSuggestionView.getSuggestion(1) : getComposingText(); String filter = repeat ? mSuggestionView.getSuggestion(1) : getComposingText();
@ -182,6 +188,7 @@ public class TraditionalT9 extends KeyPadHandler {
return true; return true;
} }
/** /**
* onNumber * onNumber
* *
@ -287,7 +294,7 @@ public class TraditionalT9 extends KeyPadHandler {
} }
mSuggestionView.scrollToSuggestion(-1); mSuggestionView.scrollToSuggestion(-1);
setComposingTextFromCurrentSuggestion(); setComposingTextWithWordStemIndication(mSuggestionView.getCurrentSuggestion());
return true; return true;
} }
@ -299,7 +306,7 @@ public class TraditionalT9 extends KeyPadHandler {
} }
mSuggestionView.scrollToSuggestion(1); mSuggestionView.scrollToSuggestion(1);
setComposingTextFromCurrentSuggestion(); setComposingTextWithWordStemIndication(mSuggestionView.getCurrentSuggestion());
return true; return true;
} }
@ -312,7 +319,7 @@ public class TraditionalT9 extends KeyPadHandler {
private void commitCurrentSuggestion(boolean entireSuggestion) { private void commitCurrentSuggestion(boolean entireSuggestion) {
if (!isSuggestionViewHidden() && currentInputConnection != null) { if (!isSuggestionViewHidden() && currentInputConnection != null) {
if (entireSuggestion) { if (entireSuggestion) {
setComposingTextFromCurrentSuggestion(); setComposingText(mSuggestionView.getCurrentSuggestion());
} }
currentInputConnection.finishComposingText(); currentInputConnection.finishComposingText();
} }
@ -346,7 +353,7 @@ public class TraditionalT9 extends KeyPadHandler {
// for a more intuitive experience. // for a more intuitive experience.
String word = mSuggestionView.getCurrentSuggestion(); String word = mSuggestionView.getCurrentSuggestion();
word = word.substring(0, Math.min(mInputMode.getSequenceLength(), word.length())); 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) { if (text != null && currentInputConnection != null) {
currentInputConnection.setComposingText(text, 1); currentInputConnection.setComposingText(text, 1);
} }
} }
private void setComposingTextFromCurrentSuggestion() { private void setComposingTextWithWordStemIndication(CharSequence word) {
if (!isSuggestionViewHidden()) { if (mInputMode.getWordStem().length() > 0) {
setComposingText(mSuggestionView.getCurrentSuggestion()); setComposingText(Util.highlightComposingText(word, 0, mInputMode.getWordStem().length()));
} else {
setComposingText(word);
} }
} }
private void refreshComposingText() {
setComposingText(getComposingText());
}
private void nextInputMode() { private void nextInputMode() {
if (mEditing == EDITING_STRICT_NUMERIC || mEditing == EDITING_DIALER) { if (mEditing == EDITING_STRICT_NUMERIC || mEditing == EDITING_DIALER) {
clearSuggestions(); clearSuggestions();
@ -416,7 +430,7 @@ public class TraditionalT9 extends KeyPadHandler {
ArrayList<String> switchedSuggestions = mInputMode.getSuggestions(mLanguage); ArrayList<String> switchedSuggestions = mInputMode.getSuggestions(mLanguage);
setSuggestions(switchedSuggestions, mSuggestionView.getCurrentIndex()); setSuggestions(switchedSuggestions, mSuggestionView.getCurrentIndex());
setComposingText(getComposingText()); // no mistake, this forces the new text case refreshComposingText();
} }
// make "abc" and "ABC" separate modes from user perspective // make "abc" and "ABC" separate modes from user perspective
else if (mInputMode.isABC() && mInputMode.getTextCase() == InputMode.CASE_LOWER) { else if (mInputMode.isABC() && mInputMode.getTextCase() == InputMode.CASE_LOWER) {

View file

@ -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;
}
}

View file

@ -102,6 +102,7 @@ abstract public class InputMode {
// Stem filtering. // Stem filtering.
// Where applicable, return "true" if the mode supports it and the operation was possible. // Where applicable, return "true" if the mode supports it and the operation was possible.
public boolean clearWordStem() { return false; } public boolean clearWordStem() { return false; }
public String getWordStem() { return ""; }
public boolean setWordStem(Language language, String stem, boolean exact) { return false; } public boolean setWordStem(Language language, String stem, boolean exact) { return false; }
public boolean shouldTrackNumPress() { return true; } public boolean shouldTrackNumPress() { return true; }

View file

@ -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 * loadSuggestions