diff --git a/Sources/App/AppDelegate.swift b/Sources/App/AppDelegate.swift index a5ecf8b..dd4bb5d 100644 --- a/Sources/App/AppDelegate.swift +++ b/Sources/App/AppDelegate.swift @@ -5,6 +5,9 @@ import Speech /// 应用主控制器,协调 Fn 键(语音输入)和 Control 键(AI 助手)两种模式 final class AppDelegate: NSObject, NSApplicationDelegate { + /// 防止 ARC 优化释放(NSApplication.delegate 是 weak) + private static var retainedInstance: AppDelegate? + private let keyMonitor = KeyMonitor() private let speechRecognizer = SpeechRecognizer() private let audioMonitor = AudioLevelMonitor() @@ -15,6 +18,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate { private var isRecording = false private var finalTranscription: String = "" + override init() { + super.init() + Self.retainedInstance = self + } + func applicationDidFinishLaunching(_ notification: Notification) { NSApp.setActivationPolicy(.accessory) beginPermissionFlow() diff --git a/Sources/main.swift b/Sources/main.swift index 624ed16..429ecd0 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -5,4 +5,8 @@ let delegate = AppDelegate() app.setActivationPolicy(.accessory) app.delegate = delegate -app.run() + +// NSApplication.delegate 是 weak 引用,必须保持强引用防止 ARC 优化释放 +withExtendedLifetime(delegate) { + app.run() +} diff --git a/VoiceInput-20260424.dmg b/VoiceInput-20260425.dmg similarity index 73% rename from VoiceInput-20260424.dmg rename to VoiceInput-20260425.dmg index 7cf9fa4..542844b 100644 Binary files a/VoiceInput-20260424.dmg and b/VoiceInput-20260425.dmg differ