forked from sketch-hq/BCCollectionView
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBCCollectionView.m
667 lines (565 loc) · 23.2 KB
/
BCCollectionView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// Created by Pieter Omvlee on 24/11/2010.
// Copyright 2010 Bohemian Coding. All rights reserved.
#import "BCCollectionView.h"
#import "BCGeometryExtensions.h"
#import "BCCollectionViewLayoutManager.h"
#import "BCCollectionViewLayoutItem.h"
#import "BCCollectionViewGroup.h"
@interface BCCollectionView ()
- (void)configureView;
@end
@implementation BCCollectionView
@synthesize delegate, contentArray, groups, backgroundColor, originalSelectionIndexes, zoomValueObserverKey, accumulatedKeyStrokes, numberOfPreRenderedRows, layoutManager;
@dynamic visibleViewControllerArray;
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
[self configureView];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self configureView];
}
return self;
}
- (void)configureView
{
reusableViewControllers = [[NSMutableArray alloc] init];
visibleViewControllers = [[NSMutableDictionary alloc] init];
contentArray = [[NSArray alloc] init];
selectionIndexes = [[NSMutableIndexSet alloc] init];
dragHoverIndex = NSNotFound;
accumulatedKeyStrokes = [[NSString alloc] init];
numberOfPreRenderedRows = 3;
layoutManager = [[BCCollectionViewLayoutManager alloc] initWithCollectionView:self];
visibleGroupViewControllers = [[NSMutableDictionary alloc] init];
[self addObserver:self forKeyPath:@"backgroundColor" options:0 context:NULL];
NSClipView *enclosingClipView = [[self enclosingScrollView] contentView];
[enclosingClipView setPostsBoundsChangedNotifications:YES];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(scrollViewDidScroll:) name:NSViewBoundsDidChangeNotification object:enclosingClipView];
[center addObserver:self selector:@selector(viewDidResize) name:NSViewFrameDidChangeNotification object:self];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"backgroundColor"])
[self setNeedsDisplay:YES];
else if ([keyPath isEqual:zoomValueObserverKey]) {
if ([self respondsToSelector:@selector(zoomValueDidChange)])
[self performSelector:@selector(zoomValueDidChange)];
} else if ([keyPath isEqualToString:@"isCollapsed"]) {
[self softReloadDataWithCompletionBlock:^{
[self performSelector:@selector(scrollViewDidScroll:)];
}];
} else
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void)dealloc
{
[self removeObserver:self forKeyPath:@"backgroundColor"];
if (zoomValueObserverKey != nil)
[[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:zoomValueObserverKey];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:NSViewBoundsDidChangeNotification object:[[self enclosingScrollView] contentView]];
[center removeObserver:self name:NSViewFrameDidChangeNotification object:self];
for (BCCollectionViewGroup *group in groups)
[group removeObserver:self forKeyPath:@"isCollapsed"];
}
- (BOOL)isFlipped
{
return YES;
}
#pragma mark Drawing Selections
- (BOOL)shoulDrawSelections
{
if ([delegate respondsToSelector:@selector(collectionViewShouldDrawSelections:)])
return [delegate collectionViewShouldDrawSelections:self];
else
return YES;
}
- (BOOL)shoulDrawHover
{
if ([delegate respondsToSelector:@selector(collectionViewShouldDrawHover:)])
return [delegate collectionViewShouldDrawHover:self];
else
return YES;
}
- (BOOL)shouldDrawSelectionRect
{
if ([delegate respondsToSelector:@selector(collectionViewShouldDrawSelectionRect:)])
return [delegate collectionViewShouldDrawSelectionRect:self];
else
return YES;
}
- (void)drawItemSelectionForInRect:(NSRect)aRect
{
NSRect insetRect = NSInsetRect(aRect, 10, 10);
if ([self needsToDrawRect:insetRect]) {
[[NSColor lightGrayColor] set];
[[NSBezierPath bezierPathWithRoundedRect:insetRect xRadius:10 yRadius:10] fill];
}
}
- (void)drawRect:(NSRect)dirtyRect
{
[backgroundColor ? backgroundColor : [NSColor whiteColor] set];
if ([backgroundColor patternImage]) {
// when filling with pattern, we want to keep the pattern anchored at top-left
[NSGraphicsContext saveGraphicsState];
CGFloat yOffset = NSMaxY([self convertRect:self.bounds toView:nil]);
CGFloat xOffset = NSMinX([self convertRect:self.bounds toView:nil]);
[[NSGraphicsContext currentContext] setPatternPhase:NSMakePoint(xOffset, yOffset)];
NSRectFill(dirtyRect);
[NSGraphicsContext restoreGraphicsState];
}
else
NSRectFill(dirtyRect);
if ([self shouldDrawSelectionRect]) {
[[NSColor grayColor] set];
NSFrameRect(BCRectFromTwoPoints(mouseDownLocation, mouseDraggedLocation));
}
if ([selectionIndexes count] > 0 && [self shoulDrawSelections]) {
for (NSNumber *number in visibleViewControllers)
if ([selectionIndexes containsIndex:[number integerValue]])
[self drawItemSelectionForInRect:[[[visibleViewControllers objectForKey:number] view] frame]];
}
if (dragHoverIndex != NSNotFound && [self shoulDrawHover])
[self drawItemSelectionForInRect:[[[visibleViewControllers objectForKey:[NSNumber numberWithInteger:dragHoverIndex]] view] frame]];
}
#pragma mark Delegate Call Wrappers
- (void)delegateUpdateSelectionForItemAtIndex:(NSUInteger)index
{
if ([delegate respondsToSelector:@selector(collectionView:updateViewControllerAsSelected:forItem:)])
[delegate collectionView:self updateViewControllerAsSelected:[self viewControllerForItemAtIndex:index]
forItem:[contentArray objectAtIndex:index]];
}
- (void)delegateUpdateDeselectionForItemAtIndex:(NSUInteger)index
{
if ([delegate respondsToSelector:@selector(collectionView:updateViewControllerAsDeselected:forItem:)])
[delegate collectionView:self updateViewControllerAsDeselected:[self viewControllerForItemAtIndex:index]
forItem:[contentArray objectAtIndex:index]];
}
- (void)delegateCollectionViewSelectionDidChange
{
if (!selectionChangedDisabled && [delegate respondsToSelector:@selector(collectionViewSelectionDidChange:)]) {
[[NSRunLoop currentRunLoop] cancelPerformSelector:@selector(collectionViewSelectionDidChange:) target:delegate argument:self];
[(id)delegate performSelector:@selector(collectionViewSelectionDidChange:) withObject:self afterDelay:0.0];
}
}
- (void)delegateDidSelectItemAtIndex:(NSUInteger)index
{
if ([delegate respondsToSelector:@selector(collectionView:didSelectItem:withViewController:)])
[delegate collectionView:self
didSelectItem:[contentArray objectAtIndex:index]
withViewController:[self viewControllerForItemAtIndex:index]];
}
- (void)delegateDidDeselectItemAtIndex:(NSUInteger)index
{
if ([delegate respondsToSelector:@selector(collectionView:didDeselectItem:withViewController:)])
[delegate collectionView:self
didDeselectItem:[contentArray objectAtIndex:index]
withViewController:[self viewControllerForItemAtIndex:index]];
}
- (void)delegateViewControllerBecameInvisibleAtIndex:(NSUInteger)index
{
if ([delegate respondsToSelector:@selector(collectionView:viewControllerBecameInvisible:)])
[delegate collectionView:self viewControllerBecameInvisible:[self viewControllerForItemAtIndex:index]];
}
- (NSSize)cellSize
{
return [delegate cellSizeForCollectionView:self];
}
- (NSUInteger)groupHeaderHeight
{
return [delegate groupHeaderHeightForCollectionView:self];
}
- (NSIndexSet *)indexesOfItemsInRect:(NSRect)aRect
{
NSArray *itemLayouts = [layoutManager itemLayouts];
NSIndexSet *visibleIndexes = [itemLayouts indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id itemLayout, NSUInteger idx, BOOL *stop) {
return NSIntersectsRect([itemLayout itemRect], aRect);
}];
return visibleIndexes;
}
- (NSIndexSet *)indexesOfItemContentRectsInRect:(NSRect)aRect
{
NSArray *itemLayouts = [layoutManager itemLayouts];
NSIndexSet *visibleIndexes = [itemLayouts indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id itemLayout, NSUInteger idx, BOOL *stop) {
return NSIntersectsRect([itemLayout itemRect], aRect);
}];
return visibleIndexes;
}
- (NSRange)rangeOfVisibleItems
{
NSIndexSet *visibleIndexes = [self indexesOfItemsInRect:[self visibleRect]];
return NSMakeRange([visibleIndexes firstIndex], [visibleIndexes lastIndex]-[visibleIndexes firstIndex]);
}
- (NSRange)rangeOfVisibleItemsWithOverflow
{
NSRange range = [self rangeOfVisibleItems];
NSInteger extraItems = [layoutManager maximumNumberOfItemsPerRow] * numberOfPreRenderedRows;
NSInteger min = range.location;
NSUInteger max = range.location + range.length;
min = MAX(0, min-extraItems);
max = MIN([contentArray count], max+extraItems);
return NSMakeRange(min, max-min);
}
#pragma mark Querying ViewControllers
- (NSIndexSet *)indexesOfViewControllers
{
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
for (NSNumber *number in [visibleViewControllers allKeys])
[set addIndex:[number integerValue]];
return set;
}
- (NSArray *)visibleViewControllerArray
{
return [visibleViewControllers allValues];
}
- (NSIndexSet *)indexesOfInvisibleViewControllers
{
NSRange visibleRange = [self rangeOfVisibleItemsWithOverflow];
return [[self indexesOfViewControllers] indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
return !NSLocationInRange(idx, visibleRange);
}];
}
- (NSViewController *)viewControllerForItemAtIndex:(NSUInteger)index
{
return [visibleViewControllers objectForKey:[NSNumber numberWithInteger:index]];
}
- (NSIndexSet*)indexesOfMissingViewControllers
{
return [[NSIndexSet indexSetWithIndexesInRange:[self rangeOfVisibleItemsWithOverflow]] indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
return (![visibleViewControllers objectForKey:[NSNumber numberWithInteger:idx]]);
}];
}
#pragma mark Swapping ViewControllers in and out
- (void)removeViewControllerForItemAtIndex:(NSUInteger)anIndex
{
// NSLog(@"removing view controller at index %lu", anIndex);
NSNumber *key = [NSNumber numberWithInteger:anIndex];
NSViewController *viewController = [visibleViewControllers objectForKey:key];
[[viewController view] removeFromSuperview];
[self delegateUpdateDeselectionForItemAtIndex:anIndex];
[self delegateViewControllerBecameInvisibleAtIndex:anIndex];
[reusableViewControllers addObject:viewController];
[visibleViewControllers removeObjectForKey:key];
}
- (void)removeInvisibleViewControllers
{
// NSLog(@"removeInvisibleViewControllers dispatch");
[[self indexesOfInvisibleViewControllers] enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[self removeViewControllerForItemAtIndex:idx];
}];
}
- (NSViewController *)emptyViewControllerForInsertion
{
if ([reusableViewControllers count] > 0) {
NSViewController *viewController = [reusableViewControllers lastObject];
[reusableViewControllers removeLastObject];
return viewController;
} else
return [delegate reusableViewControllerForCollectionView:self];
}
- (void)addMissingViewControllerForItemAtIndex:(NSUInteger)anIndex withFrame:(NSRect)aRect
{
if (anIndex < [contentArray count]) {
NSViewController *viewController = [self emptyViewControllerForInsertion];
if (viewController != nil) {
// NSLog(@"adding view controller at index %lu", anIndex);
[visibleViewControllers setObject:viewController forKey:[NSNumber numberWithInteger:anIndex]];
[[viewController view] setFrame:aRect];
[[viewController view] setAutoresizingMask:NSViewMaxXMargin | NSViewMaxYMargin];
id itemToLoad = [contentArray objectAtIndex:anIndex];
[delegate collectionView:self willShowViewController:viewController forItem:itemToLoad atIndex:anIndex];
[self addSubview:[viewController view]];
if ([selectionIndexes containsIndex:anIndex])
[self delegateUpdateSelectionForItemAtIndex:anIndex];
}
}
}
- (void)addMissingGroupHeaders
{
if ([groups count] > 0) {
[groups enumerateObjectsUsingBlock:^(id group, NSUInteger idx, BOOL *stop) {
NSRect groupRect = NSMakeRect(0, NSMinY([layoutManager rectOfItemAtIndex:[group itemRange].location])-[self groupHeaderHeight], NSWidth([self visibleRect]), [self groupHeaderHeight]);
if (idx == 0 && ![group isCollapsed] && [delegate respondsToSelector:@selector(topOffsetForItemsInCollectionView:)])
groupRect.origin.y -= [delegate topOffsetForItemsInCollectionView:self];
BOOL groupShouldBeVisible = NSIntersectsRect(groupRect, [self visibleRect]);
NSViewController *groupViewController = [visibleGroupViewControllers objectForKey:[NSNumber numberWithInteger:idx]];
[[groupViewController view] setFrame:groupRect];
if (groupShouldBeVisible && !groupViewController) {
groupViewController = [delegate collectionView:self headerForGroup:group];
[self addSubview:[groupViewController view]];
[visibleGroupViewControllers setObject:groupViewController forKey:[NSNumber numberWithInteger:idx]];
[[groupViewController view] setFrame:groupRect];
} else if (!groupShouldBeVisible && groupViewController) {
[[groupViewController view] removeFromSuperview];
[visibleGroupViewControllers removeObjectForKey:[NSNumber numberWithInteger:idx]];
}
}];
}
}
- (void)addMissingViewControllersToView
{
dispatch_async(dispatch_get_main_queue(), ^{
// NSLog(@"addMisingViewControllersToView dispatch");
[[NSIndexSet indexSetWithIndexesInRange:[self rangeOfVisibleItemsWithOverflow]] enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
if (![visibleViewControllers objectForKey:[NSNumber numberWithInteger:idx]]) {
[self addMissingViewControllerForItemAtIndex:idx withFrame:[layoutManager rectOfItemAtIndex:idx]];
}
}];
[self addMissingGroupHeaders];
});
}
- (void)moveViewControllersToProperPosition
{
for (NSNumber *number in visibleViewControllers) {
NSRect r = [layoutManager rectOfItemAtIndex:[number integerValue]];
if (!NSEqualRects(r, NSZeroRect))
[[[visibleViewControllers objectForKey:number] view] setFrame:r];
}
}
#pragma mark Selecting and Deselecting Items
- (void)selectItemAtIndex:(NSUInteger)index
{
[self selectItemAtIndex:index inBulk:NO];
}
- (void)selectItemAtIndex:(NSUInteger)index inBulk:(BOOL)bulkSelecting
{
if (index >= [contentArray count])
return;
BOOL maySelectItem = YES;
NSViewController *viewController = [self viewControllerForItemAtIndex:index];
id item = [contentArray objectAtIndex:index];
if ([delegate respondsToSelector:@selector(collectionView:shouldSelectItem:withViewController:)])
maySelectItem = [delegate collectionView:self shouldSelectItem:item withViewController:viewController];
if (maySelectItem) {
[selectionIndexes addIndex:index];
[self delegateUpdateSelectionForItemAtIndex:index];
[self delegateDidSelectItemAtIndex:index];
if (!bulkSelecting)
[self delegateCollectionViewSelectionDidChange];
if ([self shoulDrawSelections])
[self setNeedsDisplayInRect:[layoutManager rectOfItemAtIndex:index]];
}
if (!bulkSelecting)
lastSelectionIndex = index;
}
- (void)selectItemsAtIndexes:(NSIndexSet *)indexes
{
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[self selectItemAtIndex:idx inBulk:YES];
}];
lastSelectionIndex = [indexes firstIndex];
[self delegateCollectionViewSelectionDidChange];
}
- (void)deselectItemAtIndex:(NSUInteger)index
{
[self deselectItemAtIndex:index inBulk:NO];
}
- (void)deselectItemAtIndex:(NSUInteger)index inBulk:(BOOL)bulkDeselecting
{
if (index < [contentArray count]) {
[selectionIndexes removeIndex:index];
if ([self shoulDrawSelections])
[self setNeedsDisplayInRect:[layoutManager rectOfItemAtIndex:index]];
if (!bulkDeselecting)
[self delegateCollectionViewSelectionDidChange];
[self delegateDidDeselectItemAtIndex:index];
[self delegateUpdateDeselectionForItemAtIndex:index];
}
}
- (void)deselectItemsAtIndexes:(NSIndexSet *)indexes
{
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[self deselectItemAtIndex:idx inBulk:YES];
}];
[self delegateCollectionViewSelectionDidChange];
}
- (void)deselectAllItems
{
[self deselectItemsAtIndexes:[selectionIndexes copy]];
}
- (NSIndexSet *)selectionIndexes
{
return selectionIndexes;
}
- (void)selectAll:(id)sender
{
[self selectItemsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[contentArray count])]];
}
#pragma mark User-interaction
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)canBecomeKeyView
{
return YES;
}
#pragma mark Reloading and Updating the Icon View
- (void)softReloadVisibleViewControllers
{
NSMutableArray *removeKeys = [NSMutableArray array];
for (NSString *number in visibleViewControllers) {
NSUInteger index = [number integerValue];
NSViewController *controller = [visibleViewControllers objectForKey:number];
if (index < [contentArray count]) {
if ([selectionIndexes containsIndex:index])
[self delegateUpdateDeselectionForItemAtIndex:index];
[delegate collectionView:self willShowViewController:controller forItem:[contentArray objectAtIndex:index] atIndex:index];
} else {
if ([selectionIndexes containsIndex:index])
[self delegateUpdateDeselectionForItemAtIndex:index];
[self delegateViewControllerBecameInvisibleAtIndex:index];
[[controller view] removeFromSuperview];
[reusableViewControllers addObject:controller];
[removeKeys addObject:number];
}
}
[visibleViewControllers removeObjectsForKeys:removeKeys];
}
- (void)resizeFrameToFitContents
{
NSRect frame = [self frame];
frame.size.height = [self visibleRect].size.height;
if ([contentArray count] > 0) {
BCCollectionViewLayoutItem *layoutItem = [[layoutManager itemLayouts] lastObject];
frame.size.height = MAX(frame.size.height, NSMaxY([layoutItem itemRect]));
}
[self setFrame:frame];
}
- (void)reloadDataWithItems:(NSArray *)newContent emptyCaches:(BOOL)shouldEmptyCaches
{
[self reloadDataWithItems:newContent groups:nil emptyCaches:shouldEmptyCaches];
}
- (void)reloadDataWithItems:(NSArray *)newContent groups:(NSArray *)newGroups emptyCaches:(BOOL)shouldEmptyCaches
{
[self reloadDataWithItems:newContent groups:newGroups emptyCaches:shouldEmptyCaches completionBlock:^{}];
}
- (void)reloadDataWithItems:(NSArray *)newContent groups:(NSArray *)newGroups emptyCaches:(BOOL)shouldEmptyCaches completionBlock:(dispatch_block_t)completionBlock
{
[self deselectAllItems];
[layoutManager cancelItemEnumerator];
if (!delegate)
return;
NSSize cellSize = [delegate cellSizeForCollectionView:self];
if (NSWidth([self frame]) < cellSize.width || NSHeight([self frame]) < cellSize.height)
return;
for (BCCollectionViewGroup *group in groups)
[group removeObserver:self forKeyPath:@"isCollapsed"];
for (BCCollectionViewGroup *group in newGroups)
[group addObserver:self forKeyPath:@"isCollapsed" options:0 context:NULL];
self.groups = newGroups;
self.contentArray = newContent;
for (NSViewController *viewController in [visibleGroupViewControllers allValues])
[[viewController view] removeFromSuperview];
[visibleGroupViewControllers removeAllObjects];
if (shouldEmptyCaches) {
for (NSViewController *viewController in [visibleViewControllers allValues]) {
[[viewController view] removeFromSuperview];
if ([delegate respondsToSelector:@selector(collectionView:viewControllerBecameInvisible:)])
[delegate collectionView:self viewControllerBecameInvisible:viewController];
}
[reusableViewControllers removeAllObjects];
[visibleViewControllers removeAllObjects];
} else
[self softReloadVisibleViewControllers];
[selectionIndexes removeAllIndexes];
NSRect visibleRect = [self visibleRect];
[layoutManager enumerateItems:^(BCCollectionViewLayoutItem *layoutItem) {
NSViewController *viewController = [self viewControllerForItemAtIndex:[layoutItem itemIndex]];
if (viewController) {
[[viewController view] setFrame:[layoutItem itemRect]];
[delegate collectionView:self willShowViewController:viewController forItem:[contentArray objectAtIndex:[layoutItem itemIndex]] atIndex:[layoutItem itemIndex]];
} else if (NSIntersectsRect(visibleRect, [layoutItem itemRect]))
[self addMissingViewControllerForItemAtIndex:[layoutItem itemIndex] withFrame:[layoutItem itemRect]];
} completionBlock:^{
[self resizeFrameToFitContents];
[self addMissingGroupHeaders];
dispatch_async(dispatch_get_main_queue(), completionBlock);
}];
}
- (void)scrollViewDidScroll:(NSNotification *)note
{
if ([[self indexesOfInvisibleViewControllers] count] > 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
// NSLog(@"scrollViewDidScroll dispatch");
[self removeInvisibleViewControllers];
});
}
if ([[self indexesOfMissingViewControllers] count] > 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self addMissingViewControllersToView];
});
}
if ([delegate respondsToSelector:@selector(collectionViewDidScroll:inDirection:)]) {
if ([self visibleRect].origin.y > previousFrameBounds.origin.y)
[delegate collectionViewDidScroll:self inDirection:BCCollectionViewScrollDirectionDown];
else
[delegate collectionViewDidScroll:self inDirection:BCCollectionViewScrollDirectionUp];
previousFrameBounds = [self visibleRect];
}
}
- (void)viewDidResize
{
if ([contentArray count] > 0 && [visibleViewControllers count] > 0)
[self softReloadDataWithCompletionBlock:NULL];
}
- (void)softReloadDataWithCompletionBlock:(dispatch_block_t)block
{
NSSize cellSize = [delegate cellSizeForCollectionView:self];
if (NSWidth([self visibleRect]) < cellSize.width || NSHeight([self visibleRect]) < cellSize.height)
return;
NSRange range = [self rangeOfVisibleItemsWithOverflow];
[layoutManager enumerateItems:^(BCCollectionViewLayoutItem *layoutItem) {
if (NSLocationInRange([layoutItem itemIndex], range)) {
NSViewController *controller = [self viewControllerForItemAtIndex:[layoutItem itemIndex]];
if (controller)
[[controller view] setFrame:[layoutItem itemRect]];
else
[self addMissingViewControllerForItemAtIndex:[layoutItem itemIndex] withFrame:[layoutItem itemRect]];
} else {
if ([self viewControllerForItemAtIndex:[layoutItem itemIndex]])
[self removeViewControllerForItemAtIndex:[layoutItem itemIndex]];
}
} completionBlock:^(void) {
[self resizeFrameToFitContents];
[self addMissingGroupHeaders];
[self setNeedsDisplay:YES];
if (block != NULL)
block();
}];
}
- (NSMenu *)menuForEvent:(NSEvent *)anEvent
{
[self mouseDown:anEvent];
if ([delegate respondsToSelector:@selector(collectionView:menuForItemsAtIndexes:)])
return [delegate collectionView:self menuForItemsAtIndexes:[self selectionIndexes]];
else
return nil;
}
- (BOOL)resignFirstResponder
{
if ([delegate respondsToSelector:@selector(collectionViewLostFirstResponder:)])
[delegate collectionViewLostFirstResponder:self];
return [super resignFirstResponder];
}
- (BOOL)becomeFirstResponder
{
if ([delegate respondsToSelector:@selector(collectionViewBecameFirstResponder:)])
[delegate collectionViewBecameFirstResponder:self];
return [super becomeFirstResponder];
}
- (BOOL)isOpaque
{
return YES;
}
@end