gpt4 book ai didi

swift - 将按钮添加到 NSTouchBar 控制条

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:07 25 4
gpt4 key购买 nike

我正在开发一个在触摸栏上显示当前加密货币价格的宠物项目。我是 Web 开发人员,但不是 Swift 开发人员,所以进展缓慢。

https://github.com/sharkattackhq/crypt

现在,基本功能已经到位,但当然只有在应用程序窗口处于事件状态时才会显示数据。无论哪个应用程序处于事件状态,我都希望能够查看数据,而我能看到的唯一方法是在 Touch Bar 的控制条部分添加一个按钮。

如何将按钮添加到控制条?没有用于此的公共(public) API,但我可以看到其他非 Apple 应用程序可以做到这一点。

最佳答案

仅通过私有(private)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)
}

更新 10.14

+ (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

关于swift - 将按钮添加到 NSTouchBar 控制条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47988940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com