Skip to content

Commit 8d4a380

Browse files
authored
Merge pull request #13 from adaskar/perfissue
performance issues resolved
2 parents 71c53cf + 86c78fa commit 8d4a380

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

AggregateVolumeMenu/AggregateVolumeMenuApp.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
2222
private var statusItem: NSStatusItem?
2323
private var popover: NSPopover?
2424
private var audioManager = AudioDeviceManager.shared
25-
private var volumeObserverTimer: Timer?
26-
2725
func applicationDidFinishLaunching(_ notification: Notification) {
2826
setupStatusItem()
2927
setupPopover()
3028
setupMediaKeyHandling()
31-
observeVolumeChanges()
29+
observeVolumeChangeNotifications()
3230
}
3331

34-
func applicationWillTerminate(_ notification: Notification) {
35-
volumeObserverTimer?.invalidate()
32+
deinit {
33+
NotificationCenter.default.removeObserver(self)
3634
}
3735

3836
private func setupStatusItem() {
@@ -46,10 +44,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4644
}
4745
}
4846

49-
private func observeVolumeChanges() {
50-
volumeObserverTimer = Timer.scheduledTimer(withTimeInterval: 0.05, repeats: true) { _ in
51-
self.updateMenuBarIcon()
52-
}
47+
private func observeVolumeChangeNotifications() {
48+
NotificationCenter.default.addObserver(self, selector: #selector(volumeDidChange), name: NSNotification.Name("volumeDidChange"), object: nil)
49+
}
50+
51+
@objc private func volumeDidChange(_ notification: Notification) {
52+
updateMenuBarIcon()
5353
}
5454

5555
private func updateMenuBarIcon() {

AggregateVolumeMenu/AudioDeviceManager.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,22 @@ class AudioDeviceManager: ObservableObject {
7373
guard let device = currentDevice else { return }
7474
currentVolume = max(0, min(1, volume))
7575
setVolume(currentVolume, for: device)
76+
NotificationCenter.default.post(name: NSNotification.Name("volumeDidChange"), object: nil)
7677
}
7778

7879
func adjustVolume(by delta: Float) {
7980
guard let device = currentDevice else { return }
8081
let newVolume = max(0, min(1, currentVolume + delta))
8182
currentVolume = newVolume
8283
setVolume(newVolume, for: device)
84+
NotificationCenter.default.post(name: NSNotification.Name("volumeDidChange"), object: nil)
8385
}
8486

8587
func toggleMute() {
8688
guard let device = currentDevice else { return }
8789
isMuted.toggle()
8890
setMute(isMuted, for: device)
91+
NotificationCenter.default.post(name: NSNotification.Name("volumeDidChange"), object: nil)
8992
}
9093

9194
private func getOutputDevices() -> [AudioDevice] {

0 commit comments

Comments
 (0)