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] Improve track icon bounds calculation #4535

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ abstract class BaseSlider<
private boolean trackIconInactiveEndMutated = false;
@Nullable private ColorStateList trackIconInactiveColor;
@Px private int trackIconSize;
@Px private int trackIconPadding;
private int labelPadding;
private float touchDownX;
private MotionEvent lastEvent;
Expand Down Expand Up @@ -518,6 +519,8 @@ private void loadResources(@NonNull Resources resources) {
minTickSpacing = resources.getDimensionPixelSize(R.dimen.mtrl_slider_tick_min_spacing);

labelPadding = resources.getDimensionPixelSize(R.dimen.mtrl_slider_label_padding);

trackIconPadding = resources.getDimensionPixelOffset(R.dimen.m3_slider_track_icon_padding);
}

private void processAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
Expand Down Expand Up @@ -2706,7 +2709,7 @@ private void calculateBoundsAndDrawTrackIcon(
@Nullable Drawable icon,
boolean isStart) {
if (icon != null) {
calculateTrackIconBounds(trackBounds, iconRectF, trackIconSize, isStart);
calculateTrackIconBounds(trackBounds, iconRectF, trackIconSize, trackIconPadding, isStart);
if (!iconRectF.isEmpty()) {
drawTrackIcon(canvas, iconRectF, icon);
}
Expand All @@ -2726,28 +2729,25 @@ private void drawTrackIcon(
}

private void calculateTrackIconBounds(
@NonNull RectF trackBounds, @NonNull RectF iconBounds, @Px int iconSize, boolean isStart) {
float iconPadding = getResources().getDimension(R.dimen.m3_slider_track_icon_padding);
float iconLeft;
if (isStart) {
iconLeft =
isRtl() || isVertical()
? trackBounds.right - iconSize - iconPadding
: trackBounds.left + iconPadding;
@NonNull RectF trackBounds,
@NonNull RectF iconBounds,
@Px int iconSize,
@Px int iconPadding,
boolean isStart) {
if (trackBounds.right - trackBounds.left >= iconSize + 2 * iconPadding) {
float iconLeft = (isStart ^ (isRtl() || isVertical()))
? trackBounds.left + iconPadding
: trackBounds.right - iconPadding - iconSize;
float iconRight = iconLeft + iconSize;

float iconTop = calculateTrackCenter() - (float) iconSize / 2;
float iconBottom = iconTop + iconSize;

iconBounds.set(iconLeft, iconTop, iconRight, iconBottom);
} else {
iconLeft =
isRtl() || isVertical()
? trackBounds.left + iconPadding
: trackBounds.right - iconSize - iconPadding;
}
float iconRight = iconLeft + iconSize;
int iconTop = calculateTrackCenter() - iconSize / 2;
if (trackBounds.left > iconLeft - iconPadding || trackBounds.right < iconRight + iconPadding) {
// not enough space to draw icon
iconBounds.setEmpty();
return;
}
iconBounds.set(iconLeft, iconTop, iconRight, iconTop + iconSize);
}

private boolean hasGapBetweenThumbAndTrack() {
Expand Down