问题
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:
If you can provide a custom class for your window, implement
-makeTouchBar
in a subclass ofNSWindow
. With this approach, you don't need to set or override thetouchBar
property;-makeTouchBar
will be invoked lazily✝ to populate that property as needed.If you can't use your own window subclass, you can just create a
NSTouchBar
object and assign it to the window's existingtouchBar
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