Bug 843313: Make a StateListDrawable for more/checkmark in menuitem. [r=mfinkle]
authorSriram Ramasubramanian <sriram@mozilla.com>
Wed, 20 Feb 2013 23:07:07 -0800
changeset 122520 393bed39e9abea9d16eb6ed22b22b68b71d754da
parent 122519 7547bf5fe1666f87a2711d0fa6893635aab854dd
child 122521 1ac9186392e6c3391d696cebc5981ffdaaad282a
push id24342
push userryanvm@gmail.com
push dateThu, 21 Feb 2013 13:05:06 +0000
treeherdermozilla-central@702d2814efbf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs843313
milestone22.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 843313: Make a StateListDrawable for more/checkmark in menuitem. [r=mfinkle]
mobile/android/base/Makefile.in
mobile/android/base/MenuItemDefault.java
mobile/android/base/resources/drawable/menu_item_checkmark.xml
mobile/android/base/resources/drawable/menu_item_state.xml.in
mobile/android/base/resources/values/attrs.xml
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -247,16 +247,17 @@ FENNEC_PP_XML_FILES = \
   res/color/abouthome_section_subtitle.xml \
   res/color/abouthome_section_title.xml \
   res/color/awesome_bar_title.xml \
   res/color/awesome_bar_title_hint.xml \
   res/drawable/abouthome_logo.xml \
   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 \
   res/layout/awesomebar_suggestion_row.xml \
   res/layout/browser_toolbar.xml \
   res/layout/browser_toolbar_menu.xml \
   res/layout-land-v14/browser_toolbar.xml \
   res/layout-land-v14/browser_toolbar_menu.xml \
@@ -974,17 +975,16 @@ MOZ_ANDROID_DRAWABLES += \
   mobile/android/base/resources/drawable/highlight.xml                          \
   mobile/android/base/resources/drawable/handle_end_level.xml                   \
   mobile/android/base/resources/drawable/handle_start_level.xml                 \
   mobile/android/base/resources/drawable/ic_menu_back.xml                       \
   mobile/android/base/resources/drawable/ic_menu_desktop_mode_off.xml           \
   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                \
   mobile/android/base/resources/drawable/spinner.xml                            \
   mobile/android/base/resources/drawable/suggestion_selector.xml                \
   mobile/android/base/resources/drawable/tab_new_level.xml                      \
   mobile/android/base/resources/drawable/tab_row.xml                            \
   mobile/android/base/resources/drawable/tab_thumbnail.xml                      \
--- a/mobile/android/base/MenuItemDefault.java
+++ b/mobile/android/base/MenuItemDefault.java
@@ -12,48 +12,58 @@ import android.util.AttributeSet;
 import android.view.View;
 import android.widget.AbsListView;
 import android.widget.CheckBox;
 import android.widget.ImageView;
 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;
     private boolean mHasSubMenu = false;
 
     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);
+        setCompoundDrawables(mIcon, null, mState, null);
+    }
 
-            sChecked = res.getDrawable(R.drawable.menu_item_check);
-            sChecked.setBounds(stateIconBounds);
+    @Override
+    public int[] onCreateDrawableState(int extraSpace) {
+        final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
 
-            sUnChecked = res.getDrawable(R.drawable.menu_item_uncheck);
-            sUnChecked.setBounds(stateIconBounds);
+        if (mHasSubMenu)
+            mergeDrawableStates(drawableState, STATE_MORE);
+        else if (mCheckable && mChecked)
+            mergeDrawableStates(drawableState, STATE_CHECKED);
+        else if (mCheckable && !mChecked)
+            mergeDrawableStates(drawableState, STATE_UNCHECKED);
 
-            sMore = res.getDrawable(R.drawable.menu_item_more);
-            sMore.setBounds(stateIconBounds);
-        }
+        return drawableState;
     }
 
     @Override
     public void setIcon(Drawable icon) {
         mIcon = icon;
 
         if (mIcon != null)
             mIcon.setBounds(sIconBounds);
@@ -84,38 +94,30 @@ public class MenuItemDefault extends Tex
             mIcon.setAlpha(enabled ? 255 : 99);
 
         if (mState != null)
             mState.setAlpha(enabled ? 255 : 99);
     }
 
     @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();
+        }
     }
 }
rename from mobile/android/base/resources/drawable/menu_item_checkmark.xml
rename to mobile/android/base/resources/drawable/menu_item_state.xml.in
--- a/mobile/android/base/resources/drawable/menu_item_checkmark.xml
+++ b/mobile/android/base/resources/drawable/menu_item_state.xml.in
@@ -1,12 +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">
+<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 android:state_checked="true" android:drawable="@drawable/menu_item_check"/>
+    <item gecko:state_more="false"
+          android:state_checkable="true"
+          android:state_checked="true"
+          android:drawable="@drawable/menu_item_check"/>
 
-    <item android:drawable="@drawable/menu_item_uncheck"/>
+    <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>
--- a/mobile/android/base/resources/values/attrs.xml
+++ b/mobile/android/base/resources/values/attrs.xml
@@ -18,16 +18,20 @@
         <attr name="android:icon"/>
         <attr name="android:checkable"/>
         <attr name="android:checked"/>
         <attr name="android:visible"/>
         <attr name="android:enabled"/>
         <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>
 
     <declare-styleable name="MultiChoicePreference">
         <attr name="entries" format="string"/>
         <attr name="entryKeys" format="string"/>
         <attr name="initialValues" format="string"/>