Skip to content

Commit

Permalink
Fixes the AlignmentDialogPopupPositioner margins
Browse files Browse the repository at this point in the history
  • Loading branch information
SKProCH committed Jun 21, 2024
1 parent e9b114c commit 550f99c
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 @@ -86,6 +86,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 @@ -100,8 +109,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 @@ -8,7 +8,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 @@ -46,5 +46,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 550f99c

Please sign in to comment.