diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index f8486c4b8..dbd318da7 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -564,7 +564,7 @@ - (BOOL)setSelectionIndexes:(CPIndexSet)indexes { [self _selectionWillChange] - var r = [self __setSelectionIndexes:indexes]; + var r = [self __setSelectionIndexes:indexes avoidEmpty:NO]; [self _selectionDidChange]; return r; } @@ -583,6 +583,11 @@ @ignore */ - (BOOL)__setSelectionIndexes:(CPIndexSet)indexes +{ + [self __setSelectionIndexes:indexes avoidEmpty:_avoidsEmptySelection]; +} + +- (BOOL)__setSelectionIndexes:(CPIndexSet)indexes avoidEmpty:(BOOL)avoidEmpty { var newIndexes = indexes; @@ -591,7 +596,7 @@ if (![newIndexes count]) { - if (_avoidsEmptySelection && [[self arrangedObjects] count]) + if (avoidEmpty && [[self arrangedObjects] count]) newIndexes = [CPIndexSet indexSetWithIndex:0]; } else @@ -606,7 +611,7 @@ // Remove out of bounds indexes. [newIndexes removeIndexesInRange:CPMakeRange(objectsCount, [newIndexes lastIndex] + 1)]; // When avoiding empty selection and the deleted selection was at the bottom, select the last item. - if (![newIndexes count] && _avoidsEmptySelection && objectsCount) + if (![newIndexes count] && avoidEmpty && objectsCount) newIndexes = [CPIndexSet indexSetWithIndex:objectsCount - 1]; } @@ -647,7 +652,7 @@ [self willChangeValueForKey:@"selectionIndexes"]; [self _selectionWillChange]; - var r = [self __setSelectedObjects:objects]; + var r = [self __setSelectedObjects:objects avoidEmpty:NO]; [self didChangeValueForKey:@"selectionIndexes"]; [self _selectionDidChange]; @@ -659,6 +664,11 @@ @ignore */ - (BOOL)__setSelectedObjects:(CPArray)objects +{ + [self __setSelectedObjects:objects avoidEmpty:_avoidsEmptySelection]; +} + +- (BOOL)__setSelectedObjects:(CPArray)objects avoidEmpty:(BOOL)avoidEmpty { var set = [CPIndexSet indexSet], count = [objects count], @@ -672,7 +682,7 @@ [set addIndex:index]; } - [self __setSelectionIndexes:set]; + [self __setSelectionIndexes:set avoidEmpty:avoidEmpty]; return YES; } diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index 05e75a978..bef0a7f95 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -201,12 +201,32 @@ [arrayController setPreservesSelection:NO]; [arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(1, 2)]]; - [arrayController removeObjects:[arrayController selectedObjects]] + [arrayController removeObjects:[arrayController selectedObjects]]; [self assert:[CPIndexSet indexSet] equals:[arrayController selectionIndexes] message:@"selection should be empty if arraycontroller doesn't avoid empty selection"]; } +- (void)testRemoveObjectWithAvoidingEmptySelection +{ + var arrayController = [self arrayController]; + [arrayController setAvoidsEmptySelection:YES]; + + [arrayController setSelectionIndex:2]; + [arrayController removeObjectsAtArrangedObjectIndexes:[CPIndexSet indexSetWithIndex:2]]; + + [self assertTrue:([[arrayController selectionIndexes] count] > 0) message:@"Selection should not empty when arraycontroller avoids empty selection"]; + + [arrayController setContent:[self contentArray]]; + [arrayController setSelectionIndex:2]; + [arrayController removeObjects:[arrayController selectedObjects]]; + + [self assertTrue:([[arrayController selectionIndexes] count] > 0) message:@"Selection should not empty when arraycontroller avoids empty selection"]; + +// This will fail, currently we select the first index instead of an adjacent index. +// [self assertTrue:([arrayController selectionIndex] == 1) message:@"The selected index should be 1, was " + [arrayController selectionIndex]]; +} + - (void)testRemoveObjectsWithPreservesSelection_SimpleArray { [self initControllerWithSimpleArray]; @@ -287,6 +307,26 @@ [self assert:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 2)] equals:[arrayController selectionIndexes] message:@"last object cannot be selected"]; } +- (void)testSelectingEmptyIndexesExplicitlyWithAvoidsEmptySelection +{ + var arrayController = [self arrayController]; + [arrayController setAvoidsEmptySelection:YES]; + + [arrayController setSelectionIndex:0]; + [arrayController setSelectionIndexes:[CPIndexSet indexSet]]; + [self assertTrue:([[arrayController selectionIndexes] count] == 0) message:@"Selection should be empty when unselecting explicitly, even with avoidsEmptySelection"]; +} + +- (void)testSelectingEmptyObjectsExplicitlyWithAvoidsEmptySelection +{ + var arrayController = [self arrayController]; + [arrayController setAvoidsEmptySelection:YES]; + + [arrayController setSelectionIndex:0]; + [arrayController setSelectedObjects:[CPArray array]]; + [self assertTrue:([[arrayController selectionIndexes] count] == 0) message:@"Selection should be empty when unselecting explicitly, even with avoidsEmptySelection"]; +} + - (void)testContentBinding { [[self arrayController] bind:@"contentArray" toObject:self withKeyPath:@"contentArray" options:0];