Skip to content

Commit

Permalink
[ProgressIndicator] Flip the canvas for different circular directions.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 597652047
(cherry picked from commit 5e185db)
  • Loading branch information
pekingme authored and hunterstich committed Mar 21, 2024
1 parent 76207cb commit bcc27a3
Showing 1 changed file with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
/** A delegate class to help draw the graphics for {@link CircularProgressIndicator}. */
final class CircularDrawingDelegate extends DrawingDelegate<CircularProgressIndicatorSpec> {

// This is a factor effecting the positive direction to draw the arc. +1 for clockwise; -1 for
// counter-clockwise.
private int arcDirectionFactor = 1;
private float displayedTrackThickness;
private float displayedCornerRadius;
private float adjustedRadius;
Expand Down Expand Up @@ -94,18 +91,20 @@ void adjustCanvas(
canvas.translate(
scaledOuterRadiusWithInsetX + bounds.left, scaledOuterRadiusWithInsetY + bounds.top);

canvas.scale(scaleX, scaleY);
// Rotates canvas so that arc starts at top.
canvas.rotate(-90f);
// Flips canvas in Y axis (horizontal after rotation) if rotate counterclockwise.
canvas.scale(scaleX, scaleY);
if (spec.indicatorDirection != CircularProgressIndicator.INDICATOR_DIRECTION_CLOCKWISE) {
canvas.scale(1, -1);
}

// Clip all drawing to the designated area, so it doesn't draw outside of its bounds (which can
// happen in certain configuration of clipToPadding and clipChildren)
canvas.clipRect(
-outerRadiusWithInset, -outerRadiusWithInset, outerRadiusWithInset, outerRadiusWithInset);

// These are used when drawing the indicator and track.
arcDirectionFactor =
spec.indicatorDirection == CircularProgressIndicator.INDICATOR_DIRECTION_CLOCKWISE ? 1 : -1;
displayedTrackThickness = spec.trackThickness * trackThicknessFraction;
displayedCornerRadius = spec.trackCornerRadius * trackThicknessFraction;
adjustedRadius = (spec.indicatorSize - spec.trackThickness) / 2f;
Expand Down Expand Up @@ -174,13 +173,12 @@ void fillIndicator(
startFraction = lerp(1 - totalTrackLengthFraction, 1f, startFraction);
arcFraction = lerp(0f, totalTrackLengthFraction, arcFraction);
// Calculates the start and end in degrees.
float startDegree = startFraction * 360 * arcDirectionFactor;
float arcDegree = arcFraction * 360 * arcDirectionFactor;
float startDegree = startFraction * 360;
float arcDegree = arcFraction * 360;

// Draws the gaps if needed.
if (spec.indicatorTrackGapSize > 0) {
float gapSize =
min(spec.getIndicatorTrackGapSizeDegree(), Math.abs(startDegree)) * arcDirectionFactor;
float gapSize = min(spec.getIndicatorTrackGapSizeDegree(), Math.abs(startDegree));
// No need to draw if the indicator is shorter than gap.
if (Math.abs(arcDegree) <= Math.abs(gapSize) * 2) {
return;
Expand Down

0 comments on commit bcc27a3

Please sign in to comment.