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

Commit 31f581c5 authored by Jun Mukai's avatar Jun Mukai
Browse files

Introduces mouse actions for popup menu.

- mouse hover moves the selected item in the menu. It moves
  the selection rectangle, and further up/down key or enter
  key will start from the hovered item.
- when the mouse exits from the entire popup window, the
  selection is canceled. Further up/down key will start from
  the first item.

To implement these behaviors, and consider about other keyboard
behaviors which is special to menus, I believe it justifies
to create another class for the menu popups rather than using
ListPopupWindow directly.

Bug: 19642104
Change-Id: I5e405c0491c67fdef9764898701119979ec13a9f
parent a25ecac9
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -846,6 +846,10 @@ public class ListPopupWindow {
        return mDropDownList;
    }

    DropDownListView createDropDownListView(Context context, boolean hijackFocus) {
        return new DropDownListView(context, hijackFocus);
    }

    /**
     * The maximum number of list items that can be visible and still have
     * the list expand when touched.
@@ -1068,7 +1072,7 @@ public class ListPopupWindow {
                }
            };

            mDropDownList = new DropDownListView(context, !mModal);
            mDropDownList = createDropDownListView(context, !mModal);
            if (mDropDownListHighlight != null) {
                mDropDownList.setSelector(mDropDownListHighlight);
            }
@@ -1494,7 +1498,7 @@ public class ListPopupWindow {
     * displayed on screen within a drop down. The focus is never actually
     * passed to the drop down in this mode; the list only looks focused.</p>
     */
    private static class DropDownListView extends ListView {
    static class DropDownListView extends ListView {
        /** Duration in milliseconds of the drag-to-open click animation. */
        private static final long CLICK_ANIM_DURATION = 150;

+91 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package android.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.accessibility.AccessibilityManager;

/**
 * A MenuPopupWindow represents the popup window for menu.
 *
 * MenuPopupWindow is mostly same as ListPopupWindow, but it has customized
 * behaviors specific to menus,
 *
 * @hide
 */
public class MenuPopupWindow extends ListPopupWindow {
    public MenuPopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    ListPopupWindow.DropDownListView createDropDownListView(Context context, boolean hijackFocus) {
        return new MenuDropDownListView(context, hijackFocus);
    }

    static class MenuDropDownListView extends ListPopupWindow.DropDownListView {
        private boolean mHoveredOnDisabledItem = false;
        private AccessibilityManager mAccessibilityManager;

        MenuDropDownListView(Context context, boolean hijackFocus) {
            super(context, hijackFocus);
            mAccessibilityManager = (AccessibilityManager) getContext().getSystemService(
                    Context.ACCESSIBILITY_SERVICE);
        }

        @Override
        protected boolean shouldShowSelector() {
            return (isHovered() && !mHoveredOnDisabledItem) || super.shouldShowSelector();
        }

        @Override
        public boolean onHoverEvent(MotionEvent ev) {
            mHoveredOnDisabledItem = false;

            // Accessibility system should already handle hover events and selections, menu does
            // not have to handle it by itself.
            if (mAccessibilityManager.isTouchExplorationEnabled()) {
                return super.onHoverEvent(ev);
            }

            final int action = ev.getActionMasked();
            if (action == MotionEvent.ACTION_HOVER_ENTER
                    || action == MotionEvent.ACTION_HOVER_MOVE) {
                final int position = pointToPosition((int) ev.getX(), (int) ev.getY());
                if (position != INVALID_POSITION && position != mSelectedPosition) {
                    final View hoveredItem = getChildAt(position - getFirstVisiblePosition());
                    if (hoveredItem.isEnabled()) {
                        positionSelector(position, hoveredItem);
                        setSelectedPositionInt(position);
                    } else {
                        mHoveredOnDisabledItem = true;
                    }
                    updateSelectorState();
                }
            } else {
                // Do not cancel the selected position if the selection is visible by other reasons.
                if (!super.shouldShowSelector()) {
                    setSelectedPositionInt(INVALID_POSITION);
                }
            }
            return super.onHoverEvent(ev);
        }
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.ListPopupWindow;
import android.widget.MenuPopupWindow;
import android.widget.PopupWindow;

import java.util.ArrayList;
@@ -55,7 +55,7 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
    private final int mPopupStyleRes;

    private View mAnchorView;
    private ListPopupWindow mPopup;
    private MenuPopupWindow mPopup;
    private ViewTreeObserver mTreeObserver;
    private Callback mPresenterCallback;

@@ -126,7 +126,7 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
        }
    }

    public ListPopupWindow getPopup() {
    public MenuPopupWindow getPopup() {
        return mPopup;
    }

@@ -142,7 +142,7 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
            return true;
        }

        mPopup = new ListPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes);
        mPopup = new MenuPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes);
        mPopup.setOnDismissListener(this);
        mPopup.setOnItemClickListener(this);
        mPopup.setAdapter(mAdapter);