1

I'm trying to make a NSTouchBar in an SDL application and I need to attach a responder to the NSWindow object (that's the only access SDL gives into the Cocoa windowing system).

https://developer.apple.com/reference/appkit/nstouchbar

If you explicitly adopt the NSTouchBarProvider protocol in an object, you must also explicitly send the associated key-value observing notifications within NSTouchBar methods; this lets the system respond appropriately to changes in the bar.

What does that mean and how do I do it? I see lots of documentation about how to subscribe to the notifications, but not how to send them?

Right now I have:

@interface MyTouchBarResponder : NSResponder <NSTouchBarDelegate>

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

@end

and I'm attaching it to the window with the code from a previous question I asked here: How to create an NSTouchBar from an NSWindow object?

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

but my callbacks aren't ever being called (I put exit(0) in them to make it very obvious). When I hack the code directly into the SDL library, things work as expected, but that's not a viable permanent solution.

Thank you.

Community
  • 1
  • 1
xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • Did you get this working? Any github repo? I'm trying to add touchbar support to an editor via a plugin that's loaded dynamically after the window has been created, so I think I'm in a similar situation. I can get makeTouchBar to be called, but touchBar is never called. – dsvensson Dec 29 '16 at 13:57
  • @dsvensson I did but what I ended up with was sort of a jumble.. here it is, but it's a bunch of mixed up code for both creating it manually and loading it from a NIB file mixed in with stuff that is for my game, so you'll have to sort though it.. that said, it did actually mostly sort of work.. https://gist.github.com/xaxxon/fbc900e51448c7e2bbff663c061050c9 – xaxxon Dec 29 '16 at 17:36
  • Doesn't work. I need to add setting firstresponder to nil and back to its previous value for makeTouchBar to execute, and touchBar will still never execute. – dsvensson Dec 29 '16 at 18:52
  • @dsvensson sorry. I don't know much about this.. I just know this is what worked for me after creating a window with SDL. – xaxxon Dec 29 '16 at 20:42

1 Answers1

1

First, your custom responder should conform to NSTouchBarProvider (in the above, you declare the touchBar property, but not the explicit conformance)

Second, you want to make sure that your custom responder is in the responder chain of the window (whether the first responder or just later in the chain). After adjusting the responder chain with your above code, you want to call -makeFirstResponder: and pass in some view in the window (if you need that view to be first responder) or with the custom responder object. You should then verify that the window's firstResponder is that object.

With these in place, you should get at least one call to touchBar after the window is shown and made key.

To answer the question on key-value observing notifications, that is needed for when you want to change the actual NSTouchBar object being returned from touchBar. In the general case this isn't necessary, since it's unnecessary in the static touch bar case, and even in the dynamic case, you can rely on just setting the defaultItemIdentifiers on the previously created touch bar and it will update. However, should you need to change the touch bar object, you need to ensure that -willChangeValueForKey: and -didChangeValueForKey: are sent for touchBar when you change the return value. This developer documentation on KVO goes into much more detail.

Taylor
  • 3,183
  • 17
  • 18