Bug 1390791 - Adjust text size in tab counter dynamically. r?nechen,walkingice draft
authorJing-wei Wu <topwu.tw@gmail.com>
Mon, 21 Aug 2017 09:27:32 +0800
changeset 649634 33e238b142d180de7b8758be288789aff6174b9f
parent 649633 55164c4fb7ea2a6d57bdf314cfe2cde09849578b
child 727117 90e3f50126e4bb76eb32d4de92c4f2be77203065
push id75075
push userbmo:topwu.tw@gmail.com
push dateMon, 21 Aug 2017 01:30:58 +0000
reviewersnechen, walkingice
bugs1390791
milestone57.0a1
Bug 1390791 - Adjust text size in tab counter dynamically. r?nechen,walkingice MozReview-Commit-ID: KUDMEJSGOOk
mobile/android/app/src/photon/java/org/mozilla/gecko/toolbar/TabCounter.java
--- a/mobile/android/app/src/photon/java/org/mozilla/gecko/toolbar/TabCounter.java
+++ b/mobile/android/app/src/photon/java/org/mozilla/gecko/toolbar/TabCounter.java
@@ -6,16 +6,17 @@
 package org.mozilla.gecko.toolbar;
 
 import android.animation.Animator;
 import android.animation.AnimatorSet;
 import android.animation.ObjectAnimator;
 import android.content.Context;
 import android.support.annotation.NonNull;
 import android.util.AttributeSet;
+import android.util.TypedValue;
 import android.view.LayoutInflater;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.widget.themed.ThemedImageView;
 import org.mozilla.gecko.widget.themed.ThemedRelativeLayout;
 import org.mozilla.gecko.widget.themed.ThemedTextView;
 
 public class TabCounter extends ThemedRelativeLayout {
@@ -25,16 +26,19 @@ public class TabCounter extends ThemedRe
     private final ThemedTextView text;
 
     private final AnimatorSet animationSet;
     private int count;
 
     public static final int MAX_VISIBLE_TABS = 99;
     public static final String SO_MANY_TABS_OPEN = "∞";
 
+    private static final float ONE_DIGIT_SIZE_RATIO = 0.7f;
+    private static final float TWO_DIGITS_SIZE_RATIO = 0.5f;
+
     public TabCounter(Context context) {
         this(context, null);
     }
 
     public TabCounter(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
     }
 
@@ -167,31 +171,48 @@ public class TabCounter extends ThemedRe
         }
 
         // don't animate if there are still over MAX_VISIBLE_TABS tabs open
         if (this.count > MAX_VISIBLE_TABS && count > MAX_VISIBLE_TABS) {
             this.count = count;
             return;
         }
 
+        adjustTextSize(count);
+
         text.setText(formatForDisplay(count));
         this.count = count;
 
         // Trigger animation
         animationSet.start();
     }
 
     private String formatForDisplay(int count) {
         if (count > MAX_VISIBLE_TABS) {
             return SO_MANY_TABS_OPEN;
         }
         return String.valueOf(count);
     }
 
+    private void adjustTextSize(int newCount) {
+        final float oldRatio = (this.count < MAX_VISIBLE_TABS && this.count >= 10) ? TWO_DIGITS_SIZE_RATIO : ONE_DIGIT_SIZE_RATIO;
+        final float newRatio = (newCount < MAX_VISIBLE_TABS && newCount >= 10) ? TWO_DIGITS_SIZE_RATIO : ONE_DIGIT_SIZE_RATIO;
+
+        if (this.count == 0 || newRatio != oldRatio) {
+            final int sizeInPixel = (int) (box.getWidth() * newRatio);
+            if (sizeInPixel > 0) {
+                // Only apply the size when we calculate a valid value.
+                text.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizeInPixel);
+            }
+        }
+    }
+
     void setCount(int count) {
+        adjustTextSize(count);
+
         text.setText(formatForDisplay(count));
         this.count = count;
     }
 
     @Override
     public void setPrivateMode(boolean isPrivate) {
         super.setPrivateMode(isPrivate);