Skip to content

Commit c7ca052

Browse files
committed
More null check on CurrentToastNotificationService
+ Add CurrentAumid and CurrentAumidInGuid for diagnostic purposes (will be used in the futurre)
1 parent 9100467 commit c7ca052

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

CollapseLauncher/Classes/Helper/WindowUtility.cs

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -285,44 +285,64 @@ internal static IconShowOptions CurrentWindowTitlebarIconShowOption
285285
}
286286
}
287287

288-
[field: AllowNull, MaybeNull]
289-
internal static NotificationService CurrentToastNotificationService
288+
internal static Guid? CurrentAumidInGuid
289+
{
290+
get;
291+
set;
292+
}
293+
294+
private static string? _currentAumid;
295+
internal static string? CurrentAumid
296+
{
297+
get => _currentAumid;
298+
set => _currentAumid ??= value;
299+
}
300+
301+
internal static NotificationService? CurrentToastNotificationService
290302
{
291303
get
292304
{
293305
// If toast notification service field is null, then initialize
294306
if (field == null)
295307
{
296-
// Get Icon location paths
297-
(string iconLocationStartMenu, _)
298-
= TaskSchedulerHelper.GetIconLocationPaths(
299-
out string? appAumIdNameAlternative,
300-
out _,
301-
out string? executablePath,
302-
out _);
308+
try
309+
{
310+
// Get Icon location paths
311+
(string iconLocationStartMenu, _)
312+
= TaskSchedulerHelper.GetIconLocationPaths(
313+
out string? appAumIdNameAlternative,
314+
out _,
315+
out string? executablePath,
316+
out _);
303317

304-
// Register notification service
305-
field = new NotificationService(ILoggerHelper.GetILogger("ToastCOM"));
318+
// Register notification service
319+
field = new NotificationService(ILoggerHelper.GetILogger("ToastCOM"));
306320

307-
// Get AUMID name from Win32
308-
PInvoke.GetProcessAumid(out string? appAumIdName);
321+
// Get AUMID name from Win32
322+
PInvoke.GetProcessAumid(out _currentAumid);
309323

310-
// If it's empty due to an error, set the alternative AUMID
311-
appAumIdName ??= appAumIdNameAlternative;
324+
// If it's empty due to an error, set the alternative AUMID
325+
CurrentAumid ??= appAumIdNameAlternative;
312326

313-
// Get string for AumId registration
314-
if (!string.IsNullOrEmpty(appAumIdName))
327+
// Get string for AumId registration
328+
if (!string.IsNullOrEmpty(CurrentAumid))
329+
{
330+
// Initialize Toast Notification service
331+
CurrentAumidInGuid = field.Initialize(
332+
CurrentAumid,
333+
executablePath ?? "",
334+
iconLocationStartMenu,
335+
asElevatedUser: true
336+
);
337+
338+
// Subscribe ToastCallback
339+
field.ToastCallback += Service_ToastNotificationCallback;
340+
}
341+
}
342+
catch (Exception ex)
315343
{
316-
// Initialize Toast Notification service
317-
field.Initialize(
318-
appAumIdName,
319-
executablePath ?? "",
320-
iconLocationStartMenu,
321-
asElevatedUser: true
322-
);
323-
324-
// Subscribe ToastCallback
325-
field.ToastCallback += Service_ToastNotificationCallback;
344+
Logger.LogWriteLine($"Notification service initialization has failed, ignoring!\r\n{ex}", LogType.Error, true);
345+
return null;
326346
}
327347
}
328348

CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,12 @@ private void SpawnAppUpdatedNotification()
942942

943943
// Get notification service
944944
Windows.UI.Notifications.ToastNotification notificationService =
945-
WindowUtility.CurrentToastNotificationService.CreateToastNotification(toastContent);
945+
WindowUtility.CurrentToastNotificationService?.CreateToastNotification(toastContent);
946946

947947
// Spawn notification service
948948
Windows.UI.Notifications.ToastNotifier notifier =
949-
WindowUtility.CurrentToastNotificationService.CreateToastNotifier();
950-
notifier.Show(notificationService);
949+
WindowUtility.CurrentToastNotificationService?.CreateToastNotifier();
950+
notifier?.Show(notificationService);
951951
}
952952
catch (Exception ex)
953953
{

0 commit comments

Comments
 (0)