Skip to content

Commit

Permalink
create flexible onEndSwipe method
Browse files Browse the repository at this point in the history
  • Loading branch information
devycarol committed Jul 15, 2024
1 parent c0c11d7 commit 0d79d1a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public interface KeyboardActionListener {
boolean toggleNumpad(boolean withSliding, boolean forceReturnToAlpha);

void onMoveDeletePointer(int steps);
void onUpWithDeletePointerActive();
void onEndSwipe(int code, boolean vertical);
void resetMetaState();

KeyboardActionListener EMPTY_LISTENER = new Adapter();
Expand Down Expand Up @@ -150,7 +150,7 @@ public boolean toggleNumpad(boolean withSliding, boolean forceReturnToAlpha) {
@Override
public void onMoveDeletePointer(int steps) {}
@Override
public void onUpWithDeletePointerActive() {}
public void onEndSwipe(int code, boolean vertical) {}
@Override
public void resetMetaState() {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import helium314.keyboard.latin.settings.Settings
import kotlin.math.abs

class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inputLogic: InputLogic) : KeyboardActionListener {

private val keyboardSwitcher = KeyboardSwitcher.getInstance()
private val settings = Settings.getInstance()
private var metaState = 0 // is this enough, or are there threading issues with the different PointerTrackers?
Expand Down Expand Up @@ -89,32 +88,39 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp

override fun onMoveDeletePointer(steps: Int) {
inputLogic.finishInput()
val end = inputLogic.mConnection.expectedSelectionEnd
val inputConnection = inputLogic.mConnection
val end = inputConnection.expectedSelectionEnd
var actualSteps = 0 // corrected steps to avoid splitting chars belonging to the same codepoint
if (steps > 0) {
val text = inputLogic.mConnection.getSelectedText(0)
val text = inputConnection.getSelectedText(0)
if (text == null) actualSteps = steps
else loopOverCodePoints(text) {
actualSteps += Character.charCount(it)
actualSteps >= steps
}
} else {
val text = inputLogic.mConnection.getTextBeforeCursor(-steps * 4, 0)
val text = inputConnection.getTextBeforeCursor(-steps * 4, 0)
if (text == null) actualSteps = steps
else loopOverCodePointsBackwards(text) {
actualSteps -= Character.charCount(it)
actualSteps <= steps
}
}
val start = inputLogic.mConnection.expectedSelectionStart + actualSteps
val start = inputConnection.expectedSelectionStart + actualSteps
if (start > end) return
inputLogic.mConnection.setSelection(start, end)
inputConnection.setSelection(start, end)
}

override fun onUpWithDeletePointerActive() {
if (!inputLogic.mConnection.hasSelection()) return
inputLogic.finishInput()
onCodeInput(KeyCode.DELETE, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE, false)
override fun onEndSwipe(code: Int, vertical: Boolean) {
when (code) {
// todo: for space, toggle layouts here and not at the beginning of the swipe
// should prevent layout changes when doing android's "swipe down for notifications"
KeyCode.DELETE -> {
if (!inputLogic.mConnection.hasSelection()) return
inputLogic.finishInput()
onCodeInput(KeyCode.DELETE, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE, false)
}
}
}

override fun resetMetaState() {
Expand Down Expand Up @@ -148,10 +154,11 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
if (rawSteps == 0) return false
// for RTL languages we want to invert pointer movement
val steps = if (RichInputMethodManager.getInstance().currentSubtype.isRtlSubtype) -rawSteps else rawSteps
val inputConnection = inputLogic.mConnection
val moveSteps: Int
if (steps < 0) {
var actualSteps = 0 // corrected steps to avoid splitting chars belonging to the same codepoint
val text = inputLogic.mConnection.getTextBeforeCursor(-steps * 4, 0) ?: return false
val text = inputConnection.getTextBeforeCursor(-steps * 4, 0) ?: return false
loopOverCodePointsBackwards(text) {
if (StringUtils.mightBeEmoji(it)) {
actualSteps = 0
Expand All @@ -171,7 +178,7 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
}
} else {
var actualSteps = 0 // corrected steps to avoid splitting chars belonging to the same codepoint
val text = inputLogic.mConnection.getTextAfterCursor(steps * 4, 0) ?: return false
val text = inputConnection.getTextAfterCursor(steps * 4, 0) ?: return false
loopOverCodePoints(text) {
if (StringUtils.mightBeEmoji(it)) {
actualSteps = 0
Expand All @@ -191,13 +198,13 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
if (inputLogic.moveCursorByAndReturnIfInsideComposingWord(moveSteps)) {
// no need to finish input and restart suggestions if we're still in the word
// this is a noticeable performance improvement
val newPosition = inputLogic.mConnection.expectedSelectionStart + moveSteps
inputLogic.mConnection.setSelection(newPosition, newPosition)
val newPosition = inputConnection.expectedSelectionStart + moveSteps
inputConnection.setSelection(newPosition, newPosition)
return true
}
inputLogic.finishInput()
val newPosition = inputLogic.mConnection.expectedSelectionStart + moveSteps
inputLogic.mConnection.setSelection(newPosition, newPosition)
val newPosition = inputConnection.expectedSelectionStart + moveSteps
inputConnection.setSelection(newPosition, newPosition)
inputLogic.restartSuggestionsOnWordTouchedByCursor(settings.current, keyboardSwitcher.currentKeyboardScript)
return true
}
Expand Down
21 changes: 9 additions & 12 deletions app/src/main/java/helium314/keyboard/keyboard/PointerTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -1021,8 +1021,15 @@ private void onUpEventInternal(final int x, final int y, final long eventTime) {
// Release the last pressed key.
setReleasedKeyGraphics(currentKey, true);

if (mInHorizontalSwipe && currentKey.getCode() == KeyCode.DELETE) {
sListener.onUpWithDeletePointerActive();
if (mKeySwipeAllowed) {
mKeySwipeAllowed = false;
sInKeySwipe = false; // todo: only make false when no other pointers are in swipes
if (mInHorizontalSwipe || mInVerticalSwipe) {
sListener.onEndSwipe(currentKey.getCode(), mInVerticalSwipe);
mInHorizontalSwipe = false;
mInVerticalSwipe = false;
return;
}
}

if (isShowingPopupKeysPanel()) {
Expand All @@ -1037,16 +1044,6 @@ private void onUpEventInternal(final int x, final int y, final long eventTime) {
return;
}

if (mKeySwipeAllowed) {
mKeySwipeAllowed = false;
sInKeySwipe = false;
if (mInHorizontalSwipe || mInVerticalSwipe) {
mInHorizontalSwipe = false;
mInVerticalSwipe = false;
return;
}
}

if (sInGesture) {
if (currentKey != null) {
callListenerOnRelease(currentKey, currentKey.getCode(), true);
Expand Down

0 comments on commit 0d79d1a

Please sign in to comment.