-
Notifications
You must be signed in to change notification settings - Fork 12
/
MVStatusItemView.m
executable file
·206 lines (178 loc) · 6.83 KB
/
MVStatusItemView.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
//
// MVStatusItemView.m
// FileShuttle
//
// Created by Michael Villar on 12/11/11.
//
#import "MVStatusItemView.h"
#import "NSPasteboard+Files.h"
int const MVStatusItemStateNormal = 0;
int const MVStatusItemStateUploading = 1;
int const MVStatusItemStateComplete = 2;
int const MVStatusItemStateError = 3;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@interface MVStatusItemView ()
@property (assign) BOOL isMenuVisible;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation MVStatusItemView
@synthesize statusItem = statusItem_,
image = image_,
emptyImage = emptyImage_,
maskImage = maskImage_,
alternateImage = alternateImage_,
errorImage = errorImage_,
completedImage = completedImage_,
state = state_,
progression = progression_,
isMenuVisible = isMenuVisible_,
delegate = delegate_;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
[statusItem_ release];
[image_ release];
[emptyImage_ release];
[maskImage_ release];
[alternateImage_ release];
[errorImage_ release];
[completedImage_ release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if(self) {
statusItem_ = nil;
image_ = nil;
emptyImage_ = nil;
maskImage_ = nil;
state_ = MVStatusItemStateNormal;
progression_ = 1.0;
alternateImage_ = nil;
errorImage_ = nil;
completedImage_ = nil;
delegate_ = nil;
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,
NSStringPboardType,
nil]];
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)mouseDown:(NSEvent *)event {
[[self menu] setDelegate:self];
[self.statusItem popUpStatusItemMenu:[self menu]];
[self setNeedsDisplay:YES];
[self.statusItem setHighlightMode:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)rightMouseDown:(NSEvent *)event {
[self mouseDown:event];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)menuWillOpen:(NSMenu *)menu {
self.isMenuVisible = YES;
[self setNeedsDisplay:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)menuDidClose:(NSMenu *)menu {
self.isMenuVisible = NO;
[menu setDelegate:nil];
[self setNeedsDisplay:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)drawRect:(NSRect)dirtyRect {
[self.statusItem drawStatusBarBackgroundInRect:[self bounds]
withHighlight:self.isMenuVisible];
NSImage *image = nil;
if(self.isMenuVisible && self.alternateImage)
image = self.alternateImage;
else {
if(self.state == MVStatusItemStateNormal)
image = self.image;
else if(self.state == MVStatusItemStateComplete)
image = self.completedImage;
else if(self.state == MVStatusItemStateError)
image = self.errorImage;
else if(self.state == MVStatusItemStateUploading)
image = self.emptyImage;
}
if(image)
{
[image drawInRect:NSMakeRect(2, 2, image.size.width, image.size.height)
fromRect:NSMakeRect(0, 0, image.size.width, image.size.height)
operation:NSCompositeSourceOver
fraction:1.0];
if(self.state == MVStatusItemStateUploading && image != self.alternateImage)
{
NSBezierPath *mask = [[NSBezierPath alloc] init];
[mask appendBezierPathWithRect:NSMakeRect(0, 0, 6 + (self.progression * 12), image.size.height)];
[mask addClip];
[self.maskImage drawInRect:NSMakeRect(2, 2, image.size.width, image.size.height)
fromRect:NSMakeRect(0, 0, image.size.width, image.size.height)
operation:NSCompositeSourceOver
fraction:1.0];
[mask release];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Properties
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setProgression:(float)progression
{
if(progression == progression_)
return;
progression_ = progression;
[self setNeedsDisplay:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setState:(int)state
{
if(state == state_)
return;
state_ = state;
[self setNeedsDisplay:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Drag and drop
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender {
return NSDragOperationCopy;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender {
return YES;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender
{
NSPasteboard *pasteboard = sender.draggingPasteboard;
NSArray *types = [pasteboard types];
if([types containsObject:NSStringPboardType])
{
NSString *pboardString = [pasteboard stringForType:NSStringPboardType];
if([self.delegate respondsToSelector:@selector(statusItemView:didDropString:)]) {
[self.delegate statusItemView:self
didDropString:pboardString];
}
}
else if([types containsObject:NSFilenamesPboardType])
{
if([self.delegate respondsToSelector:@selector(statusItemView:didDropFiles:)]) {
[self.delegate statusItemView:self
didDropFiles:[pasteboard filesRepresentation]];
}
}
return YES;
}
@end