diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index 68949bcd6..934314707 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -363,9 +363,6 @@ if (!indexes) indexes = [CPIndexSet indexSet]; - if ([_selectionIndexes isEqualToIndexSet:indexes]) - return NO; - if (![indexes count]) { if(_avoidsEmptySelection && [[self arrangedObjects] count]) @@ -381,6 +378,9 @@ indexes = [CPIndexSet indexSetWithIndex:objectsCount-1]; } + if ([_selectionIndexes isEqualToIndexSet:indexes]) + return NO; + _selectionIndexes = [indexes copy]; // Push back the new selection to the model for selectionIndexes if we have one. diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index aa1b2676e..1b9e47160 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -113,6 +113,25 @@ [self assert:[CPIndexSet indexSet] equals:[arrayController selectionIndexes] message:@"no objects left, selection should disappear"]; } +- (void)testSelectionWhenObjectsDisappear +{ + // If the selected object disappeares during a rearrange, the selection + // should update appropriately, even if preserve selection is off. + var arrayController = [self arrayController]; + [arrayController setPreservesSelection:NO]; + + // Use a copy to make sure our original remains pristine. + [arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 3)]]; + + var newContent = [[self contentArray] copy]; + [newContent removeObjectAtIndex:2]; + + [arrayController setContent:newContent]; + + [self assert:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 2)] equals:[arrayController selectionIndexes] + message:@"last object cannot be selected"]; +} + - (void)testContentBinding { [[self arrayController] bind:@"contentArray" toObject:self withKeyPath:@"contentArray" options:0];