forked from oscardelben/MenuBarApplicationTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomStatusItem.m
56 lines (39 loc) · 1.25 KB
/
CustomStatusItem.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
#import "CustomStatusItem.h"
#import "ContentProvider.h"
@implementation CustomStatusItem
@synthesize delegate;
@synthesize freeMemory;
- (void)drawRect:(NSRect)dirtyRect
{
// We change the width programmatically
NSRect destRect = NSZeroRect;
destRect.size = dirtyRect.size;
NSString *aString = [ContentProvider content];
// We can calculate the width of the menu bar icon here
float width = [aString sizeWithAttributes:nil].width;
destRect.size = NSMakeSize(width, dirtyRect.size.height);
self.frame = destRect;
// Actual drawing
[aString drawInRect:dirtyRect withAttributes:nil];
}
#pragma mark toggleWindow
- (NSPoint)getAnchorPoint
{
NSRect frame = [[self window] frame];
NSRect screen = [[NSScreen mainScreen] frame];
NSPoint point = NSMakePoint(NSMidX(frame), screen.size.height - [[NSStatusBar systemStatusBar] thickness]);
return point;
}
- (void)toggleShowWindow
{
if ([(NSObject *)delegate respondsToSelector:@selector(toggleShowWindowFromPoint:)])
{
[delegate toggleShowWindowFromPoint:[self getAnchorPoint]];
}
}
#pragma mark Events
// The icon was clicked, we toggle the window
- (void)mouseDown:(NSEvent *)event {
[self toggleShowWindow];
}
@end