using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Notifications;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
namespace Ursa.Controls;
///
/// Control that represents and displays a message.
///
[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(OnCloseOnClickPropertyChanged);
}
///
/// Initializes a new instance of the class.
///
public MessageCard()
{
UpdateNotificationType();
}
///
/// Determines if the message is already closing.
///
public bool IsClosing
{
get => _isClosing;
private set => SetAndRaise(IsClosingProperty, ref _isClosing, value);
}
///
/// Defines the property.
///
public static readonly DirectProperty IsClosingProperty =
AvaloniaProperty.RegisterDirect(nameof(IsClosing), o => o.IsClosing);
///
/// Determines if the message is closed.
///
public bool IsClosed
{
get => GetValue(IsClosedProperty);
set => SetValue(IsClosedProperty, value);
}
///
/// Defines the property.
///
public static readonly StyledProperty IsClosedProperty =
AvaloniaProperty.Register(nameof(IsClosed));
///
/// Gets or sets the type of the message
///
public NotificationType NotificationType
{
get => GetValue(NotificationTypeProperty);
set => SetValue(NotificationTypeProperty, value);
}
///
/// Defines the property
///
public static readonly StyledProperty NotificationTypeProperty =
AvaloniaProperty.Register(nameof(NotificationType));
public bool ShowIcon
{
get => GetValue(ShowIconProperty);
set => SetValue(ShowIconProperty, value);
}
public static readonly StyledProperty ShowIconProperty =
AvaloniaProperty.Register(nameof(ShowIcon), true);
public bool ShowClose
{
get => GetValue(ShowCloseProperty);
set => SetValue(ShowCloseProperty, value);
}
public static readonly StyledProperty ShowCloseProperty =
AvaloniaProperty.Register(nameof(ShowClose), true);
///
/// Defines the event.
///
public static readonly RoutedEvent MessageClosedEvent =
RoutedEvent.Register(nameof(MessageClosed), RoutingStrategies.Bubble);
///
/// Raised when the has closed.
///
public event EventHandler? 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);
}
///
/// Defines the CloseOnClick property.
///
public static readonly AttachedProperty CloseOnClickProperty =
AvaloniaProperty.RegisterAttached("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;
}
}
///
/// Called when a button inside the Message is clicked.
///
private static void Button_Click(object? sender, RoutedEventArgs e)
{
var btn = sender as ILogical;
var message = btn?.GetLogicalAncestors().OfType().FirstOrDefault();
message?.Close();
}
///
/// Closes the .
///
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;
}
}
}