11using Flow . Launcher . Infrastructure ;
22using Microsoft . Toolkit . Uwp . Notifications ;
33using System ;
4+ using System . Collections . Generic ;
45using System . IO ;
56using System . Windows ;
67
@@ -12,10 +13,31 @@ internal static class Notification
1213
1314 internal static bool legacy = ! Win32Helper . IsNotificationSupported ( ) ;
1415
16+ private static readonly Dictionary < string , Action > _notificationActions = new ( ) ;
17+
18+ internal static void Install ( )
19+ {
20+ if ( ! legacy )
21+ {
22+ ToastNotificationManagerCompat . OnActivated += toastArgs =>
23+ {
24+ var actionId = toastArgs . Argument ; // Or use toastArgs.UserInput if using input
25+ if ( _notificationActions . TryGetValue ( actionId , out var action ) )
26+ {
27+ action ? . Invoke ( ) ;
28+ _notificationActions . Remove ( actionId ) ;
29+ }
30+ } ;
31+ }
32+ }
33+
1534 internal static void Uninstall ( )
1635 {
1736 if ( ! legacy )
37+ {
38+ _notificationActions . Clear ( ) ;
1839 ToastNotificationManagerCompat . Uninstall ( ) ;
40+ }
1941 }
2042
2143 public static void Show ( string title , string subTitle , string iconPath = null )
@@ -67,5 +89,58 @@ private static void LegacyShow(string title, string subTitle, string iconPath)
6789 var msg = new Msg ( ) ;
6890 msg . Show ( title , subTitle , iconPath ) ;
6991 }
92+
93+ public static void ShowWithButton ( string title , string buttonText , Action buttonAction , string subTitle , string iconPath = null )
94+ {
95+ Application . Current . Dispatcher . Invoke ( ( ) =>
96+ {
97+ ShowInternalWithButton ( title , buttonText , buttonAction , subTitle , iconPath ) ;
98+ } ) ;
99+ }
100+
101+ private static void ShowInternalWithButton ( string title , string buttonText , Action buttonAction , string subTitle , string iconPath = null )
102+ {
103+ // Handle notification for win7/8/early win10
104+ if ( legacy )
105+ {
106+ LegacyShowWithButton ( title , buttonText , buttonAction , subTitle , iconPath ) ;
107+ return ;
108+ }
109+
110+ // Using Windows Notification System
111+ var Icon = ! File . Exists ( iconPath )
112+ ? Path . Combine ( Constant . ProgramDirectory , "Images\\ app.png" )
113+ : iconPath ;
114+
115+ try
116+ {
117+ var guid = Guid . NewGuid ( ) . ToString ( ) ;
118+ new ToastContentBuilder ( )
119+ . AddText ( title , hintMaxLines : 1 )
120+ . AddText ( subTitle )
121+ . AddButton ( buttonText , ToastActivationType . Background , guid )
122+ . AddAppLogoOverride ( new Uri ( Icon ) )
123+ . Show ( ) ;
124+ _notificationActions . Add ( guid , buttonAction ) ;
125+ }
126+ catch ( InvalidOperationException e )
127+ {
128+ // Temporary fix for the Windows 11 notification issue
129+ // Possibly from 22621.1413 or 22621.1485, judging by post time of #2024
130+ App . API . LogException ( ClassName , "Notification InvalidOperationException Error" , e ) ;
131+ LegacyShowWithButton ( title , buttonText , buttonAction , subTitle , iconPath ) ;
132+ }
133+ catch ( Exception e )
134+ {
135+ App . API . LogException ( ClassName , "Notification Error" , e ) ;
136+ LegacyShowWithButton ( title , buttonText , buttonAction , subTitle , iconPath ) ;
137+ }
138+ }
139+
140+ private static void LegacyShowWithButton ( string title , string buttonText , Action buttonAction , string subTitle , string iconPath )
141+ {
142+ var msg = new MsgWithButton ( ) ;
143+ msg . Show ( title , buttonText , buttonAction , subTitle , iconPath ) ;
144+ }
70145 }
71146}
0 commit comments