Improve behavior for language swipe cycling

Implement logic to stop cycling through Switch languages swipe when all subtypes are cycled.
This commit is contained in:
Md. Rifat Hasan Jihan 2025-01-23 18:57:54 +06:00
parent 453683a925
commit 63651969a5
10 changed files with 72 additions and 2 deletions

View file

@ -97,6 +97,7 @@ public interface KeyboardActionListener {
*/
boolean onHorizontalSpaceSwipe(int steps);
boolean onVerticalSpaceSwipe(int steps);
void resetSubtypeSwitchCount();
boolean toggleNumpad(boolean withSliding, boolean forceReturnToAlpha);
void onMoveDeletePointer(int steps);
@ -148,6 +149,8 @@ public interface KeyboardActionListener {
return false;
}
@Override
public void resetSubtypeSwitchCount() {}
@Override
public void onMoveDeletePointer(int steps) {}
@Override
public void onUpWithDeletePointerActive() {}

View file

@ -17,6 +17,7 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
private val keyboardSwitcher = KeyboardSwitcher.getInstance()
private val settings = Settings.getInstance()
private var mSubtypeSwitchCount = 0 // for use with onLanguageSlide()
private var metaState = 0 // is this enough, or are there threading issues with the different PointerTrackers?
// todo: maybe keep meta state presses to KeyboardActionListenerImpl, and avoid calls to press/release key
@ -84,6 +85,10 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
else -> false
}
override fun resetSubtypeSwitchCount(){
mSubtypeSwitchCount = 0
}
override fun toggleNumpad(withSliding: Boolean, forceReturnToAlpha: Boolean): Boolean {
KeyboardSwitcher.getInstance().toggleNumpad(withSliding, latinIME.currentAutoCapsState, latinIME.currentRecapitalizeState, forceReturnToAlpha)
return true
@ -124,9 +129,9 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
}
private fun onLanguageSlide(steps: Int): Boolean {
if (abs(steps) < 4) return false
if (abs(steps) < settings.current.mLanguageSwipeDistance) return false
val subtypes = RichInputMethodManager.getInstance().getMyEnabledInputMethodSubtypeList(false)
if (subtypes.size <= 1) { // only allow if we have more than one subtype
if (subtypes.size - 1 <= abs(mSubtypeSwitchCount)) { // only allow if we are yet to cycle through all subtypes
return false
}
// decide next or previous dependent on up or down
@ -136,6 +141,7 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
if (wantedIndex < 0)
wantedIndex += subtypes.size
KeyboardSwitcher.getInstance().switchToSubtype(subtypes[wantedIndex])
if (steps > 0) mSubtypeSwitchCount++ else mSubtypeSwitchCount--
return true
}

View file

@ -1076,6 +1076,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
if (mInHorizontalSwipe || mInVerticalSwipe) {
mInHorizontalSwipe = false;
mInVerticalSwipe = false;
sListener.resetSubtypeSwitchCount();
return;
}
}

View file

@ -25,6 +25,7 @@ import androidx.preference.PreferenceManager
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import helium314.keyboard.dictionarypack.DictionaryPackConstants
import helium314.keyboard.keyboard.KeyboardActionListener
import helium314.keyboard.latin.utils.ChecksumCalculator
import helium314.keyboard.keyboard.KeyboardLayoutSet
import helium314.keyboard.keyboard.KeyboardSwitcher
@ -125,6 +126,8 @@ class AdvancedSettingsFragment : SubScreenFragment() {
}
setupKeyLongpressTimeoutSettings()
setupEmojiSdkSetting()
setupLanguageSwipeDistanceSettings()
updateLangSwipeDistanceVisibility(sharedPreferences)
findPreference<Preference>("load_gesture_library")?.setOnPreferenceClickListener { onClickLoadLibrary() }
findPreference<Preference>("backup_restore")?.setOnPreferenceClickListener { showBackupRestoreDialog() }
@ -560,10 +563,37 @@ class AdvancedSettingsFragment : SubScreenFragment() {
})
}
private fun setupLanguageSwipeDistanceSettings() {
val prefs = sharedPreferences
findPreference<SeekBarDialogPreference>(Settings.PREF_LANGUAGE_SWIPE_DISTANCE)?.setInterface(object : ValueProxy {
override fun writeValue(value: Int, key: String) = prefs.edit().putInt(key, value).apply()
override fun writeDefaultValue(key: String) = prefs.edit().remove(key).apply()
override fun readValue(key: String) = Settings.readLanguageSwipeDistance(prefs, resources)
override fun readDefaultValue(key: String) = Settings.readDefaultLanguageSwipeDistance(resources)
override fun getValueText(value: Int) = value.toString()
override fun feedbackValue(value: Int) {}
})
}
private fun updateLangSwipeDistanceVisibility(prefs: SharedPreferences) {
val horizontalSpaceSwipe = Settings.readHorizontalSpaceSwipe(prefs)
val verticalSpaceSwipe = Settings.readVerticalSpaceSwipe(prefs)
val visibility = horizontalSpaceSwipe == KeyboardActionListener.SWIPE_SWITCH_LANGUAGE
|| verticalSpaceSwipe == KeyboardActionListener.SWIPE_SWITCH_LANGUAGE
setPreferenceVisible(Settings.PREF_LANGUAGE_SWIPE_DISTANCE, visibility)
}
override fun onSharedPreferenceChanged(prefs: SharedPreferences, key: String?) {
when (key) {
Settings.PREF_SHOW_SETUP_WIZARD_ICON -> SystemBroadcastReceiver.toggleAppIcon(requireContext())
Settings.PREF_MORE_POPUP_KEYS -> KeyboardLayoutSet.onSystemLocaleChanged()
Settings.PREF_SPACE_HORIZONTAL_SWIPE -> updateLangSwipeDistanceVisibility(prefs)
Settings.PREF_SPACE_VERTICAL_SWIPE -> updateLangSwipeDistanceVisibility(prefs)
Settings.PREF_EMOJI_MAX_SDK -> KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
}
}

View file

@ -151,6 +151,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_MORE_POPUP_KEYS = "more_popup_keys";
public static final String PREF_SPACE_TO_CHANGE_LANG = "prefs_long_press_keyboard_to_change_lang";
public static final String PREF_LANGUAGE_SWIPE_DISTANCE = "language_swipe_distance";
public static final String PREF_ENABLE_CLIPBOARD_HISTORY = "enable_clipboard_history";
public static final String PREF_CLIPBOARD_HISTORY_RETENTION_TIME = "clipboard_history_retention_time";
@ -447,6 +448,18 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
};
}
public static int readLanguageSwipeDistance(final SharedPreferences prefs,
final Resources res) {
final int sensitivity = prefs.getInt(
PREF_LANGUAGE_SWIPE_DISTANCE, UNDEFINED_PREFERENCE_VALUE_INT);
return (sensitivity != UNDEFINED_PREFERENCE_VALUE_INT) ? sensitivity
: readDefaultLanguageSwipeDistance(res);
}
public static int readDefaultLanguageSwipeDistance(final Resources res) {
return res.getInteger(R.integer.config_default_language_swipe_distance);
}
public static boolean readDeleteSwipeEnabled(final SharedPreferences prefs) {
return prefs.getBoolean(PREF_DELETE_SWIPE, true);
}

View file

@ -80,6 +80,7 @@ public class SettingsValues {
public final boolean mBlockPotentiallyOffensive;
public final int mSpaceSwipeHorizontal;
public final int mSpaceSwipeVertical;
public final int mLanguageSwipeDistance;
public final boolean mDeleteSwipeEnabled;
public final boolean mAutospaceAfterPunctuationEnabled;
public final boolean mClipboardHistoryEnabled;
@ -227,6 +228,7 @@ public class SettingsValues {
mDisplayOrientation = res.getConfiguration().orientation;
mSpaceSwipeHorizontal = Settings.readHorizontalSpaceSwipe(prefs);
mSpaceSwipeVertical = Settings.readVerticalSpaceSwipe(prefs);
mLanguageSwipeDistance = Settings.readLanguageSwipeDistance(prefs, res);
mDeleteSwipeEnabled = Settings.readDeleteSwipeEnabled(prefs);
mAutospaceAfterPunctuationEnabled = Settings.readAutospaceAfterPunctuationEnabled(prefs);
mClipboardHistoryEnabled = Settings.readClipboardHistoryEnabled(prefs);

View file

@ -377,6 +377,7 @@
<string name="prefs_long_press_symbol_for_numpad">নম্বর প্যাডের জন্য প্রতীক বোতামে দীর্ঘ চাপ</string>
<string name="var_toolbar_direction">টুলবারের পরিবর্তনশীল দিক</string>
<string name="var_toolbar_direction_summary">ডান থেকে বাম কিবোর্ড নির্বাচন করলে দিক বিপরীত হবে</string>
<string name="prefs_language_swipe_distance">ভাষা পরিবর্তন অভিস্পর্শের দূরত্ব</string>
<string name="subtype_generic_student"><xliff:g id="LANGUAGE_NAME" example="Russian">%s</xliff:g> (শিক্ষার্থী)</string>
<string name="language_switch_key_behavior">ভাষা পরিবর্তন বোতামের আচরণ</string>
<string name="pinned_toolbar_keys">পিন করে রাখা টুলবার বোতাম নির্বাচন</string>

View file

@ -30,6 +30,11 @@
<bool name="config_default_vibration_enabled">false</bool>
<integer name="config_max_vibration_duration">100</integer>
<integer name="config_default_language_swipe_distance">10</integer>
<integer name="config_min_language_swipe_distance">2</integer>
<integer name="config_max_language_swipe_distance">18</integer>
<integer name="config_language_swipe_distance_step">1</integer>
<integer name="config_default_longpress_key_timeout">300</integer>
<integer name="config_max_longpress_timeout">700</integer>
<integer name="config_min_longpress_timeout">100</integer>

View file

@ -946,6 +946,8 @@ New dictionary:
<string name="var_toolbar_direction">Variable toolbar direction</string>
<!-- 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>
<!-- Title of the settings for adjusting the language swipe gesture distance -->
<string name="prefs_language_swipe_distance">Switch language swipe distance</string>
<!-- Title of the setting to customize toolbar key codes -->
<string name="customize_toolbar_key_codes">Customize toolbar key codes</string>
<!-- Confirmation message when resetting all custom toolbar key codes -->

View file

@ -43,6 +43,13 @@
android:title="@string/show_vertical_space_swipe"
latin:singleLineTitle="false" />
<helium314.keyboard.latin.settings.SeekBarDialogPreference
android:key="language_swipe_distance"
android:title="@string/prefs_language_swipe_distance"
latin:minValue="@integer/config_min_language_swipe_distance"
latin:maxValue="@integer/config_max_language_swipe_distance"
latin:stepValue="@integer/config_language_swipe_distance_step" />
<SwitchPreference
android:key="delete_swipe"
android:title="@string/delete_swipe"