|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Windows; |
| 5 | +using System.Windows.Controls; |
| 6 | +using PrettyScreenSHOT.Views.Controls; |
| 7 | + |
| 8 | +namespace PrettyScreenSHOT.Services |
| 9 | +{ |
| 10 | + public class ToastNotificationManager |
| 11 | + { |
| 12 | + private static ToastNotificationManager? _instance; |
| 13 | + public static ToastNotificationManager Instance => _instance ??= new ToastNotificationManager(); |
| 14 | + |
| 15 | + private Grid? toastContainer; |
| 16 | + private readonly List<ToastNotification> activeToasts = new(); |
| 17 | + private const int MaxToasts = 5; |
| 18 | + private const int ToastSpacing = 8; |
| 19 | + |
| 20 | + private ToastNotificationManager() |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + public void Initialize(Grid container) |
| 25 | + { |
| 26 | + toastContainer = container; |
| 27 | + } |
| 28 | + |
| 29 | + public void ShowSuccess(string title, string message, int durationMs = 4000) |
| 30 | + { |
| 31 | + ShowToast(title, message, ToastNotification.ToastType.Success, durationMs); |
| 32 | + } |
| 33 | + |
| 34 | + public void ShowInfo(string title, string message, int durationMs = 4000) |
| 35 | + { |
| 36 | + ShowToast(title, message, ToastNotification.ToastType.Info, durationMs); |
| 37 | + } |
| 38 | + |
| 39 | + public void ShowWarning(string title, string message, int durationMs = 4000) |
| 40 | + { |
| 41 | + ShowToast(title, message, ToastNotification.ToastType.Warning, durationMs); |
| 42 | + } |
| 43 | + |
| 44 | + public void ShowError(string title, string message, int durationMs = 5000) |
| 45 | + { |
| 46 | + ShowToast(title, message, ToastNotification.ToastType.Error, durationMs); |
| 47 | + } |
| 48 | + |
| 49 | + private void ShowToast(string title, string message, ToastNotification.ToastType type, int durationMs) |
| 50 | + { |
| 51 | + if (toastContainer == null) |
| 52 | + { |
| 53 | + // Fallback to window-based toast if container not initialized |
| 54 | + ShowToastInWindow(title, message, type, durationMs); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + Application.Current.Dispatcher.Invoke(() => |
| 59 | + { |
| 60 | + // Limit number of toasts |
| 61 | + if (activeToasts.Count >= MaxToasts) |
| 62 | + { |
| 63 | + // Remove oldest toast |
| 64 | + var oldest = activeToasts.First(); |
| 65 | + oldest.Hide(); |
| 66 | + } |
| 67 | + |
| 68 | + var toast = new ToastNotification(); |
| 69 | + toast.Closed += (s, e) => |
| 70 | + { |
| 71 | + activeToasts.Remove(toast); |
| 72 | + toastContainer.Children.Remove(toast); |
| 73 | + RepositionToasts(); |
| 74 | + }; |
| 75 | + |
| 76 | + // Position toast |
| 77 | + toast.HorizontalAlignment = HorizontalAlignment.Right; |
| 78 | + toast.VerticalAlignment = VerticalAlignment.Top; |
| 79 | + toast.Margin = new Thickness(0, CalculateTopMargin(), 0, 0); |
| 80 | + |
| 81 | + toastContainer.Children.Add(toast); |
| 82 | + activeToasts.Add(toast); |
| 83 | + |
| 84 | + toast.Show(title, message, type, durationMs); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + private double CalculateTopMargin() |
| 89 | + { |
| 90 | + double margin = 20; // Initial top margin |
| 91 | + foreach (var toast in activeToasts) |
| 92 | + { |
| 93 | + margin += toast.ActualHeight + ToastSpacing; |
| 94 | + } |
| 95 | + return margin; |
| 96 | + } |
| 97 | + |
| 98 | + private void RepositionToasts() |
| 99 | + { |
| 100 | + double margin = 20; |
| 101 | + foreach (var toast in activeToasts) |
| 102 | + { |
| 103 | + toast.Margin = new Thickness(0, margin, 0, 0); |
| 104 | + margin += toast.ActualHeight + ToastSpacing; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void ShowToastInWindow(string title, string message, ToastNotification.ToastType type, int durationMs) |
| 109 | + { |
| 110 | + // Create a standalone window for the toast |
| 111 | + var toastWindow = new Window |
| 112 | + { |
| 113 | + WindowStyle = WindowStyle.None, |
| 114 | + AllowsTransparency = true, |
| 115 | + Background = System.Windows.Media.Brushes.Transparent, |
| 116 | + Topmost = true, |
| 117 | + ShowInTaskbar = false, |
| 118 | + SizeToContent = SizeToContent.WidthAndHeight, |
| 119 | + WindowStartupLocation = WindowStartupLocation.Manual |
| 120 | + }; |
| 121 | + |
| 122 | + // Position at bottom-right of screen |
| 123 | + var workingArea = SystemParameters.WorkArea; |
| 124 | + toastWindow.Left = workingArea.Right - 420; // 400px toast + 20px margin |
| 125 | + toastWindow.Top = workingArea.Bottom - 200; // Adjust based on toast height |
| 126 | + |
| 127 | + var toast = new ToastNotification(); |
| 128 | + toast.Closed += (s, e) => toastWindow.Close(); |
| 129 | + |
| 130 | + toastWindow.Content = toast; |
| 131 | + toastWindow.Show(); |
| 132 | + |
| 133 | + toast.Show(title, message, type, durationMs); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments