feat: add new Notification.
This commit is contained in:
17
src/Ursa/Controls/Notification/INotification.cs
Normal file
17
src/Ursa/Controls/Notification/INotification.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a notification that can be shown in a window or by the host operating system.
|
||||
/// </summary>
|
||||
public interface INotification : IMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the Title of the notification.
|
||||
/// </summary>
|
||||
string? Title { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Content of the notification.
|
||||
/// </summary>
|
||||
string? Content { get; }
|
||||
}
|
||||
14
src/Ursa/Controls/Notification/INotificationManager.cs
Normal file
14
src/Ursa/Controls/Notification/INotificationManager.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a notification manager that can be used to show notifications in a window or using
|
||||
/// the host operating system.
|
||||
/// </summary>
|
||||
public interface INotificationManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Show a notification.
|
||||
/// </summary>
|
||||
/// <param name="notification">The notification to be displayed.</param>
|
||||
void Show(INotification notification);
|
||||
}
|
||||
103
src/Ursa/Controls/Notification/Notification.cs
Normal file
103
src/Ursa/Controls/Notification/Notification.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Avalonia.Controls.Notifications;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// A notification that can be shown in a window or by the host operating system.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This class represents a notification that can be displayed either in a window using
|
||||
/// <see cref="WindowNotificationManager"/> or by the host operating system (to be implemented).
|
||||
/// </remarks>
|
||||
public class Notification : INotification, INotifyPropertyChanged
|
||||
{
|
||||
private string? _title, _content;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Notification"/> class.
|
||||
/// </summary>
|
||||
/// <param name="title">The title of the notification.</param>
|
||||
/// <param name="content">The content to be displayed in the notification.</param>
|
||||
/// <param name="type">The <see cref="NotificationType"/> of the notification.</param>
|
||||
/// <param name="expiration">The expiry time at which the notification will close.
|
||||
/// Use <see cref="TimeSpan.Zero"/> for notifications that will remain open.</param>
|
||||
/// <param name="showClose">A value indicating whether the notification should show a close button.</param>
|
||||
/// <param name="onClick">An Action to call when the notification is clicked.</param>
|
||||
/// <param name="onClose">An Action to call when the notification is closed.</param>
|
||||
public Notification(
|
||||
string? title,
|
||||
string? content,
|
||||
NotificationType type = NotificationType.Information,
|
||||
TimeSpan? expiration = null,
|
||||
bool showClose = true,
|
||||
Action? onClick = null,
|
||||
Action? onClose = null)
|
||||
{
|
||||
Title = title;
|
||||
Content = content;
|
||||
Type = type;
|
||||
Expiration = expiration ?? TimeSpan.FromSeconds(3);
|
||||
ShowClose = showClose;
|
||||
OnClick = onClick;
|
||||
OnClose = onClose;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Notification"/> class.
|
||||
/// </summary>
|
||||
public Notification() : this(null, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string? Title
|
||||
{
|
||||
get => _title;
|
||||
set
|
||||
{
|
||||
if (_title != value)
|
||||
{
|
||||
_title = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string? Content
|
||||
{
|
||||
get => _content;
|
||||
set
|
||||
{
|
||||
if (_content != value)
|
||||
{
|
||||
_content = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public NotificationType Type { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public TimeSpan Expiration { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool ShowClose { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Action? OnClick { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Action? OnClose { get; set; }
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
6
src/Ursa/Controls/Notification/NotificationCard.cs
Normal file
6
src/Ursa/Controls/Notification/NotificationCard.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Control that represents and displays a notification.
|
||||
/// </summary>
|
||||
public class NotificationCard : MessageCard;
|
||||
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