How to create an NSTouchBar from an NSWindow object?

六月ゝ 毕业季﹏ 提交于 2019-12-11 05:57:41

问题


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

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.

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

Thank you.


回答1:


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. 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.

  2. 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;


来源:https://stackoverflow.com/questions/40686370/how-to-create-an-nstouchbar-from-an-nswindow-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!