Add long press to open currently playing app

This commit is contained in:
Shadowfacts 2019-07-13 22:37:22 -04:00
parent 4b17e0d361
commit 8981f51b94
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 21 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import MediaToolbox
class NowPlayingItem: NSCustomTouchBarItem {
var nowPlayingClientID: String?
var nowPlayingTitle: String?
var nowPlayingArtist: String?
var nowPlayingAlbum: String?
@ -30,6 +31,10 @@ class NowPlayingItem: NSCustomTouchBarItem {
button.widthAnchor.constraint(equalToConstant: 200).isActive = true
let longPressRecognizer = NSPressGestureRecognizer(target: self, action: #selector(buttonLongPressed))
longPressRecognizer.allowedTouchTypes = .direct
button.addGestureRecognizer(longPressRecognizer)
view = button
MRMediaRemoteRegisterForNowPlayingNotifications(.global(qos: .utility))
@ -61,6 +66,14 @@ class NowPlayingItem: NSCustomTouchBarItem {
self.updateButtonTitle()
self.updateButtonImage()
}
MRMediaRemoteGetNowPlayingClient(.main) { (client) in
guard let client = client else {
self.nowPlayingClientID = nil
return
}
self.nowPlayingClientID = MRNowPlayingClientGetBundleIdentifier(client)
}
}
func updateButtonTitle() {
@ -82,6 +95,7 @@ class NowPlayingItem: NSCustomTouchBarItem {
}
func updateButtonImage() {
// todo: custom spotify artwork helper
if let data = nowPlayingArtwork,
let image = NSImage(data: data) {
button.image = image
@ -124,4 +138,11 @@ class NowPlayingItem: NSCustomTouchBarItem {
}
}
@objc func buttonLongPressed(recognizer: NSPressGestureRecognizer) {
if recognizer.state == .began,
let nowPlayingClientID = nowPlayingClientID {
NSWorkspace.shared.launchApplication(withBundleIdentifier: nowPlayingClientID, options: .default, additionalEventParamDescriptor: nil, launchIdentifier: nil)
}
}
}