Merge pull request #398 from irihitech/toast

New Control: Notification & Toast
This commit is contained in:
Dong Bin
2024-09-12 18:55:40 +08:00
committed by GitHub
33 changed files with 2034 additions and 0 deletions

View 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; }
}

View 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);
}

View File

@@ -0,0 +1,108 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Controls.Notifications;
using Avalonia.Metadata;
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/>
[Content]
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 ShowIcon { 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));
}
}

View File

@@ -0,0 +1,49 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Notifications;
using Avalonia.LogicalTree;
namespace Ursa.Controls;
/// <summary>
/// Control that represents and displays a notification.
/// </summary>
[PseudoClasses(
WindowNotificationManager.PC_TopLeft,
WindowNotificationManager.PC_TopRight,
WindowNotificationManager.PC_BottomLeft,
WindowNotificationManager.PC_BottomRight,
WindowNotificationManager.PC_TopCenter,
WindowNotificationManager.PC_BottomCenter
)]
public class NotificationCard : MessageCard
{
private NotificationPosition _position;
public NotificationPosition Position
{
get => _position;
set => SetAndRaise(PositionProperty, ref _position, value);
}
public static readonly DirectProperty<NotificationCard, NotificationPosition> PositionProperty =
AvaloniaProperty.RegisterDirect<NotificationCard, NotificationPosition>(nameof(Position),
o => o.Position, (o, v) => o.Position = v);
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnAttachedToLogicalTree(e);
UpdatePseudoClasses(Position);
}
private void UpdatePseudoClasses(NotificationPosition position)
{
PseudoClasses.Set(WindowNotificationManager.PC_TopLeft, position == NotificationPosition.TopLeft);
PseudoClasses.Set(WindowNotificationManager.PC_TopRight, position == NotificationPosition.TopRight);
PseudoClasses.Set(WindowNotificationManager.PC_BottomLeft, position == NotificationPosition.BottomLeft);
PseudoClasses.Set(WindowNotificationManager.PC_BottomRight, position == NotificationPosition.BottomRight);
PseudoClasses.Set(WindowNotificationManager.PC_TopCenter, position == NotificationPosition.TopCenter);
PseudoClasses.Set(WindowNotificationManager.PC_BottomCenter, position == NotificationPosition.BottomCenter);
}
}

View File

@@ -0,0 +1,178 @@
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.ShowIcon, 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.ShowIcon, 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="showIcon">whether to show the icon</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 showIcon = true,
bool showClose = true,
Action? onClick = null,
Action? onClose = null,
string[]? classes = null)
{
Dispatcher.UIThread.VerifyAccess();
var notificationControl = new NotificationCard
{
Content = content,
NotificationType = type,
ShowIcon = showIcon,
ShowClose = showClose,
[!NotificationCard.PositionProperty] = this[!PositionProperty]
};
// 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);
}
}

View File

@@ -0,0 +1,40 @@
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 an icon.
/// </summary>
bool ShowIcon { 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; }
}

View File

@@ -0,0 +1,216 @@
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 ShowIcon
{
get => GetValue(ShowIconProperty);
set => SetValue(ShowIconProperty, value);
}
public static readonly StyledProperty<bool> ShowIconProperty =
AvaloniaProperty.Register<MessageCard, bool>(nameof(ShowIcon), true);
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;
}
}
}

View File

@@ -0,0 +1,89 @@
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);
}
}
public virtual void Uninstall()
{
if (Parent is AdornerLayer adornerLayer)
{
adornerLayer.Children.Remove(this);
AdornerLayer.SetAdornedElement(this, null);
}
}
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);
}
}

View File

@@ -0,0 +1,12 @@
namespace Ursa.Controls;
/// <summary>
/// Represents a toast that can be shown in a window or by the host operating system.
/// </summary>
public interface IToast : IMessage
{
/// <summary>
/// Gets the toast message.
/// </summary>
string? Content { get; }
}

View File

@@ -0,0 +1,17 @@
using Avalonia.Metadata;
namespace Ursa.Controls;
/// <summary>
/// Represents a toast manager that can be used to show toasts in a window or using
/// the host operating system.
/// </summary>
[NotClientImplementable]
public interface IToastManager
{
/// <summary>
/// Show a toast.
/// </summary>
/// <param name="toast">The toast to be displayed.</param>
void Show(IToast toast);
}

View File

@@ -0,0 +1,91 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Controls.Notifications;
using Avalonia.Metadata;
namespace Ursa.Controls;
/// <summary>
/// A toast that can be shown in a window or by the host operating system.
/// </summary>
/// <remarks>
/// This class represents a toast that can be displayed either in a window using
/// <see cref="WindowToastManager"/> or by the host operating system (to be implemented).
/// </remarks>
public class Toast : IToast, INotifyPropertyChanged
{
private string? _content;
/// <summary>
/// Initializes a new instance of the <see cref="Toast"/> class.
/// </summary>
/// <param name="content">The content to be displayed in the toast.</param>
/// <param name="type">The <see cref="NotificationType"/> of the toast.</param>
/// <param name="expiration">The expiry time at which the toast will close.
/// Use <see cref="TimeSpan.Zero"/> for toasts that will remain open.</param>
/// <param name="showClose">A value indicating whether the toast should show a close button.</param>
/// <param name="onClick">An Action to call when the toast is clicked.</param>
/// <param name="onClose">An Action to call when the toast is closed.</param>
public Toast(
string? content,
NotificationType type = NotificationType.Information,
TimeSpan? expiration = null,
bool showClose = true,
Action? onClick = null,
Action? onClose = null)
{
Content = content;
Type = type;
Expiration = expiration ?? TimeSpan.FromSeconds(3);
ShowClose = showClose;
OnClick = onClick;
OnClose = onClose;
}
/// <summary>
/// Initializes a new instance of the <see cref="Toast"/> class.
/// </summary>
public Toast() : this(null)
{
}
/// <inheritdoc/>
[Content]
public string? Content
{
get => _content;
set
{
if (_content != value)
{
_content = value;
OnPropertyChanged();
}
}
}
/// <inheritdoc/>
public NotificationType Type { get; set; }
/// <inheritdoc/>
public bool ShowIcon { get; set; }
/// <inheritdoc/>
public bool ShowClose { get; set; }
/// <inheritdoc/>
public TimeSpan Expiration { get; set; }
/// <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));
}
}

View File

@@ -0,0 +1,6 @@
namespace Ursa.Controls;
/// <summary>
/// Control that represents and displays a toast.
/// </summary>
public class ToastCard : MessageCard;

View File

@@ -0,0 +1,122 @@
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Threading;
namespace Ursa.Controls;
/// <summary>
/// An <see cref="IToastManager"/> that displays toasts in a <see cref="Window"/>.
/// </summary>
public class WindowToastManager : WindowMessageManager, IToastManager
{
/// <summary>
/// Initializes a new instance of the <see cref="WindowToastManager"/> class.
/// </summary>
/// <param name="host">The TopLevel that will host the control.</param>
public WindowToastManager(TopLevel? host) : this()
{
if (host is not null)
{
InstallFromTopLevel(host);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="WindowToastManager"/> class.
/// </summary>
public WindowToastManager()
{
}
/// <inheritdoc/>
public void Show(IToast content)
{
Show(content, content.Type, content.Expiration,
content.ShowIcon, content.ShowClose,
content.OnClick, content.OnClose);
}
/// <inheritdoc/>
public override void Show(object content)
{
if (content is IToast toast)
{
Show(toast, toast.Type, toast.Expiration,
toast.ShowIcon, toast.ShowClose,
toast.OnClick, toast.OnClose);
}
else
{
Show(content, NotificationType.Information);
}
}
/// <summary>
/// Shows a Toast
/// </summary>
/// <param name="content">the content of the toast</param>
/// <param name="type">the type of the toast</param>
/// <param name="expiration">the expiration time of the toast after which it will automatically close. If the value is Zero then the toast will remain open until the user closes it</param>
/// <param name="showIcon">whether to show the icon</param>
/// <param name="showClose">whether to show the close button</param>
/// <param name="onClick">an Action to be run when the toast is clicked</param>
/// <param name="onClose">an Action to be run when the toast is closed</param>
/// <param name="classes">style classes to apply</param>
public async void Show(
object content,
NotificationType type,
TimeSpan? expiration = null,
bool showIcon = true,
bool showClose = true,
Action? onClick = null,
Action? onClose = null,
string[]? classes = null)
{
Dispatcher.UIThread.VerifyAccess();
var toastControl = new ToastCard
{
Content = content,
NotificationType = type,
ShowIcon = showIcon,
ShowClose = showClose
};
// Add style classes if any
if (classes is not null)
{
foreach (var @class in classes)
{
toastControl.Classes.Add(@class);
}
}
toastControl.MessageClosed += (sender, _) =>
{
onClose?.Invoke();
_items?.Remove(sender);
};
toastControl.PointerPressed += (_, _) => { onClick?.Invoke(); };
Dispatcher.UIThread.Post(() =>
{
_items?.Add(toastControl);
if (_items?.OfType<ToastCard>().Count(i => !i.IsClosing) > MaxItems)
{
_items.OfType<ToastCard>().First(i => !i.IsClosing).Close();
}
});
if (expiration == TimeSpan.Zero)
{
return;
}
await Task.Delay(expiration ?? TimeSpan.FromSeconds(3));
toastControl.Close();
}
}