using System;
using System.Linq;
using Avalonia.Reactive;
using Avalonia.Controls.Metadata;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
namespace Avalonia.Controls.Notifications
{
///
/// Control that represents and displays a notification.
///
[PseudoClasses(":error", ":information", ":success", ":warning")]
public class NotificationCard : ContentControl
{
private bool _isClosing;
static NotificationCard()
{
CloseOnClickProperty.Changed.AddClassHandler(OnCloseOnClickPropertyChanged);
}
///
/// Initializes a new instance of the class.
///
public NotificationCard()
{
this.GetObservable(IsClosedProperty)
.Subscribe(x =>
{
if (!IsClosing && !IsClosed)
{
return;
}
RaiseEvent(new RoutedEventArgs(NotificationClosedEvent));
});
this.GetObservable(ContentProperty)
.Subscribe(x =>
{
if (x is INotification notification)
{
switch (notification.Type)
{
case NotificationType.Error:
PseudoClasses.Add(":error");
break;
case NotificationType.Information:
PseudoClasses.Add(":information");
break;
case NotificationType.Success:
PseudoClasses.Add(":success");
break;
case NotificationType.Warning:
PseudoClasses.Add(":warning");
break;
}
}
});
UpdateNotificationType();
}
///
/// Determines if the notification is already closing.
///
public bool IsClosing
{
get { return _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 notification 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 notification
///
public NotificationType NotificationType
{
get { return GetValue(NotificationTypeProperty); }
set { SetValue(NotificationTypeProperty, value); }
}
///
/// Defines the property
///
public static readonly StyledProperty NotificationTypeProperty =
AvaloniaProperty.Register(nameof(NotificationType));
///
/// Defines the event.
///
public static readonly RoutedEvent NotificationClosedEvent =
RoutedEvent.Register(nameof(NotificationClosed), RoutingStrategies.Bubble);
///
/// Raised when the has closed.
///
public event EventHandler? NotificationClosed
{
add { AddHandler(NotificationClosedEvent, value); }
remove { RemoveHandler(NotificationClosedEvent, value); }
}
public static bool GetCloseOnClick(Button obj)
{
_ = obj ?? throw new ArgumentNullException(nameof(obj));
return (bool)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 Notification is clicked.
///
private static void Button_Click(object? sender, RoutedEventArgs e)
{
var btn = sender as ILogical;
var notification = btn?.GetLogicalAncestors().OfType().FirstOrDefault();
notification?.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 INotification notification)
{
SetValue(NotificationTypeProperty, notification.Type);
}
if (e.Property == NotificationTypeProperty)
{
UpdateNotificationType();
}
if (e.Property == IsClosedProperty)
{
if (!IsClosing && !IsClosed)
{
return;
}
RaiseEvent(new RoutedEventArgs(NotificationClosedEvent));
}
}
private void UpdateNotificationType()
{
switch (NotificationType)
{
case NotificationType.Error:
PseudoClasses.Add(":error");
break;
case NotificationType.Information:
PseudoClasses.Add(":information");
break;
case NotificationType.Success:
PseudoClasses.Add(":success");
break;
case NotificationType.Warning:
PseudoClasses.Add(":warning");
break;
}
}
}
}