feat: separate some Notification shared files.
This commit is contained in:
35
src/Ursa/Controls/NotificationShared/IMessage.cs
Normal file
35
src/Ursa/Controls/NotificationShared/IMessage.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Avalonia.Controls.Notifications;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a message that can be shown in a window or by the host operating system.
|
||||
/// </summary>
|
||||
public interface IMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="NotificationType"/> of the message.
|
||||
/// </summary>
|
||||
NotificationType Type { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the message should show a close button.
|
||||
/// </summary>
|
||||
bool ShowClose { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the expiration time of the message after which it will automatically close.
|
||||
/// If the value is <see cref="TimeSpan.Zero"/> then the message will remain open until the user closes it.
|
||||
/// </summary>
|
||||
TimeSpan Expiration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an Action to be run when the message is clicked.
|
||||
/// </summary>
|
||||
Action? OnClick { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an Action to be run when the message is closed.
|
||||
/// </summary>
|
||||
Action? OnClose { get; }
|
||||
}
|
||||
207
src/Ursa/Controls/NotificationShared/MessageCard.cs
Normal file
207
src/Ursa/Controls/NotificationShared/MessageCard.cs
Normal file
@@ -0,0 +1,207 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.LogicalTree;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Control that represents and displays a message.
|
||||
/// </summary>
|
||||
[PseudoClasses(PC_Information, PC_Success, PC_Warning, PC_Error)]
|
||||
public abstract class MessageCard : ContentControl
|
||||
{
|
||||
public const string PC_Information = ":information";
|
||||
public const string PC_Success = ":success";
|
||||
public const string PC_Warning = ":warning";
|
||||
public const string PC_Error = ":error";
|
||||
|
||||
private bool _isClosing;
|
||||
|
||||
static MessageCard()
|
||||
{
|
||||
CloseOnClickProperty.Changed.AddClassHandler<Button>(OnCloseOnClickPropertyChanged);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MessageCard"/> class.
|
||||
/// </summary>
|
||||
public MessageCard()
|
||||
{
|
||||
UpdateNotificationType();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the message is already closing.
|
||||
/// </summary>
|
||||
public bool IsClosing
|
||||
{
|
||||
get => _isClosing;
|
||||
private set => SetAndRaise(IsClosingProperty, ref _isClosing, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IsClosing"/> property.
|
||||
/// </summary>
|
||||
public static readonly DirectProperty<MessageCard, bool> IsClosingProperty =
|
||||
AvaloniaProperty.RegisterDirect<MessageCard, bool>(nameof(IsClosing), o => o.IsClosing);
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the message is closed.
|
||||
/// </summary>
|
||||
public bool IsClosed
|
||||
{
|
||||
get => GetValue(IsClosedProperty);
|
||||
set => SetValue(IsClosedProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IsClosed"/> property.
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<bool> IsClosedProperty =
|
||||
AvaloniaProperty.Register<MessageCard, bool>(nameof(IsClosed));
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the type of the message
|
||||
/// </summary>
|
||||
public NotificationType NotificationType
|
||||
{
|
||||
get => GetValue(NotificationTypeProperty);
|
||||
set => SetValue(NotificationTypeProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="NotificationType" /> property
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<NotificationType> NotificationTypeProperty =
|
||||
AvaloniaProperty.Register<MessageCard, NotificationType>(nameof(NotificationType));
|
||||
|
||||
public bool ShowClose
|
||||
{
|
||||
get => GetValue(ShowCloseProperty);
|
||||
set => SetValue(ShowCloseProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<bool> ShowCloseProperty =
|
||||
AvaloniaProperty.Register<MessageCard, bool>(nameof(ShowClose), true);
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="MessageClosed"/> event.
|
||||
/// </summary>
|
||||
public static readonly RoutedEvent<RoutedEventArgs> MessageClosedEvent =
|
||||
RoutedEvent.Register<MessageCard, RoutedEventArgs>(nameof(MessageClosed), RoutingStrategies.Bubble);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Raised when the <see cref="MessageCard"/> has closed.
|
||||
/// </summary>
|
||||
public event EventHandler<RoutedEventArgs>? MessageClosed
|
||||
{
|
||||
add => AddHandler(MessageClosedEvent, value);
|
||||
remove => RemoveHandler(MessageClosedEvent, value);
|
||||
}
|
||||
|
||||
public static bool GetCloseOnClick(Button obj)
|
||||
{
|
||||
_ = obj ?? throw new ArgumentNullException(nameof(obj));
|
||||
return obj.GetValue(CloseOnClickProperty);
|
||||
}
|
||||
|
||||
public static void SetCloseOnClick(Button obj, bool value)
|
||||
{
|
||||
_ = obj ?? throw new ArgumentNullException(nameof(obj));
|
||||
obj.SetValue(CloseOnClickProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the CloseOnClick property.
|
||||
/// </summary>
|
||||
public static readonly AttachedProperty<bool> CloseOnClickProperty =
|
||||
AvaloniaProperty.RegisterAttached<MessageCard, Button, bool>("CloseOnClick", defaultValue: false);
|
||||
|
||||
private static void OnCloseOnClickPropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (Button)d;
|
||||
var value = (bool)e.NewValue!;
|
||||
if (value)
|
||||
{
|
||||
button.Click += Button_Click;
|
||||
}
|
||||
else
|
||||
{
|
||||
button.Click -= Button_Click;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when a button inside the Message is clicked.
|
||||
/// </summary>
|
||||
private static void Button_Click(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var btn = sender as ILogical;
|
||||
var message = btn?.GetLogicalAncestors().OfType<MessageCard>().FirstOrDefault();
|
||||
message?.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the <see cref="MessageCard"/>.
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
if (IsClosing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsClosing = true;
|
||||
}
|
||||
|
||||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e)
|
||||
{
|
||||
base.OnPropertyChanged(e);
|
||||
|
||||
if (e.Property == ContentProperty && e.NewValue is IMessage message)
|
||||
{
|
||||
SetValue(NotificationTypeProperty, message.Type);
|
||||
}
|
||||
|
||||
if (e.Property == NotificationTypeProperty)
|
||||
{
|
||||
UpdateNotificationType();
|
||||
}
|
||||
|
||||
if (e.Property == IsClosedProperty)
|
||||
{
|
||||
if (!IsClosing && !IsClosed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RaiseEvent(new RoutedEventArgs(MessageClosedEvent));
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateNotificationType()
|
||||
{
|
||||
switch (NotificationType)
|
||||
{
|
||||
case NotificationType.Error:
|
||||
PseudoClasses.Add(PC_Error);
|
||||
break;
|
||||
|
||||
case NotificationType.Information:
|
||||
PseudoClasses.Add(PC_Information);
|
||||
break;
|
||||
|
||||
case NotificationType.Success:
|
||||
PseudoClasses.Add(PC_Success);
|
||||
break;
|
||||
|
||||
case NotificationType.Warning:
|
||||
PseudoClasses.Add(PC_Warning);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
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