From 81d422fdc62e19ded7c19313157546c655992ca3 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Wed, 13 Sep 2023 18:09:53 +0200 Subject: [PATCH] Show delete button when long-pressing suggestion (#148) remove old style of removing suggestions via popup menu --- .../suggestions/SuggestionStripView.java | 145 ++++++++++-------- app/src/main/res/values-bn/strings.xml | 1 - app/src/main/res/values-fr/strings.xml | 1 - app/src/main/res/values/strings.xml | 2 - 4 files changed, 83 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/suggestions/SuggestionStripView.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/suggestions/SuggestionStripView.java index 76af30ab3..7ac291776 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/suggestions/SuggestionStripView.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/suggestions/SuggestionStripView.java @@ -23,6 +23,7 @@ import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.drawable.Drawable; +import android.text.TextUtils; import android.util.AttributeSet; import android.util.TypedValue; import android.view.GestureDetector; @@ -58,8 +59,10 @@ import org.dslul.openboard.inputmethod.latin.suggestions.MoreSuggestionsView.Mor import org.dslul.openboard.inputmethod.latin.utils.DialogUtils; import java.util.ArrayList; +import java.util.concurrent.atomic.AtomicBoolean; import androidx.appcompat.widget.PopupMenu; +import androidx.core.content.ContextCompat; import androidx.core.view.ViewCompat; public final class SuggestionStripView extends RelativeLayout implements OnClickListener, @@ -305,71 +308,89 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick } AudioAndHapticFeedbackManager.getInstance().performHapticAndAudioFeedback( Constants.NOT_A_CODE, this); - if (isShowingMoreSuggestionPanel() || !showMoreSuggestions()) { - for (int i = 0; i <= mStartIndexOfMoreSuggestions; i++) { - if (view == mWordViews.get(i)) { - showDeleteSuggestionDialog(mWordViews.get(i)); - return true; + if (view instanceof TextView && mWordViews.contains(view)) { + final Drawable icon = ContextCompat.getDrawable(getContext(), R.drawable.ic_delete); + icon.setColorFilter(Settings.getInstance().getCurrent().mColors.getKeyTextFilter()); + int w = icon.getIntrinsicWidth(); + int h = icon.getIntrinsicWidth(); + final TextView wordView = (TextView) view; + wordView.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); + wordView.setEllipsize(TextUtils.TruncateAt.END); + AtomicBoolean downOk = new AtomicBoolean(false); + wordView.setOnTouchListener((view1, motionEvent) -> { + if (motionEvent.getAction() == MotionEvent.ACTION_UP) { + final float x = motionEvent.getX(); + final float y = motionEvent.getY(); + if (0 < x && x < w && 0 < y && y < h) { + removeSuggestion(wordView); + wordView.cancelLongPress(); + wordView.setPressed(false); + return true; + } + } else if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { + final float x = motionEvent.getX(); + final float y = motionEvent.getY(); + if (0 < x && x < w && 0 < y && y < h) { + downOk.set(true); + } + } + return false; + }); + if (BuildConfig.DEBUG && (isShowingMoreSuggestionPanel() || !showMoreSuggestions())) { + showSourceDict(wordView); + return true; + } else return showMoreSuggestions(); + } else return showMoreSuggestions(); + } + + private void showSourceDict(final TextView wordView) { + final String word = wordView.getText().toString(); + final int index; + if (wordView.getTag() instanceof Integer) { + index = (int) wordView.getTag(); + } else return; + if (index >= mSuggestedWords.size()) return; + final SuggestedWordInfo info = mSuggestedWords.getInfo(index); + if (!info.getWord().equals(word)) return; + final String text = info.mSourceDict.mDictType + ":" + info.mSourceDict.mLocale; + // apparently toast is not working on some Android versions, probably + // Android 13 with the notification permission + // Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show(); + final PopupMenu uglyWorkaround = new PopupMenu(DialogUtils.getPlatformDialogThemeContext(getContext()), wordView); + uglyWorkaround.getMenu().add(Menu.NONE, 1, Menu.NONE, text); + uglyWorkaround.show(); + } + + private void removeSuggestion(TextView wordView) { + final String word = wordView.getText().toString(); + mListener.removeSuggestion(word); + mMoreSuggestionsView.dismissMoreKeysPanel(); + // show suggestions, but without the removed word + final ArrayList sw = new ArrayList<>(); + for (int i = 0; i < mSuggestedWords.size(); i ++) { + final SuggestedWordInfo info = mSuggestedWords.getInfo(i); + if (!info.getWord().equals(word)) + sw.add(info); + } + ArrayList rs = null; + if (mSuggestedWords.mRawSuggestions != null) { + rs = mSuggestedWords.mRawSuggestions; + for (int i = 0; i < rs.size(); i ++) { + if (rs.get(i).getWord().equals(word)) { + rs.remove(i); + break; } } } - return true; - } - - private void showDeleteSuggestionDialog(final TextView wordView) { - final String word = wordView.getText().toString(); - - final PopupMenu menu = new PopupMenu(DialogUtils.getPlatformDialogThemeContext(getContext()), wordView); - menu.getMenu().add(Menu.NONE, 1, Menu.NONE, R.string.remove_suggestions); - if (BuildConfig.DEBUG) - menu.getMenu().add(Menu.NONE, 2, Menu.NONE, "Show source dictionary"); - menu.setOnMenuItemClickListener(menuItem -> { - if (menuItem.getItemId() == 2) { - for (int i = 0; i < mSuggestedWords.size(); i ++) { - final SuggestedWordInfo info = mSuggestedWords.getInfo(i); - if (info.getWord().equals(word)) { - final String text = info.mSourceDict.mDictType + ":" + info.mSourceDict.mLocale; - // apparently toast is not working on some Android versions, probably - // Android 13 with the notification permission - // Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show(); - final PopupMenu uglyWorkaround = new PopupMenu(DialogUtils.getPlatformDialogThemeContext(getContext()), wordView); - uglyWorkaround.getMenu().add(Menu.NONE, 1, Menu.NONE, text); - uglyWorkaround.show(); - } - } - return true; - } - mListener.removeSuggestion(word); - mMoreSuggestionsView.dismissMoreKeysPanel(); - // show suggestions, but without the removed word - final ArrayList sw = new ArrayList<>(); - for (int i = 0; i < mSuggestedWords.size(); i ++) { - final SuggestedWordInfo info = mSuggestedWords.getInfo(i); - if (!info.getWord().equals(word)) - sw.add(info); - } - ArrayList rs = null; - if (mSuggestedWords.mRawSuggestions != null) { - rs = mSuggestedWords.mRawSuggestions; - for (int i = 0; i < rs.size(); i ++) { - if (rs.get(i).getWord().equals(word)) { - rs.remove(i); - break; - } - } - } - // copied code from setSuggestions, but without the Rtl part - clear(); - mSuggestedWords = new SuggestedWords(sw, rs, mSuggestedWords.getTypedWordInfo(), - mSuggestedWords.mTypedWordValid, mSuggestedWords.mWillAutoCorrect, - mSuggestedWords.mIsObsoleteSuggestions, mSuggestedWords.mInputStyle, - mSuggestedWords.mSequenceNumber); - mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions( - getContext(), mSuggestedWords, mSuggestionsStrip, SuggestionStripView.this); - mStripVisibilityGroup.showSuggestionsStrip(); - return true; - }); - menu.show(); + // copied code from setSuggestions, but without the Rtl part + clear(); + mSuggestedWords = new SuggestedWords(sw, rs, mSuggestedWords.getTypedWordInfo(), + mSuggestedWords.mTypedWordValid, mSuggestedWords.mWillAutoCorrect, + mSuggestedWords.mIsObsoleteSuggestions, mSuggestedWords.mInputStyle, + mSuggestedWords.mSequenceNumber); + mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions( + getContext(), mSuggestedWords, mSuggestionsStrip, SuggestionStripView.this); + mStripVisibilityGroup.showSuggestionsStrip(); } boolean showMoreSuggestions() { diff --git a/app/src/main/res/values-bn/strings.xml b/app/src/main/res/values-bn/strings.xml index 9647a1d17..d04090d5f 100644 --- a/app/src/main/res/values-bn/strings.xml +++ b/app/src/main/res/values-bn/strings.xml @@ -75,7 +75,6 @@ অতি আগ্রাসী পরবর্তী শব্দের পরামর্শ পরামর্শ দিতে পূর্ববর্তী শব্দ ব্যবহার - শব্দের প্রস্তাবনা অপসারণ অঙ্গুলিহেলন টাইপিং সক্ষম করুন বর্ণের মাঝে স্লাইড করে শব্দ সন্নিবেশ অঙ্গুলিহেলনের শেষাংশ প্রদর্শন diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 6d47edea8..e925d5a3d 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -60,7 +60,6 @@ "Très proactive" "Suggestions pour le mot suivant" "Utilise le mot précédent pour les suggestions" - Supprimer suggestion "Activer la saisie gestuelle" "Saisit un mot en faisant glisser le doigt sur les lettres" "Afficher le tracé du geste" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dcc915075..b7260e320 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -138,8 +138,6 @@ Next-word suggestions Use the previous word in making suggestions - - Remove suggestion Enable gesture typing