Skip to content

Commit

Permalink
[Slider] Use a uniform way to update labels
Browse files Browse the repository at this point in the history
Resolves #4093

GIT_ORIGIN_REV_ID=7db4936713bd31a070d4bb1d9743d99222840184
PiperOrigin-RevId: 614741678
  • Loading branch information
pubiqq authored and hunterstich committed Mar 15, 2024
1 parent 22de817 commit 3bc6612
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,25 +348,7 @@ abstract class BaseSlider<

@NonNull
private final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener =
() -> {
if (shouldAlwaysShowLabel() && isEnabled()) {
Rect contentViewBounds = new Rect();
ViewUtils.getContentView(this).getHitRect(contentViewBounds);
boolean isSliderVisibleOnScreen = getLocalVisibleRect(contentViewBounds);
for (int i = 0; i < labels.size(); i++) {
TooltipDrawable label = labels.get(i);
// Get associated value for label
if (i < values.size()) {
positionLabel(label, values.get(i));
}
if (isSliderVisibleOnScreen) {
ViewUtils.getContentViewOverlay(this).add(label);
} else {
ViewUtils.getContentViewOverlay(this).remove(label);
}
}
}
};
this::updateLabels;

/**
* Determines the behavior of the label which can be any of the following.
Expand Down Expand Up @@ -2029,12 +2011,7 @@ protected void onDraw(@NonNull Canvas canvas) {
maybeDrawCompatHalo(canvas, trackWidth, yCenter);
}

// Draw labels if there is an active thumb or the labels are always visible.
if ((activeThumbIdx != -1 || shouldAlwaysShowLabel()) && isEnabled()) {
ensureLabelsAdded();
} else {
ensureLabelsRemoved();
}
updateLabels();

drawThumbs(canvas, trackWidth, yCenter);
}
Expand Down Expand Up @@ -2660,6 +2637,37 @@ public void onAnimationUpdate(ValueAnimator animation) {
return animator;
}

private void updateLabels() {
switch (labelBehavior) {
case LABEL_GONE:
ensureLabelsRemoved();
break;
case LABEL_VISIBLE:
if (isEnabled() && isSliderVisibleOnScreen()) {
ensureLabelsAdded();
} else {
ensureLabelsRemoved();
}
break;
case LABEL_FLOATING:
case LABEL_WITHIN_BOUNDS:
if (activeThumbIdx != -1 && isEnabled()) {
ensureLabelsAdded();
} else {
ensureLabelsRemoved();
}
break;
default:
throw new IllegalArgumentException("Unexpected labelBehavior: " + labelBehavior);
}
}

private boolean isSliderVisibleOnScreen() {
final Rect contentViewBounds = new Rect();
ViewUtils.getContentView(this).getHitRect(contentViewBounds);
return getLocalVisibleRect(contentViewBounds);
}

private void ensureLabelsRemoved() {
// If the labels are animated in or in the process of animating in, create and start a new
// animator to animate out the labels and remove them once the animation ends.
Expand All @@ -2683,11 +2691,6 @@ public void onAnimationEnd(Animator animation) {
}

private void ensureLabelsAdded() {
if (labelBehavior == LABEL_GONE) {
// If the label shouldn't be drawn we can skip this.
return;
}

// If the labels are not animating in, start an animator to show them. ensureLabelsAdded will
// be called multiple times by BaseSlider's draw method, making this check necessary to avoid
// creating and starting an animator for each draw call.
Expand Down

0 comments on commit 3bc6612

Please sign in to comment.