BaconographyW8.Common.Flyout.PerformPlacement C# (CSharp) Method

PerformPlacement() private method

private PerformPlacement ( double horizontalOffset, double verticalOffset ) : void
horizontalOffset double
verticalOffset double
return void
        private void PerformPlacement(double horizontalOffset, double verticalOffset)
        {
            double x = 0.0;
            double y = 0.0;
            PlacementMode placement = this.Placement;
            FrameworkElement element = this.PlacementTarget as FrameworkElement;
            bool isRTL = (element != null) ? (element.FlowDirection == Windows.UI.Xaml.FlowDirection.RightToLeft) : false;

            if ((element != null) && !element.IsHitTestVisible)
            {
                return;
            }

            switch (placement)
            {
                case PlacementMode.Bottom:
                case PlacementMode.Left:
                case PlacementMode.Right:
                case PlacementMode.Top:
                    Point[] target = GetTransformedPoints(element, isRTL, element);
                    Point[] menu = GetTransformedPoints((FrameworkElement)_hostPopup.Child, isRTL, element);
                    if (menu[0].X > menu[1].X)
                    {
                        return;
                    }
                    Point p2 = PlacePopup(_windowBounds, target, menu, placement);
                    x = p2.X;
                    if (isRTL)
                    {
                        // TODO: Handle RTL - PerformPlacement
                        //x = _windowBounds.Width - x;
                        //this._hostPopup.VerticalOffset = y;
                        //this._hostPopup.HorizontalOffset = x;
                        //return;
                    }
                    y = p2.Y;
                    break;
                case PlacementMode.Mouse:
                    throw new NotImplementedException("Mouse PlacementMode is not implemented.");
            }

            if (x < 0.0) x = 0.0;

            if (y < 0.0) y = 0.0;


            var calcH = this.CalculateHorizontalCenterOffset(x, ((FrameworkElement)_hostPopup.Child).ActualWidth, element.ActualWidth);
            var calcY = this.CalculateVerticalCenterOffset(y, ((FrameworkElement)_hostPopup.Child).ActualHeight, element.ActualHeight);

            if (calcH < 0)
            {
                calcH = GUTTER_BUFFER;
            }
            else
            {
                // TODO: Correct right nudge positioning as it is incorrect
                if ((calcH > _windowBounds.Width) || (calcH + ((FrameworkElement)_hostPopup.Child).ActualWidth) > _windowBounds.Width)
                {
                    calcH = _windowBounds.Width - ((FrameworkElement)_hostPopup.Child).ActualWidth - GUTTER_BUFFER;
                }
            }

            UIElement parent = _hostPopup.Parent as UIElement;
            if (parent != null)
            {
                var transform = parent.TransformToVisual(Window.Current.Content);
                var offsetAdjustment = transform.TransformPoint(new Point(0, 0));
                calcH -= offsetAdjustment.X;
                calcY -= offsetAdjustment.Y;
            }

            _hostPopup.HorizontalOffset = calcH;
            _hostPopup.VerticalOffset = calcY;
            _hostPopup.IsHitTestVisible = true;
            _hostPopup.Opacity = 1;

            // for entrance animation
            // UX guidelines show a PopIn animation
            Storyboard inAnimation = new Storyboard();
            PopInThemeAnimation popin = new PopInThemeAnimation();

            // TODO: Switch statement begs of refactoring
            switch (this.Placement)
            {
                case PlacementMode.Bottom:
                    popin.FromVerticalOffset = -10;
                    popin.FromHorizontalOffset = 0;
                    break;
                case PlacementMode.Left:
                    popin.FromVerticalOffset = 0;
                    popin.FromHorizontalOffset = 10;
                    break;
                case PlacementMode.Right:
                    popin.FromVerticalOffset = 0;
                    popin.FromHorizontalOffset = -10;
                    break;
                case PlacementMode.Top:
                    popin.FromVerticalOffset = 10;
                    popin.FromHorizontalOffset = 0;
                    break;
            }
            Storyboard.SetTarget(popin, _hostPopup);
            inAnimation.Children.Add(popin);
            inAnimation.Begin();
        }