Merge pull request #2446 from cacaodev/CPCollectionView-fixes

CPCollectionView content binding & minor code refactor
This commit is contained in:
Alexandre Wilhelm committed 2016-05-08 11:16:34 -07:00
commit f67fafdb8e
2 files changed
+84 -23

No files matched your search

+41 -23
View File
@@ -132,7 +132,6 @@ var HORIZONTAL_MARGIN = 2;
CGSize _storedFrameSize;
BOOL _uniformSubviewsResizing @accessors(property=uniformSubviewsResizing);
BOOL _lockResizing;
CPInteger _currentDropIndex;
CPDragOperation _currentDragOperation;
@@ -140,6 +139,14 @@ var HORIZONTAL_MARGIN = 2;
_CPCollectionViewDropIndicator _dropView;
}
+ (Class)_binderClassForBinding:(CPString)aBinding
{
if (aBinding == CPContentBinding)
return [_CPCollectionViewContentBinder class];
return [super _binderClassForBinding:aBinding];
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
@@ -182,7 +189,7 @@ var HORIZONTAL_MARGIN = 2;
_needsMinMaxItemSizeUpdate = YES;
_uniformSubviewsResizing = NO;
_lockResizing = NO;
_inLiveResize = NO;
_currentDropIndex = -1;
_currentDragOperation = CPDragOperationNone;
@@ -384,14 +391,7 @@ var HORIZONTAL_MARGIN = 2;
_isSelectable = isSelectable;
if (!_isSelectable)
{
var index = CPNotFound,
itemCount = [_items count];
// Be wary of invalid selection ranges since setContent: does not clear selection indexes.
while ((index = [_selectionIndexes indexGreaterThanIndex:index]) != CPNotFound && index < itemCount)
[_items[index] setSelected:NO];
}
[self _applySelectionToItems:NO];
}
/*!
@@ -445,22 +445,15 @@ var HORIZONTAL_MARGIN = 2;
{
if (!anIndexSet)
anIndexSet = [CPIndexSet indexSet];
if (!_isSelectable || [_selectionIndexes isEqual:anIndexSet])
return;
var index = CPNotFound,
itemCount = [_items count];
// Be wary of invalid selection ranges since setContent: does not clear selection indexes.
while ((index = [_selectionIndexes indexGreaterThanIndex:index]) !== CPNotFound && index < itemCount)
[_items[index] setSelected:NO];
[self _applySelectionToItems:NO];
_selectionIndexes = anIndexSet;
var index = CPNotFound;
while ((index = [_selectionIndexes indexGreaterThanIndex:index]) !== CPNotFound)
[_items[index] setSelected:YES];
[self _applySelectionToItems:YES];
var binderClass = [[self class] _binderClassForBinding:@"selectionIndexes"];
[[binderClass getBinding:@"selectionIndexes" forObject:self] reverseSetValueFor:@"selectionIndexes"];
@@ -525,14 +518,14 @@ var HORIZONTAL_MARGIN = 2;
- (void)resizeWithOldSuperviewSize:(CGSize)oldBoundsSize
{
if (_lockResizing)
if (_inLiveResize)
return;
_lockResizing = YES;
_inLiveResize = YES;
[self tile];
_lockResizing = NO;
_inLiveResize = NO;
}
- (void)tile
@@ -972,6 +965,20 @@ var HORIZONTAL_MARGIN = 2;
return frame;
}
- (void)_applySelectionToItems:(BOOL)select
{
var numberOfItems = [_items count];
[_selectionIndexes enumerateIndexesUsingBlock:function(idx, stop)
{
if (idx < numberOfItems)
[[_items objectAtIndex:idx] setSelected:select];
else {
stop(YES);
}
}];
}
@end
@implementation CPCollectionView (DragAndDrop)
@@ -1586,3 +1593,14 @@ var CPCollectionViewMinItemSizeKey = @"CPCollectionViewMinItemSizeK
}
@end
@implementation _CPCollectionViewContentBinder : CPBinder
{
}
- (void)setValue:(id)aValue forBinding:(CPString)aBinding
{
[_source setContent:aValue];
}
@end
+43
View File
@@ -62,6 +62,49 @@
[self assert:[CPIndexSet indexSet] equals:[_collectionView selectionIndexes]];
}
- (void)_testCollectionViewItemIsSelected
{
var itemPrototype = [[CPCollectionViewItem alloc] init];
[_collectionView setItemPrototype:itemPrototype];
[_collectionView setContent:[1, 2, 3]];
[_collectionView setSelectionIndexes:[CPIndexSet indexSetWithIndex:1]];
[self assertTrue:[[_collectionView itemAtIndex:1] isSelected]];
[_collectionView setSelectionIndexes:[CPIndexSet indexSetWithIndex:0]];
[self assertTrue:[[_collectionView itemAtIndex:0] isSelected]];
// The item is correctly deselected when the selection indexes changes.
[self assertFalse:[[_collectionView itemAtIndex:1] isSelected]];
}
- (void)testBindingSupport
{
var content = [1,2,3];
var ac = [[CPArrayController alloc] initWithContent:content];
[ac setSelectsInsertedObjects:YES];
[_collectionView bind:CPContentBinding toObject:ac withKeyPath:@"arrangedObjects" options:nil];
[_collectionView bind:CPSelectionIndexesBinding toObject:ac withKeyPath:@"selectionIndexes" options:nil];
[self assert:[1,2,3] equals:[_collectionView content]];
[ac setContent:[6,7]];
[self assert:[6,7] equals:[_collectionView content]];
[ac setSelectionIndexes:[CPIndexSet indexSetWithIndex:1]];
[self assert:[CPIndexSet indexSetWithIndex:1] equals:[_collectionView selectionIndexes]];
[ac insertObject:4 atArrangedObjectIndex:2];
[self assert:[CPIndexSet indexSetWithIndex:2] equals:[_collectionView selectionIndexes]];
// collection view selection is reflected on the array controller selection
[_collectionView setSelectionIndexes:[CPIndexSet indexSetWithIndex:0]];
[self assert:[CPIndexSet indexSetWithIndex:0] equals:[ac selectionIndexes]];
// collection view content is reflected on the array controller content
//[_collectionView setContent:[8,9]];
//[self assert:[8,9] equals:[ac content]];
}
- (void)testSetContentAndSelectionIndexes
{
// Changing the content does not automatically clear the selection indexes. The previous