-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.swift
159 lines (127 loc) · 5.28 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
//
// 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 menu: NSMenu!
func applicationDidFinishLaunching(_ notification: Notification) {
playerManager = PlayerManager()
sleepManager = SleepManager()
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = statusItem?.button {
button.title = "🤡"
button.target = self
button.action = #selector(toggleMenu)
}
setupMenu()
setupRemoteCommandCenter()
}
func setupMenu() {
menu = NSMenu()
menu.minimumWidth = 200 // 设置固定宽
// Current track info
let trackInfoItem = NSMenuItem(title: "No selected", 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: "播放", action: #selector(togglePlayPause), keyEquivalent: "")
menu.addItem(playPauseItem)
// Previous
menu.addItem(NSMenuItem(title: "上一首", action: #selector(playPrevious), keyEquivalent: ""))
// Next
menu.addItem(NSMenuItem(title: "下一首", action: #selector(playNext), keyEquivalent: ""))
menu.addItem(NSMenuItem.separator())
// Reconfigure folder
menu.addItem(NSMenuItem(title: "设置音乐源", action: #selector(reconfigureFolder), keyEquivalent: "s"))
// 防止休眠开关
let preventSleepItem = NSMenuItem(title: "防止 Mac 休眠", action: #selector(togglePreventSleep), keyEquivalent: "")
preventSleepItem.state = sleepManager.preventSleep ? .on : .off
menu.addItem(preventSleepItem)
menu.addItem(NSMenuItem.separator())
// Quit
menu.addItem(NSMenuItem(title: "退出", 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 ?? "未选择音乐源"
}
if let playPauseItem = menu.item(at: 2) {
playPauseItem.title = playerManager.isPlaying ? "暂停" : "播放"
}
}
@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: "防止 Mac 休眠") {
preventSleepItem.state = sleepManager.preventSleep ? .on : .off
}
}
// 添加这个方法来确保应用保持活跃状态
func applicationWillTerminate(_ aNotification: Notification) {
sleepManager.preventSleep = false
}
}