forked from yujitach/MenuMeters
-
Notifications
You must be signed in to change notification settings - Fork 9
/
MenuMetersMenuExtraBase.m
74 lines (68 loc) · 2.29 KB
/
MenuMetersMenuExtraBase.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
//
// NSMenuExtraBase.m
// MenuMeters
//
// Created by Yuji on 2015/08/01.
//
//
#import "MenuMetersMenuExtraBase.h"
@implementation MenuMetersMenuExtraBase
-(instancetype)initWithBundle:(NSBundle*)bundle
{
self=[super initWithBundle:bundle];
return self;
}
-(void)willUnload {
[updateTimer invalidate];
updateTimer = nil;
[super willUnload];
}
-(void)timerFired:(id)notused
{
NSImage *oldCanvas = statusItem.button.image;
NSImage *canvas = oldCanvas;
NSSize imageSize = NSMakeSize(self.length, self.view.frame.size.height);
NSSize oldImageSize = canvas.size;
if (imageSize.width != oldImageSize.width || imageSize.height != oldImageSize.height) {
canvas = [[NSImage alloc] initWithSize:imageSize];
}
NSImage *image = self.image;
[canvas lockFocus];
[image drawAtPoint:CGPointZero fromRect:(CGRect) {.size = image.size} operation:NSCompositeCopy fraction:1.0];
[canvas unlockFocus];
if (canvas != oldCanvas) {
statusItem.button.image = canvas;
} else {
[statusItem.button displayRectIgnoringOpacity:statusItem.button.bounds];
}
}
- (void)configDisplay:(NSString*)bundleID fromPrefs:(MenuMeterDefaults*)ourPrefs withTimerInterval:(NSTimeInterval)interval
{
if([ourPrefs loadBoolPref:bundleID defaultValue:YES]){
if(!statusItem){
statusItem=[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
statusItem.menu = self.menu;
statusItem.menu.delegate = self;
}
[updateTimer invalidate];
updateTimer=[NSTimer timerWithTimeInterval:interval target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
[updateTimer setTolerance:.2*interval];
[[NSRunLoop currentRunLoop] addTimer:updateTimer forMode:NSRunLoopCommonModes];
}else if(![ourPrefs loadBoolPref:bundleID defaultValue:YES] && statusItem){
[updateTimer invalidate];
[[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
statusItem=nil;
}
}
#pragma mark NSMenuDelegate
- (void)menuNeedsUpdate:(NSMenu*)menu {
statusItem.menu = self.menu;
statusItem.menu.delegate = self;
}
- (void)menuWillOpen:(NSMenu*)menu {
_isMenuVisible = YES;
}
- (void)menuDidClose:(NSMenu*)menu {
_isMenuVisible = NO;
}
@end