using Avalonia; using Avalonia.Layout; using DialogHostAvalonia.Positioners; using InABox.Avalonia; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InABox.Avalonia.Dialogs; public class PopupPositioner : AvaloniaObject, IDialogPopupPositioner, IDialogPopupPositionerConstrainable { public static readonly StyledProperty MarginProperty = Layoutable.MarginProperty.AddOwner(); public static readonly StyledProperty ContentProperty = AvaloniaProperty.Register(nameof(Content)); public Thickness Margin { get => GetValue(MarginProperty); set => SetValue(MarginProperty, value); } public object? Content { get => GetValue(ContentProperty); set => SetValue(ContentProperty, value); } private HorizontalAlignment GetHorizontalAlignment() { return (Content as IAlignedPopup)?.HorizontalAlignment ?? HorizontalAlignment.Center; } private VerticalAlignment GetVerticalAlignment() { return (Content as IAlignedPopup)?.VerticalAlignment ?? VerticalAlignment.Center; } public Rect Update(Size anchorRectangle, Size size) { var margin = GetValue(MarginProperty); var availableSpaceRect = new Rect(anchorRectangle); var constrainRect = availableSpaceRect.Deflate(margin); var rect = new Rect(size); var hAlign = GetHorizontalAlignment(); var vAlign = GetVerticalAlignment(); if (hAlign == HorizontalAlignment.Stretch) rect = rect.WithWidth(0); if (vAlign == VerticalAlignment.Stretch) rect = rect.WithHeight(0); var aligned = rect.Align(constrainRect, hAlign, vAlign); return new Rect(margin.Left + aligned.Left, margin.Top + aligned.Top, aligned.Width, aligned.Height); } public Size Constrain(Size availableSize) { return availableSize.Deflate(Margin); } }