using System.Collections; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Layout; using Avalonia.VisualTree; namespace Ursa.Controls; /// /// An that displays messages in a . /// [TemplatePart(PART_Items, typeof(Panel))] public abstract class WindowMessageManager : TemplatedControl { public const string PART_Items = "PART_Items"; protected IList? _items; /// /// Defines the property. /// public static readonly StyledProperty MaxItemsProperty = AvaloniaProperty.Register(nameof(MaxItems), 5); /// /// Defines the maximum number of messages visible at once. /// public int MaxItems { get => GetValue(MaxItemsProperty); set => SetValue(MaxItemsProperty, value); } static WindowMessageManager() { HorizontalAlignmentProperty.OverrideDefaultValue(HorizontalAlignment.Stretch); VerticalAlignmentProperty.OverrideDefaultValue(VerticalAlignment.Stretch); } public WindowMessageManager() { } public WindowMessageManager(VisualLayerManager? visualLayerManager) : this() { if (visualLayerManager is null) return; visualLayerManager.AdornerLayer.Children.Add(this); AdornerLayer.SetAdornedElement(this, visualLayerManager.AdornerLayer); } /// protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); var itemsControl = e.NameScope.Find(PART_Items); _items = itemsControl?.Children; } public abstract void Show(object content); /// /// Installs the within the /// protected void InstallFromTopLevel(TopLevel topLevel) { topLevel.TemplateApplied += TopLevelOnTemplateApplied; var adorner = topLevel.FindDescendantOfType()?.AdornerLayer; if (adorner is not null) { adorner.Children.Add(this); AdornerLayer.SetAdornedElement(this, adorner); } } public virtual void Uninstall() { if (Parent is AdornerLayer adornerLayer) { adornerLayer.Children.Remove(this); AdornerLayer.SetAdornedElement(this, null); } } protected void TopLevelOnTemplateApplied(object? sender, TemplateAppliedEventArgs e) { if (Parent is AdornerLayer adornerLayer) { adornerLayer.Children.Remove(this); AdornerLayer.SetAdornedElement(this, null); } // Reinstall message manager on template reapplied. var topLevel = (TopLevel)sender!; topLevel.TemplateApplied -= TopLevelOnTemplateApplied; InstallFromTopLevel(topLevel); } }