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

Commit 42e039cd authored by jorgegil@google.com's avatar jorgegil@google.com
Browse files

Run alignment logic of menu icons when the menu is created

The algorithm's icon arrangement logic was previously ran in
onBoundsChange, which could cause the icons to be misaligned
the very first time the menu is shown after being created.
This change ensures that the algorithm does an initial run
when the menu is first created.

Bug: 178708863
Test: manual - open YT PIP, tap to see menu, see the gear
icon is aligned to the top-start corner

Change-Id: I2cb53578db897af62be7ebd3ed40946e16606568
parent bf7df8a8
Loading
Loading
Loading
Loading
+22 −16
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ public class PipMenuIconsAlgorithm {

    private static final String TAG = "PipMenuIconsAlgorithm";

    private boolean mFinishedLayout = false;
    protected ViewGroup mViewRoot;
    protected ViewGroup mTopEndContainer;
    protected View mDragHandle;
@@ -51,27 +50,16 @@ public class PipMenuIconsAlgorithm {
        mDragHandle = dragHandle;
        mSettingsButton = settingsButton;
        mDismissButton = dismissButton;

        bindInitialViewState();
    }

    /**
     * Updates the position of the drag handle based on where the PIP window is on the screen.
     */
    public void onBoundsChanged(Rect bounds) {
        if (mViewRoot == null || mTopEndContainer == null || mDragHandle == null
                || mSettingsButton == null || mDismissButton == null) {
            Log.e(TAG, "One if the required views is null.");
            return;
        }

        //We only need to calculate the layout once since it does not change.
        if (!mFinishedLayout) {
            mTopEndContainer.removeView(mSettingsButton);
            mViewRoot.addView(mSettingsButton);

            setLayoutGravity(mDragHandle, Gravity.START | Gravity.TOP);
            setLayoutGravity(mSettingsButton, Gravity.START | Gravity.TOP);
            mFinishedLayout = true;
        }
        // On phones, the menu icons are always static and will never move based on the PIP window
        // position. No need to do anything here.
    }

    /**
@@ -84,4 +72,22 @@ public class PipMenuIconsAlgorithm {
            v.setLayoutParams(params);
        }
    }

    /** Calculate the initial state of the menu icons. Called when the menu is first created. */
    private void bindInitialViewState() {
        if (mViewRoot == null || mTopEndContainer == null || mDragHandle == null
                || mSettingsButton == null || mDismissButton == null) {
            Log.e(TAG, "One of the required views is null.");
            return;
        }
        // The menu view layout starts out with the settings button aligned at the top|end of the
        // view group next to the dismiss button. On phones, the settings button should be aligned
        // to the top|start of the view, so move it to parent view group to then align it to the
        // top|start of the menu.
        mTopEndContainer.removeView(mSettingsButton);
        mViewRoot.addView(mSettingsButton);

        setLayoutGravity(mDragHandle, Gravity.START | Gravity.TOP);
        setLayoutGravity(mSettingsButton, Gravity.START | Gravity.TOP);
    }
}