Skip to content

Commit

Permalink
[Slider] Fix the handle width when touched in a scrolling container a…
Browse files Browse the repository at this point in the history
…nd when more than one handle is in the touch position.

Resolves #4151
Resolves #4149
Resolves #4150

GIT_ORIGIN_REV_ID=d78e6e24416135fa1a21dc6e298902ab6b893490
PiperOrigin-RevId: 635894036
  • Loading branch information
pubiqq authored and afohrman committed May 23, 2024
1 parent 8edae9b commit d85b73f
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2357,19 +2357,12 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {

requestFocus();
thumbIsPressed = true;
updateThumbWidthWhenPressed();
onStartTrackingTouch();

snapTouchPosition();
updateHaloHotspot();
// Update the thumb width when pressed.
if (hasGapBetweenThumbAndTrack()) {
defaultThumbWidth = thumbWidth;
defaultThumbTrackGapSize = thumbTrackGapSize;
int pressedThumbWidth = Math.round(thumbWidth * THUMB_WIDTH_PRESSED_RATIO);
int delta = thumbWidth - pressedThumbWidth;
setThumbWidth(pressedThumbWidth);
setThumbTrackGapSize(thumbTrackGapSize - delta / 2);
}
invalidate();
onStartTrackingTouch();
break;
case MotionEvent.ACTION_MOVE:
if (!thumbIsPressed) {
Expand All @@ -2378,15 +2371,17 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
return false;
}
getParent().requestDisallowInterceptTouchEvent(true);
onStartTrackingTouch();
}

if (!pickActiveThumb()) {
// Couldn't determine the active thumb yet.
break;
if (!pickActiveThumb()) {
// Couldn't determine the active thumb yet.
break;
}

thumbIsPressed = true;
updateThumbWidthWhenPressed();
onStartTrackingTouch();
}

thumbIsPressed = true;
snapTouchPosition();
updateHaloHotspot();
invalidate();
Expand Down Expand Up @@ -2430,6 +2425,18 @@ && abs(lastEvent.getY() - event.getY()) <= scaledTouchSlop) {
return true;
}

private void updateThumbWidthWhenPressed() {
// Update thumb width and track gap size when pressed.
if (hasGapBetweenThumbAndTrack()) {
defaultThumbWidth = thumbWidth;
defaultThumbTrackGapSize = thumbTrackGapSize;
int pressedThumbWidth = Math.round(thumbWidth * THUMB_WIDTH_PRESSED_RATIO);
int delta = thumbWidth - pressedThumbWidth;
setThumbWidth(pressedThumbWidth);
setThumbTrackGapSize(thumbTrackGapSize - delta / 2);
}
}

private double snapPosition(float position) {
if (stepSize > 0.0f) {
int stepCount = (int) ((valueTo - valueFrom) / stepSize);
Expand Down

0 comments on commit d85b73f

Please sign in to comment.