Improve behavior for language swipe cycling (#1319)

---------

Co-authored-by: Helium314 <helium314@mailbox.org>
This commit is contained in:
Md. Rifat Hasan Jihan 2025-02-03 02:46:11 +06:00 committed by GitHub
parent 82ec37f339
commit 274c6b0212
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 82 additions and 2 deletions

View file

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

View file

@ -1,6 +1,7 @@
package helium314.keyboard.keyboard package helium314.keyboard.keyboard
import android.view.KeyEvent import android.view.KeyEvent
import android.view.inputmethod.InputMethodSubtype
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode
import helium314.keyboard.latin.LatinIME import helium314.keyboard.latin.LatinIME
import helium314.keyboard.latin.RichInputMethodManager import helium314.keyboard.latin.RichInputMethodManager
@ -19,6 +20,10 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
private val settings = Settings.getInstance() private val settings = Settings.getInstance()
private var metaState = 0 // is this enough, or are there threading issues with the different PointerTrackers? private var metaState = 0 // is this enough, or are there threading issues with the different PointerTrackers?
// language slide state
private var initialSubtype: InputMethodSubtype? = null
private var subtypeSwitchCount = 0
// todo: maybe keep meta state presses to KeyboardActionListenerImpl, and avoid calls to press/release key // todo: maybe keep meta state presses to KeyboardActionListenerImpl, and avoid calls to press/release key
private fun adjustMetaState(code: Int, remove: Boolean) { private fun adjustMetaState(code: Int, remove: Boolean) {
val metaCode = when (code) { val metaCode = when (code) {
@ -84,6 +89,11 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
else -> false else -> false
} }
override fun onEndSpaceSwipe(){
initialSubtype = null
subtypeSwitchCount = 0
}
override fun toggleNumpad(withSliding: Boolean, forceReturnToAlpha: Boolean): Boolean { override fun toggleNumpad(withSliding: Boolean, forceReturnToAlpha: Boolean): Boolean {
KeyboardSwitcher.getInstance().toggleNumpad(withSliding, latinIME.currentAutoCapsState, latinIME.currentRecapitalizeState, forceReturnToAlpha) KeyboardSwitcher.getInstance().toggleNumpad(withSliding, latinIME.currentAutoCapsState, latinIME.currentRecapitalizeState, forceReturnToAlpha)
return true return true
@ -124,7 +134,7 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
} }
private fun onLanguageSlide(steps: Int): Boolean { 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) val subtypes = RichInputMethodManager.getInstance().getMyEnabledInputMethodSubtypeList(false)
if (subtypes.size <= 1) { // only allow if we have more than one subtype if (subtypes.size <= 1) { // only allow if we have more than one subtype
return false return false
@ -135,7 +145,18 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
wantedIndex %= subtypes.size wantedIndex %= subtypes.size
if (wantedIndex < 0) if (wantedIndex < 0)
wantedIndex += subtypes.size wantedIndex += subtypes.size
KeyboardSwitcher.getInstance().switchToSubtype(subtypes[wantedIndex]) val newSubtype = subtypes[wantedIndex]
// do not switch if we would switch to the initial subtype after cycling all other subtypes
if (initialSubtype == null)
initialSubtype = current
if (initialSubtype == newSubtype) {
if ((subtypeSwitchCount > 0 && steps > 0) || ((subtypeSwitchCount < 0 && steps < 0)))
return true
}
if (steps > 0) subtypeSwitchCount++ else subtypeSwitchCount--
KeyboardSwitcher.getInstance().switchToSubtype(newSubtype)
return true return true
} }

View file

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

View file

@ -25,6 +25,7 @@ import androidx.preference.PreferenceManager
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import helium314.keyboard.dictionarypack.DictionaryPackConstants import helium314.keyboard.dictionarypack.DictionaryPackConstants
import helium314.keyboard.keyboard.KeyboardActionListener
import helium314.keyboard.latin.utils.ChecksumCalculator import helium314.keyboard.latin.utils.ChecksumCalculator
import helium314.keyboard.keyboard.KeyboardLayoutSet import helium314.keyboard.keyboard.KeyboardLayoutSet
import helium314.keyboard.keyboard.KeyboardSwitcher import helium314.keyboard.keyboard.KeyboardSwitcher
@ -125,6 +126,8 @@ class AdvancedSettingsFragment : SubScreenFragment() {
} }
setupKeyLongpressTimeoutSettings() setupKeyLongpressTimeoutSettings()
setupEmojiSdkSetting() setupEmojiSdkSetting()
setupLanguageSwipeDistanceSettings()
updateLangSwipeDistanceVisibility(sharedPreferences)
findPreference<Preference>("load_gesture_library")?.setOnPreferenceClickListener { onClickLoadLibrary() } findPreference<Preference>("load_gesture_library")?.setOnPreferenceClickListener { onClickLoadLibrary() }
findPreference<Preference>("backup_restore")?.setOnPreferenceClickListener { showBackupRestoreDialog() } 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?) { override fun onSharedPreferenceChanged(prefs: SharedPreferences, key: String?) {
when (key) { when (key) {
Settings.PREF_SHOW_SETUP_WIZARD_ICON -> SystemBroadcastReceiver.toggleAppIcon(requireContext()) Settings.PREF_SHOW_SETUP_WIZARD_ICON -> SystemBroadcastReceiver.toggleAppIcon(requireContext())
Settings.PREF_MORE_POPUP_KEYS -> KeyboardLayoutSet.onSystemLocaleChanged() 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()) Settings.PREF_EMOJI_MAX_SDK -> KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
} }
} }

View file

@ -157,6 +157,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_MORE_POPUP_KEYS = "more_popup_keys"; 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_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_ENABLE_CLIPBOARD_HISTORY = "enable_clipboard_history";
public static final String PREF_CLIPBOARD_HISTORY_RETENTION_TIME = "clipboard_history_retention_time"; public static final String PREF_CLIPBOARD_HISTORY_RETENTION_TIME = "clipboard_history_retention_time";
@ -453,6 +454,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 5;
}
public static boolean readDeleteSwipeEnabled(final SharedPreferences prefs) { public static boolean readDeleteSwipeEnabled(final SharedPreferences prefs) {
return prefs.getBoolean(PREF_DELETE_SWIPE, true); return prefs.getBoolean(PREF_DELETE_SWIPE, true);
} }

View file

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

View file

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

View file

@ -958,6 +958,8 @@ 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 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 --> <!-- Title of the setting to customize toolbar key codes -->
<string name="customize_toolbar_key_codes">Customize toolbar key codes</string> <string name="customize_toolbar_key_codes">Customize toolbar key codes</string>
<!-- Confirmation message when resetting all custom toolbar key codes --> <!-- Confirmation message when resetting all custom toolbar key codes -->

View file

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