Skip to content

Commit

Permalink
Merge pull request #54 from AvaloniaUtils/fixMarginsForPositioner
Browse files Browse the repository at this point in the history
Fixes the AlignmentDialogPopupPositioner margins
  • Loading branch information
SKProCH authored Jun 19, 2024
2 parents 3c2f813 + d38483f commit 7ed3f39
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
13 changes: 11 additions & 2 deletions DialogHost.Avalonia/DialogOverlayPopupHost.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public void Hide()
_root.Children.Remove(this);
}

protected override Size MeasureOverride(Size availableSize)
{
if (PopupPositioner is IDialogPopupPositionerConstrainable constrainable)
{
return base.MeasureOverride(constrainable.Constrain(availableSize));
}
return base.MeasureOverride(availableSize);
}

/// <inheritdoc />
protected override void ArrangeCore(Rect finalRect) {
var margin = Margin;
Expand All @@ -109,8 +118,8 @@ protected override void ArrangeCore(Rect finalRect) {
var positioner = PopupPositioner ?? CenteredDialogPopupPositioner.Instance;
var bounds = positioner.Update(size, contentSize);

ArrangeOverride(bounds.Size).Constrain(size);
Bounds = new Rect(bounds.X + margin.Left, bounds.Y + margin.Top, bounds.Width, bounds.Height);
var (finalWidth, finalHeight) = ArrangeOverride(bounds.Size).Constrain(size);
Bounds = new Rect(bounds.X + margin.Left, bounds.Y + margin.Top, finalWidth, finalHeight);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace DialogHostAvalonia.Positioners {
/// <remarks>
/// Default values for <see cref="HorizontalAlignment"/> and <see cref="VerticalAlignment"/> is <c>Stretch</c> and it will be act TopLeft alignment
/// </remarks>
public class AlignmentDialogPopupPositioner : AvaloniaObject, IDialogPopupPositioner {
public class AlignmentDialogPopupPositioner : AvaloniaObject, IDialogPopupPositioner, IDialogPopupPositionerConstrainable {
public static readonly StyledProperty<HorizontalAlignment> HorizontalAlignmentProperty
= Layoutable.HorizontalAlignmentProperty.AddOwner<AlignmentDialogPopupPositioner>();

Expand Down Expand Up @@ -48,5 +48,11 @@ public Rect Update(Size anchorRectangle, Size size) {
var aligned = rect.Align(constrainRect, GetValue(HorizontalAlignmentProperty), GetValue(VerticalAlignmentProperty));
return new Rect(margin.Left + aligned.Left, margin.Top + aligned.Top, aligned.Width, aligned.Height);
}

/// <inheritdoc />
public Size Constrain(Size availableSize)
{
return availableSize.Deflate(Margin);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Avalonia;

namespace DialogHostAvalonia.Positioners
{
/// <summary>
/// Implement this for your positioner if it constrains the popup, e.g. margin
/// </summary>
public interface IDialogPopupPositionerConstrainable
{
/// <summary>
/// Constrain the space available for popup
/// </summary>
/// <param name="availableSize">Initial size</param>
/// <returns>Constrained size</returns>
public Size Constrain(Size availableSize);
}
}

0 comments on commit 7ed3f39

Please sign in to comment.