feat: separate some Notification shared files.
This commit is contained in:
80
src/Ursa/Controls/NotificationShared/WindowMessageManager.cs
Normal file
80
src/Ursa/Controls/NotificationShared/WindowMessageManager.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="WindowMessageManager"/> that displays messages in a <see cref="Window"/>.
|
||||
/// </summary>
|
||||
[TemplatePart(PART_Items, typeof(Panel))]
|
||||
public abstract class WindowMessageManager : TemplatedControl
|
||||
{
|
||||
public const string PART_Items = "PART_Items";
|
||||
|
||||
protected IList? _items;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="MaxItems"/> property.
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<int> MaxItemsProperty =
|
||||
AvaloniaProperty.Register<WindowMessageManager, int>(nameof(MaxItems), 5);
|
||||
|
||||
/// <summary>
|
||||
/// Defines the maximum number of messages visible at once.
|
||||
/// </summary>
|
||||
public int MaxItems
|
||||
{
|
||||
get => GetValue(MaxItemsProperty);
|
||||
set => SetValue(MaxItemsProperty, value);
|
||||
}
|
||||
|
||||
static WindowMessageManager()
|
||||
{
|
||||
HorizontalAlignmentProperty.OverrideDefaultValue<WindowMessageManager>(HorizontalAlignment.Stretch);
|
||||
VerticalAlignmentProperty.OverrideDefaultValue<WindowMessageManager>(VerticalAlignment.Stretch);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
|
||||
var itemsControl = e.NameScope.Find<Panel>(PART_Items);
|
||||
_items = itemsControl?.Children;
|
||||
}
|
||||
|
||||
public abstract void Show(object content);
|
||||
|
||||
/// <summary>
|
||||
/// Installs the <see cref="WindowMessageManager"/> within the <see cref="AdornerLayer"/>
|
||||
/// </summary>
|
||||
protected void InstallFromTopLevel(TopLevel topLevel)
|
||||
{
|
||||
topLevel.TemplateApplied += TopLevelOnTemplateApplied;
|
||||
var adorner = topLevel.FindDescendantOfType<VisualLayerManager>()?.AdornerLayer;
|
||||
if (adorner is not null)
|
||||
{
|
||||
adorner.Children.Add(this);
|
||||
AdornerLayer.SetAdornedElement(this, adorner);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user