Skip to content

Commit

Permalink
[M101][ContextMenu] Fix measure width issue for context menu popup
Browse files Browse the repository at this point in the history
Remove the margin applied in ContextMenuListView so ListView width will
match the parent frame.

Before: https://screenshot.googleplex.com/BQXHSLoiaLXPFJi
After: https://screenshot.googleplex.com/9E6S9ApYbCdD3vo

(cherry picked from commit ed7087b)

Bug: 1314675
Change-Id: Ib3a304ca0998197299053b5777def899d2ac265b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3584894
Reviewed-by: Sinan Sahin <sinansahin@google.com>
Commit-Queue: Wenyu Fu <wenyufu@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#992583}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3588873
Cr-Commit-Position: refs/branch-heads/4951@{#789}
Cr-Branched-From: 27de622-refs/heads/main@{#982481}
  • Loading branch information
fwy423 authored and Chromium LUCI CQ committed Apr 15, 2022
1 parent 13c70c6 commit 56e35d4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Expand Up @@ -10,14 +10,23 @@
import android.widget.ListView;

import org.chromium.chrome.R;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.content_public.browser.ContentFeatureList;
import org.chromium.content_public.common.ContentFeatures;

/**
* A custom ListView to be able to set width and height using the contents. Width and height are
* constrained to make sure the view fits the screen size with margins.
*/
public class ContextMenuListView extends ListView {
// Whether the max width of this list view is limited by screen width.
private final boolean mLimitedByScreenWidth;

public ContextMenuListView(Context context, AttributeSet attrs) {
super(context, attrs);
mLimitedByScreenWidth =
ChromeFeatureList.isEnabled(ChromeFeatureList.CONTEXT_MENU_POPUP_STYLE)
|| ContentFeatureList.isEnabled(ContentFeatures.TOUCH_DRAG_AND_CONTEXT_MENU);
}

@Override
Expand Down Expand Up @@ -47,6 +56,11 @@ private int calculateWidth() {
final int parentLateralPadding = frame.getPaddingLeft();
final int maxWidth = Math.min(maxWidthFromRes, frame.getMeasuredWidth());

// When context menu is a popup, the max width with windowWidth - 2 * lateralMargin does not
// applied since it is presented in a popup window. See https://crbug.com/1314675.
if (mLimitedByScreenWidth) {
return maxWidth - 2 * parentLateralPadding;
}
return Math.min(maxWidth, windowWidthPx - 2 * lateralMargin) - 2 * parentLateralPadding;
}
}
Expand Up @@ -29,6 +29,7 @@
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.chrome.test.util.ChromeRenderTestRule;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.content_public.common.ContentFeatures;
import org.chromium.ui.modelutil.LayoutViewBuilder;
import org.chromium.ui.modelutil.MVCListAdapter.ListItem;
import org.chromium.ui.modelutil.MVCListAdapter.ModelList;
Expand Down Expand Up @@ -72,6 +73,7 @@ public void setUpTest() throws Exception {

mTestValues = new TestValues();
mTestValues.addFeatureFlagOverride(ChromeFeatureList.CONTEXT_MENU_POPUP_STYLE, false);
mTestValues.addFeatureFlagOverride(ContentFeatures.TOUCH_DRAG_AND_CONTEXT_MENU, false);
FeatureList.setTestValues(mTestValues);

TestThreadUtils.runOnUiThreadBlocking(() -> {
Expand Down

0 comments on commit 56e35d4

Please sign in to comment.