CPWindow: Reflect undo manager action names in Undo and Redo menu items

Previously, the window and text view enabled Undo and Redo items without updating their titles, leaving them static as "Undo" and "Redo" regardless of action names set via -setActionName:.

The window now updates menu item titles from the undo manager (e.g., "Undo Delete"), and the text view delegates validation of both items to the window.
This commit is contained in:
David Richardson committed 2026-11-25 15:35:14 -06:00
1 parent 0c30132f1c
commit 75e8ee4dfb
4 files changed
+116 -11

No files matched your search

+8 -6
View File
@@ -823,7 +823,8 @@ var kDelegateRespondsTo_textShouldBeginEditing
}
/*!
Enables the Undo and Redo items only when the undo manager has a change to undo or redo.
Validates the Undo and Redo items as the window does: they are enabled only when the undo
manager has a change to undo or redo, and a menu item's title names the change.
For \c -toggleRuler:, the item's title becomes "Hide Ruler" or "Show Ruler" to reflect the
ruler's visibility, and the item is enabled only when the text view uses a ruler and lies
in a scroll view. \c -copyRuler: is enabled in rich text, and \c -pasteRuler: in editable
@@ -840,11 +841,12 @@ var kDelegateRespondsTo_textShouldBeginEditing
{
var action = [anItem action];
if (action === @selector(undo:))
return [[[self window] undoManager] canUndo];
if (action === @selector(redo:))
return [[[self window] undoManager] canRedo];
/*
2026-11-25: The window validates the Undo and Redo items, so their titles name the
change here too. This method enabled them but left the titles "Undo" and "Redo".
*/
if (action === @selector(undo:) || action === @selector(redo:))
return [[self window] validateUserInterfaceItem:anItem];
// Baseline changes apply only to editable rich text.
if (action === @selector(superscript:) || action === @selector(subscript:) || action === @selector(unscript:) ||
+15 -5
View File
@@ -3149,7 +3149,7 @@ CPTexturedBackgroundWindowMask
For \c -toggleToolbarShown:, the item's title becomes "Hide Toolbar" or "Show Toolbar"
to reflect the toolbar's visibility, and the item is enabled only when the window has
a toolbar. \c -undo: and \c -redo: are enabled only when the window's undo manager has a
change to undo or redo. \c -performMiniaturize: and \c -miniaturize: are enabled only
change to undo or redo, and a menu item's title names the change, as in "Undo Delete". \c -performMiniaturize: and \c -miniaturize: are enabled only
when the window's minimize button is enabled, and \c -performZoom: and \c -zoom: only
when its zoom button is enabled. \c -runToolbarCustomizationPalette: is enabled only when
the window's toolbar allows customization and runs no palette. Every other item is enabled.
@@ -3160,11 +3160,21 @@ CPTexturedBackgroundWindowMask
{
var action = [anItem action];
if (action === @selector(undo:))
return [[self undoManager] canUndo];
/*
2026-11-25: The Undo and Redo menu items name the change, from the undo manager's
action name. The items were only enabled, so their titles stayed "Undo" and "Redo"
and the action names given with -setActionName: never showed.
*/
if (action === @selector(undo:) || action === @selector(redo:))
{
var undoManager = [self undoManager],
isUndo = action === @selector(undo:);
if (action === @selector(redo:))
return [[self undoManager] canRedo];
if ([anItem isKindOfClass:[CPMenuItem class]])
[anItem setTitle:isUndo ? [undoManager undoMenuItemTitle] : [undoManager redoMenuItemTitle]];
return isUndo ? [undoManager canUndo] : [undoManager canRedo];
}
/*
The Minimize and Zoom items agree with the window's buttons. Minimize was enabled
+21
View File
@@ -366,6 +366,27 @@
[self assertTrue:[theWindow validateUserInterfaceItem:undoItem]];
}
/*
The text view validates the Undo item as the window does, so the item names the change.
*/
- (void)testUndoItemNamesTheChangeWhenTheTextViewValidatesIt
{
var undoManager = [theWindow undoManager],
undoItem = [[CPMenuItem alloc] initWithTitle:@"Undo" action:@selector(undo:) keyEquivalent:@""];
[self assertTrue:[theWindow makeFirstResponder:textView]];
[undoManager setGroupsByEvent:NO];
[undoManager removeAllActions];
[undoManager beginUndoGrouping];
[undoManager registerUndoWithTarget:[] selector:@selector(addObject:) object:@"Text"];
[undoManager setActionName:@"Delete"];
[undoManager endUndoGrouping];
[self assertTrue:[textView validateUserInterfaceItem:undoItem]];
[self assert:@"Undo Delete" equals:[undoItem title]];
}
// MARK: - Object value of rich text
- (void)testTheObjectValueOfARichTextViewIsACopyOfItsText
+72
View File
@@ -487,6 +487,44 @@
parent._sheetContext["isClosing"] = YES;
}
// MARK: - Undo and Redo items
/*
The Undo and Redo menu items take their titles from the undo manager: the bare command when
there is nothing to undo or redo, and the command with the action name otherwise.
*/
- (void)testUndoAndRedoItemsNameTheChange
{
var undoManager = [_window undoManager],
undoItem = [[CPMenuItem alloc] initWithTitle:@"Undo" action:@selector(undo:) keyEquivalent:@""],
redoItem = [[CPMenuItem alloc] initWithTitle:@"Redo" action:@selector(redo:) keyEquivalent:@""],
target = [[_CPWindowTestUndoTarget alloc] initWithUndoManager:undoManager];
[undoManager setGroupsByEvent:NO];
[undoManager removeAllActions];
[self assertFalse:[_window validateUserInterfaceItem:undoItem]];
[self assert:@"Undo" equals:[undoItem title]];
[undoManager beginUndoGrouping];
[target removePerson:@"Person"];
[undoManager setActionName:@"Delete"];
[undoManager endUndoGrouping];
[self assertTrue:[_window validateUserInterfaceItem:undoItem]];
[self assert:@"Undo Delete" equals:[undoItem title]];
[self assertFalse:[_window validateUserInterfaceItem:redoItem]];
[self assert:@"Redo" equals:[redoItem title]];
[_window undo:nil];
[self assert:@[@"Person"] equals:[target people] message:@"the undo ran"];
[self assertFalse:[_window validateUserInterfaceItem:undoItem]];
[self assert:@"Undo" equals:[undoItem title]];
[self assertTrue:[_window validateUserInterfaceItem:redoItem]];
[self assert:@"Redo Delete" equals:[redoItem title]];
}
@end
/*
@@ -582,3 +620,37 @@ var CPWindowTestZoomingWindowStandardFrame = CGRectMake(40.0, 50.0, 600.0, 450.0
}
@end
/*
Holds a list of people and registers the inverse of each change, so an undo leaves a redo.
*/
@implementation _CPWindowTestUndoTarget : CPObject
{
CPUndoManager _undoManager;
CPArray _people @accessors(getter=people);
}
- (id)initWithUndoManager:(CPUndoManager)anUndoManager
{
if (self = [super init])
{
_undoManager = anUndoManager;
_people = [];
}
return self;
}
- (void)removePerson:(CPString)aPerson
{
[_people removeObject:aPerson];
[[_undoManager prepareWithInvocationTarget:self] addPerson:aPerson];
}
- (void)addPerson:(CPString)aPerson
{
[_people addObject:aPerson];
[[_undoManager prepareWithInvocationTarget:self] removePerson:aPerson];
}
@end