Skip to content

CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息

feat: 重构通知音效来源与选项

Bob80e0aeb25693
diff --git a/CodexBar.xcodeproj/project.pbxproj b/CodexBar.xcodeproj/project.pbxproj
index f5913a4..97ab887 100644
--- a/CodexBar.xcodeproj/project.pbxproj
+++ b/CodexBar.xcodeproj/project.pbxproj
@@ -424,7 +424,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
- CURRENT_PROJECT_VERSION = 62;
+ CURRENT_PROJECT_VERSION = 63;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = 4KJ362VJW5;
ENABLE_APP_INTENTS_METADATA_GENERATION = NO;
@@ -441,7 +441,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
- MARKETING_VERSION = 3.5.8;
+ MARKETING_VERSION = 3.5.9;
PRODUCT_BUNDLE_IDENTIFIER = app.zabrian.codexbar.debug;
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;
@@ -464,7 +464,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
- CURRENT_PROJECT_VERSION = 62;
+ CURRENT_PROJECT_VERSION = 63;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = 4KJ362VJW5;
ENABLE_APP_INTENTS_METADATA_GENERATION = NO;
@@ -481,7 +481,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
- MARKETING_VERSION = 3.5.8;
+ MARKETING_VERSION = 3.5.9;
PRODUCT_BUNDLE_IDENTIFIER = app.zabrian.codexbar;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Basso.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Basso.wav
deleted file mode 100644
index 346efa5..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Basso.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Blow.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Blow.wav
deleted file mode 100644
index 04dc759..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Blow.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Bottle.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Bottle.wav
deleted file mode 100644
index d8d1e42..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Bottle.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Frog.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Frog.wav
deleted file mode 100644
index 044115b..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Frog.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Funk.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Funk.wav
deleted file mode 100644
index e467bd4..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Funk.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Glass.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Glass.wav
deleted file mode 100644
index 45bce92..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Glass.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Hero.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Hero.wav
deleted file mode 100644
index b650378..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Hero.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Morse.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Morse.wav
deleted file mode 100644
index de772b9..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Morse.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Ping.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Ping.wav
deleted file mode 100644
index c7d7c2e..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Ping.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Pop.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Pop.wav
deleted file mode 100644
index 3c2b27f..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Pop.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Purr.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Purr.wav
deleted file mode 100644
index 0e94a8a..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Purr.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Sosumi.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Sosumi.wav
deleted file mode 100644
index 6b9ef26..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Sosumi.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Submarine.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Submarine.wav
deleted file mode 100644
index 4437639..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Submarine.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Tink.wav b/CodexBar/Resources/NotificationSounds/CodexBar-Tink.wav
deleted file mode 100644
index d8e8e5e..0000000
Binary files a/CodexBar/Resources/NotificationSounds/CodexBar-Tink.wav and /dev/null differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Alert.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Alert.wav
new file mode 100644
index 0000000..e3a638f
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Alert.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-AlertIntense.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-AlertIntense.wav
new file mode 100644
index 0000000..9f683c4
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-AlertIntense.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Ambient.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Ambient.wav
new file mode 100644
index 0000000..c8bfa42
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Ambient.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Celebration-1.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Celebration-1.wav
new file mode 100644
index 0000000..1773e19
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Celebration-1.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Celebration-2.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Celebration-2.wav
new file mode 100644
index 0000000..240d14a
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Celebration-2.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Celebration-3.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Celebration-3.wav
new file mode 100644
index 0000000..e785413
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Celebration-3.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Decorative-1.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Decorative-1.wav
new file mode 100644
index 0000000..673a3e4
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Decorative-1.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Decorative-2.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Decorative-2.wav
new file mode 100644
index 0000000..f7a1742
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Decorative-2.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Fanfare-1.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Fanfare-1.wav
new file mode 100644
index 0000000..c332f7c
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Fanfare-1.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Fanfare-2.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Fanfare-2.wav
new file mode 100644
index 0000000..fe44381
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Fanfare-2.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Fanfare-3.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Fanfare-3.wav
new file mode 100644
index 0000000..715f5a9
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Fanfare-3.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Intense.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Intense.wav
new file mode 100644
index 0000000..8cb7aab
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Intense.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Simple-1.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Simple-1.wav
new file mode 100644
index 0000000..b07ac11
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Simple-1.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/Material/CodexBar-Simple-2.wav b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Simple-2.wav
new file mode 100644
index 0000000..7244334
Binary files /dev/null and b/CodexBar/Resources/NotificationSounds/Material/CodexBar-Simple-2.wav differ
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Antic.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Antic.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Antic.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Antic.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Cheers.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Cheers.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Cheers.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Cheers.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Droplet.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Droplet.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Droplet.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Droplet.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-Exclamation.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Exclamation.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-Exclamation.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Exclamation.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-HaHa.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-HaHa.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-HaHa.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-HaHa.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Handoff.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Handoff.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Handoff.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Handoff.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-Heart.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Heart.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-Heart.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Heart.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Milestone.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Milestone.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Milestone.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Milestone.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Passage.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Passage.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Passage.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Passage.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Portal.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Portal.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Portal.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Portal.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-QuestionMark.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-QuestionMark.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-QuestionMark.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-QuestionMark.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Rattle.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Rattle.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Rattle.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Rattle.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Rebound.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Rebound.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Rebound.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Rebound.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-ReceivedMessage.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-ReceivedMessage.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-ReceivedMessage.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-ReceivedMessage.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Slide.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-Slide.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Slide.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-Slide.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-ThumbsDown.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-ThumbsDown.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-ThumbsDown.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-ThumbsDown.wav
diff --git a/CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-ThumbsUp.wav b/CodexBar/Resources/NotificationSounds/Modern/CodexBar-ThumbsUp.wav
similarity index 100%
rename from CodexBar/Resources/NotificationSounds/CodexBar-Acknowledgement-ThumbsUp.wav
rename to CodexBar/Resources/NotificationSounds/Modern/CodexBar-ThumbsUp.wav
diff --git a/CodexBar/Services/Notifications/NotificationSoundOption.swift b/CodexBar/Services/Notifications/NotificationSoundOption.swift
index b25fe6f..8e3a5be 100644
--- a/CodexBar/Services/Notifications/NotificationSoundOption.swift
+++ b/CodexBar/Services/Notifications/NotificationSoundOption.swift
@@ -1,184 +1,227 @@
import AppKit
import Foundation
+import os
import UserNotifications
-/// CodexBar 可用于系统通知的声音, 自定义声音随 App 打包, 确保通知中心能够解析文件名
-enum NotificationSoundOption: String, Identifiable {
- case silent
- case systemDefault
- case basso
- case blow
- case bottle
- case frog
- case funk
- case glass
- case hero
- case morse
- case ping
- case pop
- case purr
- case sosumi
- case submarine
- case tink
- case antic
- case cheers
- case droplet
- case handoff
- case milestone
- case passage
- case portal
- case rattle
- case rebound
- case slide
- case receivedMessage
- case acknowledgementExclamation
- case acknowledgementHaHa
- case acknowledgementHeart
- case acknowledgementQuestionMark
- case acknowledgementThumbsDown
- case acknowledgementThumbsUp
-
- static let classicSounds: [NotificationSoundOption] = [
- .basso, .blow, .bottle, .frog, .funk, .glass, .hero,
- .morse, .ping, .pop, .purr, .sosumi, .submarine, .tink
- ]
+/// CodexBar 可用于系统通知的声音, 系统提示音按本机声音目录实时枚举, 其余随 App 打包
+/// `id` 是落在 UserDefaults 里的持久化值, 系统提示音取文件名小写形式, 与旧版本的枚举值天然对齐
+struct NotificationSoundOption: Identifiable, Hashable {
+ let id: String
+ let title: String
+ private let source: SoundSource
+
+ static let silent = NotificationSoundOption(id: "silent", title: "静音", source: .silent)
+ static let systemDefault = NotificationSoundOption(
+ id: "systemDefault",
+ title: "默认",
+ source: .systemDefault
+ )
- static let modernSounds: [NotificationSoundOption] = [
- .antic, .cheers, .droplet, .handoff, .milestone, .passage,
- .portal, .rattle, .rebound, .slide, .receivedMessage,
- .acknowledgementExclamation, .acknowledgementHaHa,
- .acknowledgementHeart, .acknowledgementQuestionMark,
- .acknowledgementThumbsDown, .acknowledgementThumbsUp
+ /// 分组的唯一登记点, 菜单渲染与 id 解析都从这里派生, 新增一组声音只改这一处
+ static let categories: [Category] = [
+ Category(title: "System", sounds: systemSounds),
+ Category(title: "Modern", sounds: modernSounds),
+ Category(title: "Material", sounds: materialSounds)
]
- var id: String {
- rawValue
+ /// 持久化值可能指向已经不存在的声音, 例如换了机器或者用户删掉了自定义音, 这时交给调用方回落
+ static func option(forID id: String) -> NotificationSoundOption? {
+ if id == silent.id {
+ return silent
+ }
+
+ if id == systemDefault.id {
+ return systemDefault
+ }
+
+ return categories.lazy.flatMap(\.sounds).first { $0.id == id }
}
- var title: String {
- switch self {
+ /// 通知中心解析不到声音文件时是静音而不是回落默认音, 用户只会发现通知不响却查不出原因
+ /// 所以这里先确认文件在位, 缺失就主动降级, 宁可换个声音也不要静默失声
+ var notificationSound: UNNotificationSound? {
+ switch source {
case .silent:
- "静音"
+ nil
case .systemDefault:
- "默认"
- case .basso:
- "Basso"
- case .blow:
- "Blow"
- case .bottle:
- "Bottle"
- case .frog:
- "Frog"
- case .funk:
- "Funk"
- case .glass:
- "Glass"
- case .hero:
- "Hero"
- case .morse:
- "Morse"
- case .ping:
- "Ping"
- case .pop:
- "Pop"
- case .purr:
- "Purr"
- case .sosumi:
- "Sosumi"
- case .submarine:
- "Submarine"
- case .tink:
- "Tink"
- case .antic:
- "Antic"
- case .cheers:
- "Cheers"
- case .droplet:
- "Droplet"
- case .handoff:
- "Handoff"
- case .milestone:
- "Milestone"
- case .passage:
- "Passage"
- case .portal:
- "Portal"
- case .rattle:
- "Rattle"
- case .rebound:
- "Rebound"
- case .slide:
- "Slide"
- case .receivedMessage:
- "Received Message"
- case .acknowledgementExclamation:
- "Exclamation"
- case .acknowledgementHaHa:
- "Ha Ha"
- case .acknowledgementHeart:
- "Heart"
- case .acknowledgementQuestionMark:
- "Question Mark"
- case .acknowledgementThumbsDown:
- "Thumbs Down"
- case .acknowledgementThumbsUp:
- "Thumbs Up"
+ .default
+ case let .system(url):
+ if FileManager.default.fileExists(atPath: url.path) {
+ UNNotificationSound(named: UNNotificationSoundName(rawValue: url.lastPathComponent))
+ } else {
+ Self.degraded(id: id, source: "system")
+ }
+ case let .bundled(name):
+ if Bundle.main.url(forResource: name, withExtension: Self.bundledExtension) != nil {
+ UNNotificationSound(
+ named: UNNotificationSoundName(rawValue: "\(name).\(Self.bundledExtension)")
+ )
+ } else {
+ Self.degraded(id: id, source: "bundled")
+ }
}
}
- var notificationSound: UNNotificationSound? {
- guard self != .silent else {
- return nil
- }
+ /// 降级本身没有用户可见的痕迹, 通知照常弹出只是换了个声音, 这条日志是事后唯一的线索
+ /// 记 id 是定位所需, 它是选项标识而不是文件路径, 用户自定义音也只会暴露文件名本身
+ private static func degraded(id: String, source: String) -> UNNotificationSound {
+ AppLog.notification.notice(
+ "通知音已降级: id=\(id, privacy: .public); source=\(source, privacy: .public); reason=missing; action=default"
+ )
+ return .default
+ }
- guard let resourceFileName else {
- return .default
+ /// 静音本就没有声音, 默认音由通知中心内部决定且不暴露文件, 两者都没有可试听的对象
+ /// 拿系统警告声冒充默认音会误导, 它和通知中心默认音是两个独立设置
+ var isPreviewable: Bool {
+ switch source {
+ case .silent, .systemDefault:
+ false
+ case .system, .bundled:
+ true
}
-
- return UNNotificationSound(
- named: UNNotificationSoundName(rawValue: resourceFileName)
- )
}
func makePreviewSound(bundle: Bundle = .main) -> NSSound? {
- guard let resourceBaseName,
- let url = bundle.url(forResource: resourceBaseName, withExtension: Self.resourceExtension) else {
- return nil
+ switch source {
+ case .silent, .systemDefault:
+ nil
+ case let .system(url):
+ NSSound(contentsOf: url, byReference: true)
+ case let .bundled(name):
+ bundle.url(forResource: name, withExtension: Self.bundledExtension)
+ .flatMap { NSSound(contentsOf: $0, byReference: true) }
}
+ }
- return NSSound(contentsOf: url, byReference: true)
+ static func == (lhs: NotificationSoundOption, rhs: NotificationSoundOption) -> Bool {
+ lhs.id == rhs.id
}
- private var resourceBaseName: String? {
- resourceName.map { "CodexBar-\($0)" }
+ func hash(into hasher: inout Hasher) {
+ hasher.combine(id)
}
- private var resourceName: String? {
- switch self {
- case .silent, .systemDefault:
- nil
- case .receivedMessage:
- "ReceivedMessage"
- case .acknowledgementExclamation:
- "Acknowledgement-Exclamation"
- case .acknowledgementHaHa:
- "Acknowledgement-HaHa"
- case .acknowledgementHeart:
- "Acknowledgement-Heart"
- case .acknowledgementQuestionMark:
- "Acknowledgement-QuestionMark"
- case .acknowledgementThumbsDown:
- "Acknowledgement-ThumbsDown"
- case .acknowledgementThumbsUp:
- "Acknowledgement-ThumbsUp"
- default:
+ struct Category: Identifiable {
+ let title: String
+ let sounds: [NotificationSoundOption]
+
+ var id: String {
title
}
}
- private var resourceFileName: String? {
- resourceBaseName.map { "\($0).\(Self.resourceExtension)" }
+ /// 系统提示音由声音目录提供, 枚举时就定下具体文件, 通知与预览都用它而不再重新搜索
+ /// 打包的声音出自私有框架或第三方资源包, 不在系统声音搜索路径内, 只能按 bundle 资源名解析
+ private enum SoundSource: Hashable {
+ case silent
+ case systemDefault
+ case system(URL)
+ case bundled(String)
+ }
+
+ /// Apple 现代提示音, 取自 ToneLibrary 框架
+ private static let modernSounds: [NotificationSoundOption] = [
+ bundled(id: "antic", title: "Antic"),
+ bundled(id: "cheers", title: "Cheers"),
+ bundled(id: "droplet", title: "Droplet"),
+ bundled(id: "handoff", title: "Handoff"),
+ bundled(id: "milestone", title: "Milestone"),
+ bundled(id: "passage", title: "Passage"),
+ bundled(id: "portal", title: "Portal"),
+ bundled(id: "rattle", title: "Rattle"),
+ bundled(id: "rebound", title: "Rebound"),
+ bundled(id: "slide", title: "Slide"),
+ bundled(id: "receivedMessage", title: "Received Message", resource: "ReceivedMessage"),
+ bundled(id: "exclamation", title: "Exclamation"),
+ bundled(id: "haHa", title: "Ha Ha", resource: "HaHa"),
+ bundled(id: "heart", title: "Heart"),
+ bundled(id: "questionMark", title: "Question Mark", resource: "QuestionMark"),
+ bundled(id: "thumbsDown", title: "Thumbs Down", resource: "ThumbsDown"),
+ bundled(id: "thumbsUp", title: "Thumbs Up", resource: "ThumbsUp")
+ ]
+
+ /// Google Material Design 提示音, 取自 material_product_sounds 的 01 Hero 与 02 Alerts and Notifications
+ private static let materialSounds: [NotificationSoundOption] = [
+ bundled(id: "materialAlert", title: "Alert", resource: "Alert"),
+ bundled(id: "materialAlertIntense", title: "Alert Intense", resource: "AlertIntense"),
+ bundled(id: "materialSimple1", title: "Simple 1", resource: "Simple-1"),
+ bundled(id: "materialSimple2", title: "Simple 2", resource: "Simple-2"),
+ bundled(id: "materialDecorative1", title: "Decorative 1", resource: "Decorative-1"),
+ bundled(id: "materialDecorative2", title: "Decorative 2", resource: "Decorative-2"),
+ bundled(id: "materialIntense", title: "Intense", resource: "Intense"),
+ bundled(id: "materialAmbient", title: "Ambient", resource: "Ambient"),
+ bundled(id: "materialCelebration1", title: "Celebration 1", resource: "Celebration-1"),
+ bundled(id: "materialCelebration2", title: "Celebration 2", resource: "Celebration-2"),
+ bundled(id: "materialCelebration3", title: "Celebration 3", resource: "Celebration-3"),
+ bundled(id: "materialFanfare1", title: "Fanfare 1", resource: "Fanfare-1"),
+ bundled(id: "materialFanfare2", title: "Fanfare 2", resource: "Fanfare-2"),
+ bundled(id: "materialFanfare3", title: "Fanfare 3", resource: "Fanfare-3")
+ ]
+
+ /// 本机声音目录里实际存在的提示音, 不同机器上内容可以不同
+ /// 只在首次访问时扫一次, 运行期间新放进声音目录的文件要重启 App 才会出现
+ private static let systemSounds: [NotificationSoundOption] = loadSystemSounds()
+
+ /// 打包声音的 id 先占位, 本机同名文件不再生成第二个同 id 的选项
+ /// 否则用户在 System 组里选中的声音, 下次读取会被解析成打包组里那个同名资源
+ private static let reservedIDs: Set<String> = Set((modernSounds + materialSounds).map(\.id))
+
+ private static func bundled(
+ id: String,
+ title: String,
+ resource: String? = nil
+ ) -> NotificationSoundOption {
+ NotificationSoundOption(
+ id: id,
+ title: title,
+ source: .bundled("CodexBar-\(resource ?? title)")
+ )
+ }
+
+ /// 同名文件以靠前的目录为准, 与系统解析声音的顺序一致
+ private static func loadSystemSounds() -> [NotificationSoundOption] {
+ var seen = reservedIDs
+ var sounds: [NotificationSoundOption] = []
+
+ for directory in systemSoundDirectories {
+ let directoryURL = URL(fileURLWithPath: directory)
+ let fileNames = (try? FileManager.default.contentsOfDirectory(atPath: directory)) ?? []
+ for fileName in fileNames {
+ let name = fileName as NSString
+ guard supportedExtensions.contains(name.pathExtension.lowercased()) else {
+ continue
+ }
+
+ let title = name.deletingPathExtension
+ let id = title.lowercased()
+ guard !title.isEmpty, seen.insert(id).inserted else {
+ continue
+ }
+
+ sounds.append(
+ NotificationSoundOption(
+ id: id,
+ title: title,
+ source: .system(directoryURL.appendingPathComponent(fileName))
+ )
+ )
+ }
+ }
+
+ return sounds.sorted {
+ $0.title.localizedStandardCompare($1.title) == .orderedAscending
+ }
}
- private static let resourceExtension = "wav"
+ /// 声音按这组目录逐级解析, 用户可以在靠前的目录放同名文件覆盖系统音
+ /// 标准搜索路径里还有 `/Network/Library/Sounds`, 这里不查它, 探测自动挂载点可能拖住调用方
+ private static let systemSoundDirectories = [
+ "\(NSHomeDirectory())/Library/Sounds",
+ "/Library/Sounds",
+ "/System/Library/Sounds"
+ ]
+
+ private static let supportedExtensions: Set<String> = ["aiff", "aif", "wav", "caf"]
+ private static let bundledExtension = "wav"
}
diff --git a/CodexBar/Services/Settings/NotificationSettings.swift b/CodexBar/Services/Settings/NotificationSettings.swift
index 6f095f3..335b8e6 100644
--- a/CodexBar/Services/Settings/NotificationSettings.swift
+++ b/CodexBar/Services/Settings/NotificationSettings.swift
@@ -72,7 +72,7 @@ final class NotificationSettings: ObservableObject {
isQuotaResetEnabled = Self.bool(from: defaults, key: Self.quotaResetEnabledKey, defaultValue: true)
isLongTaskEnabled = Self.bool(from: defaults, key: Self.longTaskEnabledKey, defaultValue: true)
isTaskWaitingEnabled = Self.bool(from: defaults, key: Self.taskWaitingEnabledKey, defaultValue: true)
- isTaskHapticEnabled = Self.bool(from: defaults, key: Self.taskHapticEnabledKey, defaultValue: true)
+ isTaskHapticEnabled = Self.bool(from: defaults, key: Self.taskHapticEnabledKey, defaultValue: false)
isCreditExpiryEnabled = Self.bool(from: defaults, key: Self.creditExpiryEnabledKey, defaultValue: true)
isLowBatteryEnabled = Self.bool(from: defaults, key: Self.lowBatteryEnabledKey, defaultValue: true)
isKeepAliveLimitEnabled = Self.bool(from: defaults, key: Self.keepAliveLimitEnabledKey, defaultValue: true)
@@ -273,7 +273,7 @@ final class NotificationSettings: ObservableObject {
}
current = sound
- defaults.set(sound.rawValue, forKey: key)
+ defaults.set(sound.id, forKey: key)
}
private static func bool(from defaults: UserDefaults, key: String, defaultValue: Bool) -> Bool {
@@ -292,9 +292,10 @@ final class NotificationSettings: ObservableObject {
return value
}
+ /// 存量值可能指向本机没有的声音, 例如换了机器或者用户删掉了自定义音, 这时回落默认音
private static func sound(from defaults: UserDefaults, key: String) -> NotificationSoundOption {
- guard let rawValue = defaults.string(forKey: key),
- let sound = NotificationSoundOption(rawValue: rawValue) else {
+ guard let id = defaults.string(forKey: key),
+ let sound = NotificationSoundOption.option(forID: id) else {
return .systemDefault
}
diff --git a/CodexBar/Views/Settings/NotificationOptionsView.swift b/CodexBar/Views/Settings/NotificationOptionsView.swift
index ce20441..4715514 100644
--- a/CodexBar/Views/Settings/NotificationOptionsView.swift
+++ b/CodexBar/Views/Settings/NotificationOptionsView.swift
@@ -280,7 +280,7 @@ struct NotificationOptionsView: View {
@ViewBuilder accessory: () -> some View
) -> some View {
let showsSoundControls = isEnabled && isOn.wrappedValue
- let canPreviewSound = sound.wrappedValue != .silent
+ let canPreviewSound = sound.wrappedValue.isPreviewable
return VStack(spacing: 0) {
HStack(spacing: Metrics.notificationControlSpacing) {
@@ -356,15 +356,11 @@ struct NotificationOptionsView: View {
Divider()
- Menu("经典提示音") {
- ForEach(NotificationSoundOption.classicSounds) { sound in
- soundMenuButton(sound, selection: selection)
- }
- }
-
- Menu("现代提示音") {
- ForEach(NotificationSoundOption.modernSounds) { sound in
- soundMenuButton(sound, selection: selection)
+ ForEach(NotificationSoundOption.categories) { category in
+ Menu(category.title) {
+ ForEach(category.sounds) { sound in
+ soundMenuButton(sound, selection: selection)
+ }
}
}
} label: {
@@ -399,16 +395,12 @@ struct NotificationOptionsView: View {
private func playPreview(for sound: NotificationSoundOption) {
stopPreview()
- switch sound {
- case .silent:
+ guard sound.isPreviewable else {
return
- case .systemDefault:
- NSSound.beep()
- default:
- previewSound = sound.makePreviewSound()
- ?? NSSound(named: NSSound.Name(sound.title))
- previewSound?.play()
}
+
+ previewSound = sound.makePreviewSound()
+ previewSound?.play()
}
private func stopPreview() {
diff --git a/appcast.xml b/appcast.xml
index 9c8ced2..ae2ac88 100644
--- a/appcast.xml
+++ b/appcast.xml
@@ -7,6 +7,15 @@
<link>https://codexbar.zabrian.app/appcast.xml</link>
<description>CodexBar appcast feed</description>
<language>zh-CN</language>
+ <item>
+ <title>CodexBar.app 3.5.9</title>
+ <sparkle:version>63</sparkle:version>
+ <sparkle:shortVersionString>3.5.9</sparkle:shortVersionString>
+ <sparkle:minimumSystemVersion>15.0</sparkle:minimumSystemVersion>
+ <sparkle:releaseNotesLink>https://codexbar.zabrian.app/notes#3.5.9</sparkle:releaseNotesLink>
+ <pubDate>Fri, 31 Jul 2026 19:34:00 +0800</pubDate>
+ <enclosure url="https://codexbar.zabrian.app/download/CodexBar-v3.5.9.dmg" sparkle:edSignature="7YC53HMkr/TUCLtc9ioD3EwFtUjvWxPSEMzsOV4FH5P7AnyyMgnxUKDlA9VzK1uZVdIUR+IndvS73TypVxBxBw==" length="6558815" type="application/octet-stream"/>
+ </item>
<item>
<title>CodexBar.app 3.5.8</title>
<sparkle:version>62</sparkle:version>