Bug 1321633 - Apply gradient based on text direction with first strong strategy, r=ahunt
authormaliu <max@mxli.us>
Thu, 29 Dec 2016 17:24:32 +0800
changeset 374521 64767e9197dffe57e1f5f10839048cf7fb5e11b8
parent 374520 bfa926f42cc5fbfb4438fa1e474c226c4f1006ad
child 374522 393ac0555624cf2e335a45fff3812e9badf32d05
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersahunt
bugs1321633
milestone53.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1321633 - Apply gradient based on text direction with first strong strategy, r=ahunt MozReview-Commit-ID: 5GMlUcp290f
mobile/android/base/java/org/mozilla/gecko/widget/FadedSingleColorTextView.java
--- a/mobile/android/base/java/org/mozilla/gecko/widget/FadedSingleColorTextView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/widget/FadedSingleColorTextView.java
@@ -4,54 +4,63 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.widget;
 
 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.LinearGradient;
 import android.graphics.Shader;
-import android.support.v4.view.ViewCompat;
+import android.support.v4.text.BidiFormatter;
 import android.util.AttributeSet;
 import android.view.View;
 
 /**
  * Fades the end of the text by gecko:fadeWidth amount,
  * if the text is too long and requires an ellipsis.
  *
  * This implementation is an improvement over Android's built-in fadingEdge
  * and the fastest of Fennec's implementations. However, it only works for
  * text of one color. It works by applying a linear gradient directly to the text.
  */
 public class FadedSingleColorTextView extends FadedTextView {
     // Shader for the fading edge.
     private FadedTextGradient mTextGradient;
+    private boolean mIsTextDirectionRtl;
 
     public FadedSingleColorTextView(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
 
     private void updateGradientShader() {
         final int color = getCurrentTextColor();
         final int width = getAvailableWidth();
 
         final boolean needsNewGradient = (mTextGradient == null ||
                                           mTextGradient.getColor() != color ||
                                           mTextGradient.getWidth() != width);
 
         final boolean needsEllipsis = needsEllipsis();
         if (needsEllipsis && needsNewGradient) {
-            final boolean isRTL = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
-            mTextGradient = new FadedTextGradient(width, fadeWidth, color, isRTL);
+            mTextGradient = new FadedTextGradient(width, fadeWidth, color, mIsTextDirectionRtl);
         }
 
         getPaint().setShader(needsEllipsis ? mTextGradient : null);
     }
 
     @Override
+    public void setText(CharSequence text, BufferType type) {
+        super.setText(text, type);
+        mIsTextDirectionRtl = BidiFormatter.getInstance().isRtl((String) text);
+        if (mIsTextDirectionRtl) {
+            setTextDirection(TEXT_DIRECTION_RTL);
+        }
+    }
+
+    @Override
     public void onDraw(Canvas canvas) {
         updateGradientShader();
         super.onDraw(canvas);
     }
 
     private static class FadedTextGradient extends LinearGradient {
         private final int mWidth;
         private final int mColor;