forked from tomasf/caffeine
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
LCMenuIconView.m
executable file
·84 lines (59 loc) · 2.01 KB
/
LCMenuIconView.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
//
// LCMenuIconView.m
// Caffeine
//
//
#import "LCMenuIconView.h"
@implementation LCMenuIconView
@synthesize isActive, statusItem, menu;
- (id)initWithFrame:(NSRect)r {
[super initWithFrame:r];
return self;
}
- (void)drawRect:(NSRect)r {
activeImage = [[NSImage imageNamed:@"active"] retain];
inactiveImage = [[NSImage imageNamed:@"inactive"] retain];
// Invert icons if in dark mode
NSString *mode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
if([mode isEqualToString: @"Dark"]) {
activeImage = [[NSImage imageNamed:@"highlightactive"] retain];
inactiveImage = [[NSImage imageNamed:@"highlighted"] retain];
}
highlightImage = [[NSImage imageNamed:@"highlighted"] retain];
highlightActiveImage = [[NSImage imageNamed:@"highlightactive"] retain];
NSImage *i = isActive ? activeImage : inactiveImage;
if(menuIsShown) i = isActive ? highlightActiveImage : highlightImage;
NSRect f = [self bounds];
NSPoint p = NSMakePoint(f.size.width/2 - [i size].width/2, f.size.height/2 - [i size].height/2 + 1);
[statusItem drawStatusBarBackgroundInRect:r withHighlight:menuIsShown];
[i drawAtPoint:p fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
}
- (void)setActive:(BOOL)flag {
isActive = flag;
[self setNeedsDisplay:YES];
}
- (void)rightMouseDown:(NSEvent*)e {
menuIsShown = YES;
[self setNeedsDisplay:YES];
[statusItem popUpStatusItemMenu:menu];
menuIsShown = NO;
[self setNeedsDisplay:YES];
}
- (void)mouseDown:(NSEvent*)e {
if([e modifierFlags] & (NSCommandKeyMask | NSControlKeyMask))
return [self rightMouseDown:e];
[NSApp sendAction:action to:target from:self];
}
- (void)mouseUp:(NSEvent *)theEvent {
if([NSDate timeIntervalSinceReferenceDate] - lastMouseUp < 0.2) {
[NSApp sendAction:@selector(showPreferences:) to:nil from:nil];
lastMouseUp = 0;
} else lastMouseUp = [NSDate timeIntervalSinceReferenceDate];
}
- (void)setAction:(SEL)a {
action = a;
}
- (void)setTarget:(id)t {
target = t;
}
@end