[automerge] Make the text for the drop target buttons fit if it's too long. 2p: d2d8e97a65
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/17454967 Change-Id: I2780d24f4e7e4032a17dcadde5f5b08d945f8cc5
This commit is contained in:
@@ -61,6 +61,10 @@
|
|||||||
<dimen name="drop_target_top_margin">36dp</dimen>
|
<dimen name="drop_target_top_margin">36dp</dimen>
|
||||||
<dimen name="drop_target_bottom_margin">16dp</dimen>
|
<dimen name="drop_target_bottom_margin">16dp</dimen>
|
||||||
|
|
||||||
|
<!-- Button drop target bar -->
|
||||||
|
<dimen name="button_drop_target_min_text_size">10sp</dimen>
|
||||||
|
<dimen name="button_drop_target_resize_text_increment">1sp</dimen>
|
||||||
|
|
||||||
<!-- App Widget resize frame -->
|
<!-- App Widget resize frame -->
|
||||||
<dimen name="widget_handle_margin">13dp</dimen>
|
<dimen name="widget_handle_margin">13dp</dimen>
|
||||||
<dimen name="resize_frame_background_padding">24dp</dimen>
|
<dimen name="resize_frame_background_padding">24dp</dimen>
|
||||||
|
|||||||
@@ -140,7 +140,7 @@ public abstract class ButtonDropTarget extends TextView
|
|||||||
y = -getMeasuredHeight();
|
y = -getMeasuredHeight();
|
||||||
message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
|
message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
|
||||||
if (mToolTipLocation == TOOLTIP_LEFT) {
|
if (mToolTipLocation == TOOLTIP_LEFT) {
|
||||||
x = - getMeasuredWidth() - message.getMeasuredWidth() / 2;
|
x = -getMeasuredWidth() - message.getMeasuredWidth() / 2;
|
||||||
} else {
|
} else {
|
||||||
x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2;
|
x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2;
|
||||||
}
|
}
|
||||||
@@ -324,6 +324,40 @@ public abstract class ButtonDropTarget extends TextView
|
|||||||
hideTooltip();
|
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) {
|
public boolean isTextTruncated(int availableWidth) {
|
||||||
availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
|
availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
|
||||||
+ getCompoundDrawablePadding());
|
+ getCompoundDrawablePadding());
|
||||||
|
|||||||
@@ -140,9 +140,22 @@ public class DropTargetBar extends FrameLayout
|
|||||||
if (visibleCount > 0) {
|
if (visibleCount > 0) {
|
||||||
int availableWidth = width / visibleCount;
|
int availableWidth = width / visibleCount;
|
||||||
boolean textVisible = true;
|
boolean textVisible = true;
|
||||||
for (ButtonDropTarget buttons : mDropTargets) {
|
boolean textResized = false;
|
||||||
if (buttons.getVisibility() != GONE) {
|
float textSize = mDropTargets[0].getTextSize();
|
||||||
textVisible = textVisible && !buttons.isTextTruncated(availableWidth);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -483,6 +483,11 @@ public final class Utilities {
|
|||||||
return res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
|
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) {
|
public static float dpiFromPx(float size, int densityDpi) {
|
||||||
float densityRatio = (float) densityDpi / DisplayMetrics.DENSITY_DEFAULT;
|
float densityRatio = (float) densityDpi / DisplayMetrics.DENSITY_DEFAULT;
|
||||||
return (size / densityRatio);
|
return (size / densityRatio);
|
||||||
|
|||||||
Reference in New Issue
Block a user