From 3b2b7a2d4730e87f02c2ce48680628b301347200 Mon Sep 17 00:00:00 2001 From: Yuriy Liskov Date: Wed, 1 Jul 2020 01:34:19 +0300 Subject: [PATCH] cyclic scroll: vertical --- .../ime/LeanbackKeyboardContainer.java | 43 ++++++++++++++++--- .../leankeyboard/ime/LeanbackUtils.java | 11 +++++ .../utils/LeanKeyPreferences.java | 2 +- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/ime/LeanbackKeyboardContainer.java b/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/ime/LeanbackKeyboardContainer.java index 6f0a1c7..84eb75c 100644 --- a/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/ime/LeanbackKeyboardContainer.java +++ b/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/ime/LeanbackKeyboardContainer.java @@ -679,13 +679,13 @@ public class LeanbackKeyboardContainer { } public void updateCyclicFocus(int dir, KeyFocus oldFocus, KeyFocus newFocus) { - if (oldFocus.equals(newFocus) || newFocus.index == 0) { // submit button has index 0 + if (oldFocus.equals(newFocus) || LeanbackUtils.isSubmitButton(newFocus)) { // submit button has index 0 if (LeanKeyPreferences.instance(mContext).getCyclicNavigationEnabled()) { if (dir == DIRECTION_LEFT) { offsetRect(mRect, mMainKeyboardView); - boolean onSameRow = oldFocus.rect.top < mRect.bottom/2f && mRect.bottom/2f < oldFocus.rect.bottom; + boolean isCentered = oldFocus.rect.top < mRect.bottom/2f && mRect.bottom/2f < oldFocus.rect.bottom; - if (onSameRow) { + if (isCentered) { offsetRect(mRect, mActionButtonView); configureFocus(newFocus, mRect, 0, KeyFocus.TYPE_ACTION); } else { @@ -695,15 +695,27 @@ public class LeanbackKeyboardContainer { configureFocus(newFocus, mRect, keyIdx, key, 0); } } else if (dir == DIRECTION_RIGHT) { - boolean onSameRow = Math.abs(oldFocus.rect.bottom - newFocus.rect.bottom) < 20; + offsetRect(mRect, mMainKeyboardView); + boolean isCentered = oldFocus.rect.top < mRect.bottom/2f && mRect.bottom/2f < oldFocus.rect.bottom; - if (oldFocus.index == 0 || !onSameRow) { - offsetRect(mRect, mMainKeyboardView); + if (LeanbackUtils.isSubmitButton(oldFocus) || !isCentered) { // leftmost key (usually a button) int keyIdx = mMainKeyboardView.getNearestIndex(0, mY - mRect.top); Key key = mMainKeyboardView.getKey(keyIdx); configureFocus(newFocus, mRect, keyIdx, key, 0); } + } else if (dir == DIRECTION_DOWN) { + offsetRect(mRect, mMainKeyboardView); + // topmost key + int keyIdx = mMainKeyboardView.getNearestIndex(mX - mRect.left, 0); + Key key = mMainKeyboardView.getKey(keyIdx); + configureFocus(newFocus, mRect, keyIdx, key, 0); + } else if (dir == DIRECTION_UP) { + offsetRect(mRect, mMainKeyboardView); + // downmost key + int keyIdx = mMainKeyboardView.getNearestIndex(mX - mRect.left, mRect.bottom); + Key key = mMainKeyboardView.getKey(keyIdx); + configureFocus(newFocus, mRect, keyIdx, key, 0); } } @@ -1574,4 +1586,23 @@ public class LeanbackKeyboardContainer { } } } + + private void focusNearestSuggestion(KeyFocus newFocus) { + int count = mSuggestions.getChildCount(); + for (int idx = 0; idx < count; ++idx) { + View view = mSuggestions.getChildAt(idx); + offsetRect(mRect, view); + if (mX < (float) mRect.right || idx + 1 == count) { + view.requestFocus(); + LeanbackUtils.sendAccessibilityEvent(view.findViewById(R.id.text), true); + configureFocus(newFocus, mRect, idx, KeyFocus.TYPE_SUGGESTION); + break; + } + } + } + + private void focusOppositeSuggestion(KeyFocus currentFocus, KeyFocus newFocus) { + int count = mSuggestions.getChildCount(); + // TODO: find opposite suggestion + } } diff --git a/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/ime/LeanbackUtils.java b/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/ime/LeanbackUtils.java index 351ffe1..cbd923c 100644 --- a/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/ime/LeanbackUtils.java +++ b/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/ime/LeanbackUtils.java @@ -13,7 +13,10 @@ import android.view.accessibility.AccessibilityEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; +import android.widget.LinearLayout; import androidx.core.text.BidiFormatter; +import com.liskovsoft.leankeyboard.ime.LeanbackKeyboardContainer.KeyFocus; +import com.liskovsoft.leankeykeyboard.R; public class LeanbackUtils { private static final int ACCESSIBILITY_DELAY_MS = 250; @@ -184,4 +187,12 @@ public class LeanbackUtils { return len; } + + public static boolean isSubmitButton(KeyFocus focus) { + return focus.index == 0 && focus.type == KeyFocus.TYPE_ACTION; + } + + public static boolean isSuggestionsButton(KeyFocus focus) { + return focus.type == KeyFocus.TYPE_SUGGESTION; + } } diff --git a/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/utils/LeanKeyPreferences.java b/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/utils/LeanKeyPreferences.java index ca3d24e..5c27171 100644 --- a/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/utils/LeanKeyPreferences.java +++ b/leankeykeyboard/src/main/java/com/liskovsoft/leankeyboard/utils/LeanKeyPreferences.java @@ -109,6 +109,6 @@ public final class LeanKeyPreferences { } public boolean getCyclicNavigationEnabled() { - return mPrefs.getBoolean(CYCLIC_NAVIGATION_ENABLED, true); + return mPrefs.getBoolean(CYCLIC_NAVIGATION_ENABLED, false); } }