Browse Source

Simplify PlacementMode.Center

pull/10492/head
Max Katz 4 years ago
parent
commit
d249848d76
  1. 46
      src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs

46
src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs

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

Loading…
Cancel
Save