Skip to content

Commit

Permalink
[NTC][ProgressIndicator] Internal changes
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 588435272
  • Loading branch information
paulfthomas authored and hunterstich committed Dec 6, 2023
1 parent 59eaee3 commit fb6a97c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.VisibleForTesting;
Expand All @@ -47,6 +48,7 @@ public final class IndeterminateDrawable<S extends BaseProgressIndicatorSpec>
private IndeterminateAnimatorDelegate<ObjectAnimator> animatorDelegate;

private Drawable staticDummyDrawable;
@Px private int tempIndicatorTrackGapSize;

IndeterminateDrawable(
@NonNull Context context,
Expand All @@ -55,10 +57,17 @@ public final class IndeterminateDrawable<S extends BaseProgressIndicatorSpec>
@NonNull IndeterminateAnimatorDelegate<ObjectAnimator> animatorDelegate) {
super(context, baseSpec);

tempIndicatorTrackGapSize = baseSpec.indicatorTrackGapSize;
setDrawingDelegate(drawingDelegate);
setAnimatorDelegate(animatorDelegate);
}

@Override
public void invalidateSelf() {
tempIndicatorTrackGapSize = baseSpec.indicatorTrackGapSize;
super.invalidateSelf();
}

/**
* Creates an instance of {@link IndeterminateDrawable} for {@link LinearProgressIndicator} with
* {@link LinearProgressIndicatorSpec}.
Expand Down Expand Up @@ -168,13 +177,11 @@ public void draw(@NonNull Canvas canvas) {
canvas.save();
drawingDelegate.validateSpecAndAdjustCanvas(canvas, getBounds(), getGrowFraction());

if (baseSpec.indicatorTrackGapSize > 0) {
if (tempIndicatorTrackGapSize > 0) {
if (drawingDelegate instanceof LinearDrawingDelegate) {
((LinearProgressIndicatorSpec) drawingDelegate.spec).trackStopIndicatorSize = 0;
} else if (drawingDelegate instanceof CircularDrawingDelegate) {
// TODO: workaround preventing exiting the indicatorTrackGapSize > 0 logic while keeping
// the animation smooth.
baseSpec.indicatorTrackGapSize = 1;
baseSpec.indicatorTrackGapSize = 0;
}

// Draws the transparent track.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public void adjustCanvas(
// Scales canvas while hiding with escape animation.
if (drawable.isHiding() && spec.hideAnimationBehavior == HIDE_ESCAPE) {
canvas.scale(
trackThicknessFraction, trackThicknessFraction, bounds.left + bounds.width() / 2f, 0);
trackThicknessFraction,
trackThicknessFraction,
bounds.left + bounds.width() / 2f,
bounds.top + bounds.height() / 2f);
}

// Clips all drawing to the track area, so it doesn't draw outside of its bounds (which can
Expand Down Expand Up @@ -136,11 +139,6 @@ public void fillIndicator(
float endPx = endFraction * trackLength;
float gapSize = min(spec.indicatorTrackGapSize, startPx);

// No need to draw if the indicator starts at or after the stop indicator.
if (startPx + gapSize >= trackLength - spec.trackStopIndicatorSize) {
return;
}

float adjustedStartX = originX + startPx + gapSize;
// TODO: workaround to maintain pixel-perfect compatibility with drawing logic
// not using indicatorTrackGapSize.
Expand All @@ -149,10 +147,6 @@ public void fillIndicator(
adjustedStartX -= displayedCornerRadius * 2;
}
float adjustedEndX = originX + endPx;
// Prevents drawing over the stop indicator.
if (endPx == trackLength) {
adjustedEndX -= spec.trackStopIndicatorSize;
}

// Sets up the paint.
paint.setStyle(Style.FILL);
Expand All @@ -169,9 +163,28 @@ public void fillIndicator(
adjustedEndX,
displayedTrackThickness / 2);
canvas.drawRoundRect(indicatorBounds, displayedCornerRadius, displayedCornerRadius, paint);

// Draw stop indicator
if (spec.trackStopIndicatorSize > 0) {
drawStopIndicator(canvas, paint);
}
canvas.restore();
}

private void drawStopIndicator(@NonNull Canvas canvas, @NonNull Paint paint) {
int indicatorColor =
MaterialColors.compositeARGBWithAlpha(spec.indicatorColors[0], drawable.getAlpha());
paint.setColor(indicatorColor);
Rect trackBounds = canvas.getClipBounds();
RectF stopBounds =
new RectF(
trackBounds.right - spec.trackStopIndicatorSize,
-displayedTrackThickness / 2,
trackBounds.right,
displayedTrackThickness / 2);
canvas.drawRoundRect(stopBounds, displayedCornerRadius, displayedCornerRadius, paint);
}

/**
* Fills the whole track with track color.
*
Expand All @@ -196,13 +209,5 @@ void fillTrack(@NonNull Canvas canvas, @NonNull Paint paint) {
displayedCornerRadius,
Path.Direction.CCW);
canvas.drawPath(displayedTrackPath, paint);

if (spec.trackStopIndicatorSize > 0) {
int indicatorColor =
MaterialColors.compositeARGBWithAlpha(spec.indicatorColors[0], drawable.getAlpha());
paint.setColor(indicatorColor);
RectF stopBounds = new RectF(right - spec.trackStopIndicatorSize, -bottom, right, bottom);
canvas.drawRoundRect(stopBounds, displayedCornerRadius, displayedCornerRadius, paint);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ void validateSpec() {
}
if (indeterminateAnimationType
== LinearProgressIndicator.INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS) {
if (trackCornerRadius > 0) {
if (trackCornerRadius > 0 && indicatorTrackGapSize == 0) {
// Throws an exception if trying to use the cornered indicator/track with contiguous
// indeterminate animation type.
// indeterminate animation type without gap.
throw new IllegalArgumentException(
"Rounded corners are not supported in contiguous indeterminate animation.");
"Rounded corners without gap are not supported in contiguous indeterminate animation.");
}
if (indicatorColors.length < 3) {
// Throws an exception if trying to set contiguous indeterminate animation with less than 3
Expand Down

0 comments on commit fb6a97c

Please sign in to comment.