-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToastNotificationService.cs
More file actions
204 lines (178 loc) · 7.5 KB
/
ToastNotificationService.cs
File metadata and controls
204 lines (178 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Wpf.Ui.Controls;
using PrettyScreenSHOT.Helpers;
namespace PrettyScreenSHOT.Services
{
public enum ToastType
{
Success,
Error,
Warning,
Info
}
public class ToastNotificationService
{
private static ToastNotificationService? instance;
public static ToastNotificationService Instance => instance ??= new ToastNotificationService();
private ToastNotificationService() { }
/// <summary>
/// Shows a toast notification
/// </summary>
public void Show(string title, string message, ToastType type = ToastType.Info, int durationMs = 3000)
{
try
{
// If MainWindow is open, show in-app toast
if (TryShowInAppToast(title, message, type, durationMs))
{
return;
}
// Otherwise, show system tray notification
ShowTrayNotification(title, message, type);
}
catch (Exception ex)
{
DebugHelper.LogError("ToastNotificationService", "Error showing toast", ex);
}
}
private bool TryShowInAppToast(string title, string message, ToastType type, int durationMs)
{
try
{
// Find MainWindow if it's open
foreach (Window window in Application.Current.Windows)
{
if (window is Views.Windows.MainWindow mainWindow)
{
Application.Current.Dispatcher.Invoke(() =>
{
ShowToastInWindow(mainWindow, title, message, type, durationMs);
});
return true;
}
}
}
catch (Exception ex)
{
DebugHelper.LogError("ToastNotificationService", "Error showing in-app toast", ex);
}
return false;
}
private void ShowToastInWindow(Views.Windows.MainWindow mainWindow, string title, string message, ToastType type, int durationMs)
{
// Create toast container if it doesn't exist
var grid = mainWindow.Content as Grid;
if (grid == null) return;
// Create toast card
var toastCard = new Card
{
Padding = new Thickness(16),
Margin = new Thickness(20, 20, 20, 0),
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Top,
MaxWidth = 400
};
// Set background color based on type
var (icon, foreground) = GetToastStyle(type);
var stackPanel = new StackPanel
{
Orientation = System.Windows.Controls.Orientation.Horizontal,
Children =
{
new SymbolIcon
{
Symbol = icon,
FontSize = 20,
Margin = new Thickness(0, 0, 12, 0),
Foreground = new SolidColorBrush(foreground)
},
new StackPanel
{
Children =
{
new System.Windows.Controls.TextBlock
{
Text = title,
FontWeight = FontWeights.SemiBold,
FontSize = 14
},
new System.Windows.Controls.TextBlock
{
Text = message,
FontSize = 12,
Opacity = 0.8,
TextWrapping = TextWrapping.Wrap
}
}
}
}
};
toastCard.Content = stackPanel;
// Add to grid
Grid.SetRowSpan(toastCard, grid.RowDefinitions.Count);
Grid.SetColumnSpan(toastCard, grid.ColumnDefinitions.Count);
grid.Children.Add(toastCard);
// Animate in
toastCard.Opacity = 0;
toastCard.RenderTransform = new TranslateTransform(0, -20);
var fadeIn = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(200));
var slideIn = new DoubleAnimation(-20, 0, TimeSpan.FromMilliseconds(200));
toastCard.BeginAnimation(UIElement.OpacityProperty, fadeIn);
((TranslateTransform)toastCard.RenderTransform).BeginAnimation(TranslateTransform.YProperty, slideIn);
// Auto-hide after duration
var timer = new System.Windows.Threading.DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(durationMs)
};
timer.Tick += (s, e) =>
{
timer.Stop();
// Animate out
var fadeOut = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(200));
var slideOut = new DoubleAnimation(0, -20, TimeSpan.FromMilliseconds(200));
fadeOut.Completed += (s2, e2) =>
{
grid.Children.Remove(toastCard);
};
toastCard.BeginAnimation(UIElement.OpacityProperty, fadeOut);
((TranslateTransform)toastCard.RenderTransform).BeginAnimation(TranslateTransform.YProperty, slideOut);
};
timer.Start();
}
private void ShowTrayNotification(string title, string message, ToastType type)
{
var trayIcon = TrayIconManager.Instance;
if (trayIcon != null)
{
var iconType = type switch
{
ToastType.Success => System.Windows.Forms.ToolTipIcon.Info,
ToastType.Error => System.Windows.Forms.ToolTipIcon.Error,
ToastType.Warning => System.Windows.Forms.ToolTipIcon.Warning,
_ => System.Windows.Forms.ToolTipIcon.Info
};
trayIcon.ShowNotification(title, message);
DebugHelper.LogInfo("ToastNotificationService", $"Tray notification: {title} - {message}");
}
}
private (Wpf.Ui.Common.SymbolRegular icon, Color foreground) GetToastStyle(ToastType type)
{
return type switch
{
ToastType.Success => (Wpf.Ui.Common.SymbolRegular.CheckmarkCircle24, Color.FromRgb(16, 185, 129)),
ToastType.Error => (Wpf.Ui.Common.SymbolRegular.DismissCircle24, Color.FromRgb(239, 68, 68)),
ToastType.Warning => (Wpf.Ui.Common.SymbolRegular.Warning24, Color.FromRgb(245, 158, 11)),
_ => (Wpf.Ui.Common.SymbolRegular.Info24, Color.FromRgb(59, 130, 246))
};
}
// Convenience methods
public void ShowSuccess(string message) => Show("Sukces", message, ToastType.Success);
public void ShowError(string message) => Show("Błąd", message, ToastType.Error);
public void ShowWarning(string message) => Show("Ostrzeżenie", message, ToastType.Warning);
public void ShowInfo(string message) => Show("Informacja", message, ToastType.Info);
}
}