feat: add close button.

This commit is contained in:
Zhang Dian
2024-09-05 03:30:02 +08:00
parent f0b23e1bdf
commit f52d62f9f5
7 changed files with 76 additions and 34 deletions

View File

@@ -20,6 +20,11 @@ public interface IToast
/// </summary>
NotificationType Type { get; }
/// <summary>
/// Gets a value indicating whether the toast should show a close button.
/// </summary>
bool ShowClose { get; }
/// <summary>
/// Gets the expiration time of the toast after which it will automatically close.
/// If the value is <see cref="TimeSpan.Zero"/> then the toast will remain open until the user closes it.

View File

@@ -22,12 +22,14 @@ public class Toast : IToast, INotifyPropertyChanged
/// <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)
{
@@ -62,6 +64,9 @@ public class Toast : IToast, INotifyPropertyChanged
/// <inheritdoc/>
public NotificationType Type { get; set; }
/// <inheritdoc/>
public bool ShowClose { get; set; }
/// <inheritdoc/>
public TimeSpan Expiration { get; set; }

View File

@@ -77,6 +77,14 @@ public class ToastCard : ContentControl
/// </summary>
public static readonly StyledProperty<NotificationType> NotificationTypeProperty =
AvaloniaProperty.Register<ToastCard, NotificationType>(nameof(NotificationType));
public bool ShowClose
{
get => GetValue(ShowCloseProperty);
set => SetValue(ShowCloseProperty, value);
}
public static readonly StyledProperty<bool> ShowCloseProperty =
AvaloniaProperty.Register<ToastCard, bool>(nameof(ShowClose), true);
/// <summary>
/// Defines the <see cref="ToastClosed"/> event.

View File

@@ -70,7 +70,7 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
/// <inheritdoc/>
public void Show(IToast content)
{
Show(content, content.Type, content.Expiration, content.OnClick, content.OnClose);
Show(content, content.Type, content.Expiration, content.ShowClose, content.OnClick, content.OnClose);
}
/// <inheritdoc/>
@@ -78,7 +78,7 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
{
if (content is IToast toast)
{
Show(toast, toast.Type, toast.Expiration, toast.OnClick, toast.OnClose);
Show(toast, toast.Type, toast.Expiration, toast.ShowClose, toast.OnClick, toast.OnClose);
}
else
{
@@ -92,12 +92,14 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
/// <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="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 showClose = true,
Action? onClick = null,
Action? onClose = null,
string[]? classes = null)
@@ -107,11 +109,12 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
var toastControl = new ToastCard
{
Content = content,
NotificationType = type
NotificationType = type,
ShowClose = showClose
};
// Add style classes if any
if (classes != null)
if (classes is not null)
{
foreach (var @class in classes)
{
@@ -119,19 +122,14 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
}
}
toastControl.ToastClosed += (sender, args) =>
toastControl.ToastClosed += (sender, _) =>
{
onClose?.Invoke();
_items?.Remove(sender);
};
toastControl.PointerPressed += (sender, args) =>
{
onClick?.Invoke();
(sender as ToastCard)?.Close();
};
toastControl.PointerPressed += (_, _) => { onClick?.Invoke(); };
Dispatcher.UIThread.Post(() =>
{
@@ -153,11 +151,6 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
toastControl.Close();
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
}
/// <summary>
/// Installs the <see cref="WindowToastManager"/> within the <see cref="AdornerLayer"/>
/// </summary>