Microsoft.Phone.Controls.ContextMenu.AdjustContextMenuPositionForPortraitMode C# (CSharp) Method

AdjustContextMenuPositionForPortraitMode() private method

Adjust the position (Y) of ContextMenu for Portrait Mode.
private AdjustContextMenuPositionForPortraitMode ( Rect bounds, double roiY, double roiHeight, bool &reversed ) : double
bounds System.Windows.Rect
roiY double
roiHeight double
reversed bool
return double
        private double AdjustContextMenuPositionForPortraitMode(Rect bounds, double roiY, double roiHeight, ref bool reversed)
        {
            double y = 0.0;
            bool notEnoughRoom = false;     // if we have enough room to place the menu without moving.

            double lowestTopOfMenu = bounds.Bottom - ActualHeight;
            double highestBottomOfMenu = bounds.Top + ActualHeight;

            if (bounds.Height <= ActualHeight)
            {
                notEnoughRoom = true;
            }
            else if (roiY + roiHeight <= lowestTopOfMenu)           // there is enough room below the owner.
            {
                y = roiY + roiHeight;
                reversed = false;
            }
            else if (roiY >= highestBottomOfMenu)                   // there is enough room above the owner.
            {
                y = roiY - ActualHeight;
                reversed = true;
            }
            else if (_popupAlignmentPoint.Y >= 0)                   // menu is displayed by Tap&Hold gesture, will try to place the menu at touch position                                                                            
            {
                y = _popupAlignmentPoint.Y;
                if (y <= lowestTopOfMenu)
                {
                    reversed = false;
                }
                else if (y >= highestBottomOfMenu)
                {
                    y -= ActualHeight;
                    reversed = true;
                }
                else
                {
                    notEnoughRoom = true;
                }
            }
            else                                                    // menu is displayed by calling "IsOpen = true", the point will be (-1, -1)
            {
                notEnoughRoom = true;
            }

            if (notEnoughRoom)                                      // failed to place menu in above scenraios, try to align it to Bottom.
            {
                y = lowestTopOfMenu;                              // align to bottom
                reversed = true;

                if (y <= bounds.Top)                              // if the menu can't be fully displayed, make sure we truncate the bottom items, not the top items.
                {
                    y = bounds.Top;
                    reversed = false;
                }
            }
            return y;
        }