From abe6eb06c357f8837ab7b0f288e85115d513a5ce Mon Sep 17 00:00:00 2001 From: Hendrik Mennen Date: Fri, 8 Dec 2023 22:46:19 +0100 Subject: [PATCH] fix tooltip placement --- .../CodeCompletion/CompletionWindow.cs | 54 +++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/AvaloniaEdit/CodeCompletion/CompletionWindow.cs b/src/AvaloniaEdit/CodeCompletion/CompletionWindow.cs index 008a43ad..51892b23 100644 --- a/src/AvaloniaEdit/CodeCompletion/CompletionWindow.cs +++ b/src/AvaloniaEdit/CodeCompletion/CompletionWindow.cs @@ -61,7 +61,6 @@ public CompletionWindow(TextArea textArea) : base(textArea) { IsLightDismissEnabled = true, PlacementTarget = this, - Placement = PlacementMode.RightEdgeAlignedTop, Child = _toolTipContent, }; @@ -92,40 +91,39 @@ private void CompletionList_SelectionChanged(object sender, SelectionChangedEven var item = CompletionList.SelectedItem; var description = item?.Description; - if (description != null) + + + if (description != null && Host is Control placementTarget && CompletionList.CurrentList != null) { - if (description is string descriptionText) + _toolTipContent.Content = description; + + double yOffset = 0; + var selectedIndex = CompletionList.ListBox.SelectedIndex; + + var itemContainer = CompletionList.ListBox.ContainerFromIndex(selectedIndex); + + if (itemContainer != null) { - _toolTipContent.Content = new TextBlock - { - Text = descriptionText, - TextWrapping = TextWrapping.Wrap - }; + _toolTip.Placement = PlacementMode.RightEdgeAlignedTop; + var position = itemContainer.TranslatePoint(new Point(0, 0), placementTarget); + if (position.HasValue) yOffset = position.Value.Y; } - else - { - _toolTipContent.Content = description; - } - - _toolTip.IsOpen = false; //Popup needs to be closed to change position - - // Calculate offset for tooltip - var popupRoot = Host as PopupRoot; - if (CompletionList.CurrentList != null) + else { - double yOffset = 0; - var itemContainer = CompletionList.ListBox.ContainerFromItem(item); - if (popupRoot != null && itemContainer != null) + //When scrolling down the container is not always ready + //If that happens we align the tooltip at the bottom or top + if (CompletionList.ListBox.FirstVisibleItem < selectedIndex) { - var position = itemContainer.TranslatePoint(new Point(0, 0), popupRoot); - if (position.HasValue) - yOffset = position.Value.Y; + _toolTip.Placement = PlacementMode.RightEdgeAlignedBottom; + } + else + { + _toolTip.Placement = PlacementMode.RightEdgeAlignedTop; } - - _toolTip.Offset = new Point(2, yOffset); } - - _toolTip.PlacementTarget = popupRoot; + + _toolTip.Offset = new Point(2, yOffset); + _toolTip.PlacementTarget = placementTarget; _toolTip.IsOpen = true; } else