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

UpdateContextMenuPlacement() private method

Updates the location and size of the Popup and overlay.
private UpdateContextMenuPlacement ( ) : void
return void
        private void UpdateContextMenuPlacement()
        {
            Debug.Assert(_page != null, "page should not be null");

            if ((null != _rootVisual) && (null != _overlay))
            {
                Point p = new Point(_popupAlignmentPoint.X, _popupAlignmentPoint.Y);

                // Determine frame/page bounds
                bool portrait = _rootVisual.Orientation.IsPortrait();

                double effectiveWidth = portrait ? _rootVisual.ActualWidth : _rootVisual.ActualHeight;
                double effectiveHeight = portrait ? _rootVisual.ActualHeight : _rootVisual.ActualWidth;
                Rect bounds = new Rect(0, 0, effectiveWidth, effectiveHeight);
                if (_page != null)
                {
                    bounds = SafeTransformToVisual(_page, _rootVisual).TransformBounds(new Rect(0, 0, _page.ActualWidth, _page.ActualHeight));
                }

                if (portrait && null != _rootVisual && null != bounds)
                {
                    double roiY;
                    double roiHeight;

                    if (RegionOfInterest.HasValue)
                    {
                        roiY = RegionOfInterest.Value.Y;
                        roiHeight = RegionOfInterest.Value.Height;
                    }
                    else if (Owner is FrameworkElement)
                    {
                        FrameworkElement el = (FrameworkElement)Owner;
                        GeneralTransform t = el.TransformToVisual(_rootVisual);

                        roiY = t.Transform(new Point(0, 0)).Y;
                        roiHeight = el.ActualHeight;
                    }
                    else
                    {
                        roiY = _popupAlignmentPoint.Y;
                        roiHeight = 0;
                    }
                                  
                    p.Y = AdjustContextMenuPositionForPortraitMode(bounds, roiY, roiHeight, ref _reversed);
                }
                    

                // Start with the current Popup alignment point
                double x = p.X;
                double y = p.Y;

                // Adjust for offset
                y += VerticalOffset;

                if (portrait)
                {
                    // Left align with full width
                    x = bounds.Left;
                    Width = bounds.Width;
                    if (null != _innerGrid)
                    {
                        _innerGrid.Width = Width;
                    }
                }
                else
                {                    
                    if (PositionIsOnScreenRight(y))
                    {
                        Width = (SystemTray.IsVisible) ? LandscapeWidth - SystemTrayLandscapeWidth : LandscapeWidth;
                        x = (SystemTray.IsVisible) ? SystemTrayLandscapeWidth : 0;
                        _reversed = true;
                    }
                    else
                    {
                        Width = (null != _page.ApplicationBar && _page.ApplicationBar.IsVisible) ? LandscapeWidth - ApplicationBarLandscapeWidth : LandscapeWidth;
                        x = bounds.Width - Width + ((SystemTray.IsVisible) ? SystemTrayLandscapeWidth : 0);
                        _reversed = false;
                    }

                    if (null != _innerGrid)
                    {
                        _innerGrid.Width = Width - TotalBorderWidth;
                    }

                    y = 0;
                }

                // Do not let it stick out too far to the left/top
                x = Math.Max(x, 0);

                // Set the new location
                Canvas.SetLeft(this, x);
                Canvas.SetTop(this, y);

                // Size the overlay to match the new container
                _overlay.Width = effectiveWidth;
                _overlay.Height = effectiveHeight;
            }
        }