Exemplo n.º 1
0
        public void HandleToolTipRequest(ToolTipRequestEventArgs args)
        {
            if (args.InDocument)
            {
                int offset = args.Editor.Document.GetOffset(args.LogicalPosition);

                FoldingManager foldings = args.Editor.GetService(typeof(FoldingManager)) as FoldingManager;
                if (foldings != null)
                {
                    var            foldingsAtOffset = foldings.GetFoldingsAt(offset);
                    FoldingSection collapsedSection = foldingsAtOffset.FirstOrDefault(section => section.IsFolded);

                    if (collapsedSection != null)
                    {
                        args.SetToolTip(GetTooltipTextForCollapsedSection(args, collapsedSection));
                    }
                }

                TextMarkerService textMarkerService = args.Editor.GetService(typeof(ITextMarkerService)) as TextMarkerService;
                if (textMarkerService != null)
                {
                    var         markersAtOffset   = textMarkerService.GetMarkersAtOffset(offset);
                    ITextMarker markerWithToolTip = markersAtOffset.FirstOrDefault(marker => marker.ToolTip != null);

                    if (markerWithToolTip != null)
                    {
                        args.SetToolTip(markerWithToolTip.ToolTip);
                    }
                }
            }
        }
Exemplo n.º 2
0
        void TextEditorMouseHover(object sender, MouseEventArgs e)
        {
            Debug.Assert(sender == this);
            ToolTipRequestEventArgs args = new ToolTipRequestEventArgs(this.Adapter);
            var pos = this.TextArea.TextView.GetPositionFloor(e.GetPosition(this.TextArea.TextView) + this.TextArea.TextView.ScrollOffset);

            args.InDocument = pos.HasValue;
            if (pos.HasValue)
            {
                args.LogicalPosition = AvalonEditDocumentAdapter.ToLocation(pos.Value.Location);
            }

            if (args.InDocument)
            {
                int offset = args.Editor.Document.PositionToOffset(args.LogicalPosition.Line, args.LogicalPosition.Column);

                FoldingManager foldings = this.Adapter.GetService(typeof(FoldingManager)) as FoldingManager;
                if (foldings != null)
                {
                    var            foldingsAtOffset = foldings.GetFoldingsAt(offset);
                    FoldingSection collapsedSection = foldingsAtOffset.FirstOrDefault(section => section.IsFolded);

                    if (collapsedSection != null)
                    {
                        args.SetToolTip(GetTooltipTextForCollapsedSection(collapsedSection));
                    }
                }

                TextMarkerService textMarkerService = this.Adapter.GetService(typeof(ITextMarkerService)) as TextMarkerService;
                if (textMarkerService != null)
                {
                    var         markersAtOffset   = textMarkerService.GetMarkersAtOffset(offset);
                    ITextMarker markerWithToolTip = markersAtOffset.FirstOrDefault(marker => marker.ToolTip != null);

                    if (markerWithToolTip != null)
                    {
                        args.SetToolTip(markerWithToolTip.ToolTip);
                    }
                }
            }

            if (!args.Handled)
            {
                // if request wasn't handled by a marker, pass it to the ToolTipRequestService
                ToolTipRequestService.RequestToolTip(args);
            }

            if (args.ContentToShow != null)
            {
                var contentToShowITooltip = args.ContentToShow as ITooltip;

                if (contentToShowITooltip != null && contentToShowITooltip.ShowAsPopup)
                {
                    if (!(args.ContentToShow is UIElement))
                    {
                        throw new NotSupportedException("Content to show in Popup must be UIElement: " + args.ContentToShow);
                    }
                    if (popup == null)
                    {
                        popup = CreatePopup();
                    }
                    // if popup was only first level, hovering somewhere else closes it
                    if (TryCloseExistingPopup(false))
                    {
                        // when popup content decides to close, close the popup
                        contentToShowITooltip.Closed += (closedSender, closedArgs) => { popup.IsOpen = false; };
                        popup.Child = (UIElement)args.ContentToShow;
                        //ICSharpCode.SharpDevelop.Debugging.DebuggerService.CurrentDebugger.IsProcessRunningChanged
                        SetPopupPosition(popup, e);
                        popup.IsOpen = true;
                    }
                    e.Handled = true;
                }
                else
                {
                    if (toolTip == null)
                    {
                        toolTip         = new ToolTip();
                        toolTip.Closed += ToolTipClosed;
                    }
                    toolTip.PlacementTarget = this;                     // required for property inheritance

                    if (args.ContentToShow is string)
                    {
                        toolTip.Content = new TextBlock
                        {
                            Text         = args.ContentToShow as string,
                            TextWrapping = TextWrapping.Wrap
                        };
                    }
                    else
                    {
                        toolTip.Content = args.ContentToShow;
                    }

                    toolTip.IsOpen = true;
                    e.Handled      = true;
                }
            }
            else
            {
                // close popup if mouse hovered over empty area
                if (popup != null)
                {
                    e.Handled = true;
                }
                TryCloseExistingPopup(false);
            }
        }