People: Support undo and redo for person removal

Register undo/redo actions with the window's undo manager for Delete, Cut, and removal from a group.

Snapshot each person's position in the library and group memberships prior to removal so undo restores them in place and reselects them, while redo removes them again.

Expose named Undo and Redo items in the Edit menu and table contextual menu, and clear the undo stack on Reset Application.
This commit is contained in:
David Richardson committed 2026-11-25 15:36:18 -06:00
1 parent 75e8ee4dfb
commit 0f528d3590
4 files changed
+172 -12

No files matched your search

@@ -330,12 +330,15 @@ var NotesMask = "url('data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4
The items have no target, so each action goes through the responder chain from the table:
Deselect All reaches the table itself, Edit and Invert Selection this controller, and
Delete Selected the window controller, which also deletes with the minus button and the
Delete key.
Delete key. Undo and Redo reach the window, which names the change in their titles.
*/
- (CPMenu)_buildTableMenu
{
var menu = [[CPMenu alloc] initWithTitle:@"People"];
[menu addItemWithTitle:@"Undo" action:@selector(undo:) keyEquivalent:@""];
[menu addItemWithTitle:@"Redo" action:@selector(redo:) keyEquivalent:@""];
[menu addItem:[CPMenuItem separatorItem]];
[menu addItemWithTitle:@"Edit" action:@selector(editClickedPerson:) keyEquivalent:@""];
[menu addItem:[CPMenuItem separatorItem]];
[menu addItemWithTitle:@"Deselect All" action:@selector(deselectAll:) keyEquivalent:@""];
+107 -1
View File
@@ -468,6 +468,107 @@ var LibraryDefaultsKey = @"People.Library.v1",
[self _noteStructuralChange];
}
// ---------------------------------------------------------------- Restoring removed people
/*
Returns the places of thePeople, for -restorePlaces: after -removePeople:. A place records a
person's index in the library, and its index in each group that holds it.
*/
- (CPArray)placesOfPeople:(CPArray)thePeople
{
var places = [],
groups = [self groups];
for (var i = 0; i < thePeople.length; i++)
{
var person = thePeople[i],
index = [_people indexOfObjectIdenticalTo:person];
if (index === CPNotFound)
continue;
places.push({ person: person, index: index, memberships: [self _membershipsOfPerson:person inGroups:groups] });
}
return places;
}
/*
Returns the places of thePeople in aGroup alone, for -restorePlaces: after
-removePeople:fromGroup:. The people stay in the library, so a place has no library index.
*/
- (CPArray)placesOfPeople:(CPArray)thePeople inGroup:(LibraryGroup)aGroup
{
var places = [];
for (var i = 0; i < thePeople.length; i++)
{
var memberships = [self _membershipsOfPerson:thePeople[i] inGroups:[aGroup]];
if (memberships.length)
places.push({ person: thePeople[i], index: CPNotFound, memberships: memberships });
}
return places;
}
- (CPArray)_membershipsOfPerson:(Person)aPerson inGroups:(CPArray)theGroups
{
var memberships = [],
uid = [aPerson uid];
for (var i = 0; i < theGroups.length; i++)
{
var index = [[theGroups[i] memberIDs] indexOfObject:uid];
if (index !== CPNotFound)
memberships.push({ group: theGroups[i], index: index });
}
return memberships;
}
/*
Puts people back at the places that -placesOfPeople: or -placesOfPeople:inGroup: recorded.
The library and each group take their people back in the order of the recorded indexes, so
every index refers to the array as it was when that person was put back, and each person
returns to its old position.
*/
- (void)restorePlaces:(CPArray)thePlaces
{
var people = [],
memberships = [];
for (var i = 0; i < thePlaces.length; i++)
{
var place = thePlaces[i];
if (place.index !== CPNotFound)
people.push(place);
for (var j = 0; j < place.memberships.length; j++)
memberships.push({ group: place.memberships[j].group, index: place.memberships[j].index, uid: [place.person uid] });
}
var byIndex = function(a, b) { return a.index - b.index; };
people.sort(byIndex);
memberships.sort(byIndex);
for (var i = 0; i < people.length; i++)
[self _insertPerson:people[i].person atIndex:people[i].index];
for (var i = 0; i < memberships.length; i++)
{
var members = [memberships[i].group memberIDs];
if (![members containsObject:memberships[i].uid])
[members insertObject:memberships[i].uid atIndex:MIN(memberships[i].index, [members count])];
}
[self _noteStructuralChange];
}
// ---------------------------------------------------------------- Source item mutation
- (LibraryGroup)addGroupWithName:(CPString)aName people:(CPArray)thePeople
@@ -562,11 +663,16 @@ var LibraryDefaultsKey = @"People.Library.v1",
// ---------------------------------------------------------------- KVO
- (void)_insertPerson:(Person)aPerson
{
[self _insertPerson:aPerson atIndex:[_people count]];
}
- (void)_insertPerson:(Person)aPerson atIndex:(int)anIndex
{
if (_peopleByUID[[aPerson uid]])
return;
_people.push(aPerson);
[_people insertObject:aPerson atIndex:MIN(anIndex, [_people count])];
_peopleByUID[[aPerson uid]] = aPerson;
for (var i = 0; i < ObservedPersonKeys.length; i++)
@@ -13,6 +13,8 @@
* - one -validateUserInterfaceItem: for the menus and the toolbar items
* - CPAlert as a sheet
* - CPPasteboard copy / paste
* - CPUndoManager: the window's undo manager undoes and redoes the removal of people, and
* the Edit menu names the change
*/
@import <AppKit/AppKit.j>
@@ -380,15 +382,7 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
- (void)removeSelectedPeople:(id)sender
{
var people = [_peopleController selectedPeople];
if (![people count])
return;
if ([_selectedItem isKindOfClass:[LibraryGroup class]])
[_library removePeople:people fromGroup:_selectedItem];
else
[_library removePeople:people];
[self _removeSelectedPeopleWithActionName:nil];
}
- (void)delete:(id)sender
@@ -396,6 +390,57 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
[self removeSelectedPeople:sender];
}
/*
Removes the selected people: from the library, or from the group when a group is
selected. aName names the change in the Edit menu; nil gives "Delete", or "Remove from
Group" in a group.
*/
- (void)_removeSelectedPeopleWithActionName:(CPString)aName
{
var people = [_peopleController selectedPeople];
if (![people count])
return;
var group = [_selectedItem isKindOfClass:[LibraryGroup class]] ? _selectedItem : nil;
[self _removePeople:people fromGroup:group actionName:aName || (group ? @"Remove from Group" : @"Delete")];
}
/*
Removes thePeople, and registers the restoration with the window's undo manager. The
library records each person's places first, so an undo puts every person back at its
position in the library and in its groups.
*/
- (void)_removePeople:(CPArray)thePeople fromGroup:(LibraryGroup)aGroup actionName:(CPString)aName
{
var places = aGroup ? [_library placesOfPeople:thePeople inGroup:aGroup] : [_library placesOfPeople:thePeople],
undoManager = [[self window] undoManager];
if (aGroup)
[_library removePeople:thePeople fromGroup:aGroup];
else
[_library removePeople:thePeople];
[[undoManager prepareWithInvocationTarget:self] _restorePlaces:places ofPeople:thePeople fromGroup:aGroup actionName:aName];
[undoManager setActionName:aName];
}
/*
Undoes a removal: puts the people back, selects them, and registers the removal again for
Redo.
*/
- (void)_restorePlaces:(CPArray)thePlaces ofPeople:(CPArray)thePeople fromGroup:(LibraryGroup)aGroup actionName:(CPString)aName
{
var undoManager = [[self window] undoManager];
[_library restorePlaces:thePlaces];
[[_peopleController arrayController] setSelectedObjects:thePeople];
[[undoManager prepareWithInvocationTarget:self] _removePeople:thePeople fromGroup:aGroup actionName:aName];
[undoManager setActionName:aName];
}
- (void)copy:(id)sender
{
[_peopleController copySelectionToPasteboard:[CPPasteboard generalPasteboard]];
@@ -404,7 +449,7 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
- (void)cut:(id)sender
{
[self copy:sender];
[self removeSelectedPeople:sender];
[self _removeSelectedPeopleWithActionName:@"Cut"];
}
- (void)paste:(id)sender
@@ -483,6 +528,8 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
_searchPredicate = nil;
_selectedItem = [_library allPeopleItem];
// The undo actions refer to people that the sample data replaces.
[[[self window] undoManager] removeAllActions];
[_library resetToSampleData];
var toolbar = [[self window] toolbar];
+4
View File
@@ -45,6 +45,9 @@ spaces, because the legacy toolchain does not accept them.
multi-select shows "Multiple Values"; changing Birth Date updates Age (dependent key).
11. Copy (⌘C) then Paste (⌘V): duplicated people appear selected. Delete key removes people
(from the group only, when a group is selected).
Edit ▸ Undo Delete (⌘Z) puts the removed people back at their places in the table and in
their groups, selected; Edit ▸ Redo Delete (⇧⌘Z) removes them again. The menu item names the
change: Delete, Cut, or Remove from Group when a group is selected.
12. The window shows three columns: sidebar, people table, detail pane. Resize the window: the table takes the
change, the sidebar and the detail pane keep their widths. Drag both dividers; the sidebar stays between
160 and 420 points, the detail pane does not become narrower than its fields allow. Reload: both widths and
@@ -100,6 +103,7 @@ to build with a web component framework.
- This exercises CPPopover, CPTrackingArea, a view controller used twice, and bindings and
the first responder in a popover window.
7. A contextual menu on the table offers:
- Undo and Redo, titled with the change, as in the Edit menu.
- Edit: edits the row that was clicked.
- Deselect All.
- Invert Selection.