From 1586475ebbac41e28ce26cc83fb40ab108435299 Mon Sep 17 00:00:00 2001 From: codokie <@> Date: Thu, 11 Apr 2024 16:56:15 +0300 Subject: [PATCH 1/3] fix inline suggestions flickering --- .../helium314/keyboard/latin/LatinIME.java | 8 +++++-- .../suggestions/SuggestionStripView.java | 22 +++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/LatinIME.java b/app/src/main/java/helium314/keyboard/latin/LatinIME.java index 591aded8f..f54823de4 100644 --- a/app/src/main/java/helium314/keyboard/latin/LatinIME.java +++ b/app/src/main/java/helium314/keyboard/latin/LatinIME.java @@ -1020,6 +1020,10 @@ void onStartInputViewInternal(final EditorInfo editorInfo, final boolean restart // Space state must be updated before calling updateShiftState switcher.requestUpdatingShiftState(getCurrentAutoCapsState(), getCurrentRecapitalizeState()); } + // This will remove old "stuck" inline suggestions + if (hasSuggestionStripView()) { + mSuggestionStripView.setInlineSuggestionsView(null); + } // This will set the punctuation suggestions if next word suggestion is off; // otherwise it will clear the suggestion strip. setNeutralSuggestionStrip(); @@ -1309,7 +1313,7 @@ public InlineSuggestionsRequest onCreateInlineSuggestionsRequest(@NonNull Bundle public boolean onInlineSuggestionsResponse(InlineSuggestionsResponse response) { Log.d(TAG,"onInlineSuggestionsResponse called"); final List inlineSuggestions = response.getInlineSuggestions(); - if (inlineSuggestions.isEmpty()) { + if (inlineSuggestions.isEmpty() || mSuggestionStripView.isInlineAutofillSuggestionsVisible()) { return false; } @@ -1573,7 +1577,7 @@ private void setSuggestedWords(final SuggestedWords suggestedWords) { final SettingsValues currentSettingsValues = mSettings.getCurrent(); mInputLogic.setSuggestedWords(suggestedWords); // TODO: Modify this when we support suggestions with hard keyboard - if (!hasSuggestionStripView()) { + if (!hasSuggestionStripView() || mSuggestionStripView.isInlineAutofillSuggestionsVisible()) { return; } if (!onEvaluateInputViewShown()) { diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java index 84e1a39c6..0656cb5d2 100644 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java @@ -114,6 +114,7 @@ public interface Listener { private final SuggestionStripLayoutHelper mLayoutHelper; private final StripVisibilityGroup mStripVisibilityGroup; private boolean isInlineAutofillSuggestionsVisible = false; // Required to disable the more suggestions if inline autofill suggestions are visible + private View mCurrentInlineAutofillSuggestionsView; private static class StripVisibilityGroup { private final View mSuggestionStripView; @@ -259,7 +260,6 @@ private void updateKeys() { : km.isKeyguardLocked(); mToolbarExpandKey.setOnClickListener(hideToolbarKeys ? null : this); mPinnedKeys.setVisibility(hideToolbarKeys ? GONE : VISIBLE); - isInlineAutofillSuggestionsVisible = false; } public void setRtl(final boolean isRtlLanguage) { @@ -280,12 +280,24 @@ public void setSuggestions(final SuggestedWords suggestedWords, final boolean is mSuggestedWords = suggestedWords; mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions( getContext(), mSuggestedWords, mSuggestionsStrip, this); + setInlineSuggestionsView(mCurrentInlineAutofillSuggestionsView); } public void setInlineSuggestionsView(final View view) { - clear(); - isInlineAutofillSuggestionsVisible = true; - mSuggestionsStrip.addView(view); + if (isInlineAutofillSuggestionsVisible) { + mSuggestionsStrip.removeView(mCurrentInlineAutofillSuggestionsView); + isInlineAutofillSuggestionsVisible = false; + mCurrentInlineAutofillSuggestionsView = null; + } + if (view != null) { + isInlineAutofillSuggestionsVisible = true; + mSuggestionsStrip.addView(view); + mCurrentInlineAutofillSuggestionsView = view; + } + } + + public boolean isInlineAutofillSuggestionsVisible(){ + return isInlineAutofillSuggestionsVisible; } @Override @@ -303,6 +315,7 @@ public void setMoreSuggestionsHeight(final int remainingHeight) { @SuppressLint("ClickableViewAccessibility") // why would "null" need to call View#performClick? private void clear() { mSuggestionsStrip.removeAllViews(); + isInlineAutofillSuggestionsVisible = false; if (DEBUG_SUGGESTIONS) removeAllDebugInfoViews(); if (mToolbarContainer.getVisibility() != VISIBLE) @@ -497,6 +510,7 @@ private void removeSuggestion(TextView wordView) { mSuggestedWords.mSequenceNumber); mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions( getContext(), mSuggestedWords, mSuggestionsStrip, SuggestionStripView.this); + setInlineSuggestionsView(mCurrentInlineAutofillSuggestionsView); mStripVisibilityGroup.showSuggestionsStrip(); } From bb00437defd264dbbc4f0a05b1e8bf86fd982c92 Mon Sep 17 00:00:00 2001 From: codokie <@> Date: Fri, 12 Apr 2024 18:12:41 +0300 Subject: [PATCH 2/3] dismiss inline suggestions --- .../helium314/keyboard/latin/LatinIME.java | 19 ++++++-- .../suggestions/SuggestionStripView.java | 47 +++++++++++++------ 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/LatinIME.java b/app/src/main/java/helium314/keyboard/latin/LatinIME.java index f54823de4..bd94d4d93 100644 --- a/app/src/main/java/helium314/keyboard/latin/LatinIME.java +++ b/app/src/main/java/helium314/keyboard/latin/LatinIME.java @@ -1022,7 +1022,7 @@ void onStartInputViewInternal(final EditorInfo editorInfo, final boolean restart } // This will remove old "stuck" inline suggestions if (hasSuggestionStripView()) { - mSuggestionStripView.setInlineSuggestionsView(null); + mSuggestionStripView.resetInlineSuggestions(); } // This will set the punctuation suggestions if next word suggestion is off; // otherwise it will clear the suggestion strip. @@ -1312,8 +1312,13 @@ public InlineSuggestionsRequest onCreateInlineSuggestionsRequest(@NonNull Bundle @RequiresApi(api = Build.VERSION_CODES.R) public boolean onInlineSuggestionsResponse(InlineSuggestionsResponse response) { Log.d(TAG,"onInlineSuggestionsResponse called"); + // Ignore spammy requests if inline suggestions are already displayed or dismissed + if (!hasSuggestionStripView() || mSuggestionStripView.isInlineAutofillSuggestionsVisible() + || mSuggestionStripView.areInlineSuggestionsDismissed()) { + return false; + } final List inlineSuggestions = response.getInlineSuggestions(); - if (inlineSuggestions.isEmpty() || mSuggestionStripView.isInlineAutofillSuggestionsVisible()) { + if (inlineSuggestions.isEmpty()) { return false; } @@ -1577,7 +1582,7 @@ private void setSuggestedWords(final SuggestedWords suggestedWords) { final SettingsValues currentSettingsValues = mSettings.getCurrent(); mInputLogic.setSuggestedWords(suggestedWords); // TODO: Modify this when we support suggestions with hard keyboard - if (!hasSuggestionStripView() || mSuggestionStripView.isInlineAutofillSuggestionsVisible()) { + if (!hasSuggestionStripView()) { return; } if (!onEvaluateInputViewShown()) { @@ -1595,8 +1600,12 @@ private void setSuggestedWords(final SuggestedWords suggestedWords) { || currentSettingsValues.isApplicationSpecifiedCompletionsOn() // We should clear the contextual strip if there is no suggestion from dictionaries. || noSuggestionsFromDictionaries) { - mSuggestionStripView.setSuggestions(suggestedWords, - mRichImm.getCurrentSubtype().isRtlSubtype()); + if (mSuggestionStripView.isInlineAutofillSuggestionsVisible()){ + mSuggestionStripView.updateSuggestedWords(suggestedWords); + } else { + mSuggestionStripView.setSuggestions(suggestedWords, + mRichImm.getCurrentSubtype().isRtlSubtype()); + } } } diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java index 0656cb5d2..17c7c945f 100644 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java @@ -91,6 +91,7 @@ public interface Listener { private final Drawable mIncognitoIcon; private final Drawable mToolbarArrowIcon; private final Drawable mBinIcon; + private final Drawable mCloseIcon; private final ViewGroup mToolbar; private final View mToolbarContainer; private final ViewGroup mPinnedKeys; @@ -114,7 +115,7 @@ public interface Listener { private final SuggestionStripLayoutHelper mLayoutHelper; private final StripVisibilityGroup mStripVisibilityGroup; private boolean isInlineAutofillSuggestionsVisible = false; // Required to disable the more suggestions if inline autofill suggestions are visible - private View mCurrentInlineAutofillSuggestionsView; + private boolean areInlineSuggestionsDismissed = false; private static class StripVisibilityGroup { private final View mSuggestionStripView; @@ -191,6 +192,7 @@ public SuggestionStripView(final Context context, final AttributeSet attrs, fina mIncognitoIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconIncognitoKey); mToolbarArrowIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconToolbarKey); mBinIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconBin); + mCloseIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconClose); final LinearLayout.LayoutParams toolbarKeyLayoutParams = new LinearLayout.LayoutParams( getResources().getDimensionPixelSize(R.dimen.config_suggestions_strip_edge_key_width), @@ -250,7 +252,11 @@ private void updateKeys() { final View pinnedVoiceKey = mPinnedKeys.findViewWithTag(ToolbarKey.VOICE); if (pinnedVoiceKey != null) pinnedVoiceKey.setVisibility(currentSettingsValues.mShowsVoiceInputKey ? VISIBLE : GONE); - mToolbarExpandKey.setImageDrawable(currentSettingsValues.mIncognitoModeEnabled ? mIncognitoIcon : mToolbarArrowIcon); + if (isInlineAutofillSuggestionsVisible){ + mToolbarExpandKey.setImageDrawable(mCloseIcon); + } else { + mToolbarExpandKey.setImageDrawable(currentSettingsValues.mIncognitoModeEnabled ? mIncognitoIcon : mToolbarArrowIcon); + } mToolbarExpandKey.setScaleX((mToolbarContainer.getVisibility() != VISIBLE ? 1f : -1f) * mRtl); // hide pinned keys if device is locked, and avoid expanding toolbar @@ -273,6 +279,10 @@ public void setRtl(final boolean isRtlLanguage) { mStripVisibilityGroup.setLayoutDirection(layoutDirection); } + public void updateSuggestedWords(final SuggestedWords suggestedWords){ + mSuggestedWords = suggestedWords; + } + public void setSuggestions(final SuggestedWords suggestedWords, final boolean isRtlLanguage) { clear(); setRtl(isRtlLanguage); @@ -280,26 +290,29 @@ public void setSuggestions(final SuggestedWords suggestedWords, final boolean is mSuggestedWords = suggestedWords; mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions( getContext(), mSuggestedWords, mSuggestionsStrip, this); - setInlineSuggestionsView(mCurrentInlineAutofillSuggestionsView); } public void setInlineSuggestionsView(final View view) { - if (isInlineAutofillSuggestionsVisible) { - mSuggestionsStrip.removeView(mCurrentInlineAutofillSuggestionsView); - isInlineAutofillSuggestionsVisible = false; - mCurrentInlineAutofillSuggestionsView = null; - } - if (view != null) { - isInlineAutofillSuggestionsVisible = true; - mSuggestionsStrip.addView(view); - mCurrentInlineAutofillSuggestionsView = view; - } + isInlineAutofillSuggestionsVisible = true; + mSuggestionsStrip.addView(view, 0); + setToolbarVisibility(false); + updateKeys(); + } + + public void resetInlineSuggestions(){ + areInlineSuggestionsDismissed = false; + clear(); + updateKeys(); } public boolean isInlineAutofillSuggestionsVisible(){ return isInlineAutofillSuggestionsVisible; } + public boolean areInlineSuggestionsDismissed(){ + return areInlineSuggestionsDismissed; + } + @Override public void onVisibilityChanged(@NonNull final View view, final int visibility) { super.onVisibilityChanged(view, visibility); @@ -510,7 +523,6 @@ private void removeSuggestion(TextView wordView) { mSuggestedWords.mSequenceNumber); mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions( getContext(), mSuggestedWords, mSuggestionsStrip, SuggestionStripView.this); - setInlineSuggestionsView(mCurrentInlineAutofillSuggestionsView); mStripVisibilityGroup.showSuggestionsStrip(); } @@ -680,7 +692,12 @@ public void onClick(final View view) { } } if (view == mToolbarExpandKey) { - setToolbarVisibility(mToolbarContainer.getVisibility() != VISIBLE); + if (isInlineAutofillSuggestionsVisible) { + areInlineSuggestionsDismissed = true; + setSuggestions(mSuggestedWords, mRtl == -1); + } else { + setToolbarVisibility(mToolbarContainer.getVisibility() != VISIBLE); + } } From 37fbecd07720c473839b0e1132c2b6b6f0d2db0c Mon Sep 17 00:00:00 2001 From: codokie <151087174+codokie@users.noreply.github.com> Date: Sat, 1 Jun 2024 14:04:11 +0300 Subject: [PATCH 3/3] hide empty space when no toolbar keys are pinned --- app/src/main/res/layout/suggestions_strip.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/res/layout/suggestions_strip.xml b/app/src/main/res/layout/suggestions_strip.xml index 6c0131240..2883109d2 100644 --- a/app/src/main/res/layout/suggestions_strip.xml +++ b/app/src/main/res/layout/suggestions_strip.xml @@ -56,6 +56,7 @@