Re: Odd behavior re: -windowWillReturnUndoManager:
Re: Odd behavior re: -windowWillReturnUndoManager:
- Subject: Re: Odd behavior re: -windowWillReturnUndoManager:
- From: Quincey Morris <email@hidden>
- Date: Fri, 17 Jan 2014 00:02:43 -0800
On Jan 16, 2014, at 11:51 , Matthew LeRoy <email@hidden> wrote:
> - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window
> {
> if (window == [self window])
> {
> return [[self inspectedDocument] undoManager];
> }
>
> return nil;
> }
It seems to me that this is fundamentally wrong. There’s already an undo chain starting at your document *window*, connecting to the undo chain in your document. This is conceptually true even if it might not in itself be screwing things up in the case.
For example, imagine that your document window also had a text field in which you can type when the inspector is not key. While the text field is active, there are undo items *in the document’s undo chain* for undoing typing. Typing in text fields in the inspector creates typing-undo actions *also* in the document’s undo chain, and things could get nasty from there — you’ve created a branching chain, but it can’t really do that.
At least, I think this is what can happen. You might be able to reason that nothing actually going wrong because the actual sequence of events always discards one branch of the chain when starting another, but this still seems like a flawed approach to me.
The other problem, more mundane but probably more directly underlying the weird behavior you’ve seen, is that the above code tells the *panel* window what its undo manager is, and the window remembers this. (You saw this yourself:
> It seems that if -windowWillReturnUndoManager: returns nil, then the method never gets called again.
Indeed, when it saw a nil return, the window created its own undo manager, and then never asked again.)
So, it seems to me, you gave the panel window an undo manager from one document, and then the panel is going to use that for any subsequent document that it targets. Hilarity ensues.
I think the proper course of action is:
1. Have the panel’s window delegate method ‘windowWillReturnUndoManager:’ return nil, or don’t implement it. This will give you a unique, single undo manager for the panel itself, which will be used for typing-undo in the panel’s text fields, and nothing else.
2. When a change is made to the document via the inspector, make sure the corresponding undo action is created in the context of the correct document undo manager. You will have to pay attention to the possibility mentioned above, if it can occur, that the document window itself may be in control of the top of the document undo chain.
Or, to put it more simply, make sure that inspector panel document changes are actually made by the document window controller, not by the panel window controller.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden