Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions Sources/TerminalNotificationStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,22 @@ enum NotificationSoundSettings {
)
private static let pendingCustomSoundPreparationLock = NSLock()
private static var pendingCustomSoundPreparationPaths: Set<String> = []
private static let activePlaybackSoundsLock = NSLock()
private static var activePlaybackSounds: [ObjectIdentifier: NSSound] = [:]
private static let activePlaybackSoundDelegate = ActivePlaybackSoundDelegate()
private static let notificationSoundSupportedExtensions: Set<String> = [
"aif",
"aiff",
"caf",
"wav",
]

private final class ActivePlaybackSoundDelegate: NSObject, NSSoundDelegate {
func sound(_ sound: NSSound, didFinishPlaying finishedPlaying: Bool) {
NotificationSoundSettings.releaseActivePlaybackSound(sound)
}
}

private struct CustomSoundSourceMetadata: Codable, Equatable {
let sourcePath: String
let sourceSize: UInt64
Expand Down Expand Up @@ -260,7 +269,16 @@ enum NotificationSoundSettings {
playSoundFile(at: url)
}

static func playSelectedSound(defaults: UserDefaults = .standard) {
let value = defaults.string(forKey: key) ?? defaultValue
playSound(value: value, defaults: defaults)
}

static func previewSound(value: String, defaults: UserDefaults = .standard) {
playSound(value: value, defaults: defaults)
}

private static func playSound(value: String, defaults: UserDefaults) {
switch value {
case "default":
NSSound.beep()
Expand Down Expand Up @@ -331,10 +349,26 @@ enum NotificationSoundSettings {
NSLog("Notification custom sound failed to load from path: \(url.path)")
return
}
sound.play()
retainActivePlaybackSound(sound)
sound.delegate = activePlaybackSoundDelegate
if !sound.play() {
releaseActivePlaybackSound(sound)
}
}
}

private static func retainActivePlaybackSound(_ sound: NSSound) {
activePlaybackSoundsLock.lock()
activePlaybackSounds[ObjectIdentifier(sound)] = sound
activePlaybackSoundsLock.unlock()
}

private static func releaseActivePlaybackSound(_ sound: NSSound) {
activePlaybackSoundsLock.lock()
activePlaybackSounds.removeValue(forKey: ObjectIdentifier(sound))
activePlaybackSoundsLock.unlock()
}

private static func cleanupStaleStagedSoundFiles(
in directoryURL: URL,
keeping fileName: String,
Expand Down Expand Up @@ -693,6 +727,11 @@ final class TerminalNotificationStore: ObservableObject {
notification in
store.scheduleUserNotification(notification)
}
private var suppressedNotificationFeedbackHandler: (TerminalNotificationStore, TerminalNotification) -> Void = {
store,
notification in
store.playSuppressedNotificationFeedback(for: notification)
}
private var indexes = NotificationIndexes()

private init() {
Expand Down Expand Up @@ -877,7 +916,9 @@ final class TerminalNotificationStore: ObservableObject {
center.removeDeliveredNotificationsOffMain(withIdentifiers: idsToClear)
center.removePendingNotificationRequestsOffMain(withIdentifiers: idsToClear)
}
if !shouldSuppressExternalDelivery {
if shouldSuppressExternalDelivery {
suppressedNotificationFeedbackHandler(self, notification)
} else {
notificationDeliveryHandler(self, notification)
}
}
Expand Down Expand Up @@ -1046,6 +1087,11 @@ final class TerminalNotificationStore: ObservableObject {
}
}

private func playSuppressedNotificationFeedback(for notification: TerminalNotification) {
_ = notification
NotificationSoundSettings.playSelectedSound()
}
Comment thread
lawrencecchen marked this conversation as resolved.
Resolved

private func ensureAuthorization(
origin: AuthorizationRequestOrigin,
_ completion: @escaping (Bool) -> Void
Expand Down Expand Up @@ -1262,6 +1308,18 @@ final class TerminalNotificationStore: ObservableObject {
}
}

func configureSuppressedNotificationFeedbackHandlerForTesting(
_ handler: @escaping (TerminalNotificationStore, TerminalNotification) -> Void
) {
suppressedNotificationFeedbackHandler = handler
}

func resetSuppressedNotificationFeedbackHandlerForTesting() {
suppressedNotificationFeedbackHandler = { store, notification in
store.playSuppressedNotificationFeedback(for: notification)
}
}

func promptToEnableNotificationsForTesting() {
promptToEnableNotifications()
}
Expand Down
51 changes: 51 additions & 0 deletions cmuxTests/NotificationAndMenuBarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ final class NotificationDockBadgeTests: XCTestCase {
override func tearDown() {
TerminalNotificationStore.shared.resetNotificationSettingsPromptHooksForTesting()
TerminalNotificationStore.shared.replaceNotificationsForTesting([])
TerminalNotificationStore.shared.resetNotificationDeliveryHandlerForTesting()
TerminalNotificationStore.shared.resetSuppressedNotificationFeedbackHandlerForTesting()
super.tearDown()
}

Expand Down Expand Up @@ -399,6 +401,55 @@ final class NotificationDockBadgeTests: XCTestCase {
}
}

func testFocusedTerminalNotificationStillRunsLocalSoundFeedbackWhenExternalDeliveryIsSuppressed() {
let appDelegate = AppDelegate.shared ?? AppDelegate()
let manager = TabManager()
let store = TerminalNotificationStore.shared
Comment thread
lawrencecchen marked this conversation as resolved.
Resolved

let originalTabManager = appDelegate.tabManager
let originalNotificationStore = appDelegate.notificationStore
let originalAppFocusOverride = AppFocusState.overrideIsFocused

var deliveredNotificationIDs: [UUID] = []
var localFeedbackNotificationIDs: [UUID] = []

store.replaceNotificationsForTesting([])
store.configureNotificationDeliveryHandlerForTesting { _, notification in
deliveredNotificationIDs.append(notification.id)
}
store.configureSuppressedNotificationFeedbackHandlerForTesting { _, notification in
localFeedbackNotificationIDs.append(notification.id)
}
appDelegate.tabManager = manager
appDelegate.notificationStore = store
AppFocusState.overrideIsFocused = true

defer {
store.replaceNotificationsForTesting([])
appDelegate.tabManager = originalTabManager
appDelegate.notificationStore = originalNotificationStore
AppFocusState.overrideIsFocused = originalAppFocusOverride
}

guard let workspace = manager.selectedWorkspace,
let terminalPanel = workspace.focusedTerminalPanel else {
XCTFail("Expected selected workspace with a focused terminal panel")
return
}

store.addNotification(
tabId: workspace.id,
surfaceId: terminalPanel.id,
title: "Unread",
subtitle: "",
body: ""
)

XCTAssertTrue(store.hasUnreadNotification(forTabId: workspace.id, surfaceId: terminalPanel.id))
XCTAssertTrue(deliveredNotificationIDs.isEmpty)
XCTAssertEqual(localFeedbackNotificationIDs.count, 1)
}

func testNotificationAuthorizationStateMappingCoversKnownUNAuthorizationStatuses() {
XCTAssertEqual(TerminalNotificationStore.authorizationState(from: .notDetermined), .notDetermined)
XCTAssertEqual(TerminalNotificationStore.authorizationState(from: .denied), .denied)
Expand Down
Loading