Deleting last item in pasteboard history browser is now a no-op

Unfortunately disabling the “Delete” button is cumbersome because our history is stored with CoreData so knowing the number of items in the history requires a fetch request, which we would not know when to re-run (to update the disabled state when the history is changed).

When the filter string is empty we can use the array controller’s results, but I’m leaving that for the future.
This commit is contained in:
Allan Odgaard
2016-06-18 11:23:37 +02:00
parent 705fc6e0e9
commit 5d9ad561dc

View File

@@ -323,14 +323,32 @@ static NSMutableDictionary* SharedChoosers;
}
}
- (NSArray*)selectedEntriesPreservingOne
{
NSFetchRequest* request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"PasteboardEntry" inManagedObjectContext:_pasteboard.managedObjectContext];
request.predicate = [NSPredicate predicateWithFormat:@"pasteboard == %@", _pasteboard];
NSUInteger countOfAllEntries = [_pasteboard.managedObjectContext countForFetchRequest:request error:nullptr];
NSArray* entries = [_arrayController selectedObjects];
if([entries count] == countOfAllEntries)
{
NSMutableArray* tmp = [entries mutableCopy];
[tmp removeObject:_pasteboard.currentEntry];
entries = tmp;
}
return entries;
}
- (void)deleteForward:(id)sender
{
[_arrayController remove:sender];
[_arrayController removeObjects:[self selectedEntriesPreservingOne]];
}
- (void)deleteBackward:(id)sender
{
NSArray* entries = [_arrayController selectedObjects];
NSArray* entries = [self selectedEntriesPreservingOne];
[_arrayController selectPrevious:self];
[_arrayController removeObjects:entries];
}