forked from KOed/welly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WLCoverFlowPortal.m
275 lines (232 loc) · 7.08 KB
/
WLCoverFlowPortal.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
//
// WLPortal.m
// Welly
//
// Created by boost on 9/6/09.
// Copyright 2009 Xi Wang. All rights reserved.
//
#import "WLCoverFlowPortal.h"
#import "WLPortalItem.h"
#import "CommonType.h"
#import "WLGlobalConfig.h"
const float xscale = 1, yscale = 0.8;
// hack
@interface IKImageFlowView : NSOpenGLView
- (void)reloadData;
- (id)cacheManager;
- (void)setSelectedIndex:(NSUInteger)index;
- (NSUInteger)selectedIndex;
- (NSUInteger)focusedIndex;
- (NSUInteger)cellIndexAtLocation:(NSPoint)point;
- (void)setBackgroundColor:(NSColor *)color;
- (NSColor *)backgroundColor;
- (void)setDraggingDestinationDelegate:(id)delegate;
@end
@interface IKCacheManager : NSObject
- (void)freeCache;
@end
@implementation WLCoverFlowPortal
//@synthesize view = _imageFlowView;
- (void)dealloc {
if (_portalItems)
[_portalItems release];
[super dealloc];
}
- (id)initWithFrame:(NSRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialize the imageFlowView
_imageFlowView = [[NSClassFromString(@"IKImageFlowView") alloc] initWithFrame:frame];
[_imageFlowView setDataSource:self];
[_imageFlowView setDelegate:self];
[_imageFlowView setDraggingDestinationDelegate:self];
[_imageFlowView setHidden:NO];
// background
NSColor *color = [[WLGlobalConfig sharedInstance] colorBG];
// cover flow doesn't support alpha
color = [color colorWithAlphaComponent:1.0];
[_imageFlowView setBackgroundColor:color];
// re-scale the image flow view
[self setFrame:self.frame];
}
return self;
}
- (void)awakeFromNib {
[self addSubview:_imageFlowView];
[[self window] makeFirstResponder:self];
// event hanlding
// Add self to _imageFlowView's next responder
NSResponder *next = [_imageFlowView nextResponder];
if (self != next) {
[_imageFlowView setNextResponder:self];
[self setNextResponder:next];
}
}
- (void)setFrame:(NSRect)frame {
[super setFrame:frame];
frame.origin.x += frame.size.width * (1 - xscale) / 2;
frame.origin.y += frame.size.height * (1 - yscale) / 2;
frame.size.width *= xscale;
frame.size.height *= yscale;
[_imageFlowView setFrame:frame];
//[_imageFlowView setNeedsDisplay:YES];
}
- (id)initWithPortalItems:(NSArray *)portalItems {
if ((self = [self init])) {
[self setPortalItems:_portalItems];
}
return self;
}
#pragma mark -
#pragma mark Display
- (void)drawRect:(NSRect)rect {
// background
NSColor *color = [[WLGlobalConfig sharedInstance] colorBG];
// cover flow doesn't support alpha
color = [color colorWithAlphaComponent:1.0];
[color set];
NSRectFill(rect);
}
- (void)refresh {
[[_imageFlowView cacheManager] freeCache];
[_imageFlowView reloadData];
}
- (void)setPortalItems:(NSArray *)portalItems {
if (_portalItems)
[_portalItems release];
_portalItems = [portalItems copy];
[self refresh];
}
- (void)select {
WLPortalItem *item = [_portalItems objectAtIndex:[_imageFlowView selectedIndex]];
[item didSelect:self];
}
#pragma mark -
#pragma mark Override
- (BOOL)acceptsFirstResponder {
return YES;
}
- (BOOL)canBecomeKeyView {
return YES;
}
/*- (NSView *)hitTest:(NSPoint)p {
return self;
}*/
#pragma mark -
#pragma mark IKImageFlowDataSource protocol
- (NSUInteger)numberOfItemsInImageFlow:(id)aFlow {
return [_portalItems count];
}
- (id)imageFlow:(id)aFlow itemAtIndex:(NSUInteger)index {
return [_portalItems objectAtIndex:index];
}
#pragma mark -
#pragma mark IKImageFlowDelegate protocol
- (void)imageFlow:(id)aFlow cellWasDoubleClickedAtIndex:(NSInteger)index {
[self select];
}
#pragma mark -
#pragma mark Event handler
- (void)keyDown:(NSEvent *)theEvent {
switch ([[theEvent charactersIgnoringModifiers] characterAtIndex:0]) {
case WLWhitespaceCharacter:
case WLReturnCharacter: {
[self select];
return;
}
}
[_imageFlowView keyDown:theEvent];
}
// private
- (NSUInteger)cellIndexAtLocation:(NSPoint)p {
NSPoint pt = [_imageFlowView convertPoint:p fromView:nil];
return [_imageFlowView cellIndexAtLocation:pt];
}
- (id)itemAtLocation:(NSPoint)p {
NSUInteger index = [self cellIndexAtLocation:p];
if (index == NSNotFound || [_portalItems count] <= index)
return nil;
return [_portalItems objectAtIndex:index];
}
- (void)mouseDragged:(NSEvent *)theEvent {
_draggingItem = [self itemAtLocation:[theEvent locationInWindow]];
if (_draggingItem) {
if (![_draggingItem conformsToProtocol:@protocol(WLDraggingSource)] ||
![(id <WLDraggingSource>)_draggingItem acceptsDragging]) {
_draggingItem = nil;
return;
}
WLPortalItem <WLDraggingSource> *draggingItem = (WLPortalItem <WLDraggingSource> *)_draggingItem;
NSImage *image = [draggingItem draggingImage];
NSSize size = [image size];
NSPoint pt = [_imageFlowView convertPoint:[theEvent locationInWindow] fromView:nil];
pt.x -= size.width/2;
pt.y -= size.height/2;
NSPasteboard *pboard = [draggingItem draggingPasteboard];
[_imageFlowView dragImage:image at:pt offset:NSZeroSize
event:theEvent pasteboard:pboard source:self slideBack:NO];
return;
}
}
#pragma mark -
#pragma mark NSDraggingSource protocol
// drag out images (remove covers)
// private
- (BOOL)draggedOut:(NSPoint)screenPoint {
NSPoint pt = [[_imageFlowView window] convertScreenToBase:screenPoint];
return ![_imageFlowView hitTest:pt];
}
- (void)draggedImage:(NSImage *)image
movedTo:(NSPoint)screenPoint {
if ([self draggedOut:screenPoint])
[[NSCursor disappearingItemCursor] set];
else
[[NSCursor arrowCursor] set];
}
- (void)draggedImage:(NSImage *)image
endedAt:(NSPoint)screenPoint
operation:(NSDragOperation)operation {
[[NSCursor arrowCursor] set];
if (![self draggedOut:screenPoint])
return;
if (_draggingItem) {
assert([_draggingItem conformsToProtocol:@protocol(WLDraggingSource)]);
id <WLDraggingSource> draggingItem = (id <WLDraggingSource>) _draggingItem;
assert([draggingItem acceptsDragging]);
[draggingItem draggedToRemove:self];
[self refresh];
_draggingItem = nil;
}
}
#pragma mark -
#pragma mark NSDraggingDestination protocol
// drop in images (add covers)
// private
- (NSDragOperation)checkSource:(id <NSDraggingInfo>)sender {
id item = [self itemAtLocation:[sender draggingLocation]];
if (!item)
return NSDragOperationNone;
if ([item acceptsPBoard:[sender draggingPasteboard]])
return NSDragOperationCopy;
return NSDragOperationNone;
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
if ([sender draggingSource] == self)
return NSDragOperationNone;
return [self draggingUpdated:sender];
}
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender {
return [self checkSource:sender];
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
return [self checkSource:sender];
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
id item = [self itemAtLocation:[sender draggingLocation]];
if (item == nil || ![item conformsToProtocol:@protocol(WLPasteboardReceiver)])
return NO;
return [(id <WLPasteboardReceiver>)item didReceivePBoard:[sender draggingPasteboard]];
}
- (void)concludeDragOperation:(id < NSDraggingInfo >)sender {
[self refresh];
}
@end