auto show / hide toolbar (#674)

Co-authored-by: codokie <@>
Co-authored-by: Helium314 <helium314@disroot.org>
Co-authored-by: Helium314 <helium314@mailbox.org>
This commit is contained in:
codokie 2024-06-01 15:32:12 +03:00 committed by GitHub
parent c048ff6ff6
commit b9451e4f09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 63 additions and 5 deletions

View file

@ -353,6 +353,10 @@ public class LatinIME extends InputMethodService implements
return hasMessages(MSG_UPDATE_SUGGESTION_STRIP); return hasMessages(MSG_UPDATE_SUGGESTION_STRIP);
} }
public boolean hasPendingResumeSuggestions() {
return hasMessages(MSG_RESUME_SUGGESTIONS);
}
public boolean hasPendingReopenDictionaries() { public boolean hasPendingReopenDictionaries() {
return hasMessages(MSG_REOPEN_DICTIONARIES); return hasMessages(MSG_REOPEN_DICTIONARIES);
} }
@ -989,7 +993,9 @@ public class LatinIME extends InputMethodService implements
// initialSelStart and initialSelEnd sometimes are lying. Make a best effort to // initialSelStart and initialSelEnd sometimes are lying. Make a best effort to
// work around this bug. // work around this bug.
mInputLogic.mConnection.tryFixLyingCursorPosition(); mInputLogic.mConnection.tryFixLyingCursorPosition();
mHandler.postResumeSuggestions(true /* shouldDelay */); if (mInputLogic.mConnection.isCursorTouchingWord(currentSettingsValues.mSpacingAndPunctuations, true)) {
mHandler.postResumeSuggestions(true /* shouldDelay */);
}
needToCallLoadKeyboardLater = false; needToCallLoadKeyboardLater = false;
} }
} else { } else {
@ -1020,9 +1026,13 @@ public class LatinIME extends InputMethodService implements
} }
// This will set the punctuation suggestions if next word suggestion is off; // This will set the punctuation suggestions if next word suggestion is off;
// otherwise it will clear the suggestion strip. // otherwise it will clear the suggestion strip.
setNeutralSuggestionStrip(); if (!mHandler.hasPendingResumeSuggestions()) {
mHandler.cancelUpdateSuggestionStrip();
mHandler.cancelUpdateSuggestionStrip(); setNeutralSuggestionStrip();
if (hasSuggestionStripView() && currentSettingsValues.mAutoShowToolbar) {
mSuggestionStripView.setToolbarVisibility(true);
}
}
mainKeyboardView.setMainDictionaryAvailability(mDictionaryFacilitator.hasAtLeastOneInitializedMainDictionary()); mainKeyboardView.setMainDictionaryAvailability(mDictionaryFacilitator.hasAtLeastOneInitializedMainDictionary());
mainKeyboardView.setKeyPreviewPopupEnabled(currentSettingsValues.mKeyPreviewPopupOn); mainKeyboardView.setKeyPreviewPopupEnabled(currentSettingsValues.mKeyPreviewPopupOn);
@ -1593,6 +1603,10 @@ public class LatinIME extends InputMethodService implements
|| noSuggestionsFromDictionaries) { || noSuggestionsFromDictionaries) {
mSuggestionStripView.setSuggestions(suggestedWords, mSuggestionStripView.setSuggestions(suggestedWords,
mRichImm.getCurrentSubtype().isRtlSubtype()); mRichImm.getCurrentSubtype().isRtlSubtype());
// Auto hide the toolbar if dictionary suggestions are available
if (currentSettingsValues.mAutoHideToolbar && !noSuggestionsFromDictionaries) {
mSuggestionStripView.setToolbarVisibility(false);
}
} }
} }
@ -1634,6 +1648,8 @@ public class LatinIME extends InputMethodService implements
// This will show either an empty suggestion strip (if prediction is enabled) or // This will show either an empty suggestion strip (if prediction is enabled) or
// punctuation suggestions (if it's disabled). // punctuation suggestions (if it's disabled).
// The toolbar will be shown automatically if the relevant setting is enabled
// and there is a selection of text or it's the start of a line.
@Override @Override
public void setNeutralSuggestionStrip() { public void setNeutralSuggestionStrip() {
final SettingsValues currentSettings = mSettings.getCurrent(); final SettingsValues currentSettings = mSettings.getCurrent();
@ -1641,6 +1657,14 @@ public class LatinIME extends InputMethodService implements
? SuggestedWords.getEmptyInstance() ? SuggestedWords.getEmptyInstance()
: currentSettings.mSpacingAndPunctuations.mSuggestPuncList; : currentSettings.mSpacingAndPunctuations.mSuggestPuncList;
setSuggestedWords(neutralSuggestions); setSuggestedWords(neutralSuggestions);
if (hasSuggestionStripView() && currentSettings.mAutoShowToolbar) {
final int codePointBeforeCursor = mInputLogic.mConnection.getCodePointBeforeCursor();
if (mInputLogic.mConnection.hasSelection()
|| codePointBeforeCursor == Constants.NOT_A_CODE
|| codePointBeforeCursor == Constants.CODE_ENTER) {
mSuggestionStripView.setToolbarVisibility(true);
}
}
} }
@Override @Override

View file

@ -149,6 +149,8 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_QUICK_PIN_TOOLBAR_KEYS = "quick_pin_toolbar_keys"; public static final String PREF_QUICK_PIN_TOOLBAR_KEYS = "quick_pin_toolbar_keys";
public static final String PREF_PINNED_TOOLBAR_KEYS = "pinned_toolbar_keys"; public static final String PREF_PINNED_TOOLBAR_KEYS = "pinned_toolbar_keys";
public static final String PREF_TOOLBAR_KEYS = "toolbar_keys"; public static final String PREF_TOOLBAR_KEYS = "toolbar_keys";
public static final String PREF_AUTO_SHOW_TOOLBAR = "auto_show_toolbar";
public static final String PREF_AUTO_HIDE_TOOLBAR = "auto_hide_toolbar";
public static final String PREF_CLIPBOARD_TOOLBAR_KEYS = "clipboard_toolbar_keys"; public static final String PREF_CLIPBOARD_TOOLBAR_KEYS = "clipboard_toolbar_keys";
// Emoji // Emoji

View file

@ -107,6 +107,8 @@ public class SettingsValues {
public final float mKeyboardHeightScale; public final float mKeyboardHeightScale;
public final boolean mUrlDetectionEnabled; public final boolean mUrlDetectionEnabled;
public final float mBottomPaddingScale; public final float mBottomPaddingScale;
public final boolean mAutoShowToolbar;
public final boolean mAutoHideToolbar;
// From the input box // From the input box
@NonNull @NonNull
@ -241,6 +243,8 @@ public class SettingsValues {
mSpacingAndPunctuations = new SpacingAndPunctuations(res, mUrlDetectionEnabled); mSpacingAndPunctuations = new SpacingAndPunctuations(res, mUrlDetectionEnabled);
mBottomPaddingScale = prefs.getFloat(Settings.PREF_BOTTOM_PADDING_SCALE, DEFAULT_SIZE_SCALE); mBottomPaddingScale = prefs.getFloat(Settings.PREF_BOTTOM_PADDING_SCALE, DEFAULT_SIZE_SCALE);
mLongPressSymbolsForNumpad = prefs.getBoolean(Settings.PREFS_LONG_PRESS_SYMBOLS_FOR_NUMPAD, false); mLongPressSymbolsForNumpad = prefs.getBoolean(Settings.PREFS_LONG_PRESS_SYMBOLS_FOR_NUMPAD, false);
mAutoShowToolbar = prefs.getBoolean(Settings.PREF_AUTO_SHOW_TOOLBAR, true);
mAutoHideToolbar = readSuggestionsEnabled(prefs) && prefs.getBoolean(Settings.PREF_AUTO_HIDE_TOOLBAR, true);
mHasCustomFunctionalLayout = CustomLayoutUtilsKt.hasCustomFunctionalLayout(selectedSubtype, context); mHasCustomFunctionalLayout = CustomLayoutUtilsKt.hasCustomFunctionalLayout(selectedSubtype, context);
} }

View file

@ -257,7 +257,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
? km.isDeviceLocked() ? km.isDeviceLocked()
: km.isKeyguardLocked(); : km.isKeyguardLocked();
mToolbarExpandKey.setOnClickListener(hideToolbarKeys ? null : this); mToolbarExpandKey.setOnClickListener(hideToolbarKeys ? null : this);
mPinnedKeys.setVisibility(hideToolbarKeys ? GONE : VISIBLE); mPinnedKeys.setVisibility(hideToolbarKeys ? GONE : mSuggestionsStrip.getVisibility());
isInlineAutofillSuggestionsVisible = false; isInlineAutofillSuggestionsVisible = false;
} }
@ -285,6 +285,8 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
clear(); clear();
isInlineAutofillSuggestionsVisible = true; isInlineAutofillSuggestionsVisible = true;
mSuggestionsStrip.addView(view); mSuggestionsStrip.addView(view);
if (Settings.getInstance().getCurrent().mAutoHideToolbar)
setToolbarVisibility(false);
} }
@Override @Override
@ -482,6 +484,10 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions( mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions(
getContext(), mSuggestedWords, mSuggestionsStrip, SuggestionStripView.this); getContext(), mSuggestedWords, mSuggestionsStrip, SuggestionStripView.this);
mStripVisibilityGroup.showSuggestionsStrip(); mStripVisibilityGroup.showSuggestionsStrip();
// Show the toolbar if no suggestions are left and the "Auto show toolbar" setting is enabled
if (mSuggestedWords.isEmpty() && Settings.getInstance().getCurrent().mAutoShowToolbar){
setToolbarVisibility(true);
}
} }
boolean showMoreSuggestions() { boolean showMoreSuggestions() {

View file

@ -830,6 +830,14 @@ New dictionary:
<string name="var_toolbar_direction">Variable toolbar direction</string> <string name="var_toolbar_direction">Variable toolbar direction</string>
<!-- Description of the variable toolbar direction setting --> <!-- Description of the variable toolbar direction setting -->
<string name="var_toolbar_direction_summary">Reverse direction when a right-to-left keyboard subtype is selected</string> <string name="var_toolbar_direction_summary">Reverse direction when a right-to-left keyboard subtype is selected</string>
<!-- Title of the setting for showing the toolbar automatically -->
<string name="auto_show_toolbar">Auto show toolbar</string>
<!-- Description of the setting for showing the toolbar automatically -->
<string name="auto_show_toolbar_summary">Show the toolbar if input starts or text is selected</string>
<!-- Title of the setting for hiding the toolbar automatically -->
<string name="auto_hide_toolbar">Auto hide toolbar</string>
<!-- Description of the setting for hiding the toolbar automatically -->
<string name="auto_hide_toolbar_summary">Hide the toolbar when suggestions become available</string>
<!-- Toast message shown when content is copied to the clipboard --> <!-- Toast message shown when content is copied to the clipboard -->
<string name="toast_msg_clipboard_copy">Content copied</string> <string name="toast_msg_clipboard_copy">Content copied</string>
</resources> </resources>

View file

@ -108,6 +108,20 @@
android:defaultValue="@bool/config_center_suggestion_text_to_enter" android:defaultValue="@bool/config_center_suggestion_text_to_enter"
android:persistent="true" /> android:persistent="true" />
<SwitchPreference
android:key="auto_show_toolbar"
android:title="@string/auto_show_toolbar"
android:summary="@string/auto_show_toolbar_summary"
android:defaultValue="true"
android:persistent="true" />
<SwitchPreference
android:key="auto_hide_toolbar"
android:title="@string/auto_hide_toolbar"
android:summary="@string/auto_hide_toolbar_summary"
android:defaultValue="true"
android:persistent="true" />
<SwitchPreference <SwitchPreference
android:key="use_contacts" android:key="use_contacts"
android:title="@string/use_contacts_dict" android:title="@string/use_contacts_dict"