Skip to content

Commit

Permalink
[Slider] Add mouse support for slider control.
Browse files Browse the repository at this point in the history
This change makes it so that mouse input does not behave exactly like touch when using a slider. Previously, any time a touch event was detected on a slider bar, we would postpone updating the slider position because there was potential for a pan drag if the container was a scrollable view. See cl/286431744. However for mouse input this should never apply because a mouse down event should not be able to scroll the container.

This change also fixes the halo not updating when clicking or touching down on the slider (without dragging). This isn't very noticeable when only using touch input but when hovering over the control with a mouse, you can see the halo was in the last position of the slider instead of the current position.

PiperOrigin-RevId: 495905645
  • Loading branch information
Material Design Team authored and imhappi committed Dec 19, 2022
1 parent af16b05 commit 93f8ffb
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Expand Up @@ -1943,7 +1943,7 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {

// If we're inside a vertical scrolling container,
// we should start dragging in ACTION_MOVE
if (isInVerticalScrollingContainer()) {
if (isPotentialVerticalScroll(event)) {
break;
}

Expand All @@ -1964,7 +1964,7 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
case MotionEvent.ACTION_MOVE:
if (!thumbIsPressed) {
// Check if we're trying to scroll vertically instead of dragging this Slider
if (isInVerticalScrollingContainer() && abs(x - touchDownX) < scaledTouchSlop) {
if (isPotentialVerticalScroll(event) && abs(x - touchDownX) < scaledTouchSlop) {
return false;
}
getParent().requestDisallowInterceptTouchEvent(true);
Expand Down Expand Up @@ -1996,6 +1996,7 @@ && abs(lastEvent.getY() - event.getY()) <= scaledTouchSlop) {

if (activeThumbIdx != -1) {
snapTouchPosition();
updateHaloHotspot();
activeThumbIdx = -1;
onStopTrackingTouch();
}
Expand Down Expand Up @@ -2347,6 +2348,14 @@ private boolean isInVerticalScrollingContainer() {
return false;
}

private static boolean isMouseEvent(MotionEvent event) {
return event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE;
}

private boolean isPotentialVerticalScroll(MotionEvent event) {
return !isMouseEvent(event) && isInVerticalScrollingContainer();
}

@SuppressWarnings("unchecked")
private void dispatchOnChangedProgrammatically() {
for (L listener : changeListeners) {
Expand Down

0 comments on commit 93f8ffb

Please sign in to comment.