Browse Source

feat: Customize Notifications

pull/12178/head
Tim 3 years ago
parent
commit
eabaa776cc
  1. 2
      samples/ControlCatalog/ViewModels/NotificationViewModel.cs
  2. 13
      src/Avalonia.Controls/Notifications/IManagedNotificationManager.cs
  3. 65
      src/Avalonia.Controls/Notifications/NotificationCard.cs
  4. 24
      src/Avalonia.Controls/Notifications/WindowNotificationManager.cs

2
samples/ControlCatalog/ViewModels/NotificationViewModel.cs

@ -11,7 +11,7 @@ namespace ControlCatalog.ViewModels
{
ShowCustomManagedNotificationCommand = MiniCommand.Create(() =>
{
NotificationManager?.Show(new NotificationViewModel() { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" , NotificationManager = NotificationManager});
NotificationManager?.Show(new NotificationViewModel() { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" , NotificationManager = NotificationManager}, NotificationType.Warning);
});
ShowManagedNotificationCommand = MiniCommand.Create(() =>

13
src/Avalonia.Controls/Notifications/IManagedNotificationManager.cs

@ -1,4 +1,5 @@
using Avalonia.Metadata;
using System;
using Avalonia.Metadata;
namespace Avalonia.Controls.Notifications
{
@ -18,6 +19,14 @@ namespace Avalonia.Controls.Notifications
/// Shows a notification.
/// </summary>
/// <param name="content">The content to be displayed.</param>
void Show(object content);
/// <param name="type">The <see cref="NotificationType"/> of the notification.</param>
/// <param name="expiration">the expiration time of the notification after which it will automatically close. If the value is <see cref="TimeSpan.Zero"/> then the notification will remain open until the user closes it.</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>
void Show(object content,
NotificationType type = NotificationType.Information,
TimeSpan? expiration = null,
Action? onClick = null,
Action? onClose = null);
}
}

65
src/Avalonia.Controls/Notifications/NotificationCard.cs

@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using Avalonia.Reactive;
using Avalonia.Controls.Metadata;
@ -61,6 +61,7 @@ namespace Avalonia.Controls.Notifications
}
}
});
UpdateNotificationType();
}
/// <summary>
@ -93,6 +94,21 @@ namespace Avalonia.Controls.Notifications
public static readonly StyledProperty<bool> IsClosedProperty =
AvaloniaProperty.Register<NotificationCard, bool>(nameof(IsClosed));
/// <summary>
/// Gets or sets the type of the notification
/// </summary>
public NotificationType NotificationType
{
get { return GetValue(NotificationTypeProperty); }
set { SetValue(NotificationTypeProperty, value); }
}
/// <summary>
/// Defines the <see cref="NotificationType" /> property
/// </summary>
public static readonly StyledProperty<NotificationType> NotificationTypeProperty =
AvaloniaProperty.Register<NotificationCard, NotificationType>(nameof(NotificationType));
/// <summary>
/// Defines the <see cref="NotificationClosed"/> event.
/// </summary>
@ -163,5 +179,52 @@ namespace Avalonia.Controls.Notifications
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;
}
}
}
}

24
src/Avalonia.Controls/Notifications/WindowNotificationManager.cs

@ -80,32 +80,32 @@ namespace Avalonia.Controls.Notifications
/// <inheritdoc/>
public void Show(INotification content)
{
Show(content as object);
Show(content, content.Type, content.Expiration, content.OnClick, content.OnClose);
}
/// <inheritdoc/>
public async void Show(object content)
public async void Show(object content,
NotificationType type = NotificationType.Information,
TimeSpan? expiration = null,
Action? onClick = null,
Action? onClose = null)
{
var notification = content as INotification;
var notificationControl = new NotificationCard
{
Content = content
Content = content,
NotificationType = type
};
notificationControl.NotificationClosed += (sender, args) =>
{
notification?.OnClose?.Invoke();
onClose?.Invoke();
_items?.Remove(sender);
};
notificationControl.PointerPressed += (sender, args) =>
{
if (notification != null && notification.OnClick != null)
{
notification.OnClick.Invoke();
}
onClick?.Invoke();
(sender as NotificationCard)?.Close();
};
@ -117,12 +117,12 @@ namespace Avalonia.Controls.Notifications
_items.OfType<NotificationCard>().First(i => !i.IsClosing).Close();
}
if (notification != null && notification.Expiration == TimeSpan.Zero)
if (expiration == TimeSpan.Zero)
{
return;
}
await Task.Delay(notification?.Expiration ?? TimeSpan.FromSeconds(5));
await Task.Delay(expiration ?? TimeSpan.FromSeconds(5));
notificationControl.Close();
}

Loading…
Cancel
Save