Switch to using maptables to track some metadata bout items so that we can match items up when we need to find them by indexPath or point. Remove uneeded ivar from layout. Add call to delegate when selecting item

This commit is contained in:
Gregory John Casamento 2022-09-20 05:02:10 -04:00
parent bf3ddc8cad
commit 2bec53bf0f
4 changed files with 27 additions and 12 deletions

View file

@ -443,12 +443,12 @@ APPKIT_EXPORT_CLASS
NSMutableSet *_indexPathsForVisibleItems;
NSMutableDictionary *_visibleSupplementaryViews;
NSMutableSet *_indexPathsForSupplementaryElementsOfKind;
NSMutableDictionary *_itemsToAttributes;
// Private
// Map items -> indexPath
NSMapTable *_itemsToIndexPaths;
NSMapTable *_indexPathsToItems;
NSMapTable *_itemsToAttributes;
// Registered class/nib for item identifier
NSMapTable *_registeredNibs;

View file

@ -162,7 +162,6 @@ typedef NSInteger NSCollectionUpdateAction;
{
NSCollectionView *_collectionView; // weak
BOOL _valid;
NSMutableDictionary *_itemsToAttributes;
Class _layoutAttributesClass;
Class _invalidationContextClass;

View file

@ -162,9 +162,9 @@ static NSString *placeholderItem = nil;
_indexPathsForVisibleItems = [[NSMutableSet alloc] init];
_visibleSupplementaryViews = [[NSMutableDictionary alloc] init];
_indexPathsForSupplementaryElementsOfKind = [[NSMutableSet alloc] init];
_itemsToAttributes = [[NSMutableDictionary alloc] init];
_itemsToIndexPaths = RETAIN([NSMapTable weakToWeakObjectsMapTable]);
_indexPathsToItems = RETAIN([NSMapTable weakToWeakObjectsMapTable]);
_itemsToAttributes = RETAIN([NSMapTable strongToStrongObjectsMapTable]);
_itemsToIndexPaths = RETAIN([NSMapTable strongToStrongObjectsMapTable]);
_indexPathsToItems = RETAIN([NSMapTable strongToStrongObjectsMapTable]);
// Registered nib/class
_registeredNibs = RETAIN([NSMapTable weakToStrongObjectsMapTable]);
@ -1418,9 +1418,25 @@ static NSString *placeholderItem = nil;
- (NSIndexPath *) indexPathForItemAtPoint: (NSPoint)point
{
NSInteger row = floor(point.y / (_itemSize.height + _verticalMargin));
NSInteger column = floor(point.x / (_itemSize.width + _horizontalMargin));
NSIndexPath *p = [NSIndexPath indexPathForRow: row inSection: column];
NSIndexPath *p = nil;
NSEnumerator *ke = [_itemsToAttributes keyEnumerator];
NSCollectionViewItem *item = nil;
while ((item = [ke nextObject]) != nil)
{
NSCollectionViewLayoutAttributes *attr = [_itemsToAttributes objectForKey: item];
if (attr != nil)
{
NSRect f = [attr frame];
if (NSPointInRect(point, f))
{
p = [self indexPathForItem: item];
break;
}
}
}
return p;
}

View file

@ -28,11 +28,11 @@
#import "GSFastEnumeration.h"
@interface NSCollectionView (__NSCollectionViewLayout__)
- (NSDictionary *) itemsToAttributes;
- (NSMapTable *) itemsToAttributes;
@end
@implementation NSCollectionView (__NSCollectionViewLayout__)
- (NSDictionary *) itemsToAttributes
- (NSMapTable *) itemsToAttributes
{
return _itemsToAttributes;
}
@ -249,7 +249,7 @@
// Initializers
- (void) _initDefaults
{
_itemsToAttributes = [[NSMutableDictionary alloc] init];
// _itemsToAttributes = [[NSMutableDictionary alloc] init];
}
- (void)invalidateLayout
@ -311,7 +311,7 @@
{
NSMutableArray *result = [NSMutableArray array];
NSArray *items = [_collectionView visibleItems];
NSDictionary *itemsToAttributes = [_collectionView itemsToAttributes];
NSMapTable *itemsToAttributes = [_collectionView itemsToAttributes];
FOR_IN(NSCollectionViewItem*, i, items)
{