12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 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<Thickness> MarginProperty
- = Layoutable.MarginProperty.AddOwner<AlignmentDialogPopupPositioner>();
- public static readonly StyledProperty<object?> ContentProperty
- = AvaloniaProperty.Register<PopupPositioner, object?>(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);
- }
- }
|