using System.Windows; using System.Windows.Threading; using Notification.Core; using Notification.Wpf; using Notification.Wpf.Constants; using ytLive.Helpers; namespace ytLive.Services; /// /// Toast notifications rendered inside the main window's ToastArea /// (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. /// public sealed class NotificationService : INotificationService { public const string AreaName = "ToastArea"; internal static readonly IReadOnlyDictionary Cards = new Dictionary { [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(); } }