mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
[Slider] Keyboard navigation fix
Resolves https://github.com/material-components/material-components-android/pull/1364 GIT_ORIGIN_REV_ID=8a0fb870fafba516db529ffee13ea448964b0a9b PiperOrigin-RevId: 315302829
This commit is contained in:
parent
c66c69de2a
commit
3e3bc41abb
@ -1825,13 +1825,11 @@ abstract class BaseSlider<
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_TAB:
|
||||
if (event.hasNoModifiers()) {
|
||||
moveFocus(1);
|
||||
return true;
|
||||
return moveFocus(1);
|
||||
} else if (event.isShiftPressed()) {
|
||||
moveFocus(-1);
|
||||
return true;
|
||||
return moveFocus(-1);
|
||||
}
|
||||
return false;
|
||||
// fall through
|
||||
case KeyEvent.KEYCODE_DPAD_LEFT:
|
||||
case KeyEvent.KEYCODE_MINUS:
|
||||
moveFocus(-1);
|
||||
@ -1850,7 +1848,7 @@ abstract class BaseSlider<
|
||||
}
|
||||
} else {
|
||||
isLongPress |= event.isLongPress();
|
||||
Float increment = calculateIncrementForKey(event, keyCode);
|
||||
Float increment = calculateIncrementForKey(keyCode);
|
||||
if (increment != null) {
|
||||
if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL) {
|
||||
increment = -increment;
|
||||
@ -1863,6 +1861,25 @@ abstract class BaseSlider<
|
||||
}
|
||||
return true;
|
||||
}
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_TAB:
|
||||
if (event.hasNoModifiers()) {
|
||||
return moveFocus(1);
|
||||
} else if (event.isShiftPressed()) {
|
||||
return moveFocus(-1);
|
||||
}
|
||||
// fall through
|
||||
case KeyEvent.KEYCODE_DPAD_CENTER:
|
||||
case KeyEvent.KEYCODE_ENTER:
|
||||
activeThumbIdx = -1;
|
||||
for (TooltipDrawable label : labels) {
|
||||
ViewUtils.getContentViewOverlay(this).remove(label);
|
||||
}
|
||||
postInvalidate();
|
||||
return true;
|
||||
default:
|
||||
// Nothing to do in this case.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1875,31 +1892,36 @@ abstract class BaseSlider<
|
||||
return super.onKeyUp(keyCode, event);
|
||||
}
|
||||
|
||||
private void moveFocus(int direction) {
|
||||
/**
|
||||
* Attempts to move focus to next or previous thumb and returns whether the focused thumb changed.
|
||||
* If focused thumb didn't change, we're at the view boundary for specified {@code direction} and
|
||||
* focus should be moved to next or previous view instead.
|
||||
*/
|
||||
private boolean moveFocus(int direction) {
|
||||
int oldFocusedThumbIdx = focusedThumbIdx;
|
||||
focusedThumbIdx += direction;
|
||||
focusedThumbIdx = MathUtils.clamp(focusedThumbIdx, 0, values.size() - 1);
|
||||
if (activeThumbIdx != -1) {
|
||||
activeThumbIdx = focusedThumbIdx;
|
||||
if (focusedThumbIdx == oldFocusedThumbIdx) {
|
||||
// Move focus to next or previous view.
|
||||
return false;
|
||||
} else {
|
||||
if (activeThumbIdx != -1) {
|
||||
activeThumbIdx = focusedThumbIdx;
|
||||
}
|
||||
updateHaloHotspot();
|
||||
postInvalidate();
|
||||
return true;
|
||||
}
|
||||
updateHaloHotspot();
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
private Float calculateIncrementForKey(KeyEvent event, int keyCode) {
|
||||
private Float calculateIncrementForKey(int keyCode) {
|
||||
// If this is a long press, increase the increment so it will only take around 20 steps.
|
||||
// Otherwise choose the smallest valid increment.
|
||||
float increment = isLongPress ? calculateStepIncrement(20) : calculateStepIncrement();
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_TAB:
|
||||
if (event.isShiftPressed()) {
|
||||
return -increment;
|
||||
} else {
|
||||
return increment;
|
||||
}
|
||||
case KeyEvent.KEYCODE_DPAD_LEFT:
|
||||
case KeyEvent.KEYCODE_MINUS:
|
||||
increment = -increment;
|
||||
// fallthrough
|
||||
return -increment;
|
||||
case KeyEvent.KEYCODE_DPAD_RIGHT:
|
||||
case KeyEvent.KEYCODE_PLUS:
|
||||
case KeyEvent.KEYCODE_EQUALS:
|
||||
@ -1937,12 +1959,35 @@ abstract class BaseSlider<
|
||||
for (TooltipDrawable label : labels) {
|
||||
ViewUtils.getContentViewOverlay(this).remove(label);
|
||||
}
|
||||
accessibilityHelper.requestKeyboardFocusForVirtualView(ExploreByTouchHelper.INVALID_ID);
|
||||
accessibilityHelper.clearKeyboardFocusForVirtualView(focusedThumbIdx);
|
||||
} else {
|
||||
focusThumbOnFocusGained(direction);
|
||||
accessibilityHelper.requestKeyboardFocusForVirtualView(focusedThumbIdx);
|
||||
}
|
||||
}
|
||||
|
||||
private void focusThumbOnFocusGained(int direction) {
|
||||
switch (direction) {
|
||||
case FOCUS_BACKWARD:
|
||||
case FOCUS_LEFT:
|
||||
moveFocus(Integer.MAX_VALUE);
|
||||
break;
|
||||
case FOCUS_FORWARD:
|
||||
case FOCUS_RIGHT:
|
||||
moveFocus(Integer.MIN_VALUE);
|
||||
break;
|
||||
case FOCUS_UP:
|
||||
case FOCUS_DOWN:
|
||||
default:
|
||||
// Don't make assumptions about where exactly focus came from. Use previously focused thumb.
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
final int getAccessibilityFocusedVirtualViewId() {
|
||||
return accessibilityHelper.getAccessibilityFocusedVirtualViewId();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public CharSequence getAccessibilityClassName() {
|
||||
|
||||
@ -116,7 +116,7 @@ public class SliderHelper {
|
||||
return s.getTrackSidePadding() + x;
|
||||
}
|
||||
|
||||
static void activateFocusedThumb(Slider s) {
|
||||
static void clickDpadCenter(Slider s) {
|
||||
new KeyEventBuilder(KeyEvent.KEYCODE_DPAD_CENTER).dispatchEvent(s);
|
||||
}
|
||||
|
||||
|
||||
@ -18,12 +18,14 @@ package com.google.android.material.slider;
|
||||
|
||||
import com.google.android.material.R;
|
||||
|
||||
import static com.google.android.material.slider.SliderHelper.activateFocusedThumb;
|
||||
import static com.google.android.material.slider.SliderHelper.clickDpadCenter;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth.assertWithMessage;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import androidx.customview.widget.ExploreByTouchHelper;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import com.google.android.material.slider.KeyUtils.KeyEventBuilder;
|
||||
import org.junit.Before;
|
||||
@ -55,16 +57,79 @@ public class SliderKeyTest {
|
||||
slider.setValueFrom(SLIDER_VALUE_FROM);
|
||||
slider.setValueTo(SLIDER_VALUE_TO);
|
||||
slider.setValues(values);
|
||||
slider.requestFocus();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThumbFocus_initialFocus_isFirstThumb() {
|
||||
slider.requestFocus();
|
||||
|
||||
assertThat(slider.getFocusedThumbIndex()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThumbFocus_focusForward_focusesFirstThumb() {
|
||||
slider.requestFocus(View.FOCUS_FORWARD);
|
||||
|
||||
assertThat(slider.getFocusedThumbIndex()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThumbFocus_focusBackward_focusesLastThumb() {
|
||||
slider.requestFocus(View.FOCUS_BACKWARD);
|
||||
|
||||
assertThat(slider.getFocusedThumbIndex()).isEqualTo(values.length - 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThumbFocus_focusLeft_focusesLastThumb() {
|
||||
slider.requestFocus(View.FOCUS_LEFT);
|
||||
|
||||
assertThat(slider.getFocusedThumbIndex()).isEqualTo(values.length - 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThumbFocus_focusRight_focusesFirstThumb() {
|
||||
slider.requestFocus(View.FOCUS_RIGHT);
|
||||
|
||||
assertThat(slider.getFocusedThumbIndex()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThumbFocus_reacquiredFocusDown_focusesPreviouslyFocusedThumb() {
|
||||
slider.requestFocus();
|
||||
slider.setFocusedThumbIndex(2);
|
||||
slider.clearFocus();
|
||||
|
||||
slider.requestFocus(View.FOCUS_DOWN);
|
||||
|
||||
assertThat(slider.getFocusedThumbIndex()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThumbFocus_reacquiredFocusUp_focusesPreviouslyFocusedThumb() {
|
||||
slider.requestFocus();
|
||||
slider.setFocusedThumbIndex(2);
|
||||
slider.clearFocus();
|
||||
|
||||
slider.requestFocus(View.FOCUS_UP);
|
||||
|
||||
assertThat(slider.getFocusedThumbIndex()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThumbFocus_clearFocus_clearsVirtualViewFocus() {
|
||||
slider.requestFocus();
|
||||
|
||||
slider.clearFocus();
|
||||
|
||||
assertThat(slider.getAccessibilityFocusedVirtualViewId())
|
||||
.isEqualTo(ExploreByTouchHelper.INVALID_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveThumbFocus_tab_correctThumbHasFocus() {
|
||||
slider.requestFocus();
|
||||
|
||||
KeyEventBuilder tab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB);
|
||||
KeyEventBuilder shiftTab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB, KeyEvent.META_SHIFT_ON);
|
||||
|
||||
@ -73,6 +138,8 @@ public class SliderKeyTest {
|
||||
|
||||
@Test
|
||||
public void testMoveThumbFocus_dPad_correctThumbHasFocus() {
|
||||
slider.requestFocus();
|
||||
|
||||
KeyEventBuilder right = new KeyEventBuilder(KeyEvent.KEYCODE_DPAD_RIGHT);
|
||||
KeyEventBuilder left = new KeyEventBuilder(KeyEvent.KEYCODE_DPAD_LEFT);
|
||||
|
||||
@ -81,6 +148,8 @@ public class SliderKeyTest {
|
||||
|
||||
@Test
|
||||
public void testMoveThumbFocus_plusMinus_correctThumbHasFocus() {
|
||||
slider.requestFocus();
|
||||
|
||||
KeyEventBuilder plus = new KeyEventBuilder(KeyEvent.KEYCODE_PLUS);
|
||||
KeyEventBuilder minus = new KeyEventBuilder(KeyEvent.KEYCODE_MINUS);
|
||||
|
||||
@ -88,16 +157,43 @@ public class SliderKeyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectThirdThumb_clickCenterDPad_activatesThirdThumb() {
|
||||
public void testFocusThirdThumb_clickCenterDPad_activatesThirdThumb() {
|
||||
slider.requestFocus();
|
||||
|
||||
slider.setFocusedThumbIndex(2);
|
||||
|
||||
activateFocusedThumb(slider);
|
||||
clickDpadCenter(slider);
|
||||
|
||||
assertThat(slider.getActiveThumbIndex()).isEqualTo(2);
|
||||
}
|
||||
|
||||
private void activateFocusedThumb(Slider s) {
|
||||
int focusedThumbIndex = s.getFocusedThumbIndex();
|
||||
if (focusedThumbIndex != -1) {
|
||||
// Clicking D-Pad in Slider isn't idempotent. Only do it here if we're changing focused thumb.
|
||||
if (focusedThumbIndex != s.getActiveThumbIndex()) {
|
||||
clickDpadCenter(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectThirdThumb_moveRight_movesThirdThumbRight() {
|
||||
public void testActivateThirdThumb_clickCenterDPad_deactivatesThirdThumb() {
|
||||
slider.requestFocus();
|
||||
|
||||
slider.setFocusedThumbIndex(2);
|
||||
|
||||
activateFocusedThumb(slider);
|
||||
|
||||
clickDpadCenter(slider);
|
||||
|
||||
assertThat(slider.getActiveThumbIndex()).isEqualTo(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivateThirdThumb_moveRight_movesThirdThumbRight() {
|
||||
slider.requestFocus();
|
||||
|
||||
slider.setFocusedThumbIndex(2);
|
||||
|
||||
activateFocusedThumb(slider);
|
||||
@ -112,7 +208,9 @@ public class SliderKeyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectThirdThumb_moveLeft_movesThirdThumbLeft() {
|
||||
public void testActivateThirdThumb_moveLeft_movesThirdThumbLeft() {
|
||||
slider.requestFocus();
|
||||
|
||||
slider.setFocusedThumbIndex(2);
|
||||
|
||||
activateFocusedThumb(slider);
|
||||
@ -126,6 +224,120 @@ public class SliderKeyTest {
|
||||
assertThat(slider.getValues()).doesNotContain(3.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFocusDefaultThumb_clickUpDPad_unhandled() {
|
||||
slider.requestFocus();
|
||||
|
||||
KeyEventBuilder up = new KeyEventBuilder(KeyEvent.KEYCODE_DPAD_UP);
|
||||
boolean handledDown = slider.dispatchKeyEvent(up.buildDown());
|
||||
boolean handledUp = slider.dispatchKeyEvent(up.buildUp());
|
||||
|
||||
assertThat(handledDown).isFalse();
|
||||
assertThat(handledUp).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivateDefaultThumb_clickUpDPad_unhandled() {
|
||||
slider.requestFocus();
|
||||
|
||||
activateFocusedThumb(slider);
|
||||
|
||||
KeyEventBuilder up = new KeyEventBuilder(KeyEvent.KEYCODE_DPAD_UP);
|
||||
boolean handledDown = slider.dispatchKeyEvent(up.buildDown());
|
||||
boolean handledUp = slider.dispatchKeyEvent(up.buildUp());
|
||||
|
||||
assertThat(handledDown).isFalse();
|
||||
assertThat(handledUp).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFocusDefaultThumb_clickDownDPad_unhandled() {
|
||||
slider.requestFocus();
|
||||
|
||||
KeyEventBuilder down = new KeyEventBuilder(KeyEvent.KEYCODE_DPAD_DOWN);
|
||||
boolean handledDown = slider.dispatchKeyEvent(down.buildDown());
|
||||
boolean handledUp = slider.dispatchKeyEvent(down.buildUp());
|
||||
|
||||
assertThat(handledDown).isFalse();
|
||||
assertThat(handledUp).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivateDefaultThumb_clickDownDPad_unhandled() {
|
||||
slider.requestFocus();
|
||||
|
||||
activateFocusedThumb(slider);
|
||||
|
||||
KeyEventBuilder down = new KeyEventBuilder(KeyEvent.KEYCODE_DPAD_DOWN);
|
||||
boolean handledDown = slider.dispatchKeyEvent(down.buildDown());
|
||||
boolean handledUp = slider.dispatchKeyEvent(down.buildUp());
|
||||
|
||||
assertThat(handledDown).isFalse();
|
||||
assertThat(handledUp).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFocusFirstThumb_shiftTab_unhandled() {
|
||||
slider.requestFocus();
|
||||
|
||||
slider.setFocusedThumbIndex(0);
|
||||
|
||||
KeyEventBuilder tab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB);
|
||||
tab.meta = KeyEvent.META_SHIFT_ON;
|
||||
boolean handledDown = slider.dispatchKeyEvent(tab.buildDown());
|
||||
boolean handledUp = slider.dispatchKeyEvent(tab.buildUp());
|
||||
|
||||
assertThat(handledDown).isFalse();
|
||||
assertThat(handledUp).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivateFirstThumb_shiftTab_unhandled() {
|
||||
slider.requestFocus();
|
||||
|
||||
slider.setFocusedThumbIndex(0);
|
||||
|
||||
activateFocusedThumb(slider);
|
||||
|
||||
KeyEventBuilder tab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB);
|
||||
tab.meta = KeyEvent.META_SHIFT_ON;
|
||||
boolean handledDown = slider.dispatchKeyEvent(tab.buildDown());
|
||||
boolean handledUp = slider.dispatchKeyEvent(tab.buildUp());
|
||||
|
||||
assertThat(handledDown).isFalse();
|
||||
assertThat(handledUp).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFocusLastThumb_tab_unhandled() {
|
||||
slider.requestFocus();
|
||||
|
||||
slider.setFocusedThumbIndex(values.length - 1);
|
||||
|
||||
KeyEventBuilder tab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB);
|
||||
boolean handledDown = slider.dispatchKeyEvent(tab.buildDown());
|
||||
boolean handledUp = slider.dispatchKeyEvent(tab.buildUp());
|
||||
|
||||
assertThat(handledDown).isFalse();
|
||||
assertThat(handledUp).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivateLastThumb_tab_unhandled() {
|
||||
slider.requestFocus();
|
||||
|
||||
slider.setFocusedThumbIndex(values.length - 1);
|
||||
|
||||
activateFocusedThumb(slider);
|
||||
|
||||
KeyEventBuilder tab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB);
|
||||
boolean handledDown = slider.dispatchKeyEvent(tab.buildDown());
|
||||
boolean handledUp = slider.dispatchKeyEvent(tab.buildUp());
|
||||
|
||||
assertThat(handledDown).isFalse();
|
||||
assertThat(handledUp).isFalse();
|
||||
}
|
||||
|
||||
private void sendKeyEventLeftAndRight(KeyEventBuilder right, KeyEventBuilder left) {
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
right.dispatchEvent(slider);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user