michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.toolbar; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.graphics.Canvas; michael@0: import android.graphics.LinearGradient; michael@0: import android.graphics.Paint; michael@0: import android.graphics.Path; michael@0: import android.graphics.Shader; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.graphics.drawable.StateListDrawable; michael@0: import android.util.AttributeSet; michael@0: michael@0: public class BackButton extends ShapedButton { michael@0: private Path mPath; michael@0: private Path mBorderPath; michael@0: private Paint mBorderPaint; michael@0: private Paint mBorderPrivatePaint; michael@0: michael@0: public BackButton(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: michael@0: // Paint to draw the border. michael@0: mBorderPaint = new Paint(); michael@0: mBorderPaint.setAntiAlias(true); michael@0: mBorderPaint.setColor(0xFF000000); michael@0: mBorderPaint.setStyle(Paint.Style.STROKE); michael@0: michael@0: mBorderPrivatePaint = new Paint(mBorderPaint); michael@0: michael@0: // Path is masked. michael@0: mPath = new Path(); michael@0: mBorderPath = new Path(); michael@0: } michael@0: michael@0: @Override michael@0: protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { michael@0: super.onSizeChanged(width, height, oldWidth, oldHeight); michael@0: michael@0: mPath.reset(); michael@0: mPath.addCircle(width/2, height/2, width/2, Path.Direction.CW); michael@0: michael@0: float borderWidth = getContext().getResources().getDimension(R.dimen.nav_button_border_width); michael@0: mBorderPaint.setStrokeWidth(borderWidth); michael@0: mBorderPrivatePaint.setStrokeWidth(borderWidth); michael@0: michael@0: mBorderPath.reset(); michael@0: mBorderPath.addCircle(width/2, height/2, (width/2) - borderWidth, Path.Direction.CW); michael@0: michael@0: mBorderPaint.setShader(new LinearGradient(0, 0, michael@0: 0, height, michael@0: 0xFFB5BBC1, 0xFFFAFBFC, michael@0: Shader.TileMode.CLAMP)); michael@0: michael@0: mBorderPrivatePaint.setShader(new LinearGradient(0, 0, michael@0: 0, height, michael@0: 0xFF040607, 0xFF0B0D0E, michael@0: Shader.TileMode.CLAMP)); michael@0: } michael@0: michael@0: @Override michael@0: public void draw(Canvas canvas) { michael@0: mCanvasDelegate.draw(canvas, mPath, getWidth(), getHeight()); michael@0: michael@0: // Draw the border on top. michael@0: canvas.drawPath(mBorderPath, isPrivateMode() ? mBorderPrivatePaint : mBorderPaint); michael@0: } michael@0: michael@0: // The drawable is constructed as per @drawable/url_bar_nav_button. michael@0: @Override michael@0: public void onLightweightThemeChanged() { michael@0: final Drawable drawable = mTheme.getDrawable(this); michael@0: if (drawable == null) michael@0: return; michael@0: michael@0: final StateListDrawable stateList = new StateListDrawable(); michael@0: stateList.addState(PRIVATE_PRESSED_STATE_SET, getColorDrawable(R.color.highlight_nav_pb)); michael@0: stateList.addState(PRESSED_ENABLED_STATE_SET, getColorDrawable(R.color.highlight_nav)); michael@0: stateList.addState(PRIVATE_FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_nav_focused_pb)); michael@0: stateList.addState(FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_nav_focused)); michael@0: stateList.addState(PRIVATE_STATE_SET, getColorDrawable(R.color.background_private)); michael@0: stateList.addState(EMPTY_STATE_SET, drawable); michael@0: michael@0: setBackgroundDrawable(stateList); michael@0: } michael@0: michael@0: @Override michael@0: public void onLightweightThemeReset() { michael@0: setBackgroundResource(R.drawable.url_bar_nav_button); michael@0: } michael@0: }