From d2d8e97a6579f3d7f2f30c02343654c8cf416383 Mon Sep 17 00:00:00 2001 From: Sebastian Franco Date: Mon, 4 Apr 2022 14:51:53 -0700 Subject: [PATCH] Make the text for the drop target buttons fit if it's too long. The text "Don't suggest app" is too long when the device language is GE or JA, or if you make the text bigger in settings. Before truncating the text completely we are going to try to make the text smaller until it gets to a predefined value (button_drop_target_min_text_size). Test: Change the language to German (Deutch) and install a new app and drag it to the workspace to see the button "App nicht vorschlagen" Test: Also, a nice way to test is to go to SecondaryDropTarget#setupUi and hardcode the option to always show "Don't suggest app" Fix: 221480721 Change-Id: I99def4e23cd17109a297aecaa620cef7a0d137e7 --- res/values/dimens.xml | 4 +++ .../android/launcher3/ButtonDropTarget.java | 36 ++++++++++++++++++- src/com/android/launcher3/DropTargetBar.java | 19 ++++++++-- src/com/android/launcher3/Utilities.java | 5 +++ 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/res/values/dimens.xml b/res/values/dimens.xml index b320bb2603..281f0e72d9 100644 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -61,6 +61,10 @@ 36dp 16dp + + 10sp + 1sp + 13dp 24dp diff --git a/src/com/android/launcher3/ButtonDropTarget.java b/src/com/android/launcher3/ButtonDropTarget.java index 69150c5e92..0b07c952bb 100644 --- a/src/com/android/launcher3/ButtonDropTarget.java +++ b/src/com/android/launcher3/ButtonDropTarget.java @@ -140,7 +140,7 @@ public abstract class ButtonDropTarget extends TextView y = -getMeasuredHeight(); message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); if (mToolTipLocation == TOOLTIP_LEFT) { - x = - getMeasuredWidth() - message.getMeasuredWidth() / 2; + x = -getMeasuredWidth() - message.getMeasuredWidth() / 2; } else { x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2; } @@ -324,6 +324,40 @@ public abstract class ButtonDropTarget extends TextView hideTooltip(); } + + /** + * Reduce the size of the text until it fits or reaches a minimum. + * + * The minimum size is defined by {@code R.dimen.button_drop_target_min_text_size} and + * it diminishes by intervals defined by + * {@code R.dimen.button_drop_target_resize_text_increment} + * This functionality is very similar to the option + * {@link TextView#setAutoSizeTextTypeWithDefaults(int)} but can't be used in this view because + * the layout width is {@code WRAP_CONTENT}. + * + * @param availableWidth Available width in the button to fit the text, used in + * {@code ButtonDropTarget#isTextTruncated(int)} + * @return The biggest text size in SP that makes the text fit or if the text can't fit returns + * the min available value + */ + public float resizeTextToFit(int availableWidth) { + float minSize = Utilities.pxToSp(getResources() + .getDimensionPixelSize(R.dimen.button_drop_target_min_text_size)); + float step = Utilities.pxToSp(getResources() + .getDimensionPixelSize(R.dimen.button_drop_target_resize_text_increment)); + float textSize = Utilities.pxToSp(getTextSize()); + + while (textSize > minSize) { + if (isTextTruncated(availableWidth)) { + textSize -= step; + setTextSize(textSize); + } else { + return textSize; + } + } + return minSize; + } + public boolean isTextTruncated(int availableWidth) { availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth() + getCompoundDrawablePadding()); diff --git a/src/com/android/launcher3/DropTargetBar.java b/src/com/android/launcher3/DropTargetBar.java index 73289fb878..b94cdbf226 100644 --- a/src/com/android/launcher3/DropTargetBar.java +++ b/src/com/android/launcher3/DropTargetBar.java @@ -140,9 +140,22 @@ public class DropTargetBar extends FrameLayout if (visibleCount > 0) { int availableWidth = width / visibleCount; boolean textVisible = true; - for (ButtonDropTarget buttons : mDropTargets) { - if (buttons.getVisibility() != GONE) { - textVisible = textVisible && !buttons.isTextTruncated(availableWidth); + boolean textResized = false; + float textSize = mDropTargets[0].getTextSize(); + for (ButtonDropTarget button : mDropTargets) { + if (button.getVisibility() == GONE) { + continue; + } + if (button.isTextTruncated(availableWidth)) { + textSize = Math.min(textSize, button.resizeTextToFit(availableWidth)); + textResized = true; + } + textVisible = textVisible && !button.isTextTruncated(availableWidth); + } + + if (textResized) { + for (ButtonDropTarget button : mDropTargets) { + button.setTextSize(textSize); } } diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java index 972a2e4fef..8192057230 100644 --- a/src/com/android/launcher3/Utilities.java +++ b/src/com/android/launcher3/Utilities.java @@ -483,6 +483,11 @@ public final class Utilities { return res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; } + /** Converts a pixel value (px) to scale pixel value (SP) for the current device. */ + public static float pxToSp(float size) { + return size / Resources.getSystem().getDisplayMetrics().scaledDensity; + } + public static float dpiFromPx(float size, int densityDpi) { float densityRatio = (float) densityDpi / DisplayMetrics.DENSITY_DEFAULT; return (size / densityRatio);