我正在开展一个宠物项目,它在触摸栏上显示当前的加密货币价格。我是一名网络开发人员,但不是Swift开发人员,因此进展缓慢。
https://github.com/sharkattackhq/crypt
目前,基本功能已到位,但当然只有在应用程序窗口处于活动状态时才会显示数据。无论哪个应用程序处于活动状态,我都希望能够查看数据,而我能看到的唯一方法就是在Touch Bar的Control Strip部分添加一个按钮。
如何在控制条上添加按钮?没有这方面的公共API,但我可以看到其他的,没有Apple应用程序。
答案 0 :(得分:6)
只有通过私有API,您才需要连接/System/Library/PrivateFrameworks/DFRFoundation.framework
桥:
// TouchBarPrivateApi-Bridging.h
#import "TouchBarPrivateApi.h"`
部首:
// TouchBarPrivateApi.
#import <AppKit/AppKit.h>`
extern void DFRElementSetControlStripPresenceForIdentifier(NSTouchBarItemIdentifier, BOOL);
extern void DFRSystemModalShowsCloseBoxWhenFrontMost(BOOL);
@interface NSTouchBarItem (PrivateMethods)
+ (void)addSystemTrayItem:(NSTouchBarItem *)item;
+ (void)removeSystemTrayItem:(NSTouchBarItem *)item;
@end
@interface NSTouchBar (PrivateMethods)
+ (void)presentSystemModalFunctionBar:(NSTouchBar *)touchBar placement:(long long)placement systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
+ (void)presentSystemModalFunctionBar:(NSTouchBar *)touchBar systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
+ (void)dismissSystemModalFunctionBar:(NSTouchBar *)touchBar;
+ (void)minimizeSystemModalFunctionBar:(NSTouchBar *)touchBar;
@end
并使用它:
// AppDelegate applicationDidFinishLaunching
func applicationDidFinishLaunching(_ aNotification: Notification) {
TouchBarController.shared.setupControlStripPresence()
}
// TouchBarController class TouchBarController: NSObject, NSTouchBarDelegate {
static let shared = TouchBarController()
let touchBar = NSTouchBar() func setupControlStripPresence() {
DFRSystemModalShowsCloseBoxWhenFrontMost(false)
let item = NSCustomTouchBarItem(identifier: .controlStripItem)
item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar))
NSTouchBarItem.addSystemTrayItem(item)
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
}
+ (void)presentSystemModalTouchBar:(NSTouchBar *)touchBar placement:(long long)placement systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
+ (void)presentSystemModalTouchBar:(NSTouchBar *)touchBar systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
+ (void)dismissSystemModalTouchBar:(NSTouchBar *)touchBar;
+ (void)minimizeSystemModalTouchBar:(NSTouchBar *)touchBar;
以实际项目为例 - https://github.com/Toxblh/MTMR