Skip to content

Commit

Permalink
[TextField] Fix hint is not displayed when expanding space is limited
Browse files Browse the repository at this point in the history
In CollapsingTextHelper we are checking both the expanded bound and the collapsed bound to be larger than 0 to decide if we are going to draw the collapsing text. However this can result in a situation that when there's no space to display expanded hint, the collapsed hint won't be drawn either, even if the hint is not expandable at all.

This CL fixes the issue in a more generic way - whenever draw() is called, we check if the current bound (should be calculated whenever the collapsing fraction is changed) is larger than 0 to decide if we need to draw the collapsing text.

This CL also adds the logic to ensure the cutout bound will never be larger than the collapsed bound.

Resolves #2573

PiperOrigin-RevId: 449597658
  • Loading branch information
drchen authored and afohrman committed May 19, 2022
1 parent 673cefc commit 73cb1b6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
Expand Up @@ -92,7 +92,6 @@ public final class CollapsingTextHelper {

private final View view;

private boolean drawTitle;
private float expandedFraction;
private boolean fadeModeEnabled;
private float fadeModeStartFraction;
Expand Down Expand Up @@ -246,7 +245,6 @@ public void setExpandedBounds(int left, int top, int right, int bottom) {
if (!rectEquals(expandedBounds, left, top, right, bottom)) {
expandedBounds.set(left, top, right, bottom);
boundsChanged = true;
onBoundsChanged();
}
}

Expand All @@ -258,7 +256,6 @@ public void setCollapsedBounds(int left, int top, int right, int bottom) {
if (!rectEquals(collapsedBounds, left, top, right, bottom)) {
collapsedBounds.set(left, top, right, bottom);
boundsChanged = true;
onBoundsChanged();
}
}

Expand All @@ -268,9 +265,10 @@ public void setCollapsedBounds(@NonNull Rect bounds) {

public void getCollapsedTextActualBounds(@NonNull RectF bounds, int labelWidth, int textGravity) {
isRtl = calculateIsRtl(text);
bounds.left = getCollapsedTextLeftBound(labelWidth, textGravity);
bounds.left = max(getCollapsedTextLeftBound(labelWidth, textGravity), collapsedBounds.left);
bounds.top = collapsedBounds.top;
bounds.right = getCollapsedTextRightBound(bounds, labelWidth, textGravity);
bounds.right =
min(getCollapsedTextRightBound(bounds, labelWidth, textGravity), collapsedBounds.right);
bounds.bottom = collapsedBounds.top + getCollapsedTextHeight();
}

Expand Down Expand Up @@ -350,14 +348,6 @@ private void getTextPaintCollapsed(@NonNull TextPaint textPaint) {
}
}

void onBoundsChanged() {
drawTitle =
collapsedBounds.width() > 0
&& collapsedBounds.height() > 0
&& expandedBounds.width() > 0
&& expandedBounds.height() > 0;
}

public void setExpandedTextGravity(int gravity) {
if (expandedTextGravity != gravity) {
expandedTextGravity = gravity;
Expand Down Expand Up @@ -839,7 +829,7 @@ private void setExpandedTextBlend(float blend) {
public void draw(@NonNull Canvas canvas) {
final int saveCount = canvas.save();
// Compute where to draw textLayout for this frame
if (textToDraw != null && drawTitle) {
if (textToDraw != null && currentBounds.width() > 0 && currentBounds.height() > 0) {
textPaint.setTextSize(currentTextSize);
float x = currentDrawX;
float y = currentDrawY;
Expand Down
Expand Up @@ -3954,6 +3954,9 @@ private void openCutout() {
final RectF cutoutBounds = tmpRectF;
collapsingTextHelper.getCollapsedTextActualBounds(
cutoutBounds, editText.getWidth(), editText.getGravity());
if (cutoutBounds.width() <= 0 || cutoutBounds.height() <= 0) {
return;
}
applyCutoutPadding(cutoutBounds);

// Offset the cutout bounds by the TextInputLayout's paddings, half of the cutout height, and
Expand Down

0 comments on commit 73cb1b6

Please sign in to comment.