Пример #1
0
        /// <summary>
        ///     The perform placement.
        /// </summary>
        internal void PerformPlacement()
        {
            if (this.owner == null)
            {
                return;
            }

            double horizontalOffset = this.HorizontalOffset;
            double verticalOffset   = this.VerticalOffset;

            PlacementMode placementMode   = PinnableTooltipService.GetPlacement(this.owner);
            UIElement     placementTarget = PinnableTooltipService.GetPlacementTarget(this.owner) ?? this.owner;

            Point            mousePosition = PinnableTooltipService.MousePosition;
            FrameworkElement rootVisual    = PinnableTooltipService.RootVisual;

            switch (placementMode)
            {
            case PlacementMode.Mouse:
                double offsetX = mousePosition.X + horizontalOffset;
                double offsetY = mousePosition.Y + new TextBlock().FontSize + verticalOffset;

                offsetX = Math.Max(2.0, offsetX);
                offsetY = Math.Max(2.0, offsetY);

                double actualHeight = rootVisual.ActualHeight;
                double actualWidth  = rootVisual.ActualWidth;
                double lastHeight   = this.lastSize.Height;
                double lastWidth    = this.lastSize.Width;

                var lastRectangle   = new Rect(offsetX, offsetY, lastWidth, lastHeight);
                var actualRectangle = new Rect(0.0, 0.0, actualWidth, actualHeight);
                actualRectangle.Intersect(lastRectangle);

                if ((Math.Abs(actualRectangle.Width - lastRectangle.Width) < 2.0) &&
                    (Math.Abs(actualRectangle.Height - lastRectangle.Height) < 2.0))
                {
                    this.parentPopup.VerticalOffset   = offsetY;
                    this.parentPopup.HorizontalOffset = offsetX;
                }
                else
                {
                    if ((offsetY + lastRectangle.Height) > actualHeight)
                    {
                        offsetY = (actualHeight - lastRectangle.Height) - 2.0;
                    }

                    if (offsetY < 0.0)
                    {
                        offsetY = 0.0;
                    }

                    if ((offsetX + lastRectangle.Width) > actualWidth)
                    {
                        offsetX = (actualWidth - lastRectangle.Width) - 2.0;
                    }

                    if (offsetX < 0.0)
                    {
                        offsetX = 0.0;
                    }

                    this.parentPopup.VerticalOffset   = offsetY;
                    this.parentPopup.HorizontalOffset = offsetX;

                    double clippingHeight = ((offsetY + lastRectangle.Height) + 2.0) - actualHeight;
                    double clippingWidth  = ((offsetX + lastRectangle.Width) + 2.0) - actualWidth;
                    if ((clippingWidth >= 2.0) || (clippingHeight >= 2.0))
                    {
                        clippingWidth  = Math.Max(0.0, clippingWidth);
                        clippingHeight = Math.Max(0.0, clippingHeight);
                        this.PerformClipping(
                            new Size(lastRectangle.Width - clippingWidth, lastRectangle.Height - clippingHeight));
                    }
                }

                break;

            case PlacementMode.Bottom:
            case PlacementMode.Right:
            case PlacementMode.Left:
            case PlacementMode.Top:
                Size    windowSize       = ScreenUtils.GetWindowSize();
                var     plugin           = new Rect(0.0, 0.0, windowSize.Width, windowSize.Height);
                Point[] translatedPoints = GetTranslatedPoints((FrameworkElement)placementTarget);
                Point[] toolTipPoints    = GetTranslatedPoints(this);
                Point   popupLocation    = PlacePopup(plugin, translatedPoints, toolTipPoints, placementMode);

                this.parentPopup.VerticalOffset   = popupLocation.Y + verticalOffset;
                this.parentPopup.HorizontalOffset = popupLocation.X + horizontalOffset;
                break;
            }
        }
Пример #2
0
        /// <summary>
        ///     The get position.
        /// </summary>
        /// <returns>
        ///     The <see cref="Point" />.
        /// </returns>
        public Point GetPosition()
        {
            var position = new Point();

            Point            mousePosition = PinnableTooltipService.MousePosition;
            FrameworkElement rootVisual    = PinnableTooltipService.RootVisual;

            double horizontalOffset = this.HorizontalOffset;
            double verticalOffset   = this.VerticalOffset;

            //using this code for non UIElements
            if (this.owner == null)
            {
                mousePosition = Mouse.GetPosition(this.userDefinedAdorner);
#if (SILVERLIGHT)
                RootVisual = Application.Current.RootVisual as FrameworkElement;
#else
                if ((this.userDefinedAdorner as FrameworkElement) == null)
                {
                    rootVisual = System.Windows.Interop.BrowserInteropHelper.IsBrowserHosted
                                     ? null
                                     : (Application.Current.MainWindow.Content as FrameworkElement) != null
                                           ? Application.Current.MainWindow.Content as FrameworkElement
                                           : Application.Current.MainWindow;
                }
                else
                {
                    rootVisual = this.userDefinedAdorner as FrameworkElement;
                }

                if (rootVisual == null)
                {
                    position = this.isPositionCalculated ? this.lastPosition : mousePosition;
                    return(position);
                }
#endif

                if (this.isPositionCalculated)
                {
                    if (lastPosition.Y + this.DesiredSize.Height > rootVisual.ActualHeight)
                    {
                        this.lastPosition.Y = rootVisual.ActualHeight - this.DesiredSize.Height;
                    }

                    if (lastPosition.X + this.DesiredSize.Width > rootVisual.ActualWidth)
                    {
                        this.lastPosition.X = rootVisual.ActualWidth - this.DesiredSize.Width;
                    }

                    position = this.lastPosition;
                    return(position);
                }

                double offsetX = mousePosition.X + horizontalOffset;
                double offsetY = mousePosition.Y + verticalOffset;

                //offsetX = Math.Max(2.0, offsetX);
                //offsetY = Math.Max(2.0, offsetY);

                double actualHeight = rootVisual.ActualHeight;
                double actualWidth  = rootVisual.ActualWidth;
                double lastHeight   = this.lastSize.Height;
                double lastWidth    = this.lastSize.Width;

                var lastRectangle   = new Rect(offsetX, offsetY, lastWidth, lastHeight);
                var actualRectangle = new Rect(0.0, 0.0, actualWidth, actualHeight);
                actualRectangle.Intersect(lastRectangle);

                if ((offsetY + this.DesiredSize.Height) > actualHeight)
                {
                    offsetY = (actualHeight - this.DesiredSize.Height) - 2.0;
                }

                if (offsetY < 0.0)
                {
                    offsetY = 0.0;
                }

                if ((offsetX + this.DesiredSize.Width) > actualWidth)
                {
                    offsetX = (actualWidth - this.DesiredSize.Width) - 2.0;
                }

                if (offsetX < 0.0)
                {
                    offsetX = 0.0;
                }

                position.Y = offsetY;
                position.X = offsetX;

                this.isPositionCalculated = this.DesiredSize.Height > 0;
                if (this.isPositionCalculated)
                {
                    this.lastPosition = position;
                }

                return(position);
            }

            PlacementMode placementMode   = PinnableTooltipService.GetPlacement(this.owner);
            UIElement     placementTarget = PinnableTooltipService.GetPlacementTarget(this.owner) ?? this.owner;

            switch (placementMode)
            {
            case PlacementMode.Mouse:
                if (this.isPositionCalculated)
                {
                    position = this.lastPosition;
                    return(position);
                }

                double offsetX = mousePosition.X + horizontalOffset;
                double offsetY = mousePosition.Y + new TextBlock().FontSize + verticalOffset;

                offsetX = Math.Max(2.0, offsetX);
                offsetY = Math.Max(2.0, offsetY);

                double actualHeight = rootVisual.ActualHeight;
                double actualWidth  = rootVisual.ActualWidth;
                double lastHeight   = this.lastSize.Height;
                double lastWidth    = this.lastSize.Width;

                var lastRectangle   = new Rect(offsetX, offsetY, lastWidth, lastHeight);
                var actualRectangle = new Rect(0.0, 0.0, actualWidth, actualHeight);
                actualRectangle.Intersect(lastRectangle);

                if ((Math.Abs(actualRectangle.Width - lastRectangle.Width) < 2.0) &&
                    (Math.Abs(actualRectangle.Height - lastRectangle.Height) < 2.0))
                {
                    position.Y = offsetY;
                    position.X = offsetX;
                }
                else
                {
                    if ((offsetY + lastRectangle.Height) > actualHeight)
                    {
                        offsetY = (actualHeight - lastRectangle.Height) - 2.0;
                    }

                    if (offsetY < 0.0)
                    {
                        offsetY = 0.0;
                    }

                    if ((offsetX + lastRectangle.Width) > actualWidth)
                    {
                        offsetX = (actualWidth - lastRectangle.Width) - 2.0;
                    }

                    if (offsetX < 0.0)
                    {
                        offsetX = 0.0;
                    }

                    position.Y = offsetY;
                    position.X = offsetX;
                }

                this.lastPosition         = position;
                this.isPositionCalculated = true;
                break;

            case PlacementMode.Bottom:
            case PlacementMode.Right:
            case PlacementMode.Left:
            case PlacementMode.Top:
                Size    windowSize       = ScreenUtils.GetWindowSize();
                var     plugin           = new Rect(0.0, 0.0, windowSize.Width, windowSize.Height);
                Point[] translatedPoints = GetTranslatedPoints((FrameworkElement)placementTarget);
                Point[] toolTipPoints    = GetTranslatedPoints(this);
                Point   popupLocation    = PlacePopup(plugin, translatedPoints, toolTipPoints, placementMode);

                position.Y = popupLocation.Y + verticalOffset;
                position.X = popupLocation.X + horizontalOffset;
                break;
            }

            return(position);
        }