From abdfa38d2da09bc4b434236ce7e3e3ce426eb238 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Tue, 22 Sep 2026 17:19:08 +0200 Subject: [PATCH] Use UserNotifications on macOS 11 and later --- Classes/QueueController.swift | 53 +++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/Classes/QueueController.swift b/Classes/QueueController.swift index c90c619..77a2b39 100644 --- a/Classes/QueueController.swift +++ b/Classes/QueueController.swift @@ -6,9 +6,10 @@ // import Cocoa +import UserNotifications import MP42Foundation -final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDelegate, ItemViewDelegate, NSTableViewDataSource, NSTableViewDelegate, ExpandedTableViewDelegate, NSUserInterfaceValidations { +final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDelegate, ItemViewDelegate, NSTableViewDataSource, NSTableViewDelegate, ExpandedTableViewDelegate, UNUserNotificationCenterDelegate, NSUserInterfaceValidations { static let shared = QueueController() @@ -82,6 +83,12 @@ final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDel table.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL, .tableViewIndex]) progressBar.isHidden = true + if #available(macOS 11, *) { + let center = UNUserNotificationCenter.current() + center.delegate = self + center.requestAuthorization(options: [.sound, .alert], completionHandler: {_,_ in }) + } + let main = OperationQueue.main let nc = NotificationCenter.default @@ -115,18 +122,36 @@ final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDel self.updateUI() if self.prefs.showDoneNotification, let info = note.userInfo { - let notification = NSUserNotification() - notification.title = NSLocalizedString("Queue Done", comment: "") + let title = NSLocalizedString("Queue Done", comment: "") + let informativeText = { + if let failedCount = info["FailedCount"] as? UInt, failedCount > 0, + let completedCount = info["CompletedCount"] as? UInt { + return "Completed: \(completedCount); Failed: \(failedCount)" + } + else if let completedCount = info["CompletedCount"] as? UInt { + return "Completed: \(completedCount)" + } else { + return "" + } + }() - if let failedCount = info["FailedCount"] as? UInt, failedCount > 0, - let completedCount = info["CompletedCount"] as? UInt { - notification.informativeText = "Completed: \(completedCount); Failed: \(failedCount)" + if #available(macOS 11, *) { + let notification = UNMutableNotificationContent() + notification.title = title + notification.body = informativeText + notification.sound = UNNotificationSound.default + + let request = UNNotificationRequest(identifier: UUID().uuidString, + content: notification, + trigger: nil) + UNUserNotificationCenter.current().add(request) { _ in } + } else { + let notification = NSUserNotification() + notification.title = title + notification.informativeText = informativeText + notification.soundName = NSUserNotificationDefaultSoundName + NSUserNotificationCenter.default.deliver(notification) } - else if let completedCount = info["CompletedCount"] as? UInt { - notification.informativeText = "Completed: \(completedCount)" - } - notification.soundName = NSUserNotificationDefaultSoundName - NSUserNotificationCenter.default.deliver(notification) } for script in self.scripts { @@ -192,6 +217,12 @@ final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDel } } + //MARK: Notification delegate + + func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { + completionHandler([.sound]) + } + //MARK: Queue func saveToDisk() throws {