From a2651cea62afd1895ef03561e7a9bc4500e3373b Mon Sep 17 00:00:00 2001 From: sspanak Date: Thu, 19 Sep 2024 15:16:15 +0300 Subject: [PATCH] Increased the hold duration for virtual keys that also support swiping. It was extremely short and uncontrollable on fast devices --- .../tt9/ui/main/keys/SoftKeyBackspace.java | 41 +++++++++++++++++++ .../tt9/ui/main/keys/SwipeableKey.java | 34 +++++++-------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java index d83c561a..4663c971 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java @@ -24,18 +24,54 @@ public class SoftKeyBackspace extends SwipeableKey { super(context, attrs, defStyleAttr); } + + private boolean isFastDeleteOn() { + return tt9 != null && tt9.getSettings().getBackspaceAcceleration(); + } + + + /** + * When fast-delete is on, decrease the hold duration threshold for smoother operation. + */ + @Override + protected float getHoldDurationThreshold() { + return isFastDeleteOn() ? SettingsStore.SOFT_KEY_REPEAT_DELAY * 3 : super.getHoldDurationThreshold(); + } + + + /** + * When fast-delete is on, prevent swapping that does nothing. It may feel frustrating if the user + * moves their finger slightly and the key does not delete anything. + */ + @Override + protected float getSwipeXThreshold(Context context) { + return isFastDeleteOn() ? super.getSwipeXThreshold(context) : Integer.MAX_VALUE; + } + + + /** + * Disable vertical swiping for backspace key. + */ + @Override + protected float getSwipeYThreshold(Context context) { + return Integer.MAX_VALUE; + } + + @Override final protected boolean handlePress() { super.handlePress(); return deleteText(); } + @Override final protected void handleHold() { repeat++; deleteText(); } + @Override final protected boolean handleRelease() { vibrate(repeat > 0 ? Vibration.getReleaseVibration() : Vibration.getNoVibration()); @@ -43,6 +79,7 @@ public class SoftKeyBackspace extends SwipeableKey { return true; } + @Override protected void handleEndSwipeX(float position, float delta) { if (validateTT9Handler()) { @@ -50,6 +87,7 @@ public class SoftKeyBackspace extends SwipeableKey { } } + private boolean deleteText() { if (validateTT9Handler() && !tt9.onBackspace(repeat)) { // Limited or special numeric field (e.g. formatted money or dates) cannot always return @@ -62,16 +100,19 @@ public class SoftKeyBackspace extends SwipeableKey { return false; } + @Override protected int getNoEmojiTitle() { return R.string.virtual_key_del; } + @Override protected String getTitle() { return LanguageKind.isRTL(tt9 != null ? tt9.getLanguage() : null) ? "⌦" : "⌫"; } + @Override public void render() { super.render(); diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SwipeableKey.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SwipeableKey.java index 2ba886e0..30c16dd8 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SwipeableKey.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SwipeableKey.java @@ -25,36 +25,30 @@ abstract public class SwipeableKey extends SoftKey { public SwipeableKey(Context context) { super(context); - init(context); + resetTimeThresholds(context); } public SwipeableKey(Context context, AttributeSet attrs) { super(context, attrs); - init(context); + resetTimeThresholds(context); } public SwipeableKey(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); - init(context); + resetTimeThresholds(context); } - private void init(Context context) { - if (HOLD_DURATION_THRESHOLD == 0) { - HOLD_DURATION_THRESHOLD = getHoldDurationThreshold(); - } - if (SWIPE_X_THRESHOLD == 0) { - SWIPE_X_THRESHOLD = getSwipeXThreshold(context); - } - if (SWIPE_Y_THRESHOLD == 0) { - SWIPE_Y_THRESHOLD = getSwipeYThreshold(context); - } + protected final void resetTimeThresholds(Context context) { + HOLD_DURATION_THRESHOLD = getHoldDurationThreshold(); + SWIPE_X_THRESHOLD = getSwipeXThreshold(context); + SWIPE_Y_THRESHOLD = getSwipeYThreshold(context); } - protected float getHoldDurationThreshold() { return SettingsStore.SOFT_KEY_REPEAT_DELAY * 3; } + protected float getHoldDurationThreshold() { return SettingsStore.SOFT_KEY_REPEAT_DELAY * 9; } protected float getSwipeXThreshold(Context context) { return context.getResources().getDimensionPixelSize(R.dimen.numpad_key_height) / 10.0f; } protected float getSwipeYThreshold(Context context) { return context.getResources().getDimensionPixelSize(R.dimen.numpad_key_height) / 10.0f; } @@ -80,11 +74,11 @@ abstract public class SwipeableKey extends SoftKey { @Override public boolean onLongClick(View view) { if (System.currentTimeMillis() - startTime < HOLD_DURATION_THRESHOLD) { - return true; + return false; } isHolding = !isSwipingY && !isSwipingX; - return isSwipingY || isSwipingX || super.onLongClick(view); + return !isHolding || super.onLongClick(view); } @@ -143,4 +137,12 @@ abstract public class SwipeableKey extends SoftKey { protected void handleEndSwipeX(float position, float delta) {} protected void handleEndSwipeY(float position, float delta) {} protected boolean notSwiped() { return notSwiped; } + + + @Override + public void render() { + // readjust the action detection delays for keys that set them dynamically + resetTimeThresholds(getContext()); + super.render(); + } }