1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/toolbar/ShapedButton.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,146 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.toolbar; 1.9 + 1.10 +import org.mozilla.gecko.GeckoApplication; 1.11 +import org.mozilla.gecko.LightweightTheme; 1.12 +import org.mozilla.gecko.LightweightThemeDrawable; 1.13 +import org.mozilla.gecko.R; 1.14 +import org.mozilla.gecko.widget.ThemedImageButton; 1.15 + 1.16 +import android.content.Context; 1.17 +import android.content.res.TypedArray; 1.18 +import android.graphics.Canvas; 1.19 +import android.graphics.Path; 1.20 +import android.graphics.PorterDuff.Mode; 1.21 +import android.graphics.drawable.Drawable; 1.22 +import android.graphics.drawable.StateListDrawable; 1.23 +import android.util.AttributeSet; 1.24 + 1.25 +public class ShapedButton extends ThemedImageButton 1.26 + implements CanvasDelegate.DrawManager { 1.27 + protected final LightweightTheme mTheme; 1.28 + 1.29 + private final Path mPath; 1.30 + private final CurveTowards mSide; 1.31 + 1.32 + protected final CanvasDelegate mCanvasDelegate; 1.33 + 1.34 + private enum CurveTowards { NONE, LEFT, RIGHT }; 1.35 + 1.36 + public ShapedButton(Context context, AttributeSet attrs) { 1.37 + super(context, attrs); 1.38 + mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme(); 1.39 + 1.40 + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BrowserToolbarCurve); 1.41 + int curveTowards = a.getInt(R.styleable.BrowserToolbarCurve_curveTowards, 0x00); 1.42 + a.recycle(); 1.43 + 1.44 + if (curveTowards == 0x00) 1.45 + mSide = CurveTowards.NONE; 1.46 + else if (curveTowards == 0x01) 1.47 + mSide = CurveTowards.LEFT; 1.48 + else 1.49 + mSide = CurveTowards.RIGHT; 1.50 + 1.51 + // Path is clipped. 1.52 + mPath = new Path(); 1.53 + mCanvasDelegate = new CanvasDelegate(this, Mode.DST_IN); 1.54 + 1.55 + setWillNotDraw(false); 1.56 + } 1.57 + 1.58 + @Override 1.59 + public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 1.60 + super.onMeasure(widthMeasureSpec, heightMeasureSpec); 1.61 + 1.62 + if (mSide == CurveTowards.NONE) 1.63 + return; 1.64 + 1.65 + final int width = getMeasuredWidth(); 1.66 + final int height = getMeasuredHeight(); 1.67 + final int curve = (int) (height * 1.125f); 1.68 + 1.69 + mPath.reset(); 1.70 + 1.71 + if (mSide == CurveTowards.RIGHT) { 1.72 + mPath.moveTo(0, 0); 1.73 + mPath.cubicTo(curve * 0.75f, 0, 1.74 + curve * 0.25f, height, 1.75 + curve, height); 1.76 + mPath.lineTo(width, height); 1.77 + mPath.lineTo(width, 0); 1.78 + mPath.lineTo(0, 0); 1.79 + } else if (mSide == CurveTowards.LEFT) { 1.80 + mPath.moveTo(width, 0); 1.81 + mPath.cubicTo((width - (curve * 0.75f)), 0, 1.82 + (width - (curve * 0.25f)), height, 1.83 + (width - curve), height); 1.84 + mPath.lineTo(0, height); 1.85 + mPath.lineTo(0, 0); 1.86 + } 1.87 + } 1.88 + 1.89 + @Override 1.90 + public void draw(Canvas canvas) { 1.91 + if (mCanvasDelegate != null && mSide != CurveTowards.NONE) 1.92 + mCanvasDelegate.draw(canvas, mPath, getWidth(), getHeight()); 1.93 + else 1.94 + defaultDraw(canvas); 1.95 + } 1.96 + 1.97 + @Override 1.98 + public void defaultDraw(Canvas canvas) { 1.99 + super.draw(canvas); 1.100 + } 1.101 + 1.102 + // The drawable is constructed as per @drawable/shaped_button. 1.103 + @Override 1.104 + public void onLightweightThemeChanged() { 1.105 + final int background = getResources().getColor(R.color.background_tabs); 1.106 + final LightweightThemeDrawable lightWeight = mTheme.getColorDrawable(this, background); 1.107 + 1.108 + if (lightWeight == null) 1.109 + return; 1.110 + 1.111 + lightWeight.setAlpha(34, 34); 1.112 + 1.113 + final StateListDrawable stateList = new StateListDrawable(); 1.114 + stateList.addState(PRESSED_ENABLED_STATE_SET, getColorDrawable(R.color.highlight_shaped)); 1.115 + stateList.addState(FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_shaped_focused)); 1.116 + stateList.addState(PRIVATE_STATE_SET, getColorDrawable(R.color.background_tabs)); 1.117 + stateList.addState(EMPTY_STATE_SET, lightWeight); 1.118 + 1.119 + setBackgroundDrawable(stateList); 1.120 + } 1.121 + 1.122 + @Override 1.123 + public void onLightweightThemeReset() { 1.124 + setBackgroundResource(R.drawable.shaped_button); 1.125 + } 1.126 + 1.127 + @Override 1.128 + public void setBackgroundDrawable(Drawable drawable) { 1.129 + if (getBackground() == null || drawable == null) { 1.130 + super.setBackgroundDrawable(drawable); 1.131 + return; 1.132 + } 1.133 + 1.134 + int[] padding = new int[] { getPaddingLeft(), 1.135 + getPaddingTop(), 1.136 + getPaddingRight(), 1.137 + getPaddingBottom() 1.138 + }; 1.139 + drawable.setLevel(getBackground().getLevel()); 1.140 + super.setBackgroundDrawable(drawable); 1.141 + 1.142 + setPadding(padding[0], padding[1], padding[2], padding[3]); 1.143 + } 1.144 + 1.145 + @Override 1.146 + public void setBackgroundResource(int resId) { 1.147 + setBackgroundDrawable(getResources().getDrawable(resId)); 1.148 + } 1.149 +}