Skip to content

Commit 93f8ffb

Browse files
Material Design Teamimhappi
authored andcommitted
[Slider] Add mouse support for slider control.
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
1 parent af16b05 commit 93f8ffb

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lib/java/com/google/android/material/slider/BaseSlider.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
19431943

19441944
// If we're inside a vertical scrolling container,
19451945
// we should start dragging in ACTION_MOVE
1946-
if (isInVerticalScrollingContainer()) {
1946+
if (isPotentialVerticalScroll(event)) {
19471947
break;
19481948
}
19491949

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

19971997
if (activeThumbIdx != -1) {
19981998
snapTouchPosition();
1999+
updateHaloHotspot();
19992000
activeThumbIdx = -1;
20002001
onStopTrackingTouch();
20012002
}
@@ -2347,6 +2348,14 @@ private boolean isInVerticalScrollingContainer() {
23472348
return false;
23482349
}
23492350

2351+
private static boolean isMouseEvent(MotionEvent event) {
2352+
return event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE;
2353+
}
2354+
2355+
private boolean isPotentialVerticalScroll(MotionEvent event) {
2356+
return !isMouseEvent(event) && isInVerticalScrollingContainer();
2357+
}
2358+
23502359
@SuppressWarnings("unchecked")
23512360
private void dispatchOnChangedProgrammatically() {
23522361
for (L listener : changeListeners) {

0 commit comments

Comments
 (0)