Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve language swipe cycling #656

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public interface KeyboardActionListener {
*/
boolean onHorizontalSpaceSwipe(int steps);
boolean onVerticalSpaceSwipe(int steps);
void resetSubtypeSwitchCount();

void onMoveDeletePointer(int steps);
void onUpWithDeletePointerActive();
Expand Down Expand Up @@ -142,6 +143,8 @@ public boolean onVerticalSpaceSwipe(int steps) {
return false;
}
@Override
public void resetSubtypeSwitchCount() {}
@Override
public void onMoveDeletePointer(int steps) {}
@Override
public void onUpWithDeletePointerActive() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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
Expand Down Expand Up @@ -96,9 +97,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
Expand All @@ -108,9 +109,14 @@ 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
}

override fun resetSubtypeSwitchCount(){
mSubtypeSwitchCount = 0
}

private fun onMoveCursorVertically(steps: Int): Boolean {
if (steps == 0) return false
val code = if (steps < 0) KeyCode.ARROW_UP else KeyCode.ARROW_DOWN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ private void onUpEventInternal(final int x, final int y, final long eventTime) {
if (mInHorizontalSwipe || mInVerticalSwipe) {
mInHorizontalSwipe = false;
mInVerticalSwipe = false;
sListener.resetSubtypeSwitchCount();
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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
Expand Down Expand Up @@ -126,6 +127,8 @@ class AdvancedSettingsFragment : SubScreenFragment() {
removePreference("load_gesture_library")
}
setupKeyLongpressTimeoutSettings()
setupLanguageSwipeDistanceSettings()
updateLangSwipeDistanceVisibility(sharedPreferences)
findPreference<Preference>("load_gesture_library")?.setOnPreferenceClickListener { onClickLoadLibrary() }
findPreference<Preference>("backup_restore")?.setOnPreferenceClickListener { showBackupRestoreDialog() }

Expand Down Expand Up @@ -469,10 +472,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)
}
}

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/helium314/keyboard/latin/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,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";
Expand Down Expand Up @@ -377,6 +378,18 @@ public static int readVerticalSpaceSwipe(final SharedPreferences prefs) {
};
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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;
Expand Down Expand Up @@ -204,6 +205,7 @@ public SettingsValues(final Context context, final SharedPreferences prefs, fina
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);
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/config-common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,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>
<!-- Toast message shown when content is copied to the clipboard -->
<string name="toast_msg_clipboard_copy">Content copied</string>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/xml/prefs_screen_advanced.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down