forked from sketch-hq/BCCollectionView
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BCCollectionView+Dragging.m
170 lines (136 loc) · 5.79 KB
/
BCCollectionView+Dragging.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
// Created by Pieter Omvlee on 13/12/2010.
// Copyright 2010 Bohemian Coding. All rights reserved.
#import "BCCollectionView+Dragging.h"
#import "BCCollectionViewLayoutManager.h"
@interface BCCollectionView ()
{
BOOL firstDrag;
}
@end
@implementation BCCollectionView (BCCollectionView_Dragging)
- (void)initiateDraggingSessionWithEvent:(NSEvent *)anEvent
{
NSUInteger index = [layoutManager indexOfItemAtPoint:mouseDownLocation];
[self selectItemAtIndex:index];
NSRect itemRect = [layoutManager rectOfItemAtIndex:index];
NSView *currentView = [[self viewControllerForItemAtIndex:index] view];
NSData *imageData = [currentView dataWithPDFInsideRect:NSMakeRect(0,0,NSWidth(itemRect),NSHeight(itemRect))];
NSImage *pdfImage = [[NSImage alloc] initWithData:imageData];
NSImage *dragImage = [[NSImage alloc] initWithSize:[pdfImage size]];
if ([dragImage size].width > 0 && [dragImage size].height > 0) {
[dragImage lockFocus];
[pdfImage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5];
[dragImage unlockFocus];
}
NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[self delegateWriteIndexes:selectionIndexes toPasteboard:pasteboard];
[self dragImage:dragImage
at:NSMakePoint(NSMinX(itemRect), NSMaxY(itemRect))
offset:NSMakeSize(0, 0)
event:anEvent
pasteboard:pasteboard
source:self
slideBack:YES];
}
- (void)draggedImage:(NSImage *)anImage beganAt:(NSPoint)aPoint
{
}
- (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
{
}
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
if ([delegate respondsToSelector:@selector(collectionView:draggingEntered:)])
return [delegate collectionView:self draggingEntered:sender];
else
return [self draggingUpdated:sender];
}
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
if (dragHoverIndex != NSNotFound)
[self setNeedsDisplayInRect:[layoutManager rectOfItemAtIndex:dragHoverIndex]];
NSPoint mouse = [self convertPoint:[sender draggingLocation] fromView:nil];
NSUInteger index = [layoutManager indexOfItemAtPoint:mouse];
NSDragOperation operation = NSDragOperationNone;
if ([sender draggingSource] == self) {
if ([selectionIndexes containsIndex:index])
[self setDragHoverIndex:NSNotFound];
else if ([self delegateCanDrop:sender onIndex:index]) {
[self setDragHoverIndex:index];
operation = NSDragOperationMove;
} else
[self setDragHoverIndex:NSNotFound];
} else {
if ([self delegateCanDrop:sender onIndex:index]) {
[self setDragHoverIndex:index];
operation = NSDragOperationCopy;
}
}
if (dragHoverIndex != NSNotFound)
[self setNeedsDisplayInRect:[layoutManager rectOfItemAtIndex:dragHoverIndex]];
return operation;
}
- (void)draggingEnded:(id <NSDraggingInfo>)sender
{
if (dragHoverIndex != NSNotFound)
[self setNeedsDisplayInRect:[layoutManager rectOfItemAtIndex:dragHoverIndex]];
[self setDragHoverIndex:NSNotFound];
if ([delegate respondsToSelector:@selector(collectionView:draggingEnded:)])
[delegate collectionView:self draggingEnded:sender];
}
- (void)draggingExited:(id<NSDraggingInfo>)sender
{
NSPoint mouse = [self convertPoint:[sender draggingLocation] fromView:nil];
NSUInteger index = [layoutManager indexOfItemAtPoint:mouse];
if (index == NSNotFound) {
[self setNeedsDisplayInRect:[layoutManager rectOfItemAtIndex:dragHoverIndex]];
[self setDragHoverIndex:NSNotFound];
if ([delegate respondsToSelector:@selector(collectionView:draggingExited:)])
[delegate collectionView:self draggingExited:sender];
}
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
id item = nil;
if (dragHoverIndex <[contentArray count])
item = [contentArray objectAtIndex:dragHoverIndex];
if ([delegate respondsToSelector:@selector(collectionView:performDragOperation:onViewController:forItem:)])
return [delegate collectionView:self performDragOperation:sender onViewController:[self viewControllerForItemAtIndex:dragHoverIndex] forItem:item];
else
return NO;
}
#pragma mark -
#pragma mark Delegate Shortcuts
- (void)setDragHoverIndex:(NSUInteger)hoverIndex
{
if (hoverIndex != dragHoverIndex) {
if (dragHoverIndex != NSNotFound)
[self setNeedsDisplayInRect:[layoutManager rectOfItemAtIndex:dragHoverIndex]];
if ([delegate respondsToSelector:@selector(collectionView:dragExitedViewController:)])
[delegate collectionView:self dragExitedViewController:[self viewControllerForItemAtIndex:dragHoverIndex]];
dragHoverIndex = hoverIndex;
if ([delegate respondsToSelector:@selector(collectionView:dragEnteredViewController:)])
[delegate collectionView:self dragEnteredViewController:[self viewControllerForItemAtIndex:dragHoverIndex]];
if (dragHoverIndex != NSNotFound)
[self setNeedsDisplayInRect:[layoutManager rectOfItemAtIndex:dragHoverIndex]];
}
}
- (BOOL)delegateSupportsDragForItemsAtIndexes:(NSIndexSet *)indexSet
{
if ([delegate respondsToSelector:@selector(collectionView:canDragItemsAtIndexes:)])
return [delegate collectionView:self canDragItemsAtIndexes:indexSet];
return NO;
}
- (void)delegateWriteIndexes:(NSIndexSet *)indexSet toPasteboard:(NSPasteboard *)pasteboard
{
if ([delegate respondsToSelector:@selector(collectionView:writeItemsAtIndexes:toPasteboard:)])
[delegate collectionView:self writeItemsAtIndexes:indexSet toPasteboard:pasteboard];
}
- (BOOL)delegateCanDrop:(id)draggingInfo onIndex:(NSUInteger)index
{
if ([delegate respondsToSelector:@selector(collectionView:validateDrop:onItemAtIndex:)])
return [delegate collectionView:self validateDrop:draggingInfo onItemAtIndex:index];
else
return NO;
}
@end