void UpdateToolTipContent(IWpfTextViewLine line)
        {
            IGlyphTextMarkerHandlerContext glyphTextMarkerHandlerContext = null;

            foreach (var marker in glyphTextViewMarkerService.GetSortedGlyphTextMarkers(line))
            {
                if (glyphTextMarkerHandlerContext == null)
                {
                    glyphTextMarkerHandlerContext = new GlyphTextMarkerHandlerContext(wpfTextViewHost, margin, line);
                }
                var toolTipContent = marker.Handler.GetToolTipContent(glyphTextMarkerHandlerContext, marker);
                if (toolTipContent != null)
                {
                    Debug.Assert(toolTip == null);
                    toolTipMarker = marker;
                    toolTip       = new ToolTip();
                    toolTip.SetResourceReference(FrameworkElement.StyleProperty, "GlyphTextMarkerToolTipStyle");
                    PopupHelper.SetScaleTransform(wpfTextViewHost.TextView, toolTip);
                    toolTip.Content = new TextBlock {
                        Text         = toolTipContent,
                        TextWrapping = TextWrapping.Wrap,
                    };
                    toolTip.Placement        = PlacementMode.Relative;
                    toolTip.PlacementTarget  = margin.VisualElement;
                    toolTip.HorizontalOffset = 0;
                    toolTip.VerticalOffset   = toolTipLine.TextBottom - wpfTextViewHost.TextView.ViewportTop + 1;
                    toolTip.IsOpen           = true;
                    return;
                }
            }
        }
        void AddPopupContent(IWpfTextViewLine line, IGlyphTextMarker marker, FrameworkElement popupContent)
        {
            // We must close it or it refuses to use the new scale transform
            popup.IsOpen             = false;
            popupTopViewLinePosition = wpfTextViewHost.TextView.TextViewLines.FirstVisibleLine.Start.Position;
            popupMarker            = marker;
            popup.Child            = popupContent;
            popup.Placement        = PlacementMode.Relative;
            popup.PlacementTarget  = margin.VisualElement;
            popup.HorizontalOffset = margin.VisualElement.Width - 1;
            var popupContentHeight = popupContent.Height;

            Debug.Assert(!double.IsNaN(popupContentHeight), "You must initialize the Height property of the popup content!");
            if (double.IsNaN(popupContentHeight))
            {
                popupContentHeight = 0;
            }
            popup.VerticalOffset = line.TextTop - wpfTextViewHost.TextView.ViewportTop + 1 - popupContentHeight;
            popup.Visibility     = Visibility.Visible;
            PopupHelper.SetScaleTransform(wpfTextViewHost.TextView, popup);
            popup.IsOpen = true;
        }
        public Geometry PositionAndDisplay(Geometry reservedSpace)
        {
            var spanBoundsTmp = GetVisualSpanBounds();

            if (spanBoundsTmp == null || spanBoundsTmp.Value.IsEmpty)
            {
                return(null);
            }
            var spanBounds  = WpfTextViewRectToScreenRect(spanBoundsTmp.Value);
            var desiredSize = ToScreenSize(PopupSize);

            var screen     = new Screen(wpfTextView.VisualElement);
            var screenRect = screen.IsValid ? screen.DisplayRect : SystemParameters.WorkArea;

            Rect?popupRect = null;

            if ((style & PopupStyles.PositionClosest) != 0)
            {
                foreach (var pos in GetValidPositions(screenRect, reservedSpace, desiredSize, spanBounds, style))
                {
                    popupRect = GetClosest(spanBounds, popupRect, pos, style);
                }
            }
            else
            {
                foreach (var pos in GetValidPositions(screenRect, reservedSpace, desiredSize, spanBounds, style))
                {
                    popupRect = pos;
                    break;
                }
            }

            if (popupRect == null)
            {
                return(null);
            }
            var viewRelativeRect = PopupHelper.TransformFromDevice(wpfTextView, popupRect.Value);

            bool isOpen = popup.IsOpen;

            if (!isOpen)
            {
                AddEvents();
            }

            if (popupZoomLevel != wpfTextView.ZoomLevel)
            {
                // Must set IsOpen to false when setting a new scale transform
                popup.IsOpen = false;
                PopupHelper.SetScaleTransform(wpfTextView, popup);
                popupZoomLevel = wpfTextView.ZoomLevel;
            }
            if (!isOpen)
            {
                popup.Child      = content;
                popup.Visibility = Visibility.Visible;
            }
            popup.VerticalOffset   = viewRelativeRect.Top;
            popup.HorizontalOffset = viewRelativeRect.Left;
            popup.IsOpen           = true;

            return(new RectangleGeometry(popupRect.Value));
        }