using Microsoft.Windows.AppNotifications; using Microsoft.Windows.AppNotifications.Builder; namespace AmiReel.Services; /// Shows a Windows app notification when a Video or Shorts render finishes, fails, or is cancelled. public static class RenderNotifier { public static void Register() { try { AppNotificationManager.Default.NotificationInvoked -= OnNotificationInvoked; AppNotificationManager.Default.NotificationInvoked += OnNotificationInvoked; AppNotificationManager.Default.Register(); } catch { // Notifications are unavailable when the process is elevated or has no identity. } } public static void ShowCompleted(bool shorts) => Show(DescribeCompleted(shorts)); public static void ShowFailed(string summary) => Show(DescribeFailed(summary)); public static void ShowCancelled() => Show(DescribeCancelled()); internal static (string Title, string Body) DescribeCompleted(bool shorts) => shorts ? ("Short complete", "The YouTube Short was rendered successfully.") : ("Render complete", "The AmiReel render completed successfully."); internal static (string Title, string Body) DescribeFailed(string summary) { string body = string.IsNullOrWhiteSpace(summary) ? "The render failed." : summary.ReplaceLineEndings(" "); if (body.Length > 180) body = body[..177] + "..."; return ("Render failed", body); } internal static (string Title, string Body) DescribeCancelled() => ("Render cancelled", "The render was cancelled."); private static void Show((string Title, string Body) content) { try { AppNotification notification = new AppNotificationBuilder() .AddText(content.Title) .AddText(content.Body) .BuildNotification(); AppNotificationManager.Default.Show(notification); } catch { // Show fails silently when notifications are unsupported. } } private static void OnNotificationInvoked(AppNotificationManager sender, AppNotificationActivatedEventArgs args) { App.MainWindowInstance?.DispatcherQueue.TryEnqueue(() => App.MainWindowInstance.Activate()); } }