67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
using System.Windows;
|
|
using System.Windows.Threading;
|
|
using Notification.Core;
|
|
using Notification.Wpf;
|
|
using Notification.Wpf.Constants;
|
|
using ytLive.Helpers;
|
|
|
|
namespace ytLive.Services;
|
|
|
|
/// <summary>
|
|
/// Toast notifications rendered inside the main window's <c>ToastArea</c>
|
|
/// (bottom-right, above the footer). Info/success auto-dismiss after ~4s,
|
|
/// warnings ~8s, errors stay until dismissed. Calls from any thread are
|
|
/// marshalled to the captured dispatcher.
|
|
/// </summary>
|
|
public sealed class NotificationService : INotificationService
|
|
{
|
|
public const string AreaName = "ToastArea";
|
|
|
|
internal static readonly IReadOnlyDictionary<AppNotificationSeverity, (string BackgroundHex, double ExpirationSeconds)> Cards
|
|
= new Dictionary<AppNotificationSeverity, (string, double)>
|
|
{
|
|
[AppNotificationSeverity.Info] = ("#3a3b52", 4),
|
|
[AppNotificationSeverity.Success] = ("#1d5c38", 4),
|
|
[AppNotificationSeverity.Warning] = ("#b8860b", 8),
|
|
[AppNotificationSeverity.Error] = ("#8f1f1f", 0),
|
|
};
|
|
|
|
private readonly NotificationManager _manager = new();
|
|
private readonly Dispatcher _dispatcher;
|
|
|
|
public NotificationService()
|
|
: this(Application.Current?.Dispatcher)
|
|
{
|
|
}
|
|
|
|
internal NotificationService(Dispatcher? dispatcher)
|
|
{
|
|
_dispatcher = dispatcher ?? Dispatcher.CurrentDispatcher;
|
|
NotificationConstants.NotificationCornerRadius = new CornerRadius(8);
|
|
NotificationConstants.KeepNotificationVisibleOnMouseOver = true;
|
|
}
|
|
|
|
public void Show(string title, string message, AppNotificationSeverity severity)
|
|
{
|
|
var request = BuildRequest(title, message, severity);
|
|
_dispatcher.Invoke(() => _manager.Show(request));
|
|
}
|
|
|
|
internal static NotificationRequest BuildRequest(string title, string message, AppNotificationSeverity severity)
|
|
{
|
|
var (backgroundHex, expirationSeconds) = Cards[severity];
|
|
var builder = NotificationBuilder
|
|
.Create(title, message)
|
|
.InArea(AreaName)
|
|
.WithBackground(NotificationColor.FromHex(backgroundHex))
|
|
.WithForeground(NotificationColor.FromHex("#FFFFFF"));
|
|
|
|
if (severity == AppNotificationSeverity.Error)
|
|
builder.NeverExpires();
|
|
else
|
|
builder.ExpiresInSeconds(expirationSeconds);
|
|
|
|
return builder.Build();
|
|
}
|
|
}
|