Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Last active May 4, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/1a13f27e6b469ca2ffcf9b53588b837a to your computer and use it in GitHub Desktop.
Save krzyzanowskim/1a13f27e6b469ca2ffcf9b53588b837a to your computer and use it in GitHub Desktop.
NSResponder.undoManager is not used (FB9959997)

According to documentation it's enough to override NSResponder.undoManager to provide custom NSUndoManager instance. I found it's not quite working:

  • Xcode Mac application template comes with Undo menu item bind to firstResponder.undo: despite NSResponder has no undo: selector defined. Unless first responder actually respond to undo: the Undo action doesn't work
  • NSFirstResponder.undoManager look up in responder chain and eventually it end up in NSWindow where the only place (that I found) that validate Undo menu item is NSWindow.validateUserInterfaceItem - that one, as turned out, only ever validate undo: action if first responder implements _getUndoManager: selector (quite private, isn't it?) - see attached disassembly

at this point 🤕 I start to think it's either documentation issue, or AppKit implementation issue. Anyway, I didn't found way to enable undo: action unless I implement one of these methods in my NSView. (notice: the view is first responder that I confirmed). Or I'm doing it wrong, and does it ever custom undoManager for custom NSView/NSControl ever worked.

In AppKit, the only implementations of undo: in appkit are on NSWindow which calls that _getActiveUndoManager method. The overriden NSResponder.undoManager is never called.

What I'm trying to achieve here is provide custom NSUndoManager subclass for my NSView (first responder)

For the reference GnuSTEP has it right: https://github.com/gnustep/libs-gui/blob/efb9bcb2e7c4bde76932702dd40358090831507c/Source/NSWindow.m#L5574

Based on Slack discussion with peers, it seems to be an old REGRESSION introduced ~10 or 20 years ago ¯_(ツ)_/¯

@krzyzanowskim
Copy link
Author

Screen Shot 2022-03-16 at 22 16 24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment