Bug 1390791 - Adjust text size in tab counter dynamically. r?nechen,walkingice draft
authorJing-wei Wu <topwu.tw@gmail.com>
Wed, 16 Aug 2017 17:52:19 +0800
changeset 647892 8cc156c8b7cd4aad85eb3431b2e243baefd61a00
parent 647891 97e902d8ca933fe625887cb2ebb4876a982517d2
child 647893 f3dd1f8f69adc492a7d5293b5260c74e5b88e390
push id74569
push userbmo:topwu.tw@gmail.com
push dateThu, 17 Aug 2017 01:40:57 +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,72 @@ 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);
     }
 
+    /**
+     * Adjust {@link #text} TextSize only when:
+     * 1. First time initialization;
+     * 2. Tab count changes from one digit to two digits;
+     * 3. Tab count changes from two digits to one digit.
+     */
+    private void adjustTextSize(int newCount) {
+        float ratio = 0;
+
+        if (this.count == 0) {
+            // Adjust the text size at first time initialization.
+            ratio = (newCount == MAX_VISIBLE_TABS || newCount < 10) ? ONE_DIGIT_SIZE_RATIO : TWO_DIGITS_SIZE_RATIO;
+        } else if (newCount > this.count) {
+            if (newCount == 10) {
+                // Tab count increased from one digit to two digits(9 -> 10).
+                ratio = TWO_DIGITS_SIZE_RATIO;
+            } else if (newCount == MAX_VISIBLE_TABS) {
+                // Tab count increased form two digits to one digit(98 -> ∞).
+                ratio = ONE_DIGIT_SIZE_RATIO;
+            }
+        } else if (newCount < this.count) {
+            if (this.count == 10) {
+                // Tab count decreased from two digits to one digit(10 -> 9).
+                ratio = ONE_DIGIT_SIZE_RATIO;
+            } else if (this.count == MAX_VISIBLE_TABS) {
+                // Tab count decreased from one digit to two digits(∞ -> 98).
+                ratio = TWO_DIGITS_SIZE_RATIO;
+            }
+        }
+
+        final int sizeInPixel = (int) (box.getWidth() * ratio);
+        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);