创建主窗口后添加NSTouchBar支持

创建主窗口后添加NSTouchBar支持

本文介绍了创建主窗口后添加NSTouchBar支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试增加对通过插件将NSTouchBar按钮公开到我无法修改的应用程序的支持.创建主窗口后,将在运行时加载该插件(共享库).我创建了一个AppDelegate,如下所示:

I'm trying to add support for exposing NSTouchBar buttons via a plugin to an application that I cannot otherwise modify. The plugin, a shared library, is loaded at runtime after the main window has been created. I've created an AppDelegate as follows:

@interface AppDelegate : NSResponder <NSTouchBarDelegate>
@end

具有实现makeTouchBartouchBar功能的@implmentation:

- (NSTouchBar *) makeTouchBar
- (nullable NSTouchBarItem *) touchBar:(NSTouchBar *)touchBar
                       makeItemForIdentifier: (NSTouchBarItemIdentifier)identifier

我最终尝试将其注入到应用程序中,当动态库被加载到应用程序中时,在动态库中调用的onload函数中具有以下内容.

I finally try to inject it into the application, I have the following in the onload function that's called in the dynamic library when it's loaded into the application.

NSWindow *w = [NSApp mainWindow];

AppDelegate *a = [[AppDelegate alloc] init];
a.nextResponder = w.nextResponder;
w.nextResponder = a;

// Re-setting FirstResponder will trigger AppDelegate::makeTouchBar
// that was attached above.
NSResponder *n = w.firstResponder;
[w makeFirstResponder: nil];
[w makeFirstResponder: n];

但是...不会被调用AppDelegate::touchBar,因此触摸栏将永远不会填充我的测试按钮.

however... AppDelegate::touchBar will never be called, thus the touchbar will never be populated with my test buttons.

如果我在Xcode中创建一个新项目,并使用完全相同的AppDelegate实现(相同的@interface@implementation的复制粘贴),那么我会看到既显示功能按钮又响应按下事件的功能按钮,因此注入部分似乎已损坏. (在Xcode中,所有内容都通过MainMenu.xib连接起来了)

If I create a new project in Xcode, and use the exact same AppDelegate implementation (copy-paste of the same @interface and @implementation), I get functional buttons that are both shown and responds to press events, so it's the injection part that seems to be broken. (In Xcode everything is hooked up via MainMenu.xib I guess)

更新:主要问题是应用程序的编译方式使TouchBar无法正常工作.也许是基于旧版本的Mac OS构建的.

Update:The main problem was that the application was compiled in a way that prevented TouchBar to work. Perhaps built on an older version of Mac OS.

推荐答案

如果该对象确实应充当应用程序委托(顾名思义),而不是将其插入主窗口的响应者链中,那会更好将其实际设置为NSApp的delegate.设置触摸条时,还会使用设置的委托人的touchBar(请参见 NSTouchBar发现和响应者链).

If this object really should act as the application delegate (as the name suggests) -- rather than inserting it in the main window's responder chain, it would be better to actually set it as the NSApp's delegate. The set delegate's touchBar is also used when building the touch bar (see NSTouchBar Discovery and the Responder Chain).

如果这不适用于您的插件(也许该应用程序已经具有它所需的特定应用程序委托),则可以将其插入NSApp的响应者链而不是mainWindow的响应者链.

If that doesn't work for your plugin (maybe the application already has a specific app delegate it needs), you could insert it into NSApp's responder chain instead of the mainWindow's.

这两种方法还具有使触摸栏在应用程序级别而不是在窗口级别关联的好处.因此,如果主/键窗口发生变化,将始终使用提供的触摸栏.

Both of these also have the benefit of making the touch bar be associated at the application level rather than window. So if the main/key window changes, the provided touch bar will always be used.

这篇关于创建主窗口后添加NSTouchBar支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:25