Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 20a85f25 authored by Patrick Gaskin's avatar Patrick Gaskin
Browse files

LatinIME: Implement slide-to-delete on backspace key

Change-Id: Ia38e8d6f72bec53cd2f12e708875dcdc2eb9370a
parent d6cc930e
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -40,6 +40,10 @@ disposition rather than other common dispositions for Latin languages. [CHAR LIM
    <string name="space_trackpad">Space bar trackpad</string>
    <string name="space_trackpad_summary">Swipe on the spacebar to move the cursor</string>

    <!-- Preference item for the backspace key track pad -->
    <string name="backspace_trackpad">Backspace key trackpad</string>
    <string name="backspace_trackpad_summary">Swipe on the backspace key to delete words</string>

    <!-- Preference item for enabling longpress key hints -->
    <string name="show_longpress_hints">Show long-press key hints</string>
    <string name="show_longpress_hints_summary">Shows long-press hints for supported keys</string>
+5 −0
Original line number Diff line number Diff line
@@ -53,6 +53,11 @@
        android:title="@string/space_trackpad"
        android:summary="@string/space_trackpad_summary"
        android:defaultValue="true" />
    <CheckBoxPreference
        android:key="pref_backspace_trackpad"
        android:title="@string/backspace_trackpad"
        android:summary="@string/backspace_trackpad_summary"
        android:defaultValue="true" />
    <com.android.inputmethod.latin.settings.SeekBarDialogPreference
        android:key="pref_keyboard_height_scale"
        android:title="@string/prefs_keyboard_height_scale"
+14 −0
Original line number Diff line number Diff line
@@ -106,6 +106,16 @@ public interface KeyboardActionListener {
     */
    public void onMovePointer(int steps);

    /**
     * Called while user is sliding backspace key.
     */
    public void onBackspaceSlide(int steps);

    /**
     * Called when user finished sliding backspace key.
     */
    public void onBackspaceSlideFinished();

    public static final KeyboardActionListener EMPTY_LISTENER = new Adapter();

    public static class Adapter implements KeyboardActionListener {
@@ -135,5 +145,9 @@ public interface KeyboardActionListener {
        }
        @Override
        public void onMovePointer(int steps) {}
        @Override
        public void onBackspaceSlide(int steps) {}
        @Override
        public void onBackspaceSlideFinished() {}
    }
}
+30 −1
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
    // Parameters for pointer handling.
    private static PointerTrackerParams sParams;
    private static int sPointerStep;
    private static int sPointerWordStep;
    private static GestureStrokeRecognitionParams sGestureStrokeRecognitionParams;
    private static GestureStrokeDrawingParams sGestureStrokeDrawingParams;
    private static boolean sNeedsPhantomSuddenMoveEventHack;
@@ -129,11 +130,12 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
    private int mLastX;
    private int mLastY;

    // For spacebar slide tracking.
    // For spacebar/backspace slide tracking.
    private int mStartX;
    private int mStartY;
    private long mStartTime;
    private boolean mSlidOnSpaceBar = false;
    private boolean mSlidOnBackspace = false;

    // true if keyboard layout has been changed.
    private boolean mKeyboardLayoutHasBeenChanged;
@@ -164,6 +166,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
            final DrawingProxy drawingProxy) {
        sParams = new PointerTrackerParams(mainKeyboardViewAttr);
        sPointerStep = (int)(10.0 * Resources.getSystem().getDisplayMetrics().density);
        sPointerWordStep = (int)(20.0 * Resources.getSystem().getDisplayMetrics().density);
        sGestureStrokeRecognitionParams = new GestureStrokeRecognitionParams(mainKeyboardViewAttr);
        sGestureStrokeDrawingParams = new GestureStrokeDrawingParams(mainKeyboardViewAttr);
        sTypingTimeRecorder = new TypingTimeRecorder(
@@ -296,6 +299,9 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
        if (ignoreModifierKey) {
            return;
        }
        if (mSlidOnBackspace && code == Constants.CODE_DELETE) {
            return;
        }
        // Even if the key is disabled, it should respond if it is in the altCodeWhileTyping state.
        if (key.isEnabled() || altersCode) {
            sTypingTimeRecorder.onCodeInput(code, eventTime);
@@ -925,6 +931,20 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
            return;
        }

        // Backspace slider
        if (oldKey != null && oldKey.getCode() == Constants.CODE_DELETE
                && Settings.getInstance().getCurrent().mBackspaceTrackpadEnabled) {
            int steps = (x - mStartX) / sPointerWordStep;
            if (steps != 0
                    && mStartTime + Settings.getInstance().getCurrent().mKeyLongpressTimeout
                    < System.currentTimeMillis()) {
                mSlidOnBackspace = true;
                mStartX += steps * sPointerWordStep;
                sListener.onBackspaceSlide(steps);
            }
            return;
        }

        if (sGestureEnabler.shouldHandleGesture()) {
            // Register move event on gesture tracker.
            onGestureMoveEvent(x, y, eventTime, true /* isMajorEvent */, newKey);
@@ -1012,6 +1032,12 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
            return;
        }

        if (mSlidOnBackspace) {
            sListener.onBackspaceSlideFinished();
            mSlidOnBackspace = false;
            return;
        }

        if (sInGesture) {
            if (currentKey != null) {
                callListenerOnRelease(currentKey, currentKey.getCode(), true /* withSliding */);
@@ -1057,6 +1083,9 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
        if (mSlidOnSpaceBar) {
            return;
        }
        if (mSlidOnBackspace) {
            return;
        }
        final Key key = getKey();
        if (key == null) {
            return;
+74 −0
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ import com.android.inputmethod.latin.utils.ViewLayoutUtils;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -159,6 +160,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
    // If it turns out we need several, it will get grown seamlessly.
    final SparseArray<HardwareEventDecoder> mHardwareEventDecoders = new SparseArray<>(1);

    BreakIterator mBreakWordIterator = BreakIterator.getWordInstance();

    // TODO: Move these {@link View}s to {@link KeyboardSwitcher}.
    private View mInputView;
    private InsetsUpdater mInsetsUpdater;
@@ -746,6 +749,19 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        mInputLogic.mSuggest.setPlausibilityThreshold(settingsValues.mPlausibilityThreshold);
    }

    /**
     * Reset the BreakIterator for the given locale.
     *
     * @param locale the locale
     */
    private void resetBreakIterator(final Locale locale) {
        if (locale == null) {
            mBreakWordIterator = BreakIterator.getWordInstance(Locale.US);
        } else {
            mBreakWordIterator = BreakIterator.getWordInstance(locale);
        }
    }

    /**
     * Reset suggest by loading the main dictionary of the current locale.
     */
@@ -1441,6 +1457,64 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        getCurrentInputConnection().setSelection(newPosition, newPosition);
    }

    @Override
    public void onBackspaceSlide(int steps) {
        int delta = 0;
        if (steps < 0) {
            CharSequence text =
                    getCurrentInputConnection().getTextBeforeCursor(MAX_SPACESLIDE_CHARS, 0);
            if (text == null || text.length() == 0) {
                return;
            }
            mBreakWordIterator.setText(text.toString());
            mBreakWordIterator.last();
            delta = text.length();
            while (steps < 0) {
                int start = mBreakWordIterator.previous();
                if (start == BreakIterator.DONE) {
                    break;
                }
                delta = start;
                steps++;
            }
            delta -= text.length();
        } else if (steps > 0) {
            CharSequence text = getCurrentInputConnection().getSelectedText(0);
            if (text == null || text.length() == 0) {
                return;
            }
            mBreakWordIterator.setText(text.toString());
            mBreakWordIterator.first();
            while (steps > 0) {
                int end = mBreakWordIterator.next();
                if (end == BreakIterator.DONE) {
                    break;
                }
                delta = end;
                steps--;
            }
        } else {
            return;
        }

        // We only extend the start of the selection, never going past the original selection end.
        int newStart = mInputLogic.mConnection.getExpectedSelectionStart() + delta;
        int newEnd = mInputLogic.mConnection.getExpectedSelectionEnd();
        if (newStart > newEnd) {
            newStart = newEnd;
        }
        getCurrentInputConnection().setSelection(newStart, newEnd);
    }

    @Override
    public void onBackspaceSlideFinished() {
        final int start = mInputLogic.mConnection.getExpectedSelectionStart();
        final int end = mInputLogic.mConnection.getExpectedSelectionEnd();
        if (start != end) {
            getCurrentInputConnection().commitText("", 1);
        }
    }

    private boolean isShowingOptionDialog() {
        return mOptionsDialog != null && mOptionsDialog.isShowing();
    }
Loading