using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.LogicalTree; using Irihi.Avalonia.Shared.Helpers; using Ursa.Controls.OverlayShared; namespace Ursa.Controls; [TemplatePart(PART_CloseButton, typeof(Button))] [TemplatePart(PART_TitleArea, typeof(Panel))] [PseudoClasses(PC_Modal, PC_FullScreen)] public abstract class DialogControlBase : OverlayFeedbackElement { public const string PART_CloseButton = "PART_CloseButton"; public const string PART_TitleArea = "PART_TitleArea"; public const string PC_Modal = ":modal"; public const string PC_FullScreen = ":full-screen"; public static readonly DirectProperty IsFullScreenProperty = AvaloniaProperty.RegisterDirect( nameof(IsFullScreen), o => o.IsFullScreen, (o, v) => o.IsFullScreen = v); public static readonly StyledProperty CanResizeProperty = AvaloniaProperty.Register( nameof(CanResize)); protected internal Button? _closeButton; private bool _isFullScreen; private Panel? _titleArea; private bool _moveDragging; private Point _moveDragStartPoint; static DialogControlBase() { CanDragMoveProperty.Changed.AddClassHandler(OnCanDragMoveChanged); CanCloseProperty.Changed.AddClassHandler(OnCanCloseChanged); IsFullScreenProperty.AffectsPseudoClass(PC_FullScreen); } public bool CanResize { get => GetValue(CanResizeProperty); set => SetValue(CanResizeProperty, value); } internal HorizontalPosition HorizontalAnchor { get; set; } = HorizontalPosition.Center; internal VerticalPosition VerticalAnchor { get; set; } = VerticalPosition.Center; internal HorizontalPosition ActualHorizontalAnchor { get; set; } internal VerticalPosition ActualVerticalAnchor { get; set; } internal double? HorizontalOffset { get; set; } internal double? VerticalOffset { get; set; } internal double? HorizontalOffsetRatio { get; set; } internal double? VerticalOffsetRatio { get; set; } internal bool CanLightDismiss { get; set; } internal bool? IsCloseButtonVisible { get; set; } public bool IsFullScreen { get => _isFullScreen; set => SetAndRaise(IsFullScreenProperty, ref _isFullScreen, value); } protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); _titleArea = e.NameScope.Find(PART_TitleArea); if (GetCanDragMove(this)) { _titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove); _titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed); _titleArea?.RemoveHandler(PointerReleasedEvent, OnTitlePointerRelease); _titleArea?.AddHandler(PointerMovedEvent, OnTitlePointerMove, RoutingStrategies.Bubble); _titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble); _titleArea?.AddHandler(PointerReleasedEvent, OnTitlePointerRelease, RoutingStrategies.Bubble); } else { if (_titleArea is not null) _titleArea.IsHitTestVisible = false; } Button.ClickEvent.RemoveHandler(OnCloseButtonClick, _closeButton); _closeButton = e.NameScope.Find