Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 648b36bc authored by Adam Powell's avatar Adam Powell Committed by Android (Google) Code Review
Browse files

Merge "Preserve spacing for icons in menus"

parents f382dc2d 9151103f
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -544,6 +544,17 @@ public class ActionMenuPresenter extends BaseMenuPresenter {
            }

            setCallback(mPopupPresenterCallback);

            boolean preserveIconSpacing = false;
            final int count = subMenu.size();
            for (int i = 0; i < count; i++) {
                MenuItem childItem = subMenu.getItem(i);
                if (childItem.isVisible() && childItem.getIcon() != null) {
                    preserveIconSpacing = true;
                    break;
                }
            }
            setForceShowIcon(preserveIconSpacing);
        }

        @Override
+23 −3
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
@@ -50,6 +51,8 @@ public class ListMenuItemView extends LinearLayout implements MenuView.ItemView
    
    private LayoutInflater mInflater;

    private boolean mForceShowIcon;

    public ListMenuItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
    
@@ -99,6 +102,10 @@ public class ListMenuItemView extends LinearLayout implements MenuView.ItemView
        setEnabled(itemData.isEnabled());
    }

    public void setForceShowIcon(boolean forceShow) {
        mPreserveIconSpacing = mForceShowIcon = forceShow;
    }

    public void setTitle(CharSequence title) {
        if (title != null) {
            mTitleView.setText(title);
@@ -189,12 +196,12 @@ public class ListMenuItemView extends LinearLayout implements MenuView.ItemView
    }
    
    public void setIcon(Drawable icon) {
        final boolean showIcon = mItemData.shouldShowIcon();
        final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon;
        if (!showIcon && !mPreserveIconSpacing) {
            return;
        }
        
        if (mIconView == null && icon == null) {
        if (mIconView == null && icon == null && !mPreserveIconSpacing) {
            return;
        }
        
@@ -213,6 +220,19 @@ public class ListMenuItemView extends LinearLayout implements MenuView.ItemView
        }
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mIconView != null && mPreserveIconSpacing) {
            // Enforce minimum icon spacing
            ViewGroup.LayoutParams lp = getLayoutParams();
            LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
            if (lp.height > 0 && iconLp.width <= 0) {
                iconLp.width = lp.height;
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void insertIconView() {
        LayoutInflater inflater = getInflater();
        mIconView = (ImageView) inflater.inflate(com.android.internal.R.layout.list_menu_item_icon,
@@ -241,7 +261,7 @@ public class ListMenuItemView extends LinearLayout implements MenuView.ItemView
    }

    public boolean showsIcon() {
        return false;
        return mForceShowIcon;
    }
    
    private LayoutInflater getInflater() {
+29 −2
Original line number Diff line number Diff line
@@ -19,15 +19,16 @@ package com.android.internal.view.menu;
import android.content.Context;
import android.content.res.Resources;
import android.os.Parcelable;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
@@ -58,6 +59,10 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On

    private Callback mPresenterCallback;

    boolean mForceShowIcon;

    private ViewGroup mMeasureParent;

    public MenuPopupHelper(Context context, MenuBuilder menu) {
        this(context, menu, null, false);
    }
@@ -86,6 +91,10 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
        mAnchorView = anchor;
    }

    public void setForceShowIcon(boolean forceShow) {
        mForceShowIcon = forceShow;
    }

    public void show() {
        if (!tryShow()) {
            throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
@@ -170,7 +179,10 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
                itemType = positionType;
                itemView = null;
            }
            itemView = adapter.getView(i, itemView, null);
            if (mMeasureParent == null) {
                mMeasureParent = new FrameLayout(mContext);
            }
            itemView = adapter.getView(i, itemView, mMeasureParent);
            itemView.measure(widthMeasureSpec, heightMeasureSpec);
            width = Math.max(width, itemView.getMeasuredWidth());
        }
@@ -228,6 +240,18 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
        if (subMenu.hasVisibleItems()) {
            MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView, false);
            subPopup.setCallback(mPresenterCallback);

            boolean preserveIconSpacing = false;
            final int count = subMenu.size();
            for (int i = 0; i < count; i++) {
                MenuItem childItem = subMenu.getItem(i);
                if (childItem.isVisible() && childItem.getIcon() != null) {
                    preserveIconSpacing = true;
                    break;
                }
            }
            subPopup.setForceShowIcon(preserveIconSpacing);

            if (subPopup.tryShow()) {
                if (mPresenterCallback != null) {
                    mPresenterCallback.onOpenSubMenu(subMenu);
@@ -293,6 +317,9 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
            }

            MenuView.ItemView itemView = (MenuView.ItemView) convertView;
            if (mForceShowIcon) {
                ((ListMenuItemView) convertView).setForceShowIcon(true);
            }
            itemView.initialize(getItem(position), 0);
            return convertView;
        }
+3 −1
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginRight="8dip"
    android:layout_marginLeft="8dip"
    android:layout_marginRight="-8dip"
    android:scaleType="center"
    android:duplicateParentState="true" />
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@
    android:layout_width="match_parent"
    android:layout_height="?android:attr/dropdownListPreferredItemHeight"
    android:minWidth="196dip"
    android:paddingLeft="16dip"
    android:paddingRight="16dip">
    
    <!-- Icon will be inserted here. -->
@@ -29,6 +28,7 @@
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dip"
        android:duplicateParentState="true">
        
        <TextView