如何从NSWindow对象创建NSTouchBar? [英] How to create an NSTouchBar from an NSWindow object?

查看:136
本文介绍了如何从NSWindow对象创建NSTouchBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将触控栏支持与SDL应用程序集成.

I'm trying to integrate touchbar support with an SDL application.

我进入了SDL中的目标C代码,直接入侵了SDLView代码并获得了可正常使用的触摸条,但是现在我想使用库存的SDL2库获得相同的功能. SDL公开了它创建的NSWindow对象,我想我可以使用makeTouchBar和makeItemForIdentifier创建一个响应者对象,但是我不知道如何将其附加"到窗口.我认为这将是响应者链",但我真的不完全知道这意味着什么或如何做.

I've gone into the objective c code in SDL and hacked directly into the SDLView code and gotten a working touchbar, but now I want to get the same functionality with the stock SDL2 library. SDL exposes the NSWindow object it creates and I think I can create a responder object with makeTouchBar and makeItemForIdentifier implemented, but I don't know how to "attach" it to the window. I think it would be a "responder chain" but I don't really know exactly what that means or how to do it.

我尝试创建以下内容,并在窗口上尝试了setNextResponder的几种不同组合,但似乎无法找出正确的组合来调用makeTouchBar,但是我处于正确的状态,因为它可以在我将这些方法直接放在视图上.

I've tried creating the following and tried a few different combinations of setNextResponder on the window, but can't seem to figure out the right combination to get makeTouchBar called, but I'm in the right ballpark since it works when I put these methods directly on the view.

@interface MyTouchbarResponder : NSResponder
- (id)init;

@property(strong, readonly) NSTouchBar *touchBar;
- (NSTouchBar *)makeTouchBar;
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier;


@end




@implementation MyTouchbarResponder
- (id)init
{
    [super init];
}

- (NSTouchBar *)makeTouchBar
{
 ...stuff...
}

- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
...stuff...
}

@end

谢谢.

推荐答案

在链中添加自定义响应器是一种方法,但是您可能需要首先考虑两种更简单的方法:

Adding a custom responder into the chain is one way to do it, but there are two simpler methods you might want to consider first:

  1. 如果可以为窗口提供自定义类,请在NSWindow的子类中实现-makeTouchBar.使用这种方法,您无需设置或覆盖touchBar属性. -makeTouchBar将被懒惰地调用,以根据需要填充该属性.

  1. If you can provide a custom class for your window, implement -makeTouchBar in a subclass of NSWindow. With this approach, you don't need to set or override the touchBar property; -makeTouchBar will be invoked lazily to populate that property as needed.

如果不能使用自己的窗口子类,则可以创建一个NSTouchBar对象并将其分配给窗口的现有touchBar属性. (基本实现是一个存储的属性,因此不需要重写它.)

If you can't use your own window subclass, you can just create a NSTouchBar object and assign it to the window's existing touchBar property. (The base implementation is a stored property, so there's no need to override it.)

这种懒惰很有用,因为如果设备没有触摸栏或正在运行的模拟器,则可以避免做额外的工作.

This laziness is useful, in that you can avoid doing extra work if the device does not have a Touch Bar or running simulator.

如果您确实希望将其实现为插入响应者链的自定义响应者,则可以执行以下操作:

If you really do want to implement this as a custom responder interposed into the responder chain, you could do something like this:

touchBarResponder.nextResponder = window.nextResponder;
window.nextResponder = touchBarResponder;

这篇关于如何从NSWindow对象创建NSTouchBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆