Skip to content
2 changes: 1 addition & 1 deletion JNWCollectionView.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "JNWCollectionView"
s.version = "1.2"
s.version = "1.3"
s.summary = "A highly customizable and performant collection view for the Mac."
s.homepage = "https://github.com/jwilling/JNWCollectionView"
s.screenshots = "http://jwilling.com/drop/github/JNWCollectionView.png"
Expand Down
2 changes: 1 addition & 1 deletion JNWCollectionView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@
ABB023E8170791D300537A92 /* Project object */ = {
isa = PBXProject;
attributes = {
LastTestingUpgradeCheck = 0510;
LastTestingUpgradeCheck = 0610;
LastUpgradeCheck = 0500;
ORGANIZATIONNAME = AppJon;
};
Expand Down
15 changes: 15 additions & 0 deletions JNWCollectionView/JNWCollectionViewFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,22 @@ typedef NS_ENUM(NSInteger, JNWCollectionViewScrollPosition) {
/// Tells the delegate that the item at the specified index path has been right-clicked.
- (void)collectionView:(JNWCollectionView *)collectionView didRightClickItemAtIndexPath:(NSIndexPath *)indexPath;

/// Asks the delegate if the item at the specified index path should be scrolled to.
- (BOOL)collectionView:(JNWCollectionView *)collectionView shouldScrollToItemAtIndexPath:(NSIndexPath *)indexPath;

/// Tells the delegate that the specified index path has been scrolled to.
- (void)collectionView:(JNWCollectionView *)collectionView didScrollToItemAtIndexPath:(NSIndexPath *)indexPath;

/// Tells the delegate that the cell for the specified index path has been put
/// back into the reuse queue.
- (void)collectionView:(JNWCollectionView *)collectionView didEndDisplayingCell:(JNWCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;

/// Tells the delegate that supplementary view will be added to document view.
- (void)collectionView:(JNWCollectionView *)collectionView willDisplaySupplementaryView:(JNWCollectionViewReusableView *)supplementaryView ofKind:(NSString *)kind forsection:(NSInteger)section;

/// Tells the delegate that supplementary view was removed from document view.
- (void)collectionView:(JNWCollectionView *)collectionView didEndDisplayingSupplementaryView:(JNWCollectionViewReusableView *)supplementaryView ofKind:(NSString *)kind forsection:(NSInteger)section;

@end

#pragma mark Reloading and customizing
Expand Down Expand Up @@ -235,6 +244,12 @@ typedef NS_ENUM(NSInteger, JNWCollectionViewScrollPosition) {
/// previously registered in -registerClass:forSupplementaryViewOfKind:reuseIdentifier:.
- (JNWCollectionViewReusableView *)supplementaryViewForKind:(NSString *)kind reuseIdentifier:(NSString *)reuseIdentifier inSection:(NSInteger)section;

/// Returns an array of all the currently visible supplementary views. The cells are not guaranteed to be in any order.
- (NSArray *)visibleSupplementaryViews;

/// Returns an array of all the currently visible supplementary views for the specified kind. The cells are not guaranteed to be in any order.
- (NSArray *)visibleSupplementaryViewsForKind:(NSString *)kind;

/// Returns an array of all the currently visible cells. The cells are not guaranteed to be in any order.
- (NSArray *)visibleCells;

Expand Down
54 changes: 51 additions & 3 deletions JNWCollectionView/JNWCollectionViewFramework.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ @interface JNWCollectionView() {
unsigned int delegateDidSelect:1;
unsigned int delegateShouldDeselect:1;
unsigned int delegateDidDeselect:1;
unsigned int delegateShouldScroll:1;
unsigned int delegateDidScroll:1;
unsigned int delegateDidDoubleClick:1;
unsigned int delegateDidRightClick:1;
unsigned int delegateDidEndDisplayingCell:1;
unsigned int delegateWillDisplaySupplementaryView:1;
unsigned int delegateDidEndDisplayingSupplementaryView:1;

unsigned int wantsLayout;
} _collectionViewFlags;
Expand All @@ -74,6 +77,8 @@ @interface JNWCollectionView() {
@property (nonatomic, strong) NSMutableDictionary *supplementaryViewClassMap; // { "kind/identifier" : class }
@property (nonatomic, strong) NSMutableDictionary *supplementaryViewNibMap; // { "kind/identifier" : nib }

@property (nonatomic, strong) NSView *documentView;

@end

@implementation JNWCollectionView
Expand Down Expand Up @@ -138,13 +143,16 @@ - (void)setDelegate:(id<JNWCollectionViewDelegate>)delegate {
_collectionViewFlags.delegateDidDeselect = [delegate respondsToSelector:@selector(collectionView:didDeselectItemAtIndexPath:)];
_collectionViewFlags.delegateDidDoubleClick = [delegate respondsToSelector:@selector(collectionView:didDoubleClickItemAtIndexPath:)];
_collectionViewFlags.delegateDidRightClick = [delegate respondsToSelector:@selector(collectionView:didRightClickItemAtIndexPath:)];
_collectionViewFlags.delegateDidEndDisplayingCell = [delegate respondsToSelector:@selector(collectionView:didEndDisplayingCell:forItemAtIndexPath:)];
_collectionViewFlags.delegateDidEndDisplayingCell = [delegate respondsToSelector:@selector(collectionView:didEndDisplayingCell:forItemAtIndexPath:)];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It seems like there's still a merge conflict. Can you run it through mergetool one more time? For me, the conflict happens right here. Sorry for the trouble!

_collectionViewFlags.delegateShouldScroll = [delegate respondsToSelector:@selector(collectionView:shouldScrollToItemAtIndexPath:)];
_collectionViewFlags.delegateDidScroll = [delegate respondsToSelector:@selector(collectionView:didScrollToItemAtIndexPath:)];
_collectionViewFlags.delegateWillDisplaySupplementaryView = [delegate respondsToSelector:@selector(collectionView:willDisplaySupplementaryView:ofKind:forsection:)];
_collectionViewFlags.delegateDidEndDisplayingSupplementaryView = [delegate respondsToSelector:@selector(collectionView:didEndDisplayingSupplementaryView:ofKind:forsection:)];
}

- (void)setDataSource:(id<JNWCollectionViewDataSource>)dataSource {
_dataSource = dataSource;
_collectionViewFlags.dataSourceNumberOfSections = [dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)];
_collectionViewFlags.delegateDidScroll = [dataSource respondsToSelector:@selector(collectionView:didScrollToItemAtIndexPath:)];
_collectionViewFlags.dataSourceViewForSupplementaryView = [dataSource respondsToSelector:@selector(collectionView:viewForSupplementaryViewOfKind:inSection:)];
NSAssert(dataSource == nil || [dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)],
@"data source must implement collectionView:numberOfItemsInSection");
Expand Down Expand Up @@ -354,6 +362,16 @@ - (void)resetAllCellsAndSupplementaryViews {
}
}
[self.visibleCellsMap removeAllObjects];

if (_collectionViewFlags.delegateDidEndDisplayingSupplementaryView) {
[self.visibleSupplementaryViewsMap enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

NSInteger section = [self sectionForSupplementaryLayoutIdentifier:key];
JNWCollectionViewReusableView *supplementaryView = obj;
[self.delegate collectionView:self didEndDisplayingSupplementaryView:supplementaryView ofKind:supplementaryView.kind forsection:section];

}];
}
[self.visibleSupplementaryViewsMap removeAllObjects];

// Remove any cells or views that might be added to the document view.
Expand Down Expand Up @@ -394,6 +412,23 @@ - (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point {
return nil;
}

- (NSArray *)visibleSupplementaryViews {
return self.visibleSupplementaryViewsMap.allValues;
}

- (NSArray *)visibleSupplementaryViewsForKind:(NSString *)kind {
NSArray *allKeys = self.visibleSupplementaryViewsMap.allKeys;
NSMutableArray *visibleSupplementaryViews = [[NSMutableArray alloc] initWithCapacity:allKeys.count];

for (NSString *key in allKeys) {
NSString *supplementaryViewKind = [self kindForSupplementaryViewIdentifier:key];
if ([kind isEqualToString:supplementaryViewKind]) {
[visibleSupplementaryViews addObject:self.visibleSupplementaryViewsMap[key]];
}
}
return visibleSupplementaryViews;
}

- (NSArray *)visibleCells {
return self.visibleCellsMap.allValues;
}
Expand Down Expand Up @@ -486,6 +521,10 @@ - (NSArray *)indexPathsForVisibleItems {
}

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(JNWCollectionViewScrollPosition)scrollPosition animated:(BOOL)animated {
if (_collectionViewFlags.delegateShouldScroll && ![self.delegate collectionView:self shouldScrollToItemAtIndexPath:indexPath]) {
return;
}

CGRect rect = [self rectForItemAtIndexPath:indexPath];
CGRect visibleRect = self.documentVisibleRect;

Expand Down Expand Up @@ -802,6 +841,10 @@ - (void)layoutSupplementaryViewsWithRedraw:(BOOL)needsVisibleRedraw {
[view removeFromSuperview];

[self enqueueReusableSupplementaryView:view ofKind:view.kind withReuseIdentifier:view.reuseIdentifier];

if (_collectionViewFlags.delegateDidEndDisplayingSupplementaryView) {
[self.delegate collectionView:self didEndDisplayingSupplementaryView:view ofKind:view.kind forsection:[self sectionForSupplementaryLayoutIdentifier:layoutIdentifier]];
}
}

// Add new views
Expand All @@ -817,6 +860,11 @@ - (void)layoutSupplementaryViewsWithRedraw:(BOOL)needsVisibleRedraw {
JNWCollectionViewLayoutAttributes *attributes = [self.collectionViewLayout layoutAttributesForSupplementaryItemInSection:section kind:kind];
view.frame = attributes.frame;
view.alphaValue = attributes.alpha;

if (_collectionViewFlags.delegateWillDisplaySupplementaryView) {
[self.delegate collectionView:self willDisplaySupplementaryView:view ofKind:view.kind forsection:[self sectionForSupplementaryLayoutIdentifier:layoutIdentifier]];
}

[self.documentView addSubview:view];

self.visibleSupplementaryViewsMap[layoutIdentifier] = view;
Expand Down Expand Up @@ -940,7 +988,7 @@ - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath
selectionType:(JNWCollectionViewSelectionType)selectionType {
if (indexPath == nil)
return;
NSMutableSet *indexesToSelect = [NSMutableSet set];

if (selectionType == JNWCollectionViewSelectionTypeSingle) {
Expand Down
14 changes: 0 additions & 14 deletions JNWCollectionView/NSIndexPath+JNWAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,4 @@ - (NSInteger)jnw_item {
return [self indexAtPosition:1];
}

- (BOOL)isEqual:(id)object {
if ([object isKindOfClass:NSIndexPath.class]) {
if (self.jnw_section == [(NSIndexPath *)object jnw_section] && self.jnw_item == [(NSIndexPath *)object jnw_item]) {
return YES;
}
}

return NO;
}

- (NSString *)debugDescription {
return [NSString stringWithFormat:@"<%@: %p; section = %ld; item = %ld>", self.class, self, self.jnw_section, self.jnw_item];
}

@end