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.

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

mercurial