diff --git a/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs b/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs index 3f1b8008b1..0c9bb89caa 100644 --- a/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs +++ b/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs @@ -464,25 +464,21 @@ namespace Avalonia.Controls.Primitives.PopupPositioning positionerParameters.Anchor = PopupAnchor.TopLeft; positionerParameters.Gravity = PopupGravity.BottomRight; } - else if (placement == PlacementMode.Center) + else { - // Start with top left position, and add offset to move it to the center - var anchorRectangle = GetAnchorRectangle(topLevel, target, rect); - positionerParameters.AnchorRectangle = anchorRectangle; - positionerParameters.Anchor = PopupAnchor.TopLeft; - positionerParameters.Gravity = PopupGravity.BottomRight; - - var targetSize = positionerParameters.Size; - if (targetSize != default) + if (target == null) + throw new InvalidOperationException("Placement mode is not Pointer and PlacementTarget is null"); + var matrix = target.TransformToVisual(topLevel); + if (matrix == null) { - positionerParameters.Offset += new Point( - (anchorRectangle.Width - targetSize.Width) / 2, - (anchorRectangle.Height - targetSize.Height) / 2); + if (target.GetVisualRoot() == null) + throw new InvalidOperationException("Target control is not attached to the visual tree"); + throw new InvalidOperationException("Target control is not in the same tree as the popup parent"); } - } - else - { - positionerParameters.AnchorRectangle = GetAnchorRectangle(topLevel, target, rect); + + var bounds = new Rect(default, target.Bounds.Size); + var anchorRect = rect ?? bounds; + positionerParameters.AnchorRectangle = anchorRect.Intersect(bounds).TransformToAABB(matrix.Value); var parameters = placement switch { @@ -490,6 +486,7 @@ namespace Avalonia.Controls.Primitives.PopupPositioning PlacementMode.Right => (PopupAnchor.Right, PopupGravity.Right), PlacementMode.Left => (PopupAnchor.Left, PopupGravity.Left), PlacementMode.Top => (PopupAnchor.Top, PopupGravity.Top), + PlacementMode.Center => (PopupAnchor.None, PopupGravity.None), PlacementMode.AnchorAndGravity => (anchor, gravity), PlacementMode.TopEdgeAlignedRight => (PopupAnchor.TopRight, PopupGravity.TopLeft), PlacementMode.TopEdgeAlignedLeft => (PopupAnchor.TopLeft, PopupGravity.TopRight), @@ -532,23 +529,6 @@ namespace Avalonia.Controls.Primitives.PopupPositioning } } } - - private static Rect GetAnchorRectangle(TopLevel topLevel, Visual target, Rect? rect) - { - if (target == null) - throw new InvalidOperationException("Placement mode is not Pointer and PlacementTarget is null"); - var matrix = target.TransformToVisual(topLevel); - if (matrix == null) - { - if (target.GetVisualRoot() == null) - throw new InvalidOperationException("Target control is not attached to the visual tree"); - throw new InvalidOperationException("Target control is not in the same tree as the popup parent"); - } - - var bounds = new Rect(default, target.Bounds.Size); - var anchorRect = rect ?? bounds; - return anchorRect.Intersect(bounds).TransformToAABB(matrix.Value); - } } }