Bug 843313: Make a StateListDrawable for more/checkmark in menuitem. [r=mfinkle]

--HG--
rename : mobile/android/base/resources/drawable/menu_item_checkmark.xml => mobile/android/base/resources/drawable/menu_item_state.xml.in
This commit is contained in:
Sriram Ramasubramanian 2013-02-20 23:07:07 -08:00
parent a15a55cf42
commit 23f2aba285
5 changed files with 69 additions and 50 deletions

View File

@ -252,6 +252,7 @@ FENNEC_PP_XML_FILES = \
res/drawable/address_bar_bg.xml \
res/drawable/address_bar_nav_button.xml \
res/drawable/address_bar_url.xml \
res/drawable/menu_item_state.xml \
res/drawable/menu_level.xml \
res/layout/abouthome_content.xml \
res/layout/awesomebar_search.xml \
@ -979,7 +980,6 @@ MOZ_ANDROID_DRAWABLES += \
mobile/android/base/resources/drawable/ic_menu_desktop_mode_on.xml \
mobile/android/base/resources/drawable/ic_menu_quit.xml \
mobile/android/base/resources/drawable/menu_button.xml \
mobile/android/base/resources/drawable/menu_item_checkmark.xml \
mobile/android/base/resources/drawable/progress_spinner.xml \
mobile/android/base/resources/drawable/remote_tabs_child_divider.xml \
mobile/android/base/resources/drawable/site_security_level.xml \

View File

@ -17,13 +17,13 @@ import android.widget.TextView;
public class MenuItemDefault extends TextView
implements GeckoMenuItem.Layout {
private static Rect sIconBounds;
private static Drawable sChecked;
private static Drawable sUnChecked;
private static Drawable sMore;
private static final int[] STATE_MORE = new int[] { R.attr.state_more };
private static final int[] STATE_CHECKED = new int[] { android.R.attr.state_checkable, android.R.attr.state_checked };
private static final int[] STATE_UNCHECKED = new int[] { android.R.attr.state_checkable };
private Drawable mIcon;
private Drawable mState;
private static Rect sIconBounds;
private boolean mCheckable = false;
private boolean mChecked = false;
@ -32,23 +32,33 @@ public class MenuItemDefault extends TextView
public MenuItemDefault(Context context, AttributeSet attrs) {
super(context, attrs);
Resources res = context.getResources();
int stateIconSize = res.getDimensionPixelSize(R.dimen.menu_item_state_icon);
Rect stateIconBounds = new Rect(0, 0, stateIconSize, stateIconSize);
mState = res.getDrawable(R.drawable.menu_item_state);
mState.setBounds(stateIconBounds);
if (sIconBounds == null) {
Resources res = context.getResources();
int iconSize = res.getDimensionPixelSize(R.dimen.menu_item_icon);
sIconBounds = new Rect(0, 0, iconSize, iconSize);
int stateIconSize = res.getDimensionPixelSize(R.dimen.menu_item_state_icon);
Rect stateIconBounds = new Rect(0, 0, stateIconSize, stateIconSize);
sChecked = res.getDrawable(R.drawable.menu_item_check);
sChecked.setBounds(stateIconBounds);
sUnChecked = res.getDrawable(R.drawable.menu_item_uncheck);
sUnChecked.setBounds(stateIconBounds);
sMore = res.getDrawable(R.drawable.menu_item_more);
sMore.setBounds(stateIconBounds);
}
setCompoundDrawables(mIcon, null, mState, null);
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
if (mHasSubMenu)
mergeDrawableStates(drawableState, STATE_MORE);
else if (mCheckable && mChecked)
mergeDrawableStates(drawableState, STATE_CHECKED);
else if (mCheckable && !mChecked)
mergeDrawableStates(drawableState, STATE_UNCHECKED);
return drawableState;
}
@Override
@ -89,33 +99,25 @@ public class MenuItemDefault extends TextView
@Override
public void setCheckable(boolean checkable) {
mCheckable = checkable;
refreshState();
}
private void refreshState() {
if (mHasSubMenu)
mState = sMore;
else if (mCheckable && mChecked)
mState = sChecked;
else if (mCheckable && !mChecked)
mState = sUnChecked;
else
mState = null;
setCompoundDrawables(mIcon, null, mState, null);
if (mCheckable != checkable) {
mCheckable = checkable;
refreshDrawableState();
}
}
@Override
public void setChecked(boolean checked) {
mChecked = checked;
refreshState();
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
}
}
@Override
public void setSubMenuIndicator(boolean hasSubMenu) {
mHasSubMenu = hasSubMenu;
mState = sMore;
refreshState();
if (mHasSubMenu != hasSubMenu) {
mHasSubMenu = hasSubMenu;
refreshDrawableState();
}
}
}

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/menu_item_check"/>
<item android:drawable="@drawable/menu_item_uncheck"/>
</selector>

View File

@ -0,0 +1,25 @@
#filter substitution
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gecko="http://schemas.android.com/apk/res/@ANDROID_PACKAGE_NAME@">
<item gecko:state_more="true"
android:drawable="@drawable/menu_item_more"/>
<item gecko:state_more="false"
android:state_checkable="true"
android:state_checked="true"
android:drawable="@drawable/menu_item_check"/>
<item gecko:state_more="false"
android:state_checkable="true"
android:state_checked="false"
android:drawable="@drawable/menu_item_uncheck"/>
<item android:drawable="@android:color/transparent"/>
</selector>

View File

@ -23,6 +23,10 @@
<attr name="android:showAsAction"/>
</declare-styleable>
<declare-styleable name="MenuItemDefault">
<attr name="state_more" format="boolean"/>
</declare-styleable>
<declare-styleable name="FlowLayout">
<attr name="spacing" format="dimension"/>
</declare-styleable>