Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Slider] Use a uniform way to update labels #4093

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 RuntimeException("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