3
3
using System . Collections . Generic ;
4
4
using System . Threading ;
5
5
using System . Threading . Tasks ;
6
+ using Coder . Desktop . App . Views ;
6
7
using Microsoft . Extensions . Logging ;
7
8
using Microsoft . Windows . AppNotifications ;
8
9
using Microsoft . Windows . AppNotifications . Builder ;
@@ -20,17 +21,26 @@ public interface IUserNotifier : INotificationHandler, IAsyncDisposable
20
21
public void UnregisterHandler ( string name ) ;
21
22
22
23
public Task ShowErrorNotification ( string title , string message , CancellationToken ct = default ) ;
23
- public Task ShowActionNotification ( string title , string message , string handlerName , IDictionary < string , string > ? args = null , CancellationToken ct = default ) ;
24
+ public Task ShowActionNotification ( string title , string message , string ? handlerName , IDictionary < string , string > ? args = null , CancellationToken ct = default ) ;
24
25
}
25
26
26
- public class UserNotifier ( ILogger < UserNotifier > logger , IDispatcherQueueManager dispatcherQueueManager ) : IUserNotifier
27
+ public class UserNotifier : IUserNotifier
27
28
{
28
29
private const string CoderNotificationHandler = "CoderNotificationHandler" ;
29
30
30
31
private readonly AppNotificationManager _notificationManager = AppNotificationManager . Default ;
32
+ private readonly ILogger < UserNotifier > _logger ;
33
+ private readonly IDispatcherQueueManager _dispatcherQueueManager ;
31
34
32
35
private ConcurrentDictionary < string , INotificationHandler > Handlers { get ; } = new ( ) ;
33
36
37
+ public UserNotifier ( ILogger < UserNotifier > logger , IDispatcherQueueManager dispatcherQueueManager )
38
+ {
39
+ _logger = logger ;
40
+ _dispatcherQueueManager = dispatcherQueueManager ;
41
+ Handlers . TryAdd ( nameof ( DefaultNotificationHandler ) , new DefaultNotificationHandler ( ) ) ;
42
+ }
43
+
34
44
public ValueTask DisposeAsync ( )
35
45
{
36
46
return ValueTask . CompletedTask ;
@@ -61,10 +71,18 @@ public Task ShowErrorNotification(string title, string message, CancellationToke
61
71
return Task . CompletedTask ;
62
72
}
63
73
64
- public Task ShowActionNotification ( string title , string message , string handlerName , IDictionary < string , string > ? args = null , CancellationToken ct = default )
74
+ public Task ShowActionNotification ( string title , string message , string ? handlerName , IDictionary < string , string > ? args = null , CancellationToken ct = default )
65
75
{
66
- if ( ! Handlers . TryGetValue ( handlerName , out _ ) )
67
- throw new InvalidOperationException ( $ "No action handler with the name '{ handlerName } ' is registered.") ;
76
+ if ( handlerName == null )
77
+ {
78
+ // Use default handler if no handler name is provided
79
+ handlerName = nameof ( DefaultNotificationHandler ) ;
80
+ }
81
+ else
82
+ {
83
+ if ( ! Handlers . TryGetValue ( handlerName , out _ ) )
84
+ throw new InvalidOperationException ( $ "No action handler with the name '{ handlerName } ' is registered. Use null for default") ;
85
+ }
68
86
69
87
var builder = new AppNotificationBuilder ( )
70
88
. AddText ( title )
@@ -90,20 +108,32 @@ public void HandleNotificationActivation(IDictionary<string, string> args)
90
108
91
109
if ( ! Handlers . TryGetValue ( handlerName , out var handler ) )
92
110
{
93
- logger . LogWarning ( "no action handler '{HandlerName}' found for notification activation, ignoring" , handlerName ) ;
111
+ _logger . LogWarning ( "no action handler '{HandlerName}' found for notification activation, ignoring" , handlerName ) ;
94
112
return ;
95
113
}
96
114
97
- dispatcherQueueManager . RunInUiThread ( ( ) =>
115
+ _dispatcherQueueManager . RunInUiThread ( ( ) =>
98
116
{
99
117
try
100
118
{
101
119
handler . HandleNotificationActivation ( args ) ;
102
120
}
103
121
catch ( Exception ex )
104
122
{
105
- logger . LogWarning ( ex , "could not handle activation for notification with handler '{HandlerName}" , handlerName ) ;
123
+ _logger . LogWarning ( ex , "could not handle activation for notification with handler '{HandlerName}" , handlerName ) ;
106
124
}
107
125
} ) ;
108
126
}
109
127
}
128
+
129
+ public class DefaultNotificationHandler : INotificationHandler
130
+ {
131
+ public void HandleNotificationActivation ( IDictionary < string , string > _ )
132
+ {
133
+ var app = ( App ) Microsoft . UI . Xaml . Application . Current ;
134
+ if ( app != null && app . TrayWindow != null )
135
+ {
136
+ app . TrayWindow . Tray_Open ( ) ;
137
+ }
138
+ }
139
+ }