feat: add new Notification.
This commit is contained in:
171
src/Ursa/Controls/Notification/WindowNotificationManager.cs
Normal file
171
src/Ursa/Controls/Notification/WindowNotificationManager.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Threading;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="INotificationManager"/> that displays notifications in a <see cref="Window"/>.
|
||||
/// </summary>
|
||||
[PseudoClasses(PC_TopLeft, PC_TopRight, PC_BottomLeft, PC_BottomRight, PC_TopCenter, PC_BottomCenter)]
|
||||
public class WindowNotificationManager : WindowMessageManager, INotificationManager
|
||||
{
|
||||
public const string PC_TopLeft = ":topleft";
|
||||
public const string PC_TopRight = ":topright";
|
||||
public const string PC_BottomLeft = ":bottomleft";
|
||||
public const string PC_BottomRight = ":bottomright";
|
||||
public const string PC_TopCenter = ":topcenter";
|
||||
public const string PC_BottomCenter = ":bottomcenter";
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="Position"/> property.
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<NotificationPosition> PositionProperty =
|
||||
AvaloniaProperty.Register<WindowNotificationManager, NotificationPosition>(nameof(Position),
|
||||
NotificationPosition.TopRight);
|
||||
|
||||
/// <summary>
|
||||
/// Defines which corner of the screen notifications can be displayed in.
|
||||
/// </summary>
|
||||
/// <seealso cref="NotificationPosition"/>
|
||||
public NotificationPosition Position
|
||||
{
|
||||
get => GetValue(PositionProperty);
|
||||
set => SetValue(PositionProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WindowNotificationManager"/> class.
|
||||
/// </summary>
|
||||
/// <param name="host">The TopLevel that will host the control.</param>
|
||||
public WindowNotificationManager(TopLevel? host) : this()
|
||||
{
|
||||
if (host is not null)
|
||||
{
|
||||
InstallFromTopLevel(host);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WindowNotificationManager"/> class.
|
||||
/// </summary>
|
||||
public WindowNotificationManager()
|
||||
{
|
||||
UpdatePseudoClasses(Position);
|
||||
}
|
||||
|
||||
static WindowNotificationManager()
|
||||
{
|
||||
HorizontalAlignmentProperty.OverrideDefaultValue<WindowNotificationManager>(HorizontalAlignment.Stretch);
|
||||
VerticalAlignmentProperty.OverrideDefaultValue<WindowNotificationManager>(VerticalAlignment.Stretch);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Show(INotification content)
|
||||
{
|
||||
Show(content, content.Type, content.Expiration, content.ShowClose, content.OnClick, content.OnClose);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Show(object content)
|
||||
{
|
||||
if (content is INotification notification)
|
||||
{
|
||||
Show(notification, notification.Type, notification.Expiration, notification.ShowClose, notification.OnClick,
|
||||
notification.OnClose);
|
||||
}
|
||||
else
|
||||
{
|
||||
Show(content, NotificationType.Information);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows a Notification
|
||||
/// </summary>
|
||||
/// <param name="content">the content of the notification</param>
|
||||
/// <param name="type">the type of the notification</param>
|
||||
/// <param name="expiration">the expiration time of the notification after which it will automatically close. If the value is Zero then the notification will remain open until the user closes it</param>
|
||||
/// <param name="showClose">whether to show the close button</param>
|
||||
/// <param name="onClick">an Action to be run when the notification is clicked</param>
|
||||
/// <param name="onClose">an Action to be run when the notification is closed</param>
|
||||
/// <param name="classes">style classes to apply</param>
|
||||
public async void Show(
|
||||
object content,
|
||||
NotificationType type,
|
||||
TimeSpan? expiration = null,
|
||||
bool showClose = true,
|
||||
Action? onClick = null,
|
||||
Action? onClose = null,
|
||||
string[]? classes = null)
|
||||
{
|
||||
Dispatcher.UIThread.VerifyAccess();
|
||||
|
||||
var notificationControl = new NotificationCard
|
||||
{
|
||||
Content = content,
|
||||
NotificationType = type,
|
||||
ShowClose = showClose
|
||||
};
|
||||
|
||||
// Add style classes if any
|
||||
if (classes is not null)
|
||||
{
|
||||
foreach (var @class in classes)
|
||||
{
|
||||
notificationControl.Classes.Add(@class);
|
||||
}
|
||||
}
|
||||
|
||||
notificationControl.MessageClosed += (sender, _) =>
|
||||
{
|
||||
onClose?.Invoke();
|
||||
|
||||
_items?.Remove(sender);
|
||||
};
|
||||
|
||||
notificationControl.PointerPressed += (_, _) => { onClick?.Invoke(); };
|
||||
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
_items?.Add(notificationControl);
|
||||
|
||||
if (_items?.OfType<NotificationCard>().Count(i => !i.IsClosing) > MaxItems)
|
||||
{
|
||||
_items.OfType<NotificationCard>().First(i => !i.IsClosing).Close();
|
||||
}
|
||||
});
|
||||
|
||||
if (expiration == TimeSpan.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(expiration ?? TimeSpan.FromSeconds(3));
|
||||
|
||||
notificationControl.Close();
|
||||
}
|
||||
|
||||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
||||
{
|
||||
base.OnPropertyChanged(change);
|
||||
|
||||
if (change.Property == PositionProperty)
|
||||
{
|
||||
UpdatePseudoClasses(change.GetNewValue<NotificationPosition>());
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePseudoClasses(NotificationPosition position)
|
||||
{
|
||||
PseudoClasses.Set(PC_TopLeft, position == NotificationPosition.TopLeft);
|
||||
PseudoClasses.Set(PC_TopRight, position == NotificationPosition.TopRight);
|
||||
PseudoClasses.Set(PC_BottomLeft, position == NotificationPosition.BottomLeft);
|
||||
PseudoClasses.Set(PC_BottomRight, position == NotificationPosition.BottomRight);
|
||||
PseudoClasses.Set(PC_TopCenter, position == NotificationPosition.TopCenter);
|
||||
PseudoClasses.Set(PC_BottomCenter, position == NotificationPosition.BottomCenter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user