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

Commit 41ac8669 authored by Vladislav Kaznacheev's avatar Vladislav Kaznacheev
Browse files

More accurate cascading submenu position

The current submenu positioning logic is based on the assumption
that the parent menu was displayed at the exact offset which was
passed to the framework. The actual parent menu position
could have been adjusted to fit the screen.

Bug: 35767083

Test: manual
Change-Id: Ib72eb7808ebf894c526d2c44c6116ee72542fd03
parent 38a65f6c
Loading
Loading
Loading
Loading
+18 −11
Original line number Diff line number Diff line
@@ -388,14 +388,22 @@ final class CascadingMenuPopup extends MenuPopup implements MenuPresenter, OnKey
            final boolean showOnRight = nextMenuPosition == HORIZ_POSITION_RIGHT;
            mLastPosition = nextMenuPosition;

            final int[] tempLocation = new int[2];

            // This popup menu will be positioned relative to the top-left edge
            // of the view representing its parent menu.
            parentView.getLocationInWindow(tempLocation);
            final int parentOffsetLeft = parentInfo.window.getHorizontalOffset() + tempLocation[0];
            final int parentOffsetTop = parentInfo.window.getVerticalOffset() + tempLocation[1];

            // A popup anchored to mAnchorView with (0,0) offset would be shown at this position.
            final int[] offsetOrigin = new int[2];
            mAnchorView.getLocationOnScreen(offsetOrigin);
            offsetOrigin[1] += mAnchorView.getHeight();

            final int[] parentViewScreenLocation = new int[2];
            parentView.getLocationOnScreen(parentViewScreenLocation);

            // Translate the parent view location into the offset coordinate space.
            // If used as horizontal/vertical offsets, these values would position the submenu
            // at the exact same position as the parent item.
            final int parentOffsetLeft = parentViewScreenLocation[0] - offsetOrigin[0];
            final int parentOffsetTop = parentViewScreenLocation[1] - offsetOrigin[1];

            // Adjust the horizontal offset to display the submenu to the right or to the left
            // of the parent item.
            // By now, mDropDownGravity is the resolved absolute gravity, so
            // this should work in both LTR and RTL.
            final int x;
@@ -412,11 +420,10 @@ final class CascadingMenuPopup extends MenuPopup implements MenuPresenter, OnKey
                    x = parentOffsetLeft - menuWidth;
                }
            }

            popupWindow.setHorizontalOffset(x);

            final int y = parentOffsetTop;
            popupWindow.setVerticalOffset(y);
            // Use the same vertical offset as the parent item.
            popupWindow.setVerticalOffset(parentOffsetTop);
        } else {
            if (mHasXOffset) {
                popupWindow.setHorizontalOffset(mXOffset);