|
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.menu; |
|
6 |
|
7 import org.mozilla.gecko.R; |
|
8 |
|
9 import android.content.Context; |
|
10 import android.content.res.Resources; |
|
11 import android.graphics.Rect; |
|
12 import android.graphics.drawable.Drawable; |
|
13 import android.util.AttributeSet; |
|
14 import android.widget.TextView; |
|
15 |
|
16 public class MenuItemDefault extends TextView |
|
17 implements GeckoMenuItem.Layout { |
|
18 private static final int[] STATE_MORE = new int[] { R.attr.state_more }; |
|
19 private static final int[] STATE_CHECKED = new int[] { android.R.attr.state_checkable, android.R.attr.state_checked }; |
|
20 private static final int[] STATE_UNCHECKED = new int[] { android.R.attr.state_checkable }; |
|
21 |
|
22 private Drawable mIcon; |
|
23 private Drawable mState; |
|
24 private static Rect sIconBounds; |
|
25 |
|
26 private boolean mCheckable = false; |
|
27 private boolean mChecked = false; |
|
28 private boolean mHasSubMenu = false; |
|
29 private boolean mShowIcon = false; |
|
30 |
|
31 public MenuItemDefault(Context context) { |
|
32 this(context, null); |
|
33 } |
|
34 |
|
35 public MenuItemDefault(Context context, AttributeSet attrs) { |
|
36 this(context, attrs, R.attr.menuItemDefaultStyle); |
|
37 } |
|
38 |
|
39 public MenuItemDefault(Context context, AttributeSet attrs, int defStyle) { |
|
40 super(context, attrs, defStyle); |
|
41 |
|
42 Resources res = getResources(); |
|
43 int width = res.getDimensionPixelSize(R.dimen.menu_item_row_width); |
|
44 int height = res.getDimensionPixelSize(R.dimen.menu_item_row_height); |
|
45 setMinimumWidth(width); |
|
46 setMinimumHeight(height); |
|
47 |
|
48 int stateIconSize = res.getDimensionPixelSize(R.dimen.menu_item_state_icon); |
|
49 Rect stateIconBounds = new Rect(0, 0, stateIconSize, stateIconSize); |
|
50 |
|
51 mState = res.getDrawable(R.drawable.menu_item_state).mutate(); |
|
52 mState.setBounds(stateIconBounds); |
|
53 |
|
54 if (sIconBounds == null) { |
|
55 int iconSize = res.getDimensionPixelSize(R.dimen.menu_item_icon); |
|
56 sIconBounds = new Rect(0, 0, iconSize, iconSize); |
|
57 } |
|
58 |
|
59 setCompoundDrawables(mIcon, null, mState, null); |
|
60 } |
|
61 |
|
62 @Override |
|
63 public int[] onCreateDrawableState(int extraSpace) { |
|
64 final int[] drawableState = super.onCreateDrawableState(extraSpace + 2); |
|
65 |
|
66 if (mHasSubMenu) |
|
67 mergeDrawableStates(drawableState, STATE_MORE); |
|
68 else if (mCheckable && mChecked) |
|
69 mergeDrawableStates(drawableState, STATE_CHECKED); |
|
70 else if (mCheckable && !mChecked) |
|
71 mergeDrawableStates(drawableState, STATE_UNCHECKED); |
|
72 |
|
73 return drawableState; |
|
74 } |
|
75 |
|
76 @Override |
|
77 public void initialize(GeckoMenuItem item) { |
|
78 if (item == null) |
|
79 return; |
|
80 |
|
81 setTitle(item.getTitle()); |
|
82 setIcon(item.getIcon()); |
|
83 setEnabled(item.isEnabled()); |
|
84 setCheckable(item.isCheckable()); |
|
85 setChecked(item.isChecked()); |
|
86 setSubMenuIndicator(item.hasSubMenu()); |
|
87 } |
|
88 |
|
89 private void refreshIcon() { |
|
90 setCompoundDrawables(mShowIcon ? mIcon : null, null, mState, null); |
|
91 } |
|
92 |
|
93 void setIcon(Drawable icon) { |
|
94 mIcon = icon; |
|
95 |
|
96 if (mIcon != null) { |
|
97 mIcon.setBounds(sIconBounds); |
|
98 mIcon.setAlpha(isEnabled() ? 255 : 99); |
|
99 } |
|
100 |
|
101 refreshIcon(); |
|
102 } |
|
103 |
|
104 void setIcon(int icon) { |
|
105 setIcon((icon == 0) ? null : getResources().getDrawable(icon)); |
|
106 } |
|
107 |
|
108 void setTitle(CharSequence title) { |
|
109 setText(title); |
|
110 } |
|
111 |
|
112 @Override |
|
113 public void setEnabled(boolean enabled) { |
|
114 super.setEnabled(enabled); |
|
115 |
|
116 if (mIcon != null) |
|
117 mIcon.setAlpha(enabled ? 255 : 99); |
|
118 |
|
119 if (mState != null) |
|
120 mState.setAlpha(enabled ? 255 : 99); |
|
121 } |
|
122 |
|
123 private void setCheckable(boolean checkable) { |
|
124 if (mCheckable != checkable) { |
|
125 mCheckable = checkable; |
|
126 refreshDrawableState(); |
|
127 } |
|
128 } |
|
129 |
|
130 private void setChecked(boolean checked) { |
|
131 if (mChecked != checked) { |
|
132 mChecked = checked; |
|
133 refreshDrawableState(); |
|
134 } |
|
135 } |
|
136 |
|
137 @Override |
|
138 public void setShowIcon(boolean show) { |
|
139 if (mShowIcon != show) { |
|
140 mShowIcon = show; |
|
141 refreshIcon(); |
|
142 } |
|
143 } |
|
144 |
|
145 void setSubMenuIndicator(boolean hasSubMenu) { |
|
146 if (mHasSubMenu != hasSubMenu) { |
|
147 mHasSubMenu = hasSubMenu; |
|
148 refreshDrawableState(); |
|
149 } |
|
150 } |
|
151 } |