parent
88e8d606b4
commit
4b17e0d361
@ -0,0 +1,127 @@
|
||||
//
|
||||
// NowPlayingItem.swift
|
||||
// Underbar
|
||||
//
|
||||
// Created by Shadowfacts on 7/13/19.
|
||||
// Copyright © 2019 Shadowfacts. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import MediaToolbox
|
||||
|
||||
class NowPlayingItem: NSCustomTouchBarItem {
|
||||
|
||||
var nowPlayingTitle: String?
|
||||
var nowPlayingArtist: String?
|
||||
var nowPlayingAlbum: String?
|
||||
var nowPlayingArtwork: Data?
|
||||
|
||||
var button: NSButton!
|
||||
|
||||
var lastPressTime: Date? = nil
|
||||
var multiPressCount: Int = 0
|
||||
var multiPressWorkItem: DispatchWorkItem? = nil
|
||||
|
||||
override init(identifier: NSTouchBarItem.Identifier) {
|
||||
super.init(identifier: identifier)
|
||||
|
||||
button = NSButton(title: "Test\nline 2", target: self, action: #selector(buttonPressed))
|
||||
button.alignment = .left
|
||||
|
||||
button.widthAnchor.constraint(equalToConstant: 200).isActive = true
|
||||
|
||||
view = button
|
||||
|
||||
MRMediaRemoteRegisterForNowPlayingNotifications(.global(qos: .utility))
|
||||
registerForNotifications()
|
||||
|
||||
updateMediaInfo()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
func registerForNotifications() {
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(updateMediaInfo), name: .init(kMRMediaRemoteNowPlayingApplicationClientStateDidChange), object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(updateMediaInfo), name: .mrNowPlayingPlaybackQueueChanged, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(updateMediaInfo), name: .mrNowPlayingPlaybackQueueChanged, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(updateMediaInfo), name: .mrMediaRemoteNowPlayingApplicationIsPlayingDidChange, object: nil)
|
||||
}
|
||||
|
||||
@objc func updateMediaInfo() {
|
||||
MRMediaRemoteGetNowPlayingInfo(.main) { [weak self] (info) in
|
||||
guard let self = self, let info = info else { return }
|
||||
|
||||
self.nowPlayingTitle = info[kMRMediaRemoteNowPlayingInfoTitle] as? String
|
||||
self.nowPlayingArtist = info[kMRMediaRemoteNowPlayingInfoArtist] as? String
|
||||
self.nowPlayingAlbum = info[kMRMediaRemoteNowPlayingInfoAlbum] as? String
|
||||
self.nowPlayingArtwork = info[kMRMediaRemoteNowPlayingInfoArtworkData] as? Data
|
||||
|
||||
self.updateButtonTitle()
|
||||
self.updateButtonImage()
|
||||
}
|
||||
}
|
||||
|
||||
func updateButtonTitle() {
|
||||
let attributedStr = NSMutableAttributedString()
|
||||
|
||||
|
||||
|
||||
let line1 = NSAttributedString(string: nowPlayingTitle ?? "Unknown")
|
||||
attributedStr.append(line1)
|
||||
attributedStr.setAttributes([.font: NSFont.systemFont(ofSize: 12)], range: NSRange(location: 0, length: line1.length))
|
||||
|
||||
attributedStr.append(NSAttributedString(string: "\n"))
|
||||
|
||||
let line2 = NSAttributedString(string: (nowPlayingArtist ?? "Unknown") + " | " + (nowPlayingAlbum ?? "Unknown"))
|
||||
attributedStr.append(line2)
|
||||
attributedStr.setAttributes([.font: NSFont.systemFont(ofSize: 10)], range: NSRange(location: line1.length + 1, length: line2.length))
|
||||
|
||||
button.attributedTitle = attributedStr
|
||||
}
|
||||
|
||||
func updateButtonImage() {
|
||||
if let data = nowPlayingArtwork,
|
||||
let image = NSImage(data: data) {
|
||||
button.image = image
|
||||
button.imagePosition = .imageLeft
|
||||
} else {
|
||||
button.image = nil
|
||||
}
|
||||
}
|
||||
|
||||
@objc func buttonPressed() {
|
||||
if let lastPressTime = lastPressTime,
|
||||
lastPressTime.timeIntervalSinceNow < 0.2 {
|
||||
multiPressCount += 1
|
||||
multiPressWorkItem?.cancel()
|
||||
} else {
|
||||
self.lastPressTime = Date()
|
||||
multiPressCount = 1
|
||||
}
|
||||
|
||||
multiPressWorkItem = DispatchWorkItem {
|
||||
self.handleMultiPress(presses: self.multiPressCount)
|
||||
self.lastPressTime = nil
|
||||
self.multiPressCount = 0
|
||||
self.multiPressWorkItem = nil
|
||||
}
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: multiPressWorkItem!)
|
||||
|
||||
}
|
||||
|
||||
func handleMultiPress(presses: Int) {
|
||||
switch presses {
|
||||
case 1:
|
||||
MRMediaRemoteSendCommand(kMRTogglePlayPause, nil)
|
||||
case 2:
|
||||
MRMediaRemoteSendCommand(kMRNextTrack, nil)
|
||||
case 3:
|
||||
MRMediaRemoteSendCommand(kMRPreviousTrack, nil)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
//
|
||||
// MRMediaRemote.h
|
||||
// Underbar
|
||||
//
|
||||
// Created by Shadowfacts on 7/13/19.
|
||||
// Copyright © 2019 Shadowfacts. All rights reserved.
|
||||
//
|
||||
|
||||
// Copied from https://github.com/pigigaldi/Pock/blob/852679a4882d0e9c5b3d8473eb9b9fbf7bef837f/Pock/Private/MRMediaRemote.h
|
||||
|
||||
typedef void (^MRMediaRemoteGetNowPlayingInfoBlock)(NSDictionary *info);
|
||||
typedef void (^MRMediaRemoteGetNowPlayingClientBlock)(id clientObj);
|
||||
typedef void (^MRMediaRemoteGetNowPlayingApplicationIsPlayingBlock)(BOOL playing);
|
||||
|
||||
extern void MRMediaRemoteRegisterForNowPlayingNotifications(dispatch_queue_t queue);
|
||||
extern void MRMediaRemoteGetNowPlayingClient(dispatch_queue_t queue, MRMediaRemoteGetNowPlayingClientBlock block);
|
||||
extern void MRMediaRemoteGetNowPlayingClients(dispatch_queue_t queue, MRMediaRemoteGetNowPlayingClientBlock block);
|
||||
extern void MRMediaRemoteGetNowPlayingInfo(dispatch_queue_t queue, MRMediaRemoteGetNowPlayingInfoBlock block);
|
||||
extern void MRMediaRemoteGetNowPlayingApplicationIsPlaying(dispatch_queue_t queue, MRMediaRemoteGetNowPlayingApplicationIsPlayingBlock block);
|
||||
|
||||
extern NSString *MRNowPlayingClientGetBundleIdentifier(id clientObj);
|
||||
extern NSString *MRNowPlayingClientGetParentAppBundleIdentifier(id clientObj);
|
||||
|
||||
extern NSString *kMRMediaRemoteNowPlayingApplicationIsPlayingDidChangeNotification;
|
||||
extern NSString *kMRMediaRemoteNowPlayingApplicationClientStateDidChange;
|
||||
extern NSString *kMRNowPlayingPlaybackQueueChangedNotification;
|
||||
extern NSString *kMRPlaybackQueueContentItemsChangedNotification;
|
||||
extern NSString *kMRMediaRemoteNowPlayingApplicationDidChangeNotification;
|
||||
|
||||
extern NSString *kMRMediaRemoteNowPlayingInfoAlbum;
|
||||
extern NSString *kMRMediaRemoteNowPlayingInfoArtist;
|
||||
extern NSString *kMRMediaRemoteNowPlayingInfoTitle;
|
||||
extern NSString *kMRMediaRemoteNowPlayingInfoArtworkData;
|
||||
|
||||
typedef enum {
|
||||
kMRPlay = 0,
|
||||
kMRPause = 1,
|
||||
kMRTogglePlayPause = 2,
|
||||
kMRNextTrack = 4,
|
||||
kMRPreviousTrack = 5,
|
||||
} MRCommand;
|
||||
|
||||
extern Boolean MRMediaRemoteSendCommand(MRCommand command, id userInfo);
|
@ -1,18 +0,0 @@
|
||||
//
|
||||
// NowPlayingWidget.swift
|
||||
// Underbar
|
||||
//
|
||||
// Created by Shadowfacts on 7/13/19.
|
||||
// Copyright © 2019 Shadowfacts. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
class NowPlayingWidget: TouchBarWidget {
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
item = NSButtonTouchBarItem(identifier: .nowPlaying, title: "Test", target: nil, action: nil)
|
||||
}
|
||||
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
//
|
||||
// TouchBarWidget.swift
|
||||
// Underbar
|
||||
//
|
||||
// Created by Shadowfacts on 7/13/19.
|
||||
// Copyright © 2019 Shadowfacts. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
class TouchBarWidget: NSObject {
|
||||
|
||||
var item: NSTouchBarItem!
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
convenience init(item: NSTouchBarItem) {
|
||||
self.init()
|
||||
self.item = item
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue