Ejemplo n.º 1
0
        public Point GetPosition()
        {
            var position = new Point();

            var mousePosition = PinnableToolTipService.MousePosition;
            var rootVisual    = PinnableToolTipService.RootVisual;

            var horizontalOffset = HorizontalOffset;
            var verticalOffset   = VerticalOffset;

            //using this code for non UIElements
            if (_owner == null)
            {
                mousePosition = Mouse.GetPosition(_userDefinedAdorner);

                if ((_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 = _userDefinedAdorner as FrameworkElement;
                }

                if (rootVisual == null)
                {
                    position = _isPositionCalculated ? _lastPosition : mousePosition;
                    return(position);
                }

                if (_isPositionCalculated)
                {
                    if (_lastPosition.Y + DesiredSize.Height > rootVisual.ActualHeight)
                    {
                        _lastPosition.Y = rootVisual.ActualHeight - DesiredSize.Height;
                    }

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

                    position = _lastPosition;
                    return(position);
                }

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

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

                var actualHeight = rootVisual.ActualHeight;
                var actualWidth  = rootVisual.ActualWidth;
                var lastHeight   = _lastSize.Height;
                var lastWidth    = _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 + DesiredSize.Height) > actualHeight)
                {
                    offsetY = (actualHeight - DesiredSize.Height) - 2.0;
                }

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

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

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

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

                _isPositionCalculated = DesiredSize.Height > 0;
                if (_isPositionCalculated)
                {
                    _lastPosition = position;
                }

                return(position);
            }

            var placementMode   = PinnableToolTipService.GetPlacement(_owner);
            var placementTarget = PinnableToolTipService.GetPlacementTarget(_owner) ?? _owner;

            switch (placementMode)
            {
            case PlacementMode.Mouse:
                if (_isPositionCalculated)
                {
                    position = _lastPosition;
                    return(position);
                }

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

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

                var actualHeight = rootVisual.ActualHeight;
                var actualWidth  = rootVisual.ActualWidth;
                var lastHeight   = _lastSize.Height;
                var lastWidth    = _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;
                }

                _lastPosition         = position;
                _isPositionCalculated = true;
                break;

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

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

            return(position);
        }
Ejemplo n.º 2
0
        public Point GetPosition()
        {
            var mousePosition = Mouse.GetPosition(_userDefinedAdorner);
            var rootVisual    = GetRootVisual();

            var fixedOffset = 0d;

            if (ResizeMode == ResizeMode.CanResize || ResizeMode == ResizeMode.CanResizeWithGrip)
            {
                fixedOffset = 3d;
            }

            var horizontalOffset = HorizontalOffset + fixedOffset;
            var verticalOffset   = VerticalOffset + fixedOffset;

            var mousePositionX = mousePosition.X;
            var mousePositionY = mousePosition.Y;

            //using this code for non UIElements
            if (_owner is null)
            {
                return(GetPostionForNonUiElement(rootVisual, mousePosition, horizontalOffset, verticalOffset));
            }

            var placementMode = PinnableToolTipService.GetPlacement(_owner);

            switch (placementMode)
            {
            case PlacementMode.Mouse:
                if (_isPositionCalculated)
                {
                    return(_lastPosition);
                }

                const int fontSize = 0;

                var offsetX = Math.Max(2.0, mousePositionX + horizontalOffset);
                var offsetY = Math.Max(2.0, mousePositionY + fontSize + verticalOffset);

                var actualHeight = rootVisual.ActualHeight;
                var actualWidth  = rootVisual.ActualWidth;
                var lastHeight   = _lastSize.Height;
                var lastWidth    = _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))
                {
                    offsetY = GetOffset(0, offsetY, actualHeight, lastRectangle.Height);
                    offsetX = GetOffset(0, offsetX, actualWidth, lastRectangle.Width);
                }

                _lastPosition         = new Point(offsetX, offsetY);
                _isPositionCalculated = true;

                break;

            case PlacementMode.Bottom:
            case PlacementMode.Right:
            case PlacementMode.Left:
            case PlacementMode.Top:
                var windowSize      = Application.Current.MainWindow.GetSize();
                var plugin          = new Rect(0.0, 0.0, windowSize.Width, windowSize.Height);
                var placementTarget = PinnableToolTipService.GetPlacementTarget(_owner) ?? _owner;
                var targetPoints    = GetTranslatedPoints((FrameworkElement)placementTarget);
                var toolTipPoints   = GetTranslatedPoints(this);
                var popupLocation   = PlacePopup(plugin, targetPoints, toolTipPoints, placementMode);

#if TRACE_DETAILS
                Debug.WriteLine($"IsPinned: {IsPinned}");
                Debug.WriteLine($"Offset: X = '{horizontalOffset}', Y = '{verticalOffset}'");
                Debug.WriteLine($"Final point: '{popupLocation.X}, {popupLocation.Y}'");
#endif
                return(popupLocation);
            }

            return(default(Point));
        }