|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.toolbar; |
|
6 |
|
7 import org.mozilla.gecko.R; |
|
8 |
|
9 import android.content.Context; |
|
10 import android.graphics.Canvas; |
|
11 import android.graphics.LinearGradient; |
|
12 import android.graphics.Paint; |
|
13 import android.graphics.Path; |
|
14 import android.graphics.Shader; |
|
15 import android.graphics.drawable.Drawable; |
|
16 import android.graphics.drawable.StateListDrawable; |
|
17 import android.util.AttributeSet; |
|
18 |
|
19 public class BackButton extends ShapedButton { |
|
20 private Path mPath; |
|
21 private Path mBorderPath; |
|
22 private Paint mBorderPaint; |
|
23 private Paint mBorderPrivatePaint; |
|
24 |
|
25 public BackButton(Context context, AttributeSet attrs) { |
|
26 super(context, attrs); |
|
27 |
|
28 // Paint to draw the border. |
|
29 mBorderPaint = new Paint(); |
|
30 mBorderPaint.setAntiAlias(true); |
|
31 mBorderPaint.setColor(0xFF000000); |
|
32 mBorderPaint.setStyle(Paint.Style.STROKE); |
|
33 |
|
34 mBorderPrivatePaint = new Paint(mBorderPaint); |
|
35 |
|
36 // Path is masked. |
|
37 mPath = new Path(); |
|
38 mBorderPath = new Path(); |
|
39 } |
|
40 |
|
41 @Override |
|
42 protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { |
|
43 super.onSizeChanged(width, height, oldWidth, oldHeight); |
|
44 |
|
45 mPath.reset(); |
|
46 mPath.addCircle(width/2, height/2, width/2, Path.Direction.CW); |
|
47 |
|
48 float borderWidth = getContext().getResources().getDimension(R.dimen.nav_button_border_width); |
|
49 mBorderPaint.setStrokeWidth(borderWidth); |
|
50 mBorderPrivatePaint.setStrokeWidth(borderWidth); |
|
51 |
|
52 mBorderPath.reset(); |
|
53 mBorderPath.addCircle(width/2, height/2, (width/2) - borderWidth, Path.Direction.CW); |
|
54 |
|
55 mBorderPaint.setShader(new LinearGradient(0, 0, |
|
56 0, height, |
|
57 0xFFB5BBC1, 0xFFFAFBFC, |
|
58 Shader.TileMode.CLAMP)); |
|
59 |
|
60 mBorderPrivatePaint.setShader(new LinearGradient(0, 0, |
|
61 0, height, |
|
62 0xFF040607, 0xFF0B0D0E, |
|
63 Shader.TileMode.CLAMP)); |
|
64 } |
|
65 |
|
66 @Override |
|
67 public void draw(Canvas canvas) { |
|
68 mCanvasDelegate.draw(canvas, mPath, getWidth(), getHeight()); |
|
69 |
|
70 // Draw the border on top. |
|
71 canvas.drawPath(mBorderPath, isPrivateMode() ? mBorderPrivatePaint : mBorderPaint); |
|
72 } |
|
73 |
|
74 // The drawable is constructed as per @drawable/url_bar_nav_button. |
|
75 @Override |
|
76 public void onLightweightThemeChanged() { |
|
77 final Drawable drawable = mTheme.getDrawable(this); |
|
78 if (drawable == null) |
|
79 return; |
|
80 |
|
81 final StateListDrawable stateList = new StateListDrawable(); |
|
82 stateList.addState(PRIVATE_PRESSED_STATE_SET, getColorDrawable(R.color.highlight_nav_pb)); |
|
83 stateList.addState(PRESSED_ENABLED_STATE_SET, getColorDrawable(R.color.highlight_nav)); |
|
84 stateList.addState(PRIVATE_FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_nav_focused_pb)); |
|
85 stateList.addState(FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_nav_focused)); |
|
86 stateList.addState(PRIVATE_STATE_SET, getColorDrawable(R.color.background_private)); |
|
87 stateList.addState(EMPTY_STATE_SET, drawable); |
|
88 |
|
89 setBackgroundDrawable(stateList); |
|
90 } |
|
91 |
|
92 @Override |
|
93 public void onLightweightThemeReset() { |
|
94 setBackgroundResource(R.drawable.url_bar_nav_button); |
|
95 } |
|
96 } |