From 8981f51b9443d17cafa78cc8bdeb332a845caf84 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 13 Jul 2019 22:37:22 -0400 Subject: [PATCH] Add long press to open currently playing app --- Underbar/Items/NowPlayingItem.swift | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Underbar/Items/NowPlayingItem.swift b/Underbar/Items/NowPlayingItem.swift index b7b4597..2fe6c06 100644 --- a/Underbar/Items/NowPlayingItem.swift +++ b/Underbar/Items/NowPlayingItem.swift @@ -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) + } + } + }