Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable undo/redo in Cocoa app

I've implemented undo/redo the standard way (NSUndoManager) but can't figure out how I disable undo/redos when my app is in a specific state.

Users draw things in my app and when what they've drawn is uploading I disable the UI and of course don't want the user to be able to undo/redo.

I use a NSView's Undo Manager so I guess one way could be to just make that view resign first responder. Is there another way?

like image 730
Sebastian Avatar asked Feb 25 '13 18:02

Sebastian


2 Answers

If the view is the first responder, you can implement the validateMenuItem: protocol to disable or enable the menu items according to your current state.

 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
     SEL action = menuItem.action;

     if (action == @selector(undo:) ||
         action == @selector(redo:)) {
          return !uploadingImage;
     }
     return YES;
 }
like image 140
NSGod Avatar answered Sep 30 '22 18:09

NSGod


You can finalize undo and redo with

 - (void) removeAllActions;

or remove actions for a specific target with

 - (void) removeAllActionsWithTarget: (id) target;

If you simply want to disable any actions for a time, leaving the undo stack unchanged, simply disable the Undo/Redo menu items using NSMenuValidationProtocol's

 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem;
like image 22
Mark Bernstein Avatar answered Sep 30 '22 16:09

Mark Bernstein