diff --git a/DialogHost.Avalonia/DialogOverlayPopupHost.axaml.cs b/DialogHost.Avalonia/DialogOverlayPopupHost.axaml.cs index cf1f6a7..9e1705f 100644 --- a/DialogHost.Avalonia/DialogOverlayPopupHost.axaml.cs +++ b/DialogHost.Avalonia/DialogOverlayPopupHost.axaml.cs @@ -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); + } + /// protected override void ArrangeCore(Rect finalRect) { var margin = Margin; @@ -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); } diff --git a/DialogHost.Avalonia/Positioners/AlignmentDialogPopupPositioner.cs b/DialogHost.Avalonia/Positioners/AlignmentDialogPopupPositioner.cs index d9312f7..f2e713e 100644 --- a/DialogHost.Avalonia/Positioners/AlignmentDialogPopupPositioner.cs +++ b/DialogHost.Avalonia/Positioners/AlignmentDialogPopupPositioner.cs @@ -8,7 +8,7 @@ namespace DialogHostAvalonia.Positioners { /// /// Default values for and is Stretch and it will be act TopLeft alignment /// - public class AlignmentDialogPopupPositioner : AvaloniaObject, IDialogPopupPositioner { + public class AlignmentDialogPopupPositioner : AvaloniaObject, IDialogPopupPositioner, IDialogPopupPositionerConstrainable { public static readonly StyledProperty HorizontalAlignmentProperty = Layoutable.HorizontalAlignmentProperty.AddOwner(); @@ -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); } + + /// + public Size Constrain(Size availableSize) + { + return availableSize.Deflate(Margin); + } } } \ No newline at end of file diff --git a/DialogHost.Avalonia/Positioners/IDialogPopupPositionerConstrainable.cs b/DialogHost.Avalonia/Positioners/IDialogPopupPositionerConstrainable.cs new file mode 100644 index 0000000..b2866d6 --- /dev/null +++ b/DialogHost.Avalonia/Positioners/IDialogPopupPositionerConstrainable.cs @@ -0,0 +1,17 @@ +using Avalonia; + +namespace DialogHostAvalonia.Positioners +{ + /// + /// Implement this for your positioner if it constrains the popup, e.g. margin + /// + public interface IDialogPopupPositionerConstrainable + { + /// + /// Constrain the space available for popup + /// + /// Initial size + /// Constrained size + public Size Constrain(Size availableSize); + } +} \ No newline at end of file