Skip to content

Commit

Permalink
MacGui: use the newer UserNotifications framework to send notifications
Browse files Browse the repository at this point in the history
The NSUserNotification framework has been deprecated since macOS 11.
  • Loading branch information
galad87 committed Sep 14, 2024
1 parent edbe488 commit 3e05778
Showing 1 changed file with 72 additions and 17 deletions.
89 changes: 72 additions & 17 deletions macosx/HBQueueController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

@import HandBrakeKit;
@import QuickLookUI;
@import UserNotifications;

@interface HBQueueController () <NSToolbarItemValidation, NSMenuItemValidation, NSUserNotificationCenterDelegate, HBQueueTableViewControllerDelegate, HBQueueDetailsViewControllerDelegate>
@interface HBQueueController () <NSToolbarItemValidation, NSMenuItemValidation, NSUserNotificationCenterDelegate, UNUserNotificationCenterDelegate, HBQueueTableViewControllerDelegate, HBQueueDetailsViewControllerDelegate>

@property (nonatomic) NSSplitViewController *splitViewController;
@property (nonatomic) HBQueueTableViewController *tableViewController;
Expand Down Expand Up @@ -61,7 +62,26 @@ - (instancetype)initWithQueue:(HBQueue *)queue
_queue = queue;
_sendQueue = dispatch_queue_create("fr.handbrake.SendToQueue", DISPATCH_QUEUE_SERIAL);

NSUserNotificationCenter.defaultUserNotificationCenter.delegate = self;
if (@available(macOS 10.14, *))
{
UNUserNotificationCenter *center = UNUserNotificationCenter.currentNotificationCenter;
center.delegate = self;

UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:HBQueueItemNotificationShowAction
title:NSLocalizedString(@"Show", @"Notification -> Show in Finder")
options:UNNotificationActionOptionForeground];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:HBQueueItemNotificationShowCategory
actions:@[action]
intentIdentifiers:@[]
options:0];
[center setNotificationCategories:[NSSet setWithObject:category]];
[center requestAuthorizationWithOptions:UNAuthorizationOptionSound | UNAuthorizationOptionAlert
completionHandler:^(BOOL granted, NSError * _Nullable error) {}];
}
else
{
NSUserNotificationCenter.defaultUserNotificationCenter.delegate = self;
}

[NSNotificationCenter.defaultCenter addObserverForName:HBQueueLowSpaceAlertNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
[self queueLowDiskSpaceAlert];
Expand Down Expand Up @@ -398,37 +418,72 @@ - (void)resetQueueItemsAtIndexes:(NSIndexSet *)indexes
#pragma mark - Encode Done Actions

NSString * const HBQueueItemNotificationPathKey = @"HBQueueItemNotificationPathKey";
NSString * const HBQueueItemNotificationShowAction = @"HBQueueItemNotificationShowAction";
NSString * const HBQueueItemNotificationShowCategory = @"HBQueueItemNotificationShowCategory";

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
- (void)showInFinder:(NSString *)path
{
// Show the file in Finder when a done notification was clicked.
NSString *path = notification.userInfo[HBQueueItemNotificationPathKey];
if ([path isKindOfClass:[NSString class]] && path.length)
{
NSURL *fileURL = [NSURL fileURLWithPath:path];
[NSWorkspace.sharedWorkspace activateFileViewerSelectingURLs:@[fileURL]];
}
}

- (void)showNotificationWithTitle:(NSString *)title description:(NSString *)description url:(NSURL *)fileURL playSound:(BOOL)playSound
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = title;
notification.informativeText = description;
notification.soundName = playSound ? NSUserNotificationDefaultSoundName : nil;
// Show the file in Finder when a done notification is clicked
NSString *path = notification.userInfo[HBQueueItemNotificationPathKey];
[self showInFinder:path];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler API_AVAILABLE(macos(10.14))
{
NSString *path = response.notification.request.content.userInfo[HBQueueItemNotificationPathKey];
[self showInFinder:path];
completionHandler();
}

if (fileURL)
- (void)showNotificationWithTitle:(NSString *)title description:(NSString *)description url:(NSURL *)fileURL playSound:(BOOL)playSound
{
if (@available(macOS 10.14, *))
{
notification.hasActionButton = YES;
notification.actionButtonTitle = NSLocalizedString(@"Show", @"Notification -> Show in Finder");
notification.userInfo = @{ HBQueueItemNotificationPathKey: fileURL.path };
UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];
notification.title = title;
notification.body = description;
notification.sound = playSound ? UNNotificationSound.defaultSound : nil;

if (fileURL)
{
notification.categoryIdentifier = HBQueueItemNotificationShowCategory;
notification.userInfo = @{ HBQueueItemNotificationPathKey: fileURL.path };
}

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[NSUUID UUID].UUIDString
content:notification
trigger:nil];
[UNUserNotificationCenter.currentNotificationCenter addNotificationRequest:request withCompletionHandler:NULL];
}
else
{
notification.hasActionButton = NO;
}
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = title;
notification.informativeText = description;
notification.soundName = playSound ? NSUserNotificationDefaultSoundName : nil;

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
if (fileURL)
{
notification.hasActionButton = YES;
notification.actionButtonTitle = NSLocalizedString(@"Show", @"Notification -> Show in Finder");
notification.userInfo = @{ HBQueueItemNotificationPathKey: fileURL.path };
}
else
{
notification.hasActionButton = NO;
}

[NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:notification];
}
}

/**
Expand Down

0 comments on commit 3e05778

Please sign in to comment.