Skip to content

Commit

Permalink
[TopAppBar] Fix expanded title margin not applied
Browse files Browse the repository at this point in the history
In the original logic we compares interpolated text size to decide if the available width should be the collapsed one or the expanded one. This logic wouldn't work if the collapsed text size and the expanded text size are the same. Refactors the logic to make the decision by interpolating fraction directly.

Resolves #2459

PiperOrigin-RevId: 410901171
  • Loading branch information
drchen authored and paulfthomas committed Nov 29, 2021
1 parent 1d50ed7 commit 89d80d0
Showing 1 changed file with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -599,21 +599,20 @@ private void calculateOffsets(final float fraction) {
currentDrawX = expandedDrawX;
currentDrawY = expandedDrawY;

setInterpolatedTextSize(expandedTextSize);
setInterpolatedTextSize(/* fraction= */ 0);
} else {
textBlendFraction = 1F;
currentDrawX = collapsedDrawX;
currentDrawY = collapsedDrawY - max(0, currentOffsetY);

setInterpolatedTextSize(collapsedTextSize);
setInterpolatedTextSize(/* fraction= */ 1);
}
} else {
textBlendFraction = fraction;
currentDrawX = lerp(expandedDrawX, collapsedDrawX, fraction, positionInterpolator);
currentDrawY = lerp(expandedDrawY, collapsedDrawY, fraction, positionInterpolator);

setInterpolatedTextSize(
lerp(expandedTextSize, collapsedTextSize, fraction, textSizeInterpolator));
setInterpolatedTextSize(fraction);
}

setCollapsedTextBlend(
Expand Down Expand Up @@ -703,10 +702,8 @@ private int getCurrentColor(@Nullable ColorStateList colorStateList) {
}

private void calculateBaseOffsets(boolean forceRecalculate) {
final float currentTextSize = this.currentTextSize;

// We then calculate the collapsed text size, using the same logic
calculateUsingTextSize(collapsedTextSize, forceRecalculate);
calculateUsingTextSize(/* fraction= */ 1, forceRecalculate);
if (textToDraw != null && textLayout != null) {
textToDrawCollapsed =
TextUtils.ellipsize(textToDraw, textPaint, textLayout.getWidth(), TruncateAt.END);
Expand Down Expand Up @@ -748,7 +745,7 @@ private void calculateBaseOffsets(boolean forceRecalculate) {
break;
}

calculateUsingTextSize(expandedTextSize, forceRecalculate);
calculateUsingTextSize(/* fraction= */ 0, forceRecalculate);
float expandedTextHeight = textLayout != null ? textLayout.getHeight() : 0;
float expandedTextWidth = 0;
if (textLayout != null && maxLines > 1) {
Expand Down Expand Up @@ -792,7 +789,7 @@ private void calculateBaseOffsets(boolean forceRecalculate) {
// The bounds have changed so we need to clear the texture
clearTexture();
// Now reset the text size back to the original
setInterpolatedTextSize(currentTextSize);
setInterpolatedTextSize(expandedFraction);
}

private float measureTextWidth(TextPaint textPaint, CharSequence textToDraw) {
Expand Down Expand Up @@ -923,8 +920,8 @@ private boolean isTextDirectionHeuristicsIsRtl(@NonNull CharSequence text, boole
.isRtl(text, 0, text.length());
}

private void setInterpolatedTextSize(float textSize) {
calculateUsingTextSize(textSize);
private void setInterpolatedTextSize(float fraction) {
calculateUsingTextSize(fraction);

// Use our texture if the scale isn't 1.0
useTexture = USE_SCALING_TEXTURE && scale != 1f;
Expand All @@ -937,12 +934,12 @@ private void setInterpolatedTextSize(float textSize) {
ViewCompat.postInvalidateOnAnimation(view);
}

private void calculateUsingTextSize(final float textSize) {
calculateUsingTextSize(textSize, /* forceRecalculate= */ false);
private void calculateUsingTextSize(final float fraction) {
calculateUsingTextSize(fraction, /* forceRecalculate= */ false);
}

@SuppressWarnings("ReferenceEquality") // Matches the Typeface comparison in TextView
private void calculateUsingTextSize(final float textSize, boolean forceRecalculate) {
private void calculateUsingTextSize(final float fraction, boolean forceRecalculate) {
if (text == null) {
return;
}
Expand All @@ -955,7 +952,7 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula
float newLetterSpacing;
boolean updateDrawText = false;

if (isClose(textSize, collapsedTextSize)) {
if (isClose(fraction, /* targetValue= */ 1)) {
newTextSize = collapsedTextSize;
newLetterSpacing = collapsedLetterSpacing;
scale = 1f;
Expand All @@ -971,12 +968,14 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula
currentTypeface = expandedTypeface;
updateDrawText = true;
}
if (isClose(textSize, expandedTextSize)) {
if (isClose(fraction, /* targetValue= */ 0)) {
// If we're close to the expanded text size, snap to it and use a scale of 1
scale = 1f;
} else {
// Else, we'll scale down from the expanded text size
scale = textSize / expandedTextSize;
scale =
lerp(expandedTextSize, collapsedTextSize, fraction, textSizeInterpolator)
/ expandedTextSize;
}

float textSizeRatio = collapsedTextSize / expandedTextSize;
Expand Down Expand Up @@ -1185,10 +1184,10 @@ public int getHyphenationFrequency() {

/**
* Returns true if {@code value} is 'close' to it's closest decimal value. Close is currently
* defined as it's difference being < 0.001.
* defined as it's difference being < 0.00001.
*/
private static boolean isClose(float value, float targetValue) {
return Math.abs(value - targetValue) < 0.001f;
return Math.abs(value - targetValue) < 0.00001f;
}

public ColorStateList getExpandedTextColor() {
Expand Down

0 comments on commit 89d80d0

Please sign in to comment.