Skip to content

Commit

Permalink
Merge pull request #386 from HendrikMennen/fix_tooltip_placement
Browse files Browse the repository at this point in the history
[WIP] Fix Completion Tooltip Placement
  • Loading branch information
danipen authored Dec 9, 2023
2 parents 4ea78c7 + abe6eb0 commit be8fc93
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions src/AvaloniaEdit/CodeCompletion/CompletionWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public CompletionWindow(TextArea textArea) : base(textArea)
{
IsLightDismissEnabled = true,
PlacementTarget = this,
Placement = PlacementMode.RightEdgeAlignedTop,
Child = _toolTipContent,
};

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit be8fc93

Please sign in to comment.