33using System . Collections . Generic ;
44using System . Threading ;
55using System . Threading . Tasks ;
6+ using Coder . Desktop . App . Views ;
67using Microsoft . Extensions . Logging ;
78using Microsoft . Windows . AppNotifications ;
89using Microsoft . Windows . AppNotifications . Builder ;
@@ -20,17 +21,26 @@ public interface IUserNotifier : INotificationHandler, IAsyncDisposable
2021 public void UnregisterHandler ( string name ) ;
2122
2223 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 ) ;
2425}
2526
26- public class UserNotifier ( ILogger < UserNotifier > logger , IDispatcherQueueManager dispatcherQueueManager ) : IUserNotifier
27+ public class UserNotifier : IUserNotifier
2728{
2829 private const string CoderNotificationHandler = "CoderNotificationHandler" ;
2930
3031 private readonly AppNotificationManager _notificationManager = AppNotificationManager . Default ;
32+ private readonly ILogger < UserNotifier > _logger ;
33+ private readonly IDispatcherQueueManager _dispatcherQueueManager ;
3134
3235 private ConcurrentDictionary < string , INotificationHandler > Handlers { get ; } = new ( ) ;
3336
37+ public UserNotifier ( ILogger < UserNotifier > logger , IDispatcherQueueManager dispatcherQueueManager )
38+ {
39+ _logger = logger ;
40+ _dispatcherQueueManager = dispatcherQueueManager ;
41+ Handlers . TryAdd ( nameof ( DefaultNotificationHandler ) , new DefaultNotificationHandler ( ) ) ;
42+ }
43+
3444 public ValueTask DisposeAsync ( )
3545 {
3646 return ValueTask . CompletedTask ;
@@ -61,10 +71,18 @@ public Task ShowErrorNotification(string title, string message, CancellationToke
6171 return Task . CompletedTask ;
6272 }
6373
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 )
6575 {
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+ }
6886
6987 var builder = new AppNotificationBuilder ( )
7088 . AddText ( title )
@@ -90,20 +108,32 @@ public void HandleNotificationActivation(IDictionary<string, string> args)
90108
91109 if ( ! Handlers . TryGetValue ( handlerName , out var handler ) )
92110 {
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 ) ;
94112 return ;
95113 }
96114
97- dispatcherQueueManager . RunInUiThread ( ( ) =>
115+ _dispatcherQueueManager . RunInUiThread ( ( ) =>
98116 {
99117 try
100118 {
101119 handler . HandleNotificationActivation ( args ) ;
102120 }
103121 catch ( Exception ex )
104122 {
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 ) ;
106124 }
107125 } ) ;
108126 }
109127}
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+ }
0 commit comments