Safe to relink nextResponder

To capture gesture events I’ve been following the pattern used by NSImageView in MacOSLib: dynamically
subclass NSView, adding an instance as a subview of your Canvas/RectControl/etc.

For these events I’ve found subclassing NSResponder and inserting into the nextResponder chain works just as well, including to the top of the window, but is this safe or is it ‘messing with the view hierarchy’?

I don’t know, but I’ve been subclassing my canvas (Xojo canvas is an NSView) and adding the events to it. Haven’t seen any bad effects yet… I wonder if that’s an ok way to approach it.

I would not do that this way.
Your subview should automatically get calls for NSResponder as a NSView is a NSResponder.

How are you adding events? Do you mean directly to the XOJView class?

The bad stuff I know about is moving a widget to something elses subview, like embedding a ContainerControl someplace then moving it to a NSPopover, or moving a Pushbutton to the themeFrame. Actually, I’m not so clear and maybe that last one is OK.

That’s how it’s working now with NSView subclass added as a subview but I’m thinking to refactor as NSResponder for greater flexibility such as a single, window-level handler of gesture events.

[quote=94231:@Will Shank]How are you adding events? Do you mean directly to the XOJView class?

[/quote]
Yes, I use something like

dim objectClass As ptr=object_getClass(self.Handle) MyCocoaSubclass= objc_allocateClassPair(objectClass, "myCustomView", 0)
then use class_replaceMethod to add handlers and such. This way I can override built-in methods like wantsDefaultClipping etc.

[quote=94345:@jim mckay]Yes, I use something like

dim objectClass As ptr=object_getClass(self.Handle) MyCocoaSubclass= objc_allocateClassPair(objectClass, "myCustomView", 0)
then use class_replaceMethod to add handlers and such. This way I can override built-in methods like wantsDefaultClipping etc.[/quote]

Are you using object_setClass to swap out the view’s class for your own subclass?

[quote=94353:@Joe Ranieri]Are you using object_setClass to swap out the view’s class for your own subclass?

[/quote]
Yes. I store the subclass as a shared property to be reused in other instances of the canvas. Is that bad?

Yes - very likely to break on you in weird & wonderful ways.
It messes with KVO and can cause problems.

Would it be better then to create an NSView and attach that to my Canvas…

If by attach you mean “add a subview”, then yes, absolutely. This is what macoslib does and about the closest you’ll get to a “supported” solution.