Redesign the ContextMenu to meet matrial spec

The new ContextMenu follows the following spec.
https://m3.material.io/components/menus/specs#cd6409d5-9523-4099-9db7-42d1e1d2d194

The new ContextMenu is implemented behind the feature flag and it is
off by default.

Bug: 240385199
Test: Manually done
Change-Id: I7c5c05899bd4cfb8678aafd2e50c53d84cdd43c7
This commit is contained in:
Seigo Nonaka
2023-03-23 15:05:12 +09:00
parent c46d48e7b2
commit ca2ac2b497
5 changed files with 135 additions and 6 deletions

View File

@@ -5,12 +5,14 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.StyleRes;
import android.app.AppGlobals;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Parcelable;
import android.os.SystemClock;
import android.text.TextFlags;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
@@ -46,8 +48,6 @@ import java.util.List;
*/
final class CascadingMenuPopup extends MenuPopup implements MenuPresenter, OnKeyListener,
PopupWindow.OnDismissListener {
private static final int ITEM_LAYOUT = com.android.internal.R.layout.cascading_menu_item_layout;
@Retention(RetentionPolicy.SOURCE)
@IntDef({HORIZ_POSITION_LEFT, HORIZ_POSITION_RIGHT})
public @interface HorizPosition {}
@@ -190,6 +190,7 @@ final class CascadingMenuPopup extends MenuPopup implements MenuPresenter, OnKey
private Callback mPresenterCallback;
private ViewTreeObserver mTreeObserver;
private PopupWindow.OnDismissListener mOnDismissListener;
private final int mItemLayout;
/** Whether popup menus should disable exit animations when closing. */
private boolean mShouldCloseImmediately;
@@ -215,6 +216,12 @@ final class CascadingMenuPopup extends MenuPopup implements MenuPresenter, OnKey
res.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth));
mSubMenuHoverHandler = new Handler();
mItemLayout = AppGlobals.getIntCoreSetting(
TextFlags.KEY_ENABLE_NEW_CONTEXT_MENU,
TextFlags.ENABLE_NEW_CONTEXT_MENU_DEFAULT ? 1 : 0) != 0
? com.android.internal.R.layout.cascading_menu_item_layout_material
: com.android.internal.R.layout.cascading_menu_item_layout;
}
@Override
@@ -348,7 +355,7 @@ final class CascadingMenuPopup extends MenuPopup implements MenuPresenter, OnKey
*/
private void showMenu(@NonNull MenuBuilder menu) {
final LayoutInflater inflater = LayoutInflater.from(mContext);
final MenuAdapter adapter = new MenuAdapter(menu, inflater, mOverflowOnly, ITEM_LAYOUT);
final MenuAdapter adapter = new MenuAdapter(menu, inflater, mOverflowOnly, mItemLayout);
// Apply "force show icon" setting. There are 3 cases:
// (1) This is the top level menu and icon spacing is forced. Add spacing.

View File

@@ -16,12 +16,12 @@
package com.android.internal.view.menu;
import com.android.internal.R;
import android.app.AppGlobals;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextFlags;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
@@ -61,6 +61,8 @@ public class ListMenuItemView extends LinearLayout
private int mMenuType;
private boolean mUseNewContextMenu;
private LayoutInflater mInflater;
private boolean mForceShowIcon;
@@ -87,6 +89,10 @@ public class ListMenuItemView extends LinearLayout
a.recycle();
b.recycle();
mUseNewContextMenu = AppGlobals.getIntCoreSetting(
TextFlags.KEY_ENABLE_NEW_CONTEXT_MENU,
TextFlags.ENABLE_NEW_CONTEXT_MENU_DEFAULT ? 1 : 0) != 0;
}
public ListMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
@@ -283,7 +289,9 @@ public class ListMenuItemView extends LinearLayout
private void insertIconView() {
LayoutInflater inflater = getInflater();
mIconView = (ImageView) inflater.inflate(com.android.internal.R.layout.list_menu_item_icon,
mIconView = (ImageView) inflater.inflate(
mUseNewContextMenu ? com.android.internal.R.layout.list_menu_item_fixed_size_icon :
com.android.internal.R.layout.list_menu_item_icon,
this, false);
addContentView(mIconView, 0);
}

View File

@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2023 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.
-->
<!-- Keep in sync with popup_menu_item_layout.xml (which only differs in the title and shortcut
position). -->
<com.android.internal.view.menu.ListMenuItemView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="112dip"
android:maxWidth="280dip"
android:orientation="vertical" >
<ImageView
android:id="@+id/group_divider"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"
android:background="@drawable/list_divider_material" />
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_marginStart="12dip"
android:layout_marginEnd="12dip"
android:duplicateParentState="true" >
<!-- Icon will be inserted here. -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="6dip"
android:textAppearance="?attr/textAppearanceLargePopupMenu"
android:singleLine="true"
android:duplicateParentState="true"
android:textAlignment="viewStart" />
<Space
android:layout_width="0dip"
android:layout_height="1dip"
android:layout_weight="1"/>
<TextView
android:id="@+id/shortcut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dip"
android:textAppearance="?attr/textAppearanceSmallPopupMenu"
android:singleLine="true"
android:duplicateParentState="true"
android:textAlignment="viewEnd" />
<ImageView
android:id="@+id/submenuarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:scaleType="center"
android:visibility="gone" />
<!-- Checkbox, and/or radio button will be inserted here. -->
</LinearLayout>
</com.android.internal.view.menu.ListMenuItemView>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2023 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.
-->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:layout_marginStart="0dip"
android:layout_marginEnd="6dip"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:scaleType="centerInside"
android:duplicateParentState="true" />

View File

@@ -1475,6 +1475,7 @@
<java-symbol type="layout" name="action_mode_close_item" />
<java-symbol type="layout" name="alert_dialog" />
<java-symbol type="layout" name="cascading_menu_item_layout" />
<java-symbol type="layout" name="cascading_menu_item_layout_material" />
<java-symbol type="layout" name="choose_account" />
<java-symbol type="layout" name="choose_account_row" />
<java-symbol type="layout" name="choose_account_type" />
@@ -1524,6 +1525,7 @@
<java-symbol type="layout" name="list_content_simple" />
<java-symbol type="layout" name="list_menu_item_checkbox" />
<java-symbol type="layout" name="list_menu_item_icon" />
<java-symbol type="layout" name="list_menu_item_fixed_size_icon" />
<java-symbol type="layout" name="list_menu_item_layout" />
<java-symbol type="layout" name="list_menu_item_radio" />
<java-symbol type="layout" name="locale_picker_item" />