mobile/android/base/toolbar/ShapedButton.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.toolbar;
     7 import org.mozilla.gecko.GeckoApplication;
     8 import org.mozilla.gecko.LightweightTheme;
     9 import org.mozilla.gecko.LightweightThemeDrawable;
    10 import org.mozilla.gecko.R;
    11 import org.mozilla.gecko.widget.ThemedImageButton;
    13 import android.content.Context;
    14 import android.content.res.TypedArray;
    15 import android.graphics.Canvas;
    16 import android.graphics.Path;
    17 import android.graphics.PorterDuff.Mode;
    18 import android.graphics.drawable.Drawable;
    19 import android.graphics.drawable.StateListDrawable;
    20 import android.util.AttributeSet;
    22 public class ShapedButton extends ThemedImageButton
    23                           implements CanvasDelegate.DrawManager {
    24     protected final LightweightTheme mTheme;
    26     private final Path mPath;
    27     private final CurveTowards mSide;
    29     protected final CanvasDelegate mCanvasDelegate;
    31     private enum CurveTowards { NONE, LEFT, RIGHT };
    33     public ShapedButton(Context context, AttributeSet attrs) {
    34         super(context, attrs);
    35         mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
    37         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BrowserToolbarCurve);
    38         int curveTowards = a.getInt(R.styleable.BrowserToolbarCurve_curveTowards, 0x00);
    39         a.recycle();
    41         if (curveTowards == 0x00)
    42             mSide = CurveTowards.NONE;
    43         else if (curveTowards == 0x01)
    44             mSide = CurveTowards.LEFT;
    45         else
    46             mSide = CurveTowards.RIGHT;
    48         // Path is clipped.
    49         mPath = new Path();
    50         mCanvasDelegate = new CanvasDelegate(this, Mode.DST_IN);
    52         setWillNotDraw(false);
    53     }
    55     @Override
    56     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    57         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    59         if (mSide == CurveTowards.NONE)
    60             return;
    62         final int width = getMeasuredWidth();
    63         final int height = getMeasuredHeight();
    64         final int curve = (int) (height * 1.125f);
    66         mPath.reset();
    68         if (mSide == CurveTowards.RIGHT) {
    69             mPath.moveTo(0, 0);
    70             mPath.cubicTo(curve * 0.75f, 0,
    71                           curve * 0.25f, height,
    72                           curve, height);
    73             mPath.lineTo(width, height);
    74             mPath.lineTo(width, 0);
    75             mPath.lineTo(0, 0);
    76         } else if (mSide == CurveTowards.LEFT) {
    77             mPath.moveTo(width, 0);
    78             mPath.cubicTo((width - (curve * 0.75f)), 0,
    79                           (width - (curve * 0.25f)), height,
    80                           (width - curve), height);
    81             mPath.lineTo(0, height);
    82             mPath.lineTo(0, 0);
    83         }
    84     }
    86     @Override
    87     public void draw(Canvas canvas) {
    88         if (mCanvasDelegate != null && mSide != CurveTowards.NONE)
    89             mCanvasDelegate.draw(canvas, mPath, getWidth(), getHeight());
    90         else
    91             defaultDraw(canvas);
    92     }
    94     @Override
    95     public void defaultDraw(Canvas canvas) {
    96         super.draw(canvas);
    97     }
    99     // The drawable is constructed as per @drawable/shaped_button.
   100     @Override
   101     public void onLightweightThemeChanged() {
   102         final int background = getResources().getColor(R.color.background_tabs);
   103         final LightweightThemeDrawable lightWeight = mTheme.getColorDrawable(this, background);
   105         if (lightWeight == null)
   106             return;
   108         lightWeight.setAlpha(34, 34);
   110         final StateListDrawable stateList = new StateListDrawable();
   111         stateList.addState(PRESSED_ENABLED_STATE_SET, getColorDrawable(R.color.highlight_shaped));
   112         stateList.addState(FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_shaped_focused));
   113         stateList.addState(PRIVATE_STATE_SET, getColorDrawable(R.color.background_tabs));
   114         stateList.addState(EMPTY_STATE_SET, lightWeight);
   116         setBackgroundDrawable(stateList);
   117     }
   119     @Override
   120     public void onLightweightThemeReset() {
   121         setBackgroundResource(R.drawable.shaped_button);
   122     }
   124     @Override
   125     public void setBackgroundDrawable(Drawable drawable) {
   126         if (getBackground() == null || drawable == null) {
   127             super.setBackgroundDrawable(drawable);
   128             return;
   129         }
   131         int[] padding =  new int[] { getPaddingLeft(),
   132                                      getPaddingTop(),
   133                                      getPaddingRight(),
   134                                      getPaddingBottom()
   135                                    };
   136         drawable.setLevel(getBackground().getLevel());
   137         super.setBackgroundDrawable(drawable);
   139         setPadding(padding[0], padding[1], padding[2], padding[3]);
   140     }
   142     @Override
   143     public void setBackgroundResource(int resId) {
   144         setBackgroundDrawable(getResources().getDrawable(resId));
   145     }
   146 }

mercurial