-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.swift
246 lines (203 loc) · 9.29 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// AppDelegate.swift
// MacMusicPlayer
//
// Created by X on 9/18/24.
//
import Cocoa
import MediaPlayer
import SwiftUI
import AVFoundation
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem?
var popover: NSPopover?
var playerManager: PlayerManager!
var sleepManager: SleepManager!
var launchManager: LaunchManager!
var menu: NSMenu!
func applicationDidFinishLaunching(_ notification: Notification) {
playerManager = PlayerManager()
sleepManager = SleepManager()
launchManager = LaunchManager()
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = statusItem?.button {
button.target = self
button.action = #selector(toggleMenu)
button.imagePosition = .imageLeft
updateStatusBarIcon()
}
setupMenu()
setupRemoteCommandCenter()
}
func setupMenu() {
menu = NSMenu()
menu.minimumWidth = 200
// Current track info
let trackInfoItem = NSMenuItem(title: NSLocalizedString("No Music Source", comment: ""), action: nil, keyEquivalent: "")
trackInfoItem.isEnabled = false
trackInfoItem.view = NSView(frame: NSRect(x: 0, y: 0, width: 180, height: 20))
let trackLabel = NSTextField(frame: NSRect(x: 10, y: 0, width: 160, height: 20))
trackLabel.isEditable = false
trackLabel.isBordered = false
trackLabel.backgroundColor = .clear
trackLabel.lineBreakMode = .byTruncatingTail
trackInfoItem.view?.addSubview(trackLabel)
menu.addItem(trackInfoItem)
menu.addItem(NSMenuItem.separator())
// Play/Pause
let playPauseItem = NSMenuItem(title: NSLocalizedString("Play", comment: ""), action: #selector(togglePlayPause), keyEquivalent: "")
menu.addItem(playPauseItem)
// Previous
menu.addItem(NSMenuItem(title: NSLocalizedString("Previous", comment: ""), action: #selector(playPrevious), keyEquivalent: ""))
// Next
menu.addItem(NSMenuItem(title: NSLocalizedString("Next", comment: ""), action: #selector(playNext), keyEquivalent: ""))
menu.addItem(NSMenuItem.separator())
// 播放模式子菜单
let playModeMenu = NSMenu()
let playModeItem = NSMenuItem(title: NSLocalizedString("Playback Mode", comment: ""), action: nil, keyEquivalent: "")
let sequentialItem = NSMenuItem(title: PlayerManager.PlayMode.sequential.localizedString, action: #selector(setPlayMode(_:)), keyEquivalent: "")
sequentialItem.tag = 0
let singleLoopItem = NSMenuItem(title: PlayerManager.PlayMode.singleLoop.localizedString, action: #selector(setPlayMode(_:)), keyEquivalent: "")
singleLoopItem.tag = 1
let randomItem = NSMenuItem(title: PlayerManager.PlayMode.random.localizedString, action: #selector(setPlayMode(_:)), keyEquivalent: "")
randomItem.tag = 2
playModeMenu.addItem(sequentialItem)
playModeMenu.addItem(singleLoopItem)
playModeMenu.addItem(randomItem)
playModeItem.submenu = playModeMenu
menu.addItem(playModeItem)
// Reconfigure folder
menu.addItem(NSMenuItem(title: NSLocalizedString("Set Music Source", comment: ""), action: #selector(reconfigureFolder), keyEquivalent: "s"))
// 防止休眠开关
let preventSleepItem = NSMenuItem(title: NSLocalizedString("Prevent Mac Sleep", comment: ""), action: #selector(togglePreventSleep), keyEquivalent: "")
preventSleepItem.state = sleepManager.preventSleep ? .on : .off
menu.addItem(preventSleepItem)
// 开机自启动开关
let launchAtLoginItem = NSMenuItem(title: NSLocalizedString("Launch at Login", comment: ""), action: #selector(toggleLaunchAtLogin), keyEquivalent: "")
launchAtLoginItem.state = launchManager.launchAtLogin ? .on : .off
menu.addItem(launchAtLoginItem)
menu.addItem(NSMenuItem.separator())
// Version info
let versionString = getVersionString()
let versionItem = NSMenuItem(title: versionString, action: nil, keyEquivalent: "")
versionItem.isEnabled = false
menu.addItem(versionItem)
// Quit
menu.addItem(NSMenuItem(title: NSLocalizedString("Quit", comment: ""), action: #selector(quit), keyEquivalent: ""))
// Set up observers for player state changes
NotificationCenter.default.addObserver(self, selector: #selector(updateMenuItems), name: NSNotification.Name("TrackChanged"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateMenuItems), name: NSNotification.Name("PlaybackStateChanged"), object: nil)
}
func setupRemoteCommandCenter() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget { [weak self] _ in
self?.playerManager.play()
return .success
}
commandCenter.pauseCommand.addTarget { [weak self] _ in
self?.playerManager.pause()
return .success
}
commandCenter.nextTrackCommand.addTarget { [weak self] _ in
self?.playerManager.playNext()
return .success
}
commandCenter.previousTrackCommand.addTarget { [weak self] _ in
self?.playerManager.playPrevious()
return .success
}
}
@objc func toggleMenu() {
updateMenuItems()
statusItem?.menu = menu
statusItem?.button?.performClick(nil)
}
@objc func updateMenuItems() {
if let trackInfoItem = menu.item(at: 0),
let trackLabel = trackInfoItem.view?.subviews.first as? NSTextField {
trackLabel.stringValue = playerManager.currentTrack?.title ?? NSLocalizedString("No Music Source", comment: "")
}
if let playPauseItem = menu.item(at: 2) {
playPauseItem.title = playerManager.isPlaying ? NSLocalizedString("Pause", comment: "") : NSLocalizedString("Play", comment: "")
}
updateStatusBarIcon()
updatePlayModeMenuItems()
}
private func updateStatusBarIcon() {
if let button = statusItem?.button {
let configuration = NSImage.SymbolConfiguration(pointSize: 16, weight: .regular)
let symbolName = playerManager.isPlaying ? "headphones.circle.fill" : "headphones.circle"
let icon = NSImage(systemSymbolName: symbolName, accessibilityDescription: "Music")?.withSymbolConfiguration(configuration)
button.image = icon
}
}
@objc func togglePlayPause() {
if playerManager.isPlaying {
playerManager.pause()
} else {
playerManager.play()
}
}
@objc func playPrevious() {
playerManager.playPrevious()
}
@objc func playNext() {
playerManager.playNext()
}
@objc func reconfigureFolder() {
playerManager.requestMusicFolderAccess()
}
@objc func quit() {
NSApplication.shared.terminate(self)
}
@objc func togglePreventSleep() {
sleepManager.preventSleep.toggle()
if let preventSleepItem = menu.item(withTitle: NSLocalizedString("Prevent Mac Sleep", comment: "")) {
preventSleepItem.state = sleepManager.preventSleep ? .on : .off
}
}
@objc func setPlayMode(_ sender: NSMenuItem) {
let mode: PlayerManager.PlayMode
switch sender.tag {
case 0:
mode = .sequential
case 1:
mode = .singleLoop
case 2:
mode = .random
default:
return
}
playerManager.playMode = mode
updatePlayModeMenuItems()
}
func updatePlayModeMenuItems() {
if let playModeItem = menu.item(withTitle: NSLocalizedString("Playback Mode", comment: "")),
let playModeMenu = playModeItem.submenu {
for item in playModeMenu.items {
item.state = item.tag == playerManager.playMode.tag ? .on : .off
}
}
}
@objc func toggleLaunchAtLogin() {
launchManager.launchAtLogin.toggle()
if let launchAtLoginItem = menu.item(withTitle: NSLocalizedString("Launch at Login", comment: "")) {
launchAtLoginItem.state = launchManager.launchAtLogin ? .on : .off
}
}
// 添加这个方法来确保应用保持活跃状态
func applicationWillTerminate(_ aNotification: Notification) {
sleepManager.preventSleep = false
}
private func getVersionString() -> String {
#if DEBUG
// 在 Debug 模式下显示 Git 信息
let gitCommit = Bundle.main.object(forInfoDictionaryKey: "GitCommit") as? String ?? "unknown"
return String(format: NSLocalizedString("Dev: %@", comment: ""), gitCommit)
#else
// 在 Release 模式下显示正式版本号
let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "1.0"
return String(format: NSLocalizedString("Version %@", comment: ""), appVersion)
#endif
}
}